From 9cae9c98efe779a877db53d008b9eac2e3c6e420 Mon Sep 17 00:00:00 2001 From: Nico Esteves Date: Mon, 18 Jan 2021 22:52:35 +0100 Subject: [PATCH] Cleanup warnings --- common/config_values.go | 41 ++++++++++++++++++------------------ common/config_values_test.go | 2 +- common/iam.go | 7 +++--- common/kms.go | 2 +- common/utils.go | 6 +++--- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/common/config_values.go b/common/config_values.go index 7dbdfcd..4903c07 100644 --- a/common/config_values.go +++ b/common/config_values.go @@ -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 } @@ -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 @@ -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") { @@ -218,32 +218,31 @@ 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 } @@ -251,21 +250,21 @@ func RefreshMap(src map[string]interface{}, state *RefreshState) (map[string]int 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 } @@ -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 diff --git a/common/config_values_test.go b/common/config_values_test.go index caf9283..3f0cdf2 100644 --- a/common/config_values_test.go +++ b/common/config_values_test.go @@ -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) @@ -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"]) diff --git a/common/iam.go b/common/iam.go index 1f998fc..bc6e71d 100644 --- a/common/iam.go +++ b/common/iam.go @@ -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 { @@ -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 } diff --git a/common/kms.go b/common/kms.go index 0bceedd..f05e6c2 100644 --- a/common/kms.go +++ b/common/kms.go @@ -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 } } diff --git a/common/utils.go b/common/utils.go index b50d3a9..4f6067d 100644 --- a/common/utils.go +++ b/common/utils.go @@ -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 }