-
Notifications
You must be signed in to change notification settings - Fork 1
/
retriever.go
119 lines (100 loc) · 2.98 KB
/
retriever.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
// Package retriever provides primitives for interacting with
// AWS Parameter Store and Secrets Manager.
package retriever
import (
"context"
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/spf13/viper"
)
func init() {
log.SetFlags(log.Lshortfile)
}
// getParam retrieves the specified secret from Parameter Store.
func getParam(ctx context.Context, c *ssm.Client, p string) (*ssm.GetParameterOutput, error) {
i := ssm.GetParameterInput{
Name: aws.String(p),
WithDecryption: true,
}
out, err := c.GetParameter(ctx, &i)
if err != nil {
return nil, err
}
return out, nil
}
// getSecret retrieves the specified secret from Secrets Manager.
func getSecret(ctx context.Context, c *secretsmanager.Client, p string) (*secretsmanager.GetSecretValueOutput, error) {
i := secretsmanager.GetSecretValueInput{
SecretId: aws.String(p),
}
out, err := c.GetSecretValue(ctx, &i)
if err != nil {
return nil, err
}
return out, nil
}
func configure() (*viper.Viper, error) {
rtvrViper := viper.New()
rtvrViper.SetConfigName("retriever")
rtvrViper.SetConfigType("yaml")
rtvrViper.AddConfigPath(".")
rtvrViper.SetEnvPrefix("RTVR")
rtvrViper.AutomaticEnv()
cfg := rtvrViper.GetString("conf")
if cfg != "" {
rtvrViper.SetConfigFile(cfg)
}
if err := rtvrViper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("retriever config not found; using environment")
} else {
return nil, err
}
}
return rtvrViper, nil
}
// Fetch retrieves secrets specified via configuration or environment
// from Parameter Store or Secrets Manager and returns a map with
// secret names as keys.
func Fetch() (map[string]string, error) {
creds := make(map[string]string)
ctx := context.TODO()
v, err := configure()
if err != nil {
return nil, err
}
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil, fmt.Errorf("ERROR: unable to load AWS configuration: %v", err.Error())
}
p := v.GetString("prefix")
t := strings.ToLower(v.GetString("type"))
if t == "parameter" {
client := ssm.NewFromConfig(cfg)
for _, v := range v.GetStringSlice("credentials") {
res, err := getParam(ctx, client, fmt.Sprintf("%s/%s", p, v))
if err != nil {
return nil, fmt.Errorf("ERROR: unable to retrieve %v/%v (%v)", p, v, err.Error())
}
creds[v] = aws.ToString(res.Parameter.Value)
}
} else if t == "secret" {
client := secretsmanager.NewFromConfig(cfg)
for _, v := range v.GetStringSlice("credentials") {
res, err := getSecret(ctx, client, fmt.Sprintf("%s/%s", p, v))
if err != nil {
return nil, fmt.Errorf("ERROR: unable to retrieve %v/%v (%v)", p, v, err.Error())
}
creds[v] = aws.ToString(res.SecretString)
}
} else {
fmt.Printf("DEBUG: %+v", v.ConfigFileUsed())
return nil, fmt.Errorf("ERROR: unknown secret type \"%v\"", t)
}
return creds, nil
}