-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathevaluator.go
719 lines (611 loc) · 17.5 KB
/
evaluator.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package spruce
import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"github.com/starkandwayne/goutils/ansi"
. "github.com/geofffranks/spruce/log"
"github.com/starkandwayne/goutils/tree"
)
// Evaluator ...
type Evaluator struct {
Tree map[interface{}]interface{}
Deps map[string][]tree.Cursor
SkipEval bool
Here *tree.Cursor
CheckOps []*Opcall
Only []string
pointer *interface{}
}
func nameOfObj(o interface{}, def string) string {
for _, field := range tree.NameFields {
switch o.(type) {
case map[string]interface{}:
if value, ok := o.(map[string]interface{})[field]; ok {
if s, ok := value.(string); ok {
return s
}
}
case map[interface{}]interface{}:
if value, ok := o.(map[interface{}]interface{})[field]; ok {
if s, ok := value.(string); ok {
return s
}
}
}
}
return def
}
// DataFlow ...
func (ev *Evaluator) DataFlow(phase OperatorPhase) ([]*Opcall, error) {
ev.Here = &tree.Cursor{}
all := map[string]*Opcall{}
locs := []*tree.Cursor{}
errors := MultiError{Errors: []error{}}
// forward decls of co-recursive function
var check func(interface{})
var scan func(interface{})
check = func(v interface{}) {
if s, ok := v.(string); ok {
op, err := ParseOpcall(phase, s)
if err != nil {
errors.Append(err)
} else if op != nil {
op.where = ev.Here.Copy()
if canon, err := op.where.Canonical(ev.Tree); err == nil {
op.canonical = canon
} else {
op.canonical = op.where
}
all[op.canonical.String()] = op
TRACE("found an operation at %s: %s", op.where.String(), op.src)
TRACE(" (canonical at %s)", op.canonical.String())
locs = append(locs, op.canonical)
}
} else {
scan(v)
}
}
scan = func(o interface{}) {
switch o.(type) {
case map[interface{}]interface{}:
for k, v := range o.(map[interface{}]interface{}) {
ev.Here.Push(fmt.Sprintf("%v", k))
check(v)
ev.Here.Pop()
}
case []interface{}:
for i, v := range o.([]interface{}) {
name := nameOfObj(v, fmt.Sprintf("%d", i))
op, _ := ParseOpcall(phase, name)
if op == nil {
ev.Here.Push(name)
} else {
ev.Here.Push(fmt.Sprintf("%d", i))
}
check(v)
ev.Here.Pop()
}
}
}
scan(ev.Tree)
// construct the data flow graph, where a -> b means 'b' calls or requires 'a'
// represent the graph as list of adjancies, where [a,b] = a -> b
// []{ []*Opcall{ grabStaticValue, grabTheThingThatGrabsTheStaticValue}}
var g [][]*Opcall
for _, a := range all {
for _, path := range a.Dependencies(ev, locs) {
if b, found := all[path.String()]; found {
g = append(g, []*Opcall{b, a})
}
}
}
if len(ev.Only) > 0 {
/*
[],
[
{ name:(( concat env "-" type )), list.1:(( grab name )) }
{ name:(( concat env "-" type )), list.2:(( grab name )) }
{ name:(( concat env "-" type )), list.3:(( grab name )) }
{ name:(( concat env "-" type )), list.4:(( grab name )) }
{ name:(( concat env "-" type )), params.bosh_username:(( grab name )) }
{ type:(( grab meta.type )), name:(( concat env "-" type )) }
{ name:(( concat env "-" type )), list.0:(( grab name )) }
]
pass 1:
[
# add this one, because it is under `params`
{ name:(( concat env "-" type )), params.bosh_username:(( grab name )) }
], [
{ name:(( concat env "-" type )), list.1:(( grab name )) }
{ name:(( concat env "-" type )), list.2:(( grab name )) }
{ name:(( concat env "-" type )), list.3:(( grab name )) }
{ name:(( concat env "-" type )), list.4:(( grab name )) }
{ type:(( grab meta.type )), name:(( concat env "-" type )) }
{ name:(( concat env "-" type )), list.0:(( grab name )) }
]
pass2:
[
{ name:(( concat env "-" type )), params.bosh_username:(( grab name )) }
# add this one, because in[1] is a out[0] of a previously partitioned element.
{ type:(( grab meta.type )), name:(( concat env "-" type )) }
], [
{ name:(( concat env "-" type )), list.1:(( grab name )) }
{ name:(( concat env "-" type )), list.2:(( grab name )) }
{ name:(( concat env "-" type )), list.3:(( grab name )) }
{ name:(( concat env "-" type )), list.4:(( grab name )) }
{ name:(( concat env "-" type )), list.0:(( grab name )) }
]
pass3:
[
{ name:(( concat env "-" type )), params.bosh_username:(( grab name )) }
{ type:(( grab meta.type )), name:(( concat env "-" type )) }
# add nothing, because there is no [1] in the second list that is also a [0]
# in the first list. partitioning is complete, and we use just the first list.
], [
{ name:(( concat env "-" type )), list.1:(( grab name )) }
{ name:(( concat env "-" type )), list.2:(( grab name )) }
{ name:(( concat env "-" type )), list.3:(( grab name )) }
{ name:(( concat env "-" type )), list.4:(( grab name )) }
{ name:(( concat env "-" type )), list.0:(( grab name )) }
]
*/
// filter `in`, migrating elements to `out` if they are
// dependencies of anything already in `out`.
filter := func(out, in *[][]*Opcall) int {
l := make([][]*Opcall, 0)
for i, candidate := range *in {
if candidate == nil {
continue
}
for _, op := range *out {
if candidate[1] == op[0] {
TRACE("data flow - adding [%s: %s, %s: %s] to data flow set (it matched {%s})",
candidate[0].canonical, candidate[0].src,
candidate[1].canonical, candidate[1].src,
op[0].canonical)
l = append(l, candidate)
(*in)[i] = nil
break
}
}
}
*out = append(*out, l...)
return len(l)
}
// return a subset of `ops` that is strictly related to
// the processing of the top-levels listed in `picks`
firsts := func(ops [][]*Opcall, picks []*tree.Cursor) [][]*Opcall {
final := make([][]*Opcall, 0)
for i, op := range ops {
// check to see if this op.src is underneath
// any of the paths in `picks` -- if so, we
// want that opcall adjacency in `final`
for _, pick := range picks {
if pick.Contains(op[1].canonical) {
final = append(final, op)
ops[i] = nil
TRACE("data flow - adding [%s: %s, %s: %s] to data flow set (it matched --cherry-pick %s)",
op[0].canonical, op[0].src,
op[1].canonical, op[1].src,
pick)
break
}
}
}
for filter(&final, &ops) > 0 {
}
return final
}
picks := make([]*tree.Cursor, len(ev.Only))
for i, s := range ev.Only {
c, err := tree.ParseCursor(s)
if err != nil {
panic(err) // FIXME
}
picks[i] = c
}
g = firsts(g, picks)
// repackage `all`, since follow-on logic needs it
newAll := map[string]*Opcall{}
// findall ops underneath cherry-picked paths
for path, op := range all {
for _, pickedPath := range ev.Only {
cursor, err := tree.ParseCursor(pickedPath)
if err != nil {
panic(err) // FIXME
}
if cursor.Contains(op.canonical) {
newAll[path] = op
}
}
}
all = newAll
// add in any dependencies of things cherry-picked
for _, ops := range g {
all[ops[0].canonical.String()] = ops[0]
all[ops[1].canonical.String()] = ops[1]
}
}
for i, node := range g {
TRACE("data flow -- g[%d] is { %s:%s, %s:%s }\n", i, node[0].where, node[0].src, node[1].where, node[1].src)
}
// construct a sorted list of keys in $all, so that we
// can reliably generate the same DFA every time
var sortedKeys []string
for k := range all {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
// find all nodes in g that are free (no further dependencies)
freeNodes := func(g [][]*Opcall) []*Opcall {
l := []*Opcall{}
for _, k := range sortedKeys {
node, ok := all[k]
if !ok {
continue
}
called := false
for _, pair := range g {
if pair[1] == node {
called = true
break
}
}
if !called {
delete(all, k)
l = append(l, node)
}
}
return l
}
// removes (nullifies) all dependencies on n in g
remove := func(old [][]*Opcall, n *Opcall) [][]*Opcall {
l := [][]*Opcall{}
for _, pair := range old {
if pair[0] != n {
l = append(l, pair)
}
}
return l
}
// Kahn topological sort
ops := []*Opcall{} // order in which to call the ops
wave := 0
for len(all) > 0 {
wave++
free := freeNodes(g)
if len(free) == 0 {
return nil, ansi.Errorf("@*{cycle detected in operator data-flow graph}")
}
for _, node := range free {
TRACE("data flow: [%d] wave %d, op %s: %s", len(ops), wave, node.where, node.src)
ops = append(ops, node)
g = remove(g, node)
}
}
if len(errors.Errors) > 0 {
return nil, errors
}
return ops, nil
}
// RunOps ...
func (ev *Evaluator) RunOps(ops []*Opcall) error {
DEBUG("patching up YAML by evaluating outstanding operators\n")
errors := MultiError{Errors: []error{}}
for _, op := range ops {
err := ev.RunOp(op)
if err != nil {
errors.Append(err)
}
}
if len(errors.Errors) > 0 {
return errors
}
return nil
}
// Prune ...
func (ev *Evaluator) Prune(paths []string) error {
DEBUG("pruning %d paths from the final YAML structure", len(paths))
for _, path := range paths {
c, err := tree.ParseCursor(path)
if err != nil {
return err
}
key := c.Component(-1)
parent := c.Copy()
parent.Pop()
o, err := parent.Resolve(ev.Tree)
if err != nil {
continue
}
switch o.(type) {
case map[interface{}]interface{}:
if _, ok := o.(map[interface{}]interface{}); ok {
DEBUG(" pruning %s", path)
delete(o.(map[interface{}]interface{}), key)
}
case []interface{}:
if list, ok := o.([]interface{}); ok {
if idx, err := strconv.Atoi(key); err == nil {
parent.Pop()
if s, err := parent.Resolve(ev.Tree); err == nil {
if reflect.TypeOf(s).Kind() == reflect.Map {
parentName := fmt.Sprintf("%s", c.Component(-2))
DEBUG(" pruning index %d of array '%s'", idx, parentName)
length := len(list) - 1
replacement := make([]interface{}, length)
copy(replacement, append(list[:idx], list[idx+1:]...))
delete(s.(map[interface{}]interface{}), parentName)
s.(map[interface{}]interface{})[parentName] = replacement
}
}
}
}
default:
DEBUG(" I don't know how to prune %s\n value=%v\n", path, o)
}
}
DEBUG("")
return nil
}
// SortPaths sorts all paths (keys in map) using the provided sort-key (respective value)
func (ev *Evaluator) SortPaths(pathKeyMap map[string]string) error {
DEBUG("sorting %d paths in the final YAML structure", len(pathKeyMap))
for path, sortBy := range pathKeyMap {
DEBUG(" sorting path %s (sort-key %s)", path, sortBy)
cursor, err := tree.ParseCursor(path)
if err != nil {
return err
}
value, err := cursor.Resolve(ev.Tree)
if err != nil {
return err
}
switch value.(type) {
case []interface{}:
// no-op, that's what we want ...
case map[interface{}]interface{}:
return tree.TypeMismatchError{
Path: []string{path},
Wanted: "a list",
Got: "a map",
}
default:
return tree.TypeMismatchError{
Path: []string{path},
Wanted: "a list",
Got: "a scalar",
}
}
if err := sortList(path, value.([]interface{}), sortBy); err != nil {
return err
}
}
DEBUG("")
return nil
}
// Cherry-pick ...
func (ev *Evaluator) CherryPick(paths []string) error {
DEBUG("cherry-picking %d paths from the final YAML structure", len(paths))
if len(paths) > 0 {
// This will serve as the replacement tree ...
replacement := make(map[interface{}]interface{})
for _, path := range paths {
cursor, err := tree.ParseCursor(path)
if err != nil {
return err
}
// These variables will potentially be modified (depending on the structure)
var cherryName string
var cherryValue interface{}
// Resolve the value that needs to be cherry picked
cherryValue, err = cursor.Resolve(ev.Tree)
if err != nil {
return err
}
// Name of the parameter of the to-be-picked value
cherryName = cursor.Nodes[len(cursor.Nodes)-1]
// Since the cherry can be deep down the structure, we need to go down
// (or up, depending how you read it) the structure to include the parent
// names of the respective cherry. The pointer will be reassigned with
// each level.
pointer := cursor
for pointer != nil {
parent := pointer.Copy()
parent.Pop()
if parent.String() == "" {
// Empty parent string means we reached the root, setting the pointer nil to stop processing ...
pointer = nil
// ... create the final cherry wrapped in its container ...
tmp := make(map[interface{}]interface{})
tmp[cherryName] = cherryValue
// ... and add it to the replacement map
DEBUG("Merging '%s' into the replacement tree", path)
merger := &Merger{AppendByDefault: true}
merged := merger.mergeObj(tmp, replacement, path)
if err := merger.Error(); err != nil {
return err
}
replacement = merged.(map[interface{}]interface{})
} else {
// Reassign the pointer to the parent and restructre the current cherry value to address the parent structure and name
pointer = parent
// Depending on the type of the parent, either a map or a list is created for the new parent of the cherry value
if obj, err := parent.Resolve(ev.Tree); err == nil {
switch obj.(type) {
case map[interface{}]interface{}:
tmp := make(map[interface{}]interface{})
tmp[cherryName] = cherryValue
cherryName = parent.Nodes[len(parent.Nodes)-1]
cherryValue = tmp
case []interface{}:
tmp := make([]interface{}, 0, 0)
tmp = append(tmp, cherryValue)
cherryName = parent.Nodes[len(parent.Nodes)-1]
cherryValue = tmp
default:
return ansi.Errorf("@*{Unsupported type detected, %s is neither a map nor a list}", parent.String())
}
} else {
return err
}
}
}
}
// replace the existing tree with a new one that contain the cherry-picks
ev.Tree = replacement
}
DEBUG("")
return nil
}
// CheckForCycles ...
func (ev *Evaluator) CheckForCycles(maxDepth int) error {
DEBUG("checking for cycles in final YAML structure")
var check func(o interface{}, depth int) error
check = func(o interface{}, depth int) error {
if depth == 0 {
return ansi.Errorf("@*{Hit max recursion depth. You seem to have a self-referencing dataset}")
}
switch o.(type) {
case []interface{}:
for _, v := range o.([]interface{}) {
if err := check(v, depth-1); err != nil {
return err
}
}
case map[interface{}]interface{}:
for _, v := range o.(map[interface{}]interface{}) {
if err := check(v, depth-1); err != nil {
return err
}
}
}
return nil
}
err := check(ev.Tree, maxDepth)
if err != nil {
DEBUG("error: %s\n", err)
return err
}
DEBUG("no cycles detected.\n")
return nil
}
// RunOp ...
func (ev *Evaluator) RunOp(op *Opcall) error {
resp, err := op.Run(ev)
if err != nil {
return err
}
switch resp.Type {
case Replace:
DEBUG("executing a Replace instruction on %s", op.where)
key := op.where.Component(-1)
parent := op.where.Copy()
parent.Pop()
o, err := parent.Resolve(ev.Tree)
if err != nil {
DEBUG(" error: %s\n continuing\n", err)
return err
}
switch o.(type) {
case []interface{}:
i, err := strconv.ParseUint(key, 10, 0)
if err != nil {
DEBUG(" error: %s\n continuing\n", err)
return err
}
o.([]interface{})[i] = resp.Value
case map[interface{}]interface{}:
o.(map[interface{}]interface{})[key] = resp.Value
default:
err := tree.TypeMismatchError{
Path: parent.Nodes,
Wanted: "a map or a list",
Got: "a scalar",
}
DEBUG(" error: %s\n continuing\n", err)
return err
}
DEBUG("")
case Inject:
DEBUG("executing an Inject instruction on %s", op.where)
key := op.where.Component(-1)
parent := op.where.Copy()
parent.Pop()
o, err := parent.Resolve(ev.Tree)
if err != nil {
DEBUG(" error: %s\n continuing\n", err)
return err
}
m := o.(map[interface{}]interface{})
delete(m, key)
for k, v := range resp.Value.(map[interface{}]interface{}) {
path := fmt.Sprintf("%s.%s", parent, k)
_, set := m[k]
if !set {
DEBUG(" %s is not set, using the injected value", path)
m[k] = v
} else {
DEBUG(" %s is set, merging the injected value", path)
merger := &Merger{AppendByDefault: true}
merged := merger.mergeObj(v, m[k], path)
if err := merger.Error(); err != nil {
return err
}
m[k] = merged
}
}
}
return nil
}
// RunPhase ...
func (ev *Evaluator) RunPhase(p OperatorPhase) error {
err := SetupOperators(p)
if err != nil {
return err
}
op, err := ev.DataFlow(p)
if err != nil {
return err
}
return ev.RunOps(op)
}
// Run ...
func (ev *Evaluator) Run(prune []string, picks []string) error {
errors := MultiError{Errors: []error{}}
paramErrs := MultiError{Errors: []error{}}
if os.Getenv("REDACT") != "" {
DEBUG("Setting vault & aws operators to redact keys")
SkipVault = true
SkipAws = true
}
if !ev.SkipEval {
ev.Only = picks
errors.Append(ev.RunPhase(MergePhase))
paramErrs.Append(ev.RunPhase(ParamPhase))
if len(paramErrs.Errors) > 0 {
return paramErrs
}
errors.Append(ev.RunPhase(EvalPhase))
}
// this is a big failure...
if err := ev.CheckForCycles(4096); err != nil {
return err
}
// post-processing: prune
addToPruneListIfNecessary(prune...)
errors.Append(ev.Prune(keysToPrune))
keysToPrune = nil
// post-processing: sorting
errors.Append(ev.SortPaths(pathsToSort))
pathsToSort = map[string]string{}
// post-processing: cherry-pick
errors.Append(ev.CherryPick(picks))
if len(errors.Errors) > 0 {
return errors
}
return nil
}