-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws.go
132 lines (106 loc) · 3.12 KB
/
aws.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/sts"
stsTypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
"go.uber.org/zap"
"math/rand"
)
// AWSSecretManager represents the AWS Secret Manager
type AWSSecretManager struct {
GenericProvider
client *secretsmanager.Client
}
// Name returns the provider name
func (s *AWSSecretManager) Name() string {
return "aws"
}
// Init initializes the AWSSecretManager instance that
// contains a client for interacting with Secret Manager API.
//
// It populates an AWS Config with the values from the external
// configurations.
//
// The default configuration sources are:
// * Environment Variables
// * Shared Configuration and Shared Credentials files.
func (s *AWSSecretManager) Init(params map[string]string) error {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return err
}
if role, ok := params[AssumeRoleKey]; ok && role != "" {
cfg, err = s.AssumeRole(cfg, role)
if err != nil {
return err
}
}
svc := secretsmanager.NewFromConfig(cfg)
s.client = svc
return nil
}
func (s *AWSSecretManager) AssumeRole(cfg aws.Config, role string) (aws.Config, error) {
sourceAccount := sts.NewFromConfig(cfg)
logger.Info("Assuming role", zap.String("role", role))
rand.Seed(time.Now().UnixNano())
response, err := sourceAccount.AssumeRole(context.TODO(), &sts.AssumeRoleInput{
RoleArn: aws.String(role),
RoleSessionName: aws.String("sc_" + strconv.Itoa(10000+rand.Intn(25000))),
})
if err != nil {
return aws.Config{}, err
}
var assumedRoleCreds *stsTypes.Credentials = response.Credentials
return config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
*assumedRoleCreds.AccessKeyId,
*assumedRoleCreds.SecretAccessKey,
*assumedRoleCreds.SessionToken,
)))
}
// ListSecrets lists the AWS Secrets using external configurations.
// Use prefix to filter the secrets starting with a term.
// If prefix is empty, all secrets are listed.
func (s *AWSSecretManager) ListSecrets(_ string, prefix string) ([]*SecretData, error) {
var (
data []*SecretData
token string
)
input := &secretsmanager.ListSecretsInput{}
for {
result, err := s.client.ListSecrets(context.Background(), input)
if err != nil {
return nil, err
}
token = aws.ToString(result.NextToken)
for _, secret := range result.SecretList {
name := aws.ToString(secret.Name)
if s.Filter(name, prefix) {
resp, err := s.client.GetSecretValue(context.Background(), &secretsmanager.GetSecretValueInput{
SecretId: secret.Name,
})
if err != nil {
logger.Error("error getting the secret value", zap.String("name", name))
continue
}
data = append(data, &SecretData{
Path: name,
Name: name,
Data: aws.ToString(resp.SecretString),
})
}
}
if token != "" {
input.NextToken = aws.String(token)
} else {
break
}
}
return data, nil
}