/
map.go
523 lines (456 loc) 路 13.4 KB
/
map.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright 2016-2018, Pulumi Corporation.
//
// 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 config
import (
"encoding/json"
"fmt"
"strconv"
"github.com/pulumi/pulumi/pkg/util/contract"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/resource"
)
var errSecureKeyReserved = errors.New(`"secure" key in maps of length 1 are reserved`)
// Map is a bag of config stored in the settings file.
type Map map[Key]Value
// Decrypt returns the configuration as a map from module member to decrypted value.
func (m Map) Decrypt(decrypter Decrypter) (map[Key]string, error) {
r := map[Key]string{}
for k, c := range m {
v, err := c.Value(decrypter)
if err != nil {
return nil, err
}
r[k] = v
}
return r, nil
}
// HasSecureValue returns true if the config map contains a secure (encrypted) value.
func (m Map) HasSecureValue() bool {
for _, v := range m {
if v.Secure() {
return true
}
}
return false
}
// Get gets the value for a given key. If path is true, the key's name portion is treated as a path.
func (m Map) Get(k Key, path bool) (Value, bool, error) {
// If the key isn't a path, go ahead and lookup the value.
if !path {
v, ok := m[k]
return v, ok, nil
}
// Otherwise, parse the path and get the new config key.
p, configKey, err := parseKeyPath(k)
if err != nil {
return Value{}, false, err
}
// If we only have a single path segment, go ahead and lookup the value.
if len(p) == 1 {
v, ok := m[configKey]
return v, ok, nil
}
// Otherwise, lookup the current root value and save it into a temporary map.
root := make(map[string]interface{})
if val, ok := m[configKey]; ok {
obj, err := val.ToObject()
if err != nil {
return Value{}, false, err
}
root[configKey.Name()] = obj
}
// Get the value within the object.
_, v, ok := getValueForPath(root, p)
if !ok {
return Value{}, false, nil
}
// If the value is a secure value, return it as one.
if is, s := isSecureValue(v); is {
return NewSecureValue(s), true, nil
}
// If it's a simple type, return it as a regular value.
switch t := v.(type) {
case string:
return NewValue(t), true, nil
case bool, int, uint, int32, uint32, int64, uint64, float32, float64:
return NewValue(fmt.Sprintf("%v", v)), true, nil
}
// Otherwise, return it as an object value.
json, err := json.Marshal(v)
if err != nil {
return Value{}, false, err
}
if hasSecureValue(v) {
return NewSecureObjectValue(string(json)), true, nil
}
return NewObjectValue(string(json)), true, nil
}
// Remove removes the value for a given key. If path is true, the key's name portion is treated as a path.
func (m Map) Remove(k Key, path bool) error {
// If the key isn't a path, go ahead and delete it and return.
if !path {
delete(m, k)
return nil
}
// Parse the path.
p, err := resource.ParsePropertyPath(k.Name())
if err != nil {
return errors.Wrap(err, "invalid config key path")
}
if len(p) == 0 {
return nil
}
firstKey, ok := p[0].(string)
if !ok || firstKey == "" {
return nil
}
configKey := MustMakeKey(k.Namespace(), firstKey)
// If we only have a single path segment, delete the key and return.
if len(p) == 1 {
delete(m, configKey)
return nil
}
// Otherwise, lookup the current root value and save it into a temporary map.
root := make(map[string]interface{})
if val, ok := m[configKey]; ok {
obj, err := val.ToObject()
if err != nil {
return err
}
root[configKey.Name()] = obj
}
// Get the value within the object up to the second-to-last path segment.
// If not found, exit early.
parent, dest, ok := getValueForPath(root, p[:len(p)-1])
if !ok {
return nil
}
// Remove the last path segment.
key := p[len(p)-1]
switch t := dest.(type) {
case []interface{}:
index, ok := key.(int)
if !ok || index < 0 || index >= len(t) {
return nil
}
t = append(t[:index], t[index+1:]...)
// Since we changed the array, we need to update the parent.
if parent != nil {
pkey := p[len(p)-2]
if _, err := setValue(parent, pkey, t, nil, nil); err != nil {
return err
}
}
case map[string]interface{}:
k, ok := key.(string)
if !ok {
return nil
}
delete(t, k)
// Secure values are reserved, so return an error when attempting to add one.
if isSecure, _ := isSecureValue(t); isSecure {
return errSecureKeyReserved
}
}
// Now, marshal then unmarshal the value, which will handle detecting
// whether it's a secure object or not.
jsonBytes, err := json.Marshal(root[configKey.Name()])
if err != nil {
return err
}
var v Value
if err = json.Unmarshal(jsonBytes, &v); err != nil {
return err
}
m[configKey] = v
return nil
}
// Set sets the value for a given key. If path is true, the key's name portion is treated as a path.
func (m Map) Set(k Key, v Value, path bool) error {
// If the key isn't a path, go ahead and set the value and return.
if !path {
m[k] = v
return nil
}
// Otherwise, parse the path and get the new config key.
p, configKey, err := parseKeyPath(k)
if err != nil {
return err
}
// If we only have a single path segment, set the value and return.
if len(p) == 1 {
m[configKey] = v
return nil
}
// Otherwise, lookup the current value and save it into a temporary map.
root := make(map[string]interface{})
if val, ok := m[configKey]; ok {
obj, err := val.ToObject()
if err != nil {
return err
}
// If obj is a string, set it to nil, which allows overwriting the existing
// top-level string value in the first iteration of the loop below.
if _, ok := obj.(string); ok {
obj = nil
}
root[configKey.Name()] = obj
}
// Now, loop through the path segments, and walk the object tree.
// If the value for a given segment is nil, create a new array/map.
// The root map is the initial cursor value, and parent is nil.
var parent interface{}
var parentKey interface{}
var cursor interface{}
var cursorKey interface{}
cursor = root
cursorKey = p[0]
for _, pkey := range p[1:] {
pvalue, err := getValue(cursor, cursorKey)
if err != nil {
return err
}
// If the value is nil, create a new array/map.
// Otherwise, return an error due to the type mismatch.
var newValue interface{}
switch pkey.(type) {
case int:
if pvalue == nil {
newValue = make([]interface{}, 0)
} else if _, ok := pvalue.([]interface{}); !ok {
return errors.Errorf("an array was expected for index %v", pkey)
}
case string:
if pvalue == nil {
newValue = make(map[string]interface{})
} else if _, ok := pvalue.(map[string]interface{}); !ok {
return errors.Errorf("a map was expected for key %q", pkey)
}
default:
contract.Failf("unexpected path type")
}
if newValue != nil {
pvalue = newValue
cursor, err = setValue(cursor, cursorKey, pvalue, parent, parentKey)
if err != nil {
return err
}
}
parent = cursor
parentKey = cursorKey
cursor = pvalue
cursorKey = pkey
}
// Adjust the value (e.g. convert "true"/"false" to booleans and integers to ints) and set it.
adjustedValue := adjustObjectValue(v, path)
if _, err = setValue(cursor, cursorKey, adjustedValue, parent, parentKey); err != nil {
return err
}
// Secure values are reserved, so return an error when attempting to add one.
if isSecure, _ := isSecureValue(cursor); isSecure {
return errSecureKeyReserved
}
// Serialize the updated object as JSON, and save it in the config map.
json, err := json.Marshal(root[configKey.Name()])
if err != nil {
return err
}
if v.Secure() {
m[configKey] = NewSecureObjectValue(string(json))
} else {
m[configKey] = NewObjectValue(string(json))
}
return nil
}
func (m Map) MarshalJSON() ([]byte, error) {
rawMap := make(map[string]Value, len(m))
for k, v := range m {
rawMap[k.String()] = v
}
return json.Marshal(rawMap)
}
func (m *Map) UnmarshalJSON(b []byte) error {
rawMap := make(map[string]Value)
if err := json.Unmarshal(b, &rawMap); err != nil {
return errors.Wrap(err, "could not unmarshal map")
}
newMap := make(Map, len(rawMap))
for k, v := range rawMap {
pk, err := ParseKey(k)
if err != nil {
return errors.Wrap(err, "could not unmarshal map")
}
newMap[pk] = v
}
*m = newMap
return nil
}
func (m Map) MarshalYAML() (interface{}, error) {
rawMap := make(map[string]Value, len(m))
for k, v := range m {
rawMap[k.String()] = v
}
return rawMap, nil
}
func (m *Map) UnmarshalYAML(unmarshal func(interface{}) error) error {
rawMap := make(map[string]Value)
if err := unmarshal(&rawMap); err != nil {
return errors.Wrap(err, "could not unmarshal map")
}
newMap := make(Map, len(rawMap))
for k, v := range rawMap {
pk, err := ParseKey(k)
if err != nil {
return errors.Wrap(err, "could not unmarshal map")
}
newMap[pk] = v
}
*m = newMap
return nil
}
// parseKeyPath returns the property paths in the key and a new config key with the first
// path segment as the name.
func parseKeyPath(k Key) (resource.PropertyPath, Key, error) {
// Parse the path, which will be in the name portion of the key.
p, err := resource.ParsePropertyPath(k.Name())
if err != nil {
return nil, Key{}, errors.Wrap(err, "invalid config key path")
}
if len(p) == 0 {
return nil, Key{}, errors.New("empty config key path")
}
// Create a new key that has the first path segment as the name.
firstKey, ok := p[0].(string)
if !ok {
return nil, Key{}, errors.New("first path segement of config key must be a string")
}
if firstKey == "" {
return nil, Key{}, errors.New("config key is empty")
}
configKey := MustMakeKey(k.Namespace(), firstKey)
return p, configKey, nil
}
// getValueForPath returns the parent, value, and true if the value is found in source given the path segments in p.
func getValueForPath(source interface{}, p resource.PropertyPath) (interface{}, interface{}, bool) {
// If the source is nil, exit early.
if source == nil {
return nil, nil, false
}
// Lookup the value by each path segment.
var parent interface{}
v := source
for _, key := range p {
parent = v
switch t := v.(type) {
case []interface{}:
index, ok := key.(int)
if !ok || index < 0 || index >= len(t) {
return nil, nil, false
}
v = t[index]
case map[string]interface{}:
k, ok := key.(string)
if !ok {
return nil, nil, false
}
v, ok = t[k]
if !ok {
return nil, nil, false
}
default:
return nil, nil, false
}
}
return parent, v, true
}
// getValue returns the value in the container for the given key.
func getValue(container, key interface{}) (interface{}, error) {
switch t := container.(type) {
case []interface{}:
i, ok := key.(int)
contract.Assertf(ok, "key for an array must be an int")
// We explicitly allow i == len(t) here, which indicates a
// value that will be appended to the end of the array.
if i < 0 || i > len(t) {
return nil, errors.New("array index out of range")
}
if i == len(t) {
return nil, nil
}
return t[i], nil
case map[string]interface{}:
k, ok := key.(string)
contract.Assertf(ok, "key for a map must be a string")
return t[k], nil
}
contract.Failf("should not reach here")
return nil, nil
}
// Set value sets the value in the container for the given key, and returns the container.
// If the container is an array, and a value is being appended, containerParent and containerParentKey must
// not be nil since a new slice will be created to append the value, which needs to be saved in the parent.
// In this case, the new slice will be returned.
func setValue(container, key, value, containerParent, containerParentKey interface{}) (interface{}, error) {
switch t := container.(type) {
case []interface{}:
i, ok := key.(int)
contract.Assertf(ok, "key for an array must be an int")
// We allow i == len(t), which indicates the value should be appended to the end of the array.
if i < 0 || i > len(t) {
return nil, errors.New("array index out of range")
}
// If i == len(t), we need to append to the end of the array, which involves creating a new slice
// and saving it in the parent container.
if i == len(t) {
t = append(t, value)
contract.Assertf(containerParent != nil, "parent must not be nil")
contract.Assertf(containerParentKey != nil, "parentKey must not be nil")
if _, err := setValue(containerParent, containerParentKey, t, nil, nil); err != nil {
return nil, err
}
return t, nil
}
t[i] = value
case map[string]interface{}:
k, ok := key.(string)
contract.Assertf(ok, "key for a map must be a string")
t[k] = value
}
return container, nil
}
// adjustObjectValue returns a more suitable value for objects:
func adjustObjectValue(v Value, path bool) interface{} {
contract.Assertf(!v.Object(), "v must not be an Object")
// If the path flag isn't set, just return the value as-is.
if !path {
return v
}
// If it's a secure value, return as-is.
if v.Secure() {
return v
}
// If "false" or "true", return the boolean value.
if v.value == "false" {
return false
} else if v.value == "true" {
return true
}
// If it's convertible to an int, return the int.
i, err := strconv.Atoi(v.value)
if err == nil {
return i
}
// Otherwise, just return the string value.
return v.value
}