This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcloud_env.go
310 lines (282 loc) · 8.27 KB
/
cloud_env.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* © Copyright IBM Corp. 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package IBMCloudEnv
import (
"encoding/json"
"fmt"
"github.com/oliveagle/jsonpath"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"io/ioutil"
"os"
"strings"
)
const PREFIX_PATTERN_CF = "cloudfoundry"
const PREFIX_PATTERN_ENV = "env"
const PREFIX_PATTERN_FILE = "file"
const PREFIX_PATTERN_USER = "user-provided"
var loadedMappings = make(map[string]interface{})
func Initialize(mappingsFilePath string) string {
json, err := ioutil.ReadFile(mappingsFilePath)
if err != nil {
log.Error(err)
}
dir, err := os.Getwd()
if err != nil {
log.Error(err)
}
mappingsFilePath = dir + mappingsFilePath
result := gjson.Parse(string(json))
version := result.Get("version").Int()
result.ForEach(func(key, value gjson.Result) bool {
if !result.Get("version").Exists() {
processMapping(key.String(), value)
} else if version == 1 {
processMapping(key.String(), value)
} else if version == 2 {
processMappingV2(key.String(), value)
}
return true
})
return mappingsFilePath
}
func processMapping(mappingName string, config gjson.Result) bool {
searchPatterns := config.Get("searchPatterns")
if !searchPatterns.Exists() || len(searchPatterns.Array()) == 0 {
log.Warnln("No searchPatterns found for mapping", mappingName)
return false
}
searchPatterns.ForEach(func(_, searchPattern gjson.Result) bool {
value, OK := processSearchPattern(mappingName, searchPattern.String())
if OK {
loadedMappings[mappingName] = value
return false
} else {
return true
}
})
return true
}
func processMappingV2(mappingName string, config gjson.Result) {
config.ForEach(func(key, value gjson.Result) bool {
searchPatterns := value.Get("searchPatterns")
if !searchPatterns.Exists() || len(searchPatterns.Array()) == 0 {
log.Warningln("No credentials found uusing searchPatterns under ", mappingName)
}
searchPatterns.ForEach(func(_, searchPattern gjson.Result) bool {
value, ok := processSearchPattern(fmt.Sprintf("$%s[%s]", mappingName, key.String()), searchPattern.String())
if ok {
_, exists := loadedMappings[mappingName]
if !exists {
loadedMappings[mappingName] = make(map[string]string)
}
loadedMappings[mappingName].(map[string]string)[key.String()] = value
return false
} else {
return true
}
})
return true
})
}
func processSearchPattern(mappingName string, searchPattern string) (string, bool) {
patternComponents := strings.Split(searchPattern, ":")
value := ""
OK := false
switch patternComponents[0] {
case PREFIX_PATTERN_FILE:
value, OK = processFileSearchPattern(patternComponents)
case PREFIX_PATTERN_CF:
value, OK = processCFSearchPattern(patternComponents)
case PREFIX_PATTERN_ENV:
value, OK = processEnvSearchPattern(patternComponents)
case PREFIX_PATTERN_USER:
value, OK = processUserProvidedSearchPattern(patternComponents)
default:
log.Warnln("Unknown searchPattern prefix", patternComponents[0], "Supported prefixes: user-provided, cloudfoundry, env, file")
return "", false
}
if !OK {
return "", false
}
return value, true
}
func processFileSearchPattern(patternComponents []string) (string, bool) {
filePath, _ := os.Getwd()
if _, err := os.Stat(filePath); err != nil {
log.Errorln("File does not exist", filePath)
return "", false
} else {
fullPathName := filePath + patternComponents[1]
json, err := ioutil.ReadFile(fullPathName)
if err != nil {
log.Errorln(err)
return "", false
}
if len(patternComponents) == 3 {
return processJSONPath(string(json), patternComponents[2])
} else {
return string(json), true
}
}
}
func processCFSearchPattern(patternComponents []string) (string, bool) {
vcapServicesString, ok_service := os.LookupEnv("VCAP_SERVICES")
vcapApplicationString, ok_app := os.LookupEnv("VCAP_APPLICATION")
if !ok_service && !ok_app {
return "", false
} else {
if patternComponents[1][0] == '$' {
value, OK := processJSONPath(vcapServicesString, patternComponents[1])
if OK {
return value, true
} else {
return processJSONPath(vcapApplicationString, patternComponents[1])
}
} else {
// patternComponents[1] is a service instance name, find it in VCAP_SERVICES and return credentials object
json := gjson.Parse(vcapServicesString)
res, ok := "", false
json.ForEach(func(k, v gjson.Result) bool {
v.ForEach(func(_, item gjson.Result) bool {
if item.Get("name").String() == patternComponents[1] {
res, ok = item.Get("credentials").String(), true
return false
} else {
return true
}
})
if ok {
return false
} else {
return true
}
})
return res, ok
}
}
}
func processEnvSearchPattern(patternComponents []string) (string, bool) {
value, OK := os.LookupEnv(patternComponents[1])
if OK && (len(patternComponents) == 3) {
return processJSONPath(value, patternComponents[2])
}
return value, OK
}
func processUserProvidedSearchPattern(patternComponents []string) (string, bool) {
vcapServicesString, ok := os.LookupEnv("VCAP_SERVICES")
if !ok {
return "", false
}
if len(patternComponents) == 3 {
serviceName := patternComponents[1]
return processJSONCredentials(vcapServicesString, serviceName, patternComponents[2])
}
return "", false
}
func processJSONCredentials(jsonString, servicename, credkey string) (string, bool) {
if !gjson.Valid(jsonString) {
log.Errorln("Failed to apply JSONPath", jsonString)
return "", false
}
jsonObj := gjson.Parse(jsonString)
credArray := jsonObj.Get(PREFIX_PATTERN_USER)
ret, ok := "", false
credArray.ForEach(func(_, searchPattern gjson.Result) bool {
if searchPattern.Get("name").String() == servicename {
ret, ok = deepSearch(searchPattern, credkey)
return !ok
}
return true
})
return ret, ok
}
func deepSearch(current gjson.Result, search string) (string, bool) {
res := current.Get(search).String()
ok := false
if current.Get(search).Exists() {
return res, true
}
_, isMap := current.Value().(map[string]interface{})
_, isArr := current.Value().([]interface{})
if isMap || isArr {
current.ForEach(func(_, v gjson.Result) bool {
res, ok = deepSearch(v, search)
return !ok
})
}
return res, ok
}
func processJSONPath(jsonString string, jsonPath string) (string, bool) {
var json_data interface{}
err := json.Unmarshal([]byte(jsonString), &json_data)
if err != nil {
return "", false
}
res, err := jsonpath.JsonPathLookup(json_data, jsonPath)
_, isMap := res.(map[string]interface{})
_, isArr := res.([]interface{})
if isMap || isArr {
test, _ := json.Marshal(res)
return string(test), err == nil
}
return fmt.Sprintf("%v", res), err == nil
}
func GetCredentialsForService(serviceTag, serviceLabel, credentials string) map[string]string {
creds_json := gjson.Parse(credentials)
creds := make(map[string]string)
key := serviceTag + "_" + serviceLabel + "_"
if creds_json.Exists() {
creds_json.ForEach(func(k, v gjson.Result) bool {
if strings.Index(k.String(), key) == 0 {
credKey := k.String()[len(key):]
if credKey == "apikey" && serviceTag == "watson" {
creds["iam_"+credKey] = v.String()
} else {
creds[credKey] = v.String()
}
}
return true
})
}
return creds
}
func GetString(name string) (string, bool) {
val, ok := loadedMappings[name]
if !ok {
return "", false
}
_, isStr := val.(string)
if !isStr {
bytes, _ := json.Marshal(val)
return string(bytes), true
}
return val.(string), true
}
func GetDictionary(name string) gjson.Result {
value, ok := GetString(name)
if !ok {
log.Warnln(value + " does not exist")
return gjson.Parse("{\"value\": \"" + value + "\"}")
} else {
if gjson.Valid(value) {
return gjson.Parse(value)
} else {
//value is not a valid json object, return object
return gjson.Parse("{\"value\": \"" + value + "\"}")
}
}
}