-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder.go
More file actions
111 lines (90 loc) · 1.98 KB
/
builder.go
File metadata and controls
111 lines (90 loc) · 1.98 KB
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
package vcrypt
import (
"fmt"
"github.com/vcrypt/vcrypt/config"
"github.com/vcrypt/vcrypt/cryptex"
"github.com/vcrypt/vcrypt/graph"
"github.com/vcrypt/vcrypt/secret"
)
type builder struct {
plan config.Plan
verts map[string]*graph.Vertex
}
func build(plan config.Plan) (*Graph, error) {
root, ok := plan.CryptexNode(plan.Root)
if !ok {
return nil, fmt.Errorf("missing root cryptex %q", plan.Root)
}
bldr := builder{
plan: plan,
verts: make(map[string]*graph.Vertex),
}
g, err := bldr.buildGraph(root)
if err != nil {
return nil, err
}
g.Nodes() // load digests & nonces maps
return g, nil
}
func (b builder) buildGraph(root config.CryptexNode) (*Graph, error) {
cptx, err := root.Cryptex()
if err != nil {
return nil, err
}
g, err := NewGraph(cptx)
if err != nil {
return nil, err
}
for _, edge := range root.Edges() {
if err := b.buildEdge(g, edge, g.Root); err != nil {
return nil, err
}
}
return g, nil
}
func (b builder) buildEdge(g *Graph, name string, from *graph.Vertex) error {
if to, ok := b.verts[name]; ok {
return g.AddEdge(to, from)
}
return b.buildVertex(g, name, from)
}
func (b builder) buildVertex(g *Graph, name string, from *graph.Vertex) error {
if node, ok := b.plan.CryptexNode(name); ok {
cptx, err := node.Cryptex()
if err != nil {
return err
}
env, err := cryptex.Wrap(cptx)
if err != nil {
return err
}
to, err := g.Add(env, from)
if err != nil {
return err
}
b.verts[name] = to
for _, edge := range node.Edges() {
if err := b.buildEdge(g, edge, to); err != nil {
return err
}
}
return nil
}
if node, ok := b.plan.SecretNode(name); ok {
sec, err := node.Secret()
if err != nil {
return err
}
env, err := secret.Wrap(sec)
if err != nil {
return err
}
_, err = g.Add(env, from)
return err
}
if mrkr, ok := b.plan.Materials[name]; ok {
_, err := g.Add(&Marker{Comment: mrkr.Comment}, from)
return err
}
return fmt.Errorf("missing node for edge %q", name)
}