forked from gmr/env-aws-params
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssm.go
75 lines (62 loc) · 1.58 KB
/
ssm.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
package main
import (
"fmt"
"os"
"strings"
"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"
log "github.com/sirupsen/logrus"
)
type SSMClient struct {
client *ssm.SSM
}
func NewSSMClient(region string) (*SSMClient, error) {
var config *aws.Config
awsSession := session.Must(session.NewSession(
&aws.Config{Region: aws.String(region)}))
_, err := awsSession.Config.Credentials.Get()
if err != nil {
return nil, err
}
config = nil
endpoint := os.Getenv("SSM_ENDPOINT")
if endpoint != "" {
config = &aws.Config{
Endpoint: &endpoint,
}
}
client := ssm.New(awsSession, config)
return &SSMClient{client}, nil
}
func (c *SSMClient) GetParametersByPath(path string) (map[string]string, error) {
if strings.HasSuffix(path, "/") != true {
path = fmt.Sprintf("%s/", path)
}
var nextToken *string
parameters := make(map[string]string)
for {
params := &ssm.GetParametersByPathInput{
Path: aws.String(path),
Recursive: aws.Bool(true),
WithDecryption: aws.Bool(true),
MaxResults: aws.Int64(10),
NextToken: nextToken,
}
response, err := c.client.GetParametersByPath(params)
if err != nil {
awsErr, _ := err.(awserr.Error)
log.Errorf("Error Getting Parameters from SSM: %s", awsErr.Code())
return nil, err
}
for _, p := range response.Parameters {
parameters[strings.TrimPrefix(*p.Name, path)] = *p.Value
}
if response.NextToken == nil {
break
}
nextToken = response.NextToken
}
return parameters, nil
}