Skip to content

Commit

Permalink
Cleanup warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hamstah committed Jan 18, 2021
1 parent cd8b089 commit 9cae9c9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 30 deletions.
41 changes: 20 additions & 21 deletions common/config_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func (c *ConfigValues) GenerateFromMap(src map[string]interface{}) (map[string]i
dst := map[string]interface{}{}

for key, value := range src {
switch value.(type) {
switch v := value.(type) {
case map[string]interface{}:
val, err := c.GenerateFromMap(value.(map[string]interface{}))
val, err := c.GenerateFromMap(v)
if err != nil {
return nil, err
}
Expand All @@ -113,7 +113,7 @@ func (c *ConfigValues) GenerateFromMap(src map[string]interface{}) (map[string]i
dst[name] = Source{
Type: secretType,
Name: name,
Identifier: value.(string),
Identifier: v,
Collapse: collapse,
}
found = true
Expand All @@ -124,8 +124,8 @@ func (c *ConfigValues) GenerateFromMap(src map[string]interface{}) (map[string]i
if !found {
collapse := strings.HasPrefix(key, "_")
for secretType, prefix := range c.ValuePrefixes {
if strings.HasPrefix(value.(string), prefix) {
value := value.(string)[len(prefix):]
if strings.HasPrefix(v, prefix) {
value := v[len(prefix):]

name := key
if collapse && (secretType == "SECRETS_MANAGER" || secretType == "SSM") {
Expand Down Expand Up @@ -218,54 +218,53 @@ func RefreshMap(src map[string]interface{}, state *RefreshState) (map[string]int
dst := map[string]interface{}{}

for key, value := range src {
switch value.(type) {
switch v := value.(type) {
case map[string]interface{}:
res, err := RefreshMap(value.(map[string]interface{}), state)
res, err := RefreshMap(v, state)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to refresh sub map with key %s", key))
}
dst[key] = res
case Source:
source := value.(Source)
switch source.Type {
switch v.Type {
case "FILE":
bytes, err := ioutil.ReadFile(source.Identifier)
bytes, err := ioutil.ReadFile(v.Identifier)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to load file %s for key %s", source.Identifier, key))
return nil, errors.Wrap(err, fmt.Sprintf("failed to load file %s for key %s", v.Identifier, key))
}
dst[source.Name] = string(bytes)
dst[v.Name] = string(bytes)
case "SSM":
if state.SSMClient == nil {
state.SSMClient = ssm.New(state.Session, state.Config)
}
if strings.HasSuffix(source.Identifier, "/*") {
values, err := getParametersByPath(state.SSMClient, source.Identifier[:len(source.Identifier)-2])
if strings.HasSuffix(v.Identifier, "/*") {
values, err := getParametersByPath(state.SSMClient, v.Identifier[:len(v.Identifier)-2])
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch from SSM by path for key %s", key))
}
if source.Collapse {
if v.Collapse {
for subKey, subValue := range values {
dst[subKey] = subValue
}
} else {
dst[key] = values
}
} else {
value, err := ssmGetParameter(state.SSMClient, source.Identifier)
value, err := ssmGetParameter(state.SSMClient, v.Identifier)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch from SSM for key %s", key))
}
dst[source.Name] = value
dst[v.Name] = value
}
case "SECRETS_MANAGER":
if state.SecretsManagerClient == nil {
state.SecretsManagerClient = secretsmanager.New(state.Session, state.Config)
}
values, err := secretsManagerGetSecretValue(state.SecretsManagerClient, source.Identifier, source.Name, state.Settings["secrets_manager_version_stage"])
values, err := secretsManagerGetSecretValue(state.SecretsManagerClient, v.Identifier, v.Name, state.Settings["secrets_manager_version_stage"])
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to fetch from Secrets Manager for key %s", key))
}
if source.Collapse {
if v.Collapse {
for subKey, subValue := range values {
dst[subKey] = subValue
}
Expand All @@ -276,11 +275,11 @@ func RefreshMap(src map[string]interface{}, state *RefreshState) (map[string]int
if state.KMSClient == nil {
state.KMSClient = kms.New(state.Session, state.Config)
}
value, err := DecryptWithKMS(state.KMSClient, source.Identifier)
value, err := DecryptWithKMS(state.KMSClient, v.Identifier)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("failed to decrypt KMS data for key %s", key))
}
dst[source.Name] = string(value)
dst[v.Name] = string(value)
}
default:
dst[key] = value
Expand Down
2 changes: 1 addition & 1 deletion common/config_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestSetFromMap(t *testing.T) {

res := Data{}
err = c.Refresh(nil, nil, &res)
require.NoError(t, err)

assert.Equal(t, "123", res.A)
assert.Equal(t, float64(456), res.B)
Expand Down Expand Up @@ -288,7 +289,6 @@ func TestSecretsManagerWithoutPrefix(t *testing.T) {
config, err := setFromMap(data)
require.NoError(t, err)

fmt.Println(config.Static)
assert.Equal(t, Source{Type: "SECRETS_MANAGER", Name: "a", Identifier: "hamstah/awstools/tests/test-1", Collapse: true}, config.Static["a"])
assert.Equal(t, Source{Type: "SECRETS_MANAGER", Name: "B", Identifier: "hamstah/awstools/tests/test-2", Collapse: true}, config.Static["B"])

Expand Down
7 changes: 3 additions & 4 deletions common/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func NewConfig(region string) *aws.Config {
return &aws.Config{Region: aws.String(region)}
}

func NewSession(region string) *session.Session {
func NewSession(region string) (*session.Session, error) {
awsConfig := NewConfig(region)
return session.New(awsConfig)
return session.NewSession(awsConfig)
}

type SessionTokenProvider struct {
Expand Down Expand Up @@ -121,8 +121,7 @@ func OpenSession(sessionFlags *SessionFlags) (*session.Session, *aws.Config) {
func AssumeRoleConfig(sessionFlags *SessionFlags, sess *session.Session) *aws.Config {
conf := NewConfig(*sessionFlags.Region)
if sessionFlags.RoleArn != nil && *sessionFlags.RoleArn != "" {
var creds *credentials.Credentials
creds = stscreds.NewCredentials(sess, *sessionFlags.RoleArn, func(p *stscreds.AssumeRoleProvider) {
creds := stscreds.NewCredentials(sess, *sessionFlags.RoleArn, func(p *stscreds.AssumeRoleProvider) {
if *sessionFlags.RoleExternalID != "" {
p.ExternalID = sessionFlags.RoleExternalID
}
Expand Down
2 changes: 1 addition & 1 deletion common/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func DecryptWithKMS(kmsClient *kms.KMS, ciphertext string) ([]byte, error) {
usingSecretBox := false
if len(content) > 14 {
if content[6] == 0x07 {
if bytes.Compare(content[7:14], []byte("payload")) == 0 {
if bytes.Equal(content[7:14], []byte("payload")) {
usingSecretBox = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ func FlattenMap(input map[string]interface{}, transformKey func(string) string,

for key, value := range input {
key = transformKey(key)
switch value.(type) {
switch v := value.(type) {
case int, string, float64, bool:
result[key] = value.(string)
result[key] = v.(string)
case map[string]interface{}:
sub, err := FlattenMap(value.(map[string]interface{}), transformKey, separator)
sub, err := FlattenMap(v, transformKey, separator)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9cae9c9

Please sign in to comment.