-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsm.go
225 lines (197 loc) · 5.3 KB
/
sm.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package sm
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
const format = "%v\t%v\t\n"
type Config struct {
sess *session.Session
region *string
}
func NewConfig(region string) (c Config) {
c.region = ®ion
c.sess = session.Must(session.NewSession(&aws.Config{
Region: c.region,
}))
return c
}
func (c Config) GetSecretValue(s string, raw bool) (map[string]interface{}, error) {
svc := secretsmanager.New(c.sess)
input := &secretsmanager.GetSecretValueInput{
SecretId: &s,
}
secretV, err := svc.GetSecretValue(input)
if err != nil {
return nil, err
}
if raw {
fmt.Println(*secretV.SecretString)
return nil, nil
}
var secretMap map[string]interface{}
json.Unmarshal([]byte(*secretV.SecretString), &secretMap)
return secretMap, nil
}
func PrintSecretValue(secretMap map[string]interface{}) {
for k, v := range secretMap {
fmt.Printf("%s: %v\n", k, v)
}
}
func (c Config) ListSecrets(nextToken *string, maxResults int64) ([]*secretsmanager.SecretListEntry, *string, error) {
svc := secretsmanager.New(c.sess)
input := &secretsmanager.ListSecretsInput{
MaxResults: &maxResults,
NextToken: nextToken,
}
result, err := svc.ListSecrets(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case secretsmanager.ErrCodeInvalidParameterException:
fmt.Println(secretsmanager.ErrCodeInvalidParameterException, aerr.Error())
case secretsmanager.ErrCodeInvalidNextTokenException:
fmt.Println(secretsmanager.ErrCodeInvalidNextTokenException, aerr.Error())
case secretsmanager.ErrCodeInternalServiceError:
fmt.Println(secretsmanager.ErrCodeInternalServiceError, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Printf("Unknown error: %v\n", err.Error())
}
return nil, nil, err
}
return result.SecretList, result.NextToken, nil
}
func (c Config) ListSecretsForComplete() ([]string, error) {
var maxResults int64 = 100
var nextToken *string
result, nextToken, err := c.ListSecrets(nextToken, maxResults)
if err != nil {
return nil, err
}
for nextToken != nil {
secrets, nt, err := c.ListSecrets(nextToken, maxResults)
if err != nil {
return nil, err
}
nextToken = nt
result = append(result, secrets...)
}
var secretNames []string
for _, s := range result {
name := *s.Name
secretNames = append(secretNames, name)
}
return secretNames, nil
}
func PrintSecretList(secrets []*secretsmanager.SecretListEntry, debug bool, sorted bool) {
if debug {
fmt.Printf("Printing %d secrets\n", len(secrets))
fmt.Printf("Format: %s\n", format)
}
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(tw, format, "Name", "ARN")
if sorted {
sort.Sort(secretsByName(secrets))
}
for _, s := range secrets {
fmt.Fprintf(tw, format, *s.Name, *s.ARN)
}
tw.Flush()
}
// sorting by name
type secretsByName []*secretsmanager.SecretListEntry
func (s secretsByName) Len() int {
return len(s)
}
func (s secretsByName) Less(i, j int) bool {
one := *s[i].Name
two := *s[j].Name
return one < two
}
func (s secretsByName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func UpdateSecretValue(c Config, s string, add map[string]string, remove []string) (map[string]interface{}, error) {
secret, err := c.GetSecretValue(s, false)
if err != nil {
return nil, err
}
for k, v := range add {
if _, ok := secret[k]; !ok {
fmt.Printf("Key %s doesn't exist, adding it\n", k)
}
secret[k] = v
}
for _, d := range remove {
if _, ok := secret[d]; ok {
delete(secret, d)
} else {
return nil, fmt.Errorf("Key %s doesn't exist, can't be deleted", d)
}
}
return secret, nil
}
func PutSecretValue(c Config, s string, secretMap map[string]interface{}) error {
secretByte, err := json.Marshal(secretMap)
if err != nil {
return fmt.Errorf("Error marshalling secret: %v", err)
}
svc := secretsmanager.New(c.sess)
secretString := string(secretByte)
input := &secretsmanager.PutSecretValueInput{
SecretId: aws.String(s),
SecretString: aws.String(secretString),
}
out, err := svc.PutSecretValue(input)
if err != nil {
return fmt.Errorf("Puting secret %s error: %v", s, err)
}
fmt.Printf("%s output: %s", s, out.String())
return nil
}
func ListSecretKeys(c Config, s string) (keysL []string) {
secret, _ := c.GetSecretValue(s, false)
for k := range secret {
keysL = append(keysL, k)
}
return keysL
}
func CompareSecrets(a, b map[string]interface{}) map[string]string {
diff := make(map[string]string)
am := convertMap(a)
bm := convertMap(b)
for k, v := range a {
if v != b[k] {
diff[fmt.Sprintf("%s=%s", k, am[k])] = fmt.Sprintf("%s=%s", k, bm[k])
}
}
return diff
}
func convertMap(a map[string]interface{}) map[string]string {
b := make(map[string]string)
for k, v := range a {
b[k] = v.(string)
}
return b
}
func PrintDiff(s1, s2 string, diff map[string]string) {
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintf(tw, format, fmt.Sprintf("%s =>", s1), s2)
for k, v := range diff {
fmt.Fprintf(tw, format, k, v)
}
tw.Flush()
fmt.Println(strings.Repeat("-", 100))
}