-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
) | ||
|
||
var _ SecretStorage = &AWSSecretsManager{} | ||
|
||
type AWSSecretsManager struct { | ||
UseSecretMaps bool // TODO: support storing to json maps if this is enabled. | ||
|
||
client *secretsmanager.SecretsManager | ||
} | ||
|
||
func NewAWSSecretsManager(client *secretsmanager.SecretsManager) *AWSSecretsManager { | ||
return &AWSSecretsManager{ | ||
client: client, | ||
} | ||
} | ||
|
||
// SetSecret | ||
// must have the secretsmanager:CreateSecret permission | ||
// if using tags, must have secretsmanager:TagResource | ||
// if using kms customer-managed keys, also need: | ||
// - kms:GenerateDataKey | ||
// - 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: | ||
// try replacing instead | ||
_, err = s.client.UpdateSecretWithContext(context.TODO(), &secretsmanager.UpdateSecretInput{ | ||
SecretBinary: secret, | ||
SecretId: &name, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("update secret: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
return fmt.Errorf("creating secret: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// GetSecret | ||
// must have permission secretsmanager:GetSecretValue | ||
// 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: | ||
return nil, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("get secret: %w", err) | ||
} | ||
return sec.SecretBinary, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/service/secretsmanager" | ||
) | ||
|
||
func waitForSecretsManagerReady(t *testing.T, ssm *secretsmanager.SecretsManager) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) | ||
defer cancel() | ||
for { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters