Skip to content

Commit

Permalink
random number string in default snapshot filename suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Aug 8, 2024
1 parent 4ecc53e commit 13572e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
12 changes: 10 additions & 2 deletions vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,16 @@ func NewVaultConfig(backupVaultConfig *util.VaultConfig) (*vaultConfig, error) {
// provide snapshot path default if unspecified
snapshotPath := backupVaultConfig.SnapshotPath
if len(snapshotPath) == 0 {
// assign default path in tmp-dir
snapshotPath = os.TempDir() + "/vault.bak"
// create random tmp file in tmp dir and then close it for later backup
snapshotTmpFile, err := os.CreateTemp(os.TempDir(), "vault*.bak")
if err != nil {
log.Printf("could not create a temporary file for the local snapshot file in the temporary directory '%s'", os.TempDir())
return nil, err
}
snapshotTmpFile.Close()

// assign to snapshot path config field member
snapshotPath = snapshotTmpFile.Name()
log.Printf("vault raft snapshot path defaulting to '%s'", snapshotPath)
}

Expand Down
21 changes: 13 additions & 8 deletions vault/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vault

import (
"os"
"regexp"
"strings"
"testing"

Expand All @@ -17,7 +18,6 @@ var (
token: "",
awsMountPath: "aws",
awsRole: "",
snapshotPath: "/tmp/vault.bak",
}
backupVaultTokenConfig = &util.VaultConfig{
Address: "https://127.0.0.1:8234",
Expand Down Expand Up @@ -47,7 +47,6 @@ var (
token: "",
awsMountPath: "gcp",
awsRole: "my_role",
snapshotPath: "/tmp/vault.bak",
}
)

Expand All @@ -60,9 +59,12 @@ func TestNewVaultConfig(test *testing.T) {
}

if *vaultConfigDefault != expectedDefaultConfig {
test.Error("vault config default constructor did not initialize with expected values")
test.Errorf("expected vault config values: %v", expectedDefaultConfig)
test.Errorf("actual vault config values: %v", *vaultConfigDefault)
// regexp match for random vault raft snapshot tmp file
if matched, _ := regexp.MatchString(`/tmp/vault\d+\.bak`, vaultConfigDefault.snapshotPath); !matched {
test.Error("vault config default constructor did not initialize with expected values")
test.Errorf("expected vault config values: %v", expectedDefaultConfig)
test.Errorf("actual vault config values: %v", *vaultConfigDefault)
}
}

// test with token
Expand All @@ -86,9 +88,12 @@ func TestNewVaultConfig(test *testing.T) {
}

if *vaultConfigAWS != expectedAWSConfig {
test.Error("vault config aws constructor did not initialize with expected values")
test.Errorf("expected vault config values: %v", expectedAWSConfig)
test.Errorf("actual vault config values: %v", *vaultConfigAWS)
// regexp match for random vault raft snapshot tmp file
if matched, _ := regexp.MatchString(`/tmp/vault\d+\.bak`, vaultConfigDefault.snapshotPath); !matched {
test.Error("vault config aws constructor did not initialize with expected values")
test.Errorf("expected vault config values: %v", expectedAWSConfig)
test.Errorf("actual vault config values: %v", *vaultConfigAWS)
}
}

// test errors in reverse validation order
Expand Down

0 comments on commit 13572e4

Please sign in to comment.