-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.go
61 lines (53 loc) · 1.83 KB
/
secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package util
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/rs/zerolog/log"
)
// CreateSecret will create a secret with id sid and write the plaintext into it
func CreateSecret(sid string, plaintext string) error {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
log.Fatal().Err(err).Msg("could not load AWS config")
}
svc := secretsmanager.New(cfg)
input := &secretsmanager.CreateSecretInput{
Description: aws.String("created by automation"),
Name: aws.String(sid),
SecretString: aws.String(plaintext),
}
req := svc.CreateSecretRequest(input)
result, err := req.Send(context.Background())
log.Trace().Interface("result", result).Msg("from create secret")
return err
}
// UpdateSecret will write the plaintext into the AWS Secrets Manager secret referred to by sid
func UpdateSecret(sid string, plaintext string) error {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
log.Fatal().Err(err).Msg("could not load AWS config")
}
svc := secretsmanager.New(cfg)
input := &secretsmanager.UpdateSecretInput {
SecretId: aws.String(sid),
SecretString: aws.String(plaintext),
}
req := svc.UpdateSecretRequest(input)
result, err := req.Send(context.Background())
log.Trace().Interface("result", result).Msg("from create secret")
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeResourceNotFoundException:
log.Info().Str("secretid", sid).Msg("not found, creating")
return CreateSecret(sid, plaintext)
case secretsmanager.ErrCodeInternalServiceError:
log.Error().Err(err).Msg("ISE from AWS, implement retry if appropriate")
}
}
}
return err
}