This repository was archived by the owner on Jul 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
186 lines (160 loc) · 4.8 KB
/
Copy pathclient.go
File metadata and controls
186 lines (160 loc) · 4.8 KB
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
package awsconfig
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
type awsLoader struct {
environment string
serviceName string
client *ssm.SSM
config map[string]string
}
// NewAWSLoader creates a Loader that will cache the provided namespace on initialization
// and return data from that cache on Get
func NewAWSLoader(environment, serviceName string) Loader {
ret := &awsLoader{
environment: environment,
serviceName: serviceName,
config: make(map[string]string),
}
ret.client = ssm.New(session.Must(session.NewSession()))
return ret
}
// Import loads key values into parameter store at /env/serviceName/key
func (a *awsLoader) Import(data []byte) error {
conf := make(map[string]*json.RawMessage)
err := json.Unmarshal(data, &conf)
if err != nil {
return fmt.Errorf("Unable to parse json data: %v", err)
}
for k, v := range conf {
if v != nil {
// strings will be wrapped in quotes; remove them.
value := *v
// Parameter store doesn't allow storing empty strings. We store a space and it will be stripped during Initialize()
if string(value) == `""` {
value = json.RawMessage(`" "`)
}
if len(value) > 0 {
if value[0] == '"' && value[len(value)-1] == '"' {
value = value[1 : len(value)-1]
}
}
err = a.Put(k, value)
if err != nil {
return fmt.Errorf("Error writing key (%s) to parameter store: %v", k, err)
}
}
}
return nil
}
// Initialize
func (a *awsLoader) Initialize() error {
// pull all the config down for this service
prefix := "/" + a.environment + "/" + a.serviceName + "/"
globalPrefix := "/" + a.environment + "/global/"
var err error
a.config, err = a.pullConfigWithPrefix(globalPrefix, nil) // pull global config
if err != nil {
return err
}
serviceConfig, err := a.pullConfigWithPrefix(prefix, nil) // pull service specific config
if err != nil {
return err
}
for k, v := range serviceConfig {
a.config[k] = v
}
return nil
}
func (a *awsLoader) pullConfigWithPrefix(prefix string, nextToken *string) (map[string]string, error) {
result := make(map[string]string)
getParamInput := &ssm.GetParametersByPathInput{
Path: aws.String(prefix),
WithDecryption: aws.Bool(true),
Recursive: aws.Bool(true),
NextToken: nextToken,
}
paramOut, err := a.client.GetParametersByPath(getParamInput)
if err != nil {
return nil, err
}
for _, v := range paramOut.Parameters {
result[strings.Replace(*v.Name, prefix, "", 1)] = strings.TrimSpace(*v.Value)
}
if paramOut.NextToken != nil {
ret, err := a.pullConfigWithPrefix(prefix, paramOut.NextToken)
if err != nil {
return nil, err
}
for k, v := range ret {
result[k] = v
}
}
return result, nil
}
// Put a value to a key
func (a *awsLoader) Put(key string, value []byte) error {
fullKey := fmt.Sprintf("/%s/%s/%s", a.environment, a.serviceName, key)
putParamInput := &ssm.PutParameterInput{
Name: aws.String(fullKey),
Type: aws.String(ssm.ParameterTypeSecureString),
Value: aws.String(string(value)),
Overwrite: aws.Bool(true),
}
// PutParamter returns the version number of the param, which is not useful
_, err := a.client.PutParameter(putParamInput)
if err != nil {
return err
}
return nil
}
// Get fetches the raw config from the environment
func (a *awsLoader) Get(key string) ([]byte, error) {
val, ok := a.config[key]
if !ok {
return nil, fmt.Errorf("[%s] Could not find value for key: %s", a.serviceName, key)
}
return []byte(val), nil
}
// MustGetString fetches the config and parses it into a string. Panics on failure.
func (a *awsLoader) MustGetString(key string) string {
val, ok := a.config[key]
if !ok {
panic(fmt.Sprintf("[%s] Could not fetch config (%s)", a.serviceName, key))
}
return val
}
// MustGetBool fetches the config and parses it into a bool. Panics on failure.
func (a *awsLoader) MustGetBool(key string) bool {
v := a.MustGetString(key)
ret, err := strconv.ParseBool(v)
if err != nil {
panic(fmt.Sprintf("[%s] Config value at (%s) was not a bool: %v", a.serviceName, key, err))
}
return ret
}
// MustGetInt fetches the config and parses it into an int. Panics on failure.
func (a *awsLoader) MustGetInt(key string) int {
v := a.MustGetString(key)
ret, err := strconv.Atoi(v)
if err != nil {
panic(fmt.Sprintf("[%s] Config value at (%s) was not an int: %v", a.serviceName, key, err))
}
return ret
}
// MustGetDuration fetches the config and parses it into a duration. Panics on failure.
func (a *awsLoader) MustGetDuration(key string) time.Duration {
s := a.MustGetString(key)
ret, err := time.ParseDuration(s)
if err != nil {
panic(fmt.Sprintf("[%s] Could not parse config (%s) into a duration: %v", a.serviceName, key, err))
}
return ret
}