/
environment.go
221 lines (164 loc) · 7.98 KB
/
environment.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package serializer
import (
"errors"
"github.com/engi-fyi/go-credentials/global"
"os"
"strings"
)
/*
ToEnv is responsible for serializing a Credential/Profile combination into the environment. Both of the credential
values are serialized as so:
APPLICATION_NAME::PROFILE_NAME::USERNAME (or alternate)
APPLICATION_NAME::PROFILE_NAME::PASSWORD (or alternate)
Profile attributes are also added to the profile in the following format:
APPLICATION_NAME::PROFILE_NAME::ATTRIBUTE::SECTION_NAME::KEY_VALUE
SECTION_NAME can be blank.
*/
func (thisSerializer *Serializer) ToEnv(username string, password string, attributes map[string]map[string]string) error {
thisSerializer.Factory.Log.Info().Msg("Serializing credential and profile to environment.")
credentialErr := thisSerializer.saveCredentialEnv(username, password)
if credentialErr != nil {
return credentialErr
}
profileErr := thisSerializer.saveProfileEnv(attributes)
if profileErr != nil {
return profileErr
}
return nil
}
func (thisSerializer *Serializer) saveCredentialEnv(username string, password string) error {
prefix := thisSerializer.getEnvPrefix()
usernameKey := strings.ToUpper(prefix + thisSerializer.Factory.GetAlternateUsername())
thisSerializer.Factory.Log.Trace().Str("key", usernameKey).Msg("Setting username environment variable.")
setErr := os.Setenv(usernameKey, username)
if setErr != nil {
thisSerializer.Factory.Log.Error().Err(setErr).Msg("Error setting username in environment.")
return setErr
}
thisSerializer.Factory.Log.Info().Msg("Username set.")
passwordKey := strings.ToUpper(prefix + thisSerializer.Factory.GetAlternatePassword())
thisSerializer.Factory.Log.Trace().Str("key", passwordKey).Msg("Setting password environment variable.")
setErr = os.Setenv(passwordKey, password)
if setErr != nil {
thisSerializer.Factory.Log.Error().Err(setErr).Msg("Error setting password in environment.")
return setErr
}
thisSerializer.Factory.Log.Info().Msg("Password set.")
return nil
}
func (thisSerializer *Serializer) saveProfileEnv(attributes map[string]map[string]string) error {
prefix := thisSerializer.getEnvPrefix()
for key, value := range attributes {
for subKey, subValue := range value {
fullKey := prefix + "ATTRIBUTE::" + strings.ToUpper(key) + "::" + strings.ToUpper(subKey)
thisSerializer.Factory.Log.Trace().Str("key", fullKey).Msg("Setting attribute environment variable.")
setErr := os.Setenv(fullKey, subValue)
if setErr != nil {
return setErr
}
}
}
thisSerializer.Factory.Log.Trace().Msg("Attribute environment variables set successfully.")
return nil
}
/*
FromEnv is responsible for scanning environment variables and retrieves applicable variables that have
the prefix of applicationName and that have at least two "::" in them (which is the separator). The format for an
environment variable managed by serializer is:
APPLICATION_NAME::PROFILE_NAME::FIELD_TYPE::SECTION_NAME::KEY_VALUE
Important to note is that if SECTION_NAME is blank then the default profile will be filled, and FIELD_TYPE can be one
of three values which are USERNAME, PASSWORD, or ATTRIBUTE. If the FIELD_TYPE is ATTRIBUTE, then KEY_VALUE is mandatory.
*/
func (thisSerializer *Serializer) FromEnv() (string, string, map[string]map[string]string, error) {
thisSerializer.Factory.Log.Info().Msg("Deserializing credential and profile from environment.")
parsedVariables, parseErr := thisSerializer.loadVariablesEnv()
if parseErr != nil {
return "", "", make(map[string]map[string]string), parseErr
}
username, password, credErr := thisSerializer.loadCredentialEnv(parsedVariables[global.NO_SECTION_KEY])
if credErr != nil {
return "", "", make(map[string]map[string]string), credErr
}
attributes, attrErr := thisSerializer.loadProfileEnv(parsedVariables)
if attrErr != nil {
return "", "", make(map[string]map[string]string), attrErr
}
return username, password, attributes, nil
}
func (thisSerializer *Serializer) loadCredentialEnv(parsedVariables map[string]string) (string, string, error) {
if _, exists := parsedVariables[thisSerializer.Factory.GetAlternateUsername()]; !exists {
return "", "", errors.New(ERR_REQUIRED_VARIABLE_USERNAME_NOT_FOUND)
}
thisSerializer.Factory.Log.Debug().Str("username_label", thisSerializer.Factory.GetAlternateUsername()).Msg("Found username label.")
if _, exists := parsedVariables[thisSerializer.Factory.GetAlternatePassword()]; !exists {
return "", "", errors.New(ERR_REQUIRED_VARIABLE_PASSWORD_NOT_FOUND)
}
thisSerializer.Factory.Log.Debug().Str("password_label", thisSerializer.Factory.GetAlternatePassword()).Msg("Found password label.")
return parsedVariables[thisSerializer.Factory.GetAlternateUsername()],
parsedVariables[thisSerializer.Factory.GetAlternatePassword()],
nil
}
// currently only supports default profile, maybe change key name at some point in future
// like maybe APP_NAME_ATTRIBUTE::SECTION_NAME::VARIABLE_NAME
func (thisSerializer *Serializer) loadProfileEnv(parsedVariables map[string]map[string]string) (map[string]map[string]string, error) {
delete(parsedVariables[global.NO_SECTION_KEY], thisSerializer.Factory.GetAlternateUsername())
delete(parsedVariables[global.NO_SECTION_KEY], thisSerializer.Factory.GetAlternatePassword())
return parsedVariables, nil
}
func (thisSerializer *Serializer) loadVariablesEnv() (map[string]map[string]string, error) {
envVariables := os.Environ()
parsedVariables := make(map[string]map[string]string)
parsedVariables[global.NO_SECTION_KEY] = make(map[string]string)
for i := range envVariables {
splitIndex := strings.Index(envVariables[i], "=")
key := strings.ToUpper(envVariables[i][0:splitIndex])
value := envVariables[i][splitIndex+1 : len(envVariables[i])]
profileName, fieldName, sectionName, didParse := thisSerializer.ParseEnvironmentVariable(key)
if didParse {
if profileName == thisSerializer.ProfileName {
if sectionName == "" {
parsedVariables[global.NO_SECTION_KEY][fieldName] = value
} else {
if _, ok := parsedVariables[sectionName]; !ok {
parsedVariables[sectionName] = make(map[string]string)
}
parsedVariables[sectionName][fieldName] = value
}
}
}
}
return parsedVariables, nil
}
func (thisSerializer *Serializer) getEnvPrefix() string {
return strings.ToUpper(thisSerializer.Factory.ApplicationName) + "::" + strings.ToUpper(thisSerializer.ProfileName) + "::"
}
/*
ParseEnvironmentVariable is able to process an environment variable and see if matches the expected format for our
environment variables.
The format for an environment variable managed by serializer is:
APPLICATION_NAME::PROFILE_NAME::FIELD_TYPE::SECTION_NAME::KEY_VALUE
Important to note is that if SECTION_NAME is blank then the default profile will be filled, and FIELD_TYPE can be one
of three values which are USERNAME, PASSWORD, or ATTRIBUTE. If the FIELD_TYPE is ATTRIBUTE, then KEY_VALUE is mandatory.
*/
func (thisSerializer *Serializer) ParseEnvironmentVariable(environmentVariable string) (string, string, string, bool) {
if strings.Count(environmentVariable, "::") < 2 {
return "", "", "", false
}
if len(environmentVariable) < len(thisSerializer.Factory.ApplicationName+"::") {
return "", "", "", false
}
environmentVariable = environmentVariable[strings.Index(environmentVariable, "::")+2:]
profile := strings.ToLower(environmentVariable[0:strings.Index(environmentVariable, "::")])
environmentVariable = environmentVariable[strings.Index(environmentVariable, "::")+2:]
fieldName := strings.ToLower(environmentVariable)
if strings.Count(environmentVariable, "::") > 1 {
fieldName = strings.ToLower(environmentVariable[0:strings.Index(environmentVariable, "::")])
}
if fieldName != "attribute" {
return profile, fieldName, "", true
}
environmentVariable = environmentVariable[strings.Index(environmentVariable, "::")+2:]
sectionName := strings.ToLower(environmentVariable[0:strings.Index(environmentVariable, "::")])
fieldName = strings.ToLower(environmentVariable[strings.Index(environmentVariable, "::")+2:])
return profile, fieldName, sectionName, true
}