Skip to content

Commit

Permalink
Updated get secret value to handle getting numbers as secrets (hashic…
Browse files Browse the repository at this point in the history
  • Loading branch information
teddylear authored Jan 20, 2021
1 parent 8a28198 commit a9c72ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 15 additions & 3 deletions template/interpolate/aws/secretsmanager/secretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down Expand Up @@ -76,7 +77,7 @@ func (c *Client) GetSecret(spec *SecretSpec) (string, error) {
}

func getSecretValue(s *SecretString, spec *SecretSpec) (string, error) {
var secretValue map[string]string
var secretValue map[string]interface{}
blob := []byte(s.SecretString)

//For those plaintext secrets just return the value
Expand All @@ -96,13 +97,24 @@ func getSecretValue(s *SecretString, spec *SecretSpec) (string, error) {

if spec.Key == "" {
for _, v := range secretValue {
return v, nil
return getStringSecretValue(v)
}
}

if v, ok := secretValue[spec.Key]; ok {
return v, nil
return getStringSecretValue(v)
}

return "", fmt.Errorf("No secret found for key %q", spec.Key)
}

func getStringSecretValue(v interface{}) (string, error) {
switch valueType := v.(type) {
case string:
return valueType, nil
case float64:
return strconv.FormatFloat(valueType, 'f', 0, 64), nil
default:
return "", fmt.Errorf("Unsupported secret value type: %T", valueType)
}
}
10 changes: 10 additions & 0 deletions template/interpolate/aws/secretsmanager/secretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ func TestGetSecret(t *testing.T) {
want: "ThisIsThePassword",
ok: true,
},
{
description: "input as secret stored with 'String: int' value",
arg: &SecretSpec{Name: "test"},
mock: secretsmanager.GetSecretValueOutput{
Name: aws.String("test"),
SecretString: aws.String(`{"port": 5432}`),
},
want: "5432",
ok: true,
},
}

for _, test := range testCases {
Expand Down

0 comments on commit a9c72ec

Please sign in to comment.