This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
params.go
608 lines (587 loc) · 17 KB
/
params.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package service
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/Azure/open-service-broker-azure/pkg/crypto"
"github.com/Azure/open-service-broker-azure/pkg/slice"
)
// Parameters is a wrapper for a map that uses a schema to inform data access,
// marshaling, and unmarshaling with seamless encryption and decryption of
// sensitive string fields using the globally configured codec.
type Parameters struct {
Schema KeyedPropertySchemaContainer
Data map[string]interface{}
}
// MarshalJSON marshals Parameters to JSON
func (p Parameters) MarshalJSON() ([]byte, error) {
// TODO: krancour: Ideally, if we constrain how Params are created using some
// constructor-like function, perhaps we can forgo this check.
if p.Schema == nil {
return nil, errors.New(
`error marshaling parameters: cannot marshal without a schema`,
)
}
ips, ok := p.Schema.(*InputParametersSchema)
if !ok {
return nil, errors.New(
`error marshaling parameters: cannot marshal with a schema that is ` +
`not an *InputParametersSchema`,
)
}
data := map[string]interface{}{}
for k, schema := range ips.PropertySchemas {
if v, ok := p.Data[k]; ok {
if slice.ContainsString(ips.SecureProperties, k) {
if _, ok := schema.(*StringPropertySchema); !ok {
return nil, fmt.Errorf(
`error marshaling parameters: cannot encrypt non-string field "%s"`,
k,
)
}
vStr, ok := v.(string)
if !ok {
return nil, fmt.Errorf(
`error marshaling parameters: cannot encrypt non-string value of `+
`string field "%s"`,
k,
)
}
encryptedBytes, err := crypto.Encrypt([]byte(vStr))
if err != nil {
return nil, err
}
// When the whole map gets marshaled, this []byte will automatically be
// base64 encoded and wrapped in quotes.
v = encryptedBytes
}
data[k] = v
}
}
return json.Marshal(data)
}
// UnmarshalJSON umarshals JSON to a Parameters objects
func (p *Parameters) UnmarshalJSON(bytes []byte) error {
// TODO: krancour: Ideally, if we constrain how Params are created using some
// constructor-like function, perhaps we can forgo this check.
if p.Schema == nil {
return errors.New(
`error unmarshaling parameters: cannot unmarshal without a schema`,
)
}
ips, ok := p.Schema.(*InputParametersSchema)
if !ok {
return errors.New(
`error unmarshaling parameters: cannot unmarshal with a schema that is ` +
`not an *InputParametersSchema`,
)
}
p.Data = map[string]interface{}{}
if ips != nil {
data := map[string]interface{}{}
if err := json.Unmarshal(bytes, &data); err != nil {
return err
}
for k, schema := range ips.PropertySchemas {
if v, ok := data[k]; ok {
if slice.ContainsString(ips.SecureProperties, k) {
if _, ok := schema.(*StringPropertySchema); !ok {
return fmt.Errorf(
`error unmarshaling parameters: cannot decrypt non-string field `+
`"%s"`,
k,
)
}
vStr, ok := v.(string)
if !ok {
return fmt.Errorf(
`error marshaling parameters: cannot decrypt non-string value of `+
`string field "%s"`,
k,
)
}
encryptedBytes, err := base64.StdEncoding.DecodeString(vStr)
if err != nil {
return err
}
decryptedBytes, err := crypto.Decrypt(encryptedBytes)
if err != nil {
return err
}
// The map we're building should have a string in it
v = string(decryptedBytes)
}
p.Data[k] = v
}
}
}
return nil
}
// GetString retrieves a string by key from the Parameters' underlying map. If
// the key does not exist in the schema, an empty string is returned. If the
// key does exist in the schema, also exists in the map, and that value from the
// map can be coerced to a string, it will be returned. If, however, the key
// does not exist in the map or its value cannot be coerced to a string, any
// default value specified for that field in the schema will be returned. If
// no default is defined, an empty string is returned.
func (p *Parameters) GetString(key string) string {
if p.Schema == nil {
return ""
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return ""
}
}
stringSchema, ok := schema.(*StringPropertySchema)
if !ok {
return ""
}
return ifaceToString(p.Data[key], stringSchema.DefaultValue)
}
// GetStringArray retrieves a []string by key from the Parameters' underlying
// map. If the key does not exist in the schema, nil is returned. If the key
// does exist in the schema, also exists in the map, and that value from the map
// can be coerced to a []string, it will be returned. If, however, the key
// does not exist in the map or its value cannot be coerced to a []string, any
// default value specified for that field in the schema will be returned. If
// no default is defined, nil is returned.
func (p *Parameters) GetStringArray(key string) []string {
if p.Schema == nil {
return nil
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return nil
}
}
arrSchema, ok := schema.(*ArrayPropertySchema)
if !ok {
return nil
}
itemDefault := ""
if arrSchema.ItemsSchema != nil {
var itemSchema *StringPropertySchema
itemSchema, ok = arrSchema.ItemsSchema.(*StringPropertySchema)
if !ok {
return nil
}
itemDefault = itemSchema.DefaultValue
}
valIface, ok := p.Data[key]
if !ok {
return ifaceArrayToStringArray(arrSchema.DefaultValue, itemDefault)
}
val, ok := valIface.([]interface{})
if !ok {
return ifaceArrayToStringArray(arrSchema.DefaultValue, itemDefault)
}
return ifaceArrayToStringArray(val, itemDefault)
}
func ifaceArrayToStringArray(arr []interface{}, itemDefault string) []string {
if len(arr) == 0 {
return nil
}
retArr := make([]string, len(arr))
for i, item := range arr {
retArr[i] = ifaceToString(item, itemDefault)
}
return retArr
}
func ifaceToString(valIface interface{}, defaultVal string) string {
if valIface == nil {
return defaultVal
}
if val, ok := valIface.(*string); ok {
return *val
}
if val, ok := valIface.(string); ok {
return val
}
return defaultVal
}
// GetInt64 retrieves an int64 by key from the Parameters' underlying map. If
// the key does not exist in the schema, 0 is returned. If the key does exist in
// the schema, also exists in the map, and that value from the map can be
// coerced to an int64, it will be returned. If, however, the key does not exist
// in the map or its value cannot be coerced to an int64, any default value
// specified for that field in the schema will be returned. If no default is
// defined, 0 is returned.
func (p *Parameters) GetInt64(key string) int64 {
if p.Schema == nil {
return 0
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return 0
}
}
intSchema, ok := schema.(*IntPropertySchema)
if !ok {
return 0
}
return ifaceToInt64(p.Data[key], intSchema.DefaultValue)
}
// GetInt64Array retrieves an []int64 by key from the Parameters' underlying
// map. If the key does not exist in the schema, nil is returned. If the key
// does exist in the schema, also exists in the map, and that value from the map
// can be coerced to an []int64, it will be returned. If, however, the key does
// not exist in the map or its value cannot be coerced to an []int64, any
// default value specified for that field in the schema will be returned. If no
// default is defined, nil is returned.
func (p *Parameters) GetInt64Array(key string) []int64 {
if p.Schema == nil {
return nil
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return nil
}
}
arrSchema, ok := schema.(*ArrayPropertySchema)
if !ok {
return nil
}
var itemDefault *int64
if arrSchema.ItemsSchema != nil {
var itemSchema *IntPropertySchema
itemSchema, ok = arrSchema.ItemsSchema.(*IntPropertySchema)
if !ok {
return nil
}
itemDefault = itemSchema.DefaultValue
}
valIface, ok := p.Data[key]
if !ok {
return ifaceArrayToInt64Array(arrSchema.DefaultValue, itemDefault)
}
val, ok := valIface.([]interface{})
if !ok {
return ifaceArrayToInt64Array(arrSchema.DefaultValue, itemDefault)
}
return ifaceArrayToInt64Array(val, itemDefault)
}
func ifaceArrayToInt64Array(arr []interface{}, itemDefault *int64) []int64 {
if len(arr) == 0 {
return nil
}
retArr := make([]int64, len(arr))
for i, item := range arr {
retArr[i] = ifaceToInt64(item, itemDefault)
}
return retArr
}
func ifaceToInt64(valIface interface{}, defaultVal *int64) int64 {
if valIface == nil {
if defaultVal == nil {
return 0
}
return *defaultVal
}
if val, ok := valIface.(*float64); ok {
return int64(*val)
}
if val, ok := valIface.(float64); ok {
return int64(val)
}
if val, ok := valIface.(*float32); ok {
return int64(*val)
}
if val, ok := valIface.(float32); ok {
return int64(val)
}
if val, ok := valIface.(*int64); ok {
return *val
}
if val, ok := valIface.(int64); ok {
return val
}
if val, ok := valIface.(*int32); ok {
return int64(*val)
}
if val, ok := valIface.(int32); ok {
return int64(val)
}
if val, ok := valIface.(*int); ok {
return int64(*val)
}
if val, ok := valIface.(int); ok {
return int64(val)
}
if defaultVal == nil {
return 0
}
return *defaultVal
}
// GetFloat64 retrieves a float64 by key from the Parameters' underlying map. If
// the key does not exist in the schema, 0 is returned. If the key does exist in
// the schema, also exists in the map, and that value from the map can be
// coerced to a float64, it will be returned. If, however, the key does not
// exist in the map or its value cannot be coerced to a float64, any default
// value specified for that field in the schema will be returned. If no default
// is defined, 0 is returned.
func (p *Parameters) GetFloat64(key string) float64 {
if p.Schema == nil {
return 0
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return 0
}
}
floatSchema, ok := schema.(*FloatPropertySchema)
if !ok {
return 0
}
return ifaceToFloat64(p.Data[key], floatSchema.DefaultValue)
}
// GetFloat64Array retrieves a []float64 by key from the Parameters' underlying
// map. If the key does not exist in the schema, nil is returned. If the key
// does exist in the schema, also exists in the map, and that value from the map
// can be coerced to a []float64, it will be returned. If, however, the key does
// not exist in the map or its value cannot be coerced to a []float64, any
// default value specified for that field in the schema will be returned. If no
// default is defined, nil is returned.
func (p *Parameters) GetFloat64Array(key string) []float64 {
if p.Schema == nil {
return nil
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return nil
}
}
arrSchema, ok := schema.(*ArrayPropertySchema)
if !ok {
return nil
}
var itemDefault *float64
if arrSchema.ItemsSchema != nil {
var itemSchema *FloatPropertySchema
itemSchema, ok = arrSchema.ItemsSchema.(*FloatPropertySchema)
if !ok {
return nil
}
itemDefault = itemSchema.DefaultValue
}
valIface, ok := p.Data[key]
if !ok {
return ifaceArrayToFloat64Array(arrSchema.DefaultValue, itemDefault)
}
val, ok := valIface.([]interface{})
if !ok {
return ifaceArrayToFloat64Array(arrSchema.DefaultValue, itemDefault)
}
return ifaceArrayToFloat64Array(val, itemDefault)
}
func ifaceArrayToFloat64Array(
arr []interface{},
itemDefault *float64,
) []float64 {
if len(arr) == 0 {
return nil
}
retArr := make([]float64, len(arr))
for i, item := range arr {
retArr[i] = ifaceToFloat64(item, itemDefault)
}
return retArr
}
func ifaceToFloat64(valIface interface{}, defaultVal *float64) float64 {
if valIface == nil {
if defaultVal == nil {
return 0
}
return *defaultVal
}
if val, ok := valIface.(*float64); ok {
return *val
}
if val, ok := valIface.(float64); ok {
return val
}
if val, ok := valIface.(*float32); ok {
return float64(*val)
}
if val, ok := valIface.(float32); ok {
return float64(val)
}
if val, ok := valIface.(*int64); ok {
return float64(*val)
}
if val, ok := valIface.(int64); ok {
return float64(val)
}
if val, ok := valIface.(*int32); ok {
return float64(*val)
}
if val, ok := valIface.(int32); ok {
return float64(val)
}
if val, ok := valIface.(*int); ok {
return float64(*val)
}
if val, ok := valIface.(int); ok {
return float64(val)
}
if defaultVal == nil {
return 0
}
return *defaultVal
}
// GetObject retrieves a map[string]interface{} by key from the Parameters'
// underlying map wrapped in a new Parameters object. If the key does not exist
// in the schema, a Parameters object with no underlying map is returned. If the
// key does exist in the schema, also exists in the map, and that value from the
// map can be coerced to a map[string]interface{}, it will be wrapped and
// returned. If, however, the key does not exist in the map or its value cannot
// be coerced to a map[string]interface{}, any default value specified for that
// field in the schema will be wrapped and returned. If no default is defined, a
// Parameters object with no underlying map is returned. GetObject calls can
// be chained to "drill down" into a complex set of parameters.
func (p *Parameters) GetObject(key string) Parameters {
params := Parameters{}
if p.Schema == nil {
return params
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return params
}
}
objectSchema, ok := schema.(*ObjectPropertySchema)
if !ok {
return params
}
params.Schema = objectSchema
return ifaceToParams(
p.Data[key],
objectSchema,
objectSchema.DefaultValue,
)
}
// GetObjectArray retrieves a []map[string]interface{} by key from the
// Parameters' underlying map, wraps each element in a new Parameters object and
// returns []Parameters containing those wrapped []map[string]interface{}. If
// the key does not exist in the schema, nil is returned. If the key does exist
// in the schema, also exists in the map, and that value from the map can be
// coerced to a []map[string]interface{}, each element will be wrapped in a new
// Parameters object and []Parameters will be returned. If, however, the key
// does not exist in the map or its value cannot be coerced to a
// []map[string]interface{}, any default value specified for that
// field in the schema will have each of its elements wrapped and []Parameters
// will be returned. If no default is defined, nil is returned.
func (p *Parameters) GetObjectArray(key string) []Parameters {
if p.Schema == nil {
return nil
}
schema, ok := p.Schema.GetPropertySchemas()[key]
if !ok {
// Schema for the key wasn't found, but maybe "additional" properties are
// supported?
schema = p.Schema.GetAdditionalPropertySchema()
if schema == nil {
return nil
}
}
arrSchema, ok := schema.(*ArrayPropertySchema)
if !ok {
return nil
}
var itemDefault map[string]interface{}
var itemSchema *ObjectPropertySchema
if arrSchema.ItemsSchema != nil {
itemSchema, ok = arrSchema.ItemsSchema.(*ObjectPropertySchema)
if !ok {
return nil
}
itemDefault = itemSchema.DefaultValue
}
valIface, ok := p.Data[key]
if !ok {
return ifaceArrayToParamsArray(
arrSchema.DefaultValue,
itemSchema,
itemDefault,
)
}
val, ok := valIface.([]interface{})
if !ok {
return ifaceArrayToParamsArray(
arrSchema.DefaultValue,
itemSchema,
itemDefault,
)
}
return ifaceArrayToParamsArray(
val,
itemSchema,
itemDefault,
)
}
func ifaceArrayToParamsArray(
arr []interface{},
itemSchema *ObjectPropertySchema, // nolint: interfacer
itemDefault map[string]interface{},
) []Parameters {
if len(arr) == 0 {
return nil
}
retArr := make([]Parameters, len(arr))
for i, item := range arr {
retArr[i] = ifaceToParams(
item,
itemSchema,
itemDefault,
)
}
return retArr
}
func ifaceToParams(
valIface interface{},
schema KeyedPropertySchemaContainer,
defaultVal map[string]interface{},
) Parameters {
params := Parameters{
Schema: schema,
}
if valIface == nil {
params.Data = defaultVal
return params
}
if val, ok := valIface.(map[string]interface{}); ok {
params.Data = val
return params
}
params.Data = defaultVal
return params
}