forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
103 lines (92 loc) · 2.51 KB
/
client.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
package ssm
import (
"os"
"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/ssm"
"github.com/kelseyhightower/confd/log"
)
type Client struct {
client *ssm.SSM
}
func New() (*Client, error) {
// Create a session to share configuration, and load external configuration.
sess := session.Must(session.NewSession())
// Fail early, if no credentials can be found
_, err := sess.Config.Credentials.Get()
if err != nil {
return nil, err
}
var c *aws.Config
if os.Getenv("SSM_LOCAL") != "" {
log.Debug("SSM_LOCAL is set")
endpoint := "http://localhost:8001"
c = &aws.Config{
Endpoint: &endpoint,
}
} else {
c = nil
}
// Create the service's client with the session.
svc := ssm.New(sess, c)
return &Client{svc}, nil
}
// GetValues retrieves the values for the given keys from AWS SSM Parameter Store
func (c *Client) GetValues(keys []string) (map[string]string, error) {
vars := make(map[string]string)
var err error
for _, key := range keys {
log.Debug("Processing key=%s", key)
var resp map[string]string
resp, err = c.getParametersWithPrefix(key)
if err != nil {
return vars, err
}
if len(resp) == 0 {
resp, err = c.getParameter(key)
if err != nil && err.(awserr.Error).Code() != ssm.ErrCodeParameterNotFound {
return vars, err
}
}
for k, v := range resp {
vars[k] = v
}
}
return vars, nil
}
func (c *Client) getParametersWithPrefix(prefix string) (map[string]string, error) {
var err error
parameters := make(map[string]string)
params := &ssm.GetParametersByPathInput{
Path: aws.String(prefix),
Recursive: aws.Bool(true),
WithDecryption: aws.Bool(true),
}
c.client.GetParametersByPathPages(params,
func(page *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, p := range page.Parameters {
parameters[*p.Name] = *p.Value
}
return !lastPage
})
return parameters, err
}
func (c *Client) getParameter(name string) (map[string]string, error) {
parameters := make(map[string]string)
params := &ssm.GetParameterInput{
Name: aws.String(name),
WithDecryption: aws.Bool(true),
}
resp, err := c.client.GetParameter(params)
if err != nil {
return parameters, err
}
parameters[*resp.Parameter.Name] = *resp.Parameter.Value
return parameters, nil
}
// WatchPrefix is not implemented
func (c *Client) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
<-stopChan
return 0, nil
}