-
Notifications
You must be signed in to change notification settings - Fork 0
/
envinject.go
165 lines (129 loc) · 3.68 KB
/
envinject.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
package envinject
import (
"fmt"
"os"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"strings"
)
const ParamPathEnvVar = "AWS_PARAM_STORE_PATH"
type InjectedEnv struct {
passThrough bool
environment map[string]string
}
func makePassThroughEnv() *InjectedEnv {
injectEnv := InjectedEnv{
passThrough: true,
environment: make(map[string]string),
}
return &injectEnv
}
func makeInjectedEnv() *InjectedEnv {
injectEnv := InjectedEnv{
passThrough: false,
environment: make(map[string]string),
}
return &injectEnv
}
func (i *InjectedEnv) InjectVar(name, value string) {
if i.passThrough != true {
i.environment[name] = value
}
}
// Getenv retrieves the value of the environment variable named by the key.
// It returns the value, which will be empty if the variable is not present
// in either the injected env or the external environment.
// To distinguish between an empty value and an unset value, use LookupEnv.
// Note: spec borrowed from golang.org os.Getenv
func (i *InjectedEnv) Getenv(name string) string {
if i.passThrough == true {
return os.Getenv(name)
}
v, ok := i.environment[name]
if ok {
return v
}
return os.Getenv(name)
}
// LookupEnv retrieves the value of the environment variable named by the key. If
// the variable is present in the environment the value (which may be empty)
// is returned and the boolean is true. Otherwise the returned
// value will be empty and the boolean will be false.
// Note: spec borrowed from golang.org os.LookupEnv
func (i *InjectedEnv) LookupEnv(name string) (string, bool) {
if i.passThrough == true {
return os.LookupEnv(name)
}
v, ok := i.environment[name]
if ok {
return v, ok
}
return os.LookupEnv(name)
}
// Environ returns a copy of strings representing the environment, in the form "key=value".
// Note: spec borrowed from golang.org os.Environ
func (i *InjectedEnv) Environ() []string {
if i.passThrough == true {
return os.Environ()
}
//Baseline is the environment
env := os.Environ()
//Overwrite with param store
for k, v := range i.environment {
env = append(env,
fmt.Sprintf("%s=%s", k, v),
)
}
return env
}
func NewInjectedEnv() (*InjectedEnv, error) {
//Need a parameter path if we are reading from the SSM parameter store
paramPath := os.Getenv(ParamPathEnvVar)
if paramPath == "" {
log.Infof("%s env variable not set - reading configuration from os environment.", ParamPathEnvVar)
return makePassThroughEnv(), nil
}
//Parameter store is indicated - create a session
log.Infof("Looking for parameters starting with %s", paramPath)
log.Info("Create AWS session")
sess, err := session.NewSession()
if err != nil {
return nil, err
}
//Read the params and inject them into the environment
svc := ssm.New(sess)
getParamsInput := &ssm.GetParametersByPathInput{
Path:aws.String(paramPath),
WithDecryption:aws.Bool(true),
}
injected := makeInjectedEnv()
for {
resp, err := svc.GetParametersByPath(getParamsInput)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
return nil, err
}
params := resp.Parameters
for _, p := range params {
//Guard for the prefix strip below
if !strings.HasPrefix(*p.Name, paramPath) {
log.Infof("skipping %s", *p.Name)
continue
}
keyMinusPrefix := (*p.Name)[len(paramPath) + 1:]
log.Infof("Injecting %s as %s", *p.Name, keyMinusPrefix)
injected.InjectVar(keyMinusPrefix, *p.Value)
}
nextToken := resp.NextToken
if nextToken == nil {
break
}
getParamsInput = &ssm.GetParametersByPathInput{
NextToken: nextToken,
}
}
return injected, nil
}