Skip to content

Commit

Permalink
Allow non-string values in keys (#317)
Browse files Browse the repository at this point in the history
Signed-off-by: Lasse Gaardsholt <[email protected]>
Co-authored-by: Andrei Predoiu <[email protected]>
  • Loading branch information
Gaardsholt and Andrei-Predoiu authored Nov 19, 2024
1 parent e7bc218 commit 4f68e5d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 2 additions & 2 deletions files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Read(filePath string) string {
}

// Write will write some string data to a file
func Write(output string, fileName string, content string, owner *int, append bool) {
func Write(output string, fileName string, content interface{}, owner *int, append bool) {
fileName = fixFileName(fileName)
path := filepath.Join(output, fileName)

Expand All @@ -48,7 +48,7 @@ func Write(output string, fileName string, content string, owner *int, append bo

defer f.Close()

if _, err = f.WriteString(content); err != nil {
if _, err = f.WriteString(fmt.Sprintf("%v", content)); err != nil {
log.Fatal().Err(err).Msgf("Unable to write to file '%s'", path)
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions vault/extractSecrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func GetTestVaultServer(t *testing.T) vaultTest {

// put secrets
secretPath := "secret/data/secret"
secret := map[string]interface{}{"key1": "value1", "key2": "value2", "key3": "value3"}
secret := map[string]interface{}{"key1": "value1", "key2": "value2", "key3": "value3", "key4": 123, "key5": true}

_, err := client.Logical().Write(secretPath, secret)
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestExtractSecretsWithFormatAsExpected(t *testing.T) {
for _, v := range result {

// assert
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3"})
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3", input.Prefix + "key4": 123, input.Prefix + "key5": true})
actual := fmt.Sprintf("%v", v.Result)

assert.Equal(t, expected, actual)
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestExtractSecretsAsExpected(t *testing.T) {
}
for _, v := range result {
// assert
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3"})
expected := fmt.Sprintf("%v", map[string]interface{}{input.Prefix + "key1": "value1", input.Prefix + "key2": "value2", input.Prefix + "key3": "value3", input.Prefix + "key4": 123, input.Prefix + "key5": true})
actual := fmt.Sprintf("%v", v.Result)

assert.Equal(t, expected, actual)
Expand Down
4 changes: 2 additions & 2 deletions vault/readSecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (client *API) ReadSecret(path string) (map[string]interface{}, error) {
}

// ReadSecretKey from Vault
func (client *API) ReadSecretKey(path string, key string) (string, error) {
func (client *API) ReadSecretKey(path string, key string) (interface{}, error) {
secret, err := client.ReadSecret(path)
if secret == nil {
return "", fmt.Errorf(keyNotFound, key, path, err)
Expand All @@ -70,5 +70,5 @@ func (client *API) ReadSecretKey(path string, key string) (string, error) {
return "", fmt.Errorf(keyNotFound, key, path, err)
}

return secretKey.(string), nil
return secretKey, nil
}
52 changes: 52 additions & 0 deletions vault/readSecret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,55 @@ func TestReadSecretWrongPath(t *testing.T) {
// assert
assert.Equal(t, fmt.Sprintf(secretNotFound, path, nil), err.Error())
}

func testReadSecretKey(path string, key string, expectedValue interface{}, t *testing.T) {
// mock the ReadSecret function
vaultClient := &API{
Client: testVault.Client,
}

// act
value, err := vaultClient.ReadSecretKey(path, key)

// assert
assert.NilError(t, err)
assert.Equal(t, expectedValue, value)
}

// TestReadSecretKeyWithNumberAsValue tests that the function returns the value as a number
func TestReadSecretKeyWithNumberAsValue(t *testing.T) {
// arrange
path := "secret/data/secret"
key := "key4"
expectedValue := float64(123)

testReadSecretKey(path, key, expectedValue, t)
}

// TestReadSecretKeyWithBooleanAsValue tests that the function returns the value as a boolean
func TestReadSecretKeyWithBooleanAsValue(t *testing.T) {
// arrange
path := "secret/data/secret"
key := "key5"
expectedValue := true

testReadSecretKey(path, key, expectedValue, t)
}

// TestReadSecretKeyNotFound tests that the function will fail when trying to fetch an unknown key
func TestReadSecretKeyNotFound(t *testing.T) {
// arrange
path := "secret/data/secret"
key := "keys666"

// mock the ReadSecret function
vaultClient := &API{
Client: testVault.Client,
}

// act
_, err := vaultClient.ReadSecretKey(path, key)

// assert
assert.Error(t, err, "the key 'keys666' was not found in the path 'secret/data/secret': <nil>")
}

0 comments on commit 4f68e5d

Please sign in to comment.