Skip to content

Commit

Permalink
linter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka committed Oct 22, 2021
1 parent c3ded67 commit e31a5a8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
19 changes: 13 additions & 6 deletions secrets/awssecretsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package secrets

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -31,14 +32,15 @@ func NewAWSSecretsManager(client *secretsmanager.SecretsManager) *AWSSecretsMana
// - kms:Decrypt
func (s *AWSSecretsManager) SetSecret(name string, secret []byte) error {
name = strings.ReplaceAll(name, ":", "_")

_, err := s.client.CreateSecretWithContext(context.TODO(), &secretsmanager.CreateSecretInput{
Name: &name,
SecretBinary: secret,
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeResourceExistsException:
var aerr awserr.Error
if errors.As(err, &aerr) {
if aerr.Code() == secretsmanager.ErrCodeResourceExistsException {
// try replacing instead
_, err = s.client.UpdateSecretWithContext(context.TODO(), &secretsmanager.UpdateSecretInput{
SecretBinary: secret,
Expand All @@ -47,12 +49,14 @@ func (s *AWSSecretsManager) SetSecret(name string, secret []byte) error {
if err != nil {
return fmt.Errorf("update secret: %w", err)
}

return nil
}
}

return fmt.Errorf("creating secret: %w", err)
}

return nil
}

Expand All @@ -61,17 +65,20 @@ func (s *AWSSecretsManager) SetSecret(name string, secret []byte) error {
// kms:Decrypt - required only if you use a customer-managed Amazon Web Services KMS key to encrypt the secret
func (s *AWSSecretsManager) GetSecret(name string) (secret []byte, err error) {
name = strings.ReplaceAll(name, ":", "_")

sec, err := s.client.GetSecretValueWithContext(context.TODO(), &secretsmanager.GetSecretValueInput{
SecretId: &name,
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeResourceNotFoundException:
var aerr awserr.Error
if errors.As(err, &aerr) {
if aerr.Code() == secretsmanager.ErrCodeResourceNotFoundException {
return nil, nil
}
}

return nil, fmt.Errorf("get secret: %w", err)
}

return sec.SecretBinary, nil
}
6 changes: 6 additions & 0 deletions secrets/awssecretsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@ import (
func waitForSecretsManagerReady(t *testing.T, ssm *secretsmanager.SecretsManager) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()

for {
// nolint
resp, err := http.Get(ssm.Client.Endpoint)
// server responds with 404 and body of status running 😂
if err == nil {
b, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()

if err == nil {
if strings.Contains(string(b), "running") {
return // ready!
}
}
}

if ctx.Err() != nil {
t.Error("timeout waiting for secrets manager to be ready")
t.FailNow()

return
}

time.Sleep(100 * time.Millisecond)
}
}
1 change: 1 addition & 0 deletions secrets/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func setup() {
if testing.Short() {
return
}

var containerID string

// setup localstack
Expand Down

0 comments on commit e31a5a8

Please sign in to comment.