/
hcl.go
279 lines (242 loc) · 6.83 KB
/
hcl.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
package generate
import (
"errors"
"fmt"
"strings"
"github.com/cycloidio/inframap/errcode"
"github.com/cycloidio/inframap/graph"
"github.com/cycloidio/inframap/provider"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/configs/hcl2shim"
uuid "github.com/satori/go.uuid"
"github.com/spf13/afero"
)
// FromHCL generates a new graph from the HCL on the path,
// it can be a file or a Module/Dir
func FromHCL(fs afero.Fs, path string, opt Options) (*graph.Graph, error) {
parser := configs.NewParser(fs)
g := graph.New()
var (
mod *configs.Module
diags hcl.Diagnostics
err error
)
if parser.IsConfigDir(path) {
mod, diags = parser.LoadConfigDir(path)
} else {
f, dgs := parser.LoadConfigFile(path)
if dgs.HasErrors() {
return nil, errors.New(dgs.Error())
}
mod, diags = configs.NewModule([]*configs.File{f}, nil)
}
if diags.HasErrors() {
return nil, errors.New(diags.Error())
}
// nodeCanID holds as key the `aws_alb.front` (graph.Node.Canonical)
// and as value the UUID (graph.Node.ID) we give to it
nodeCanID := make(map[string]string)
// nodeIDEdges holds as key the UUID (graph.Node.ID) and as value
// all the edges it has, in this case it's the `depends_on` values
// that we find on the TFState
nodeIDEdges := make(map[string][]string)
// resourcesRawConfig holds the actual configuration of each element
// it's represented as: graph.Node.Canonical -> Attrs
resourcesRawConfig := make(map[string]map[string]interface{})
if !opt.Raw {
opt, err = checkHCLProviders(mod, opt)
if err != nil {
return nil, err
}
}
for rk, rv := range mod.ManagedResources {
pv, rs, err := getProviderAndResource(rk, opt)
if err != nil {
if errors.Is(err, errcode.ErrProviderNotFound) {
continue
}
return nil, err
}
// If it's not a Node or Edge we ignore it
if !pv.IsNode(rs) && !pv.IsEdge(rs) {
continue
}
res, err := pv.Resource(rs)
if err != nil {
return nil, err
}
n := &graph.Node{
ID: uuid.NewV4().String(),
Canonical: rk,
Resource: *res,
}
err = g.AddNode(n)
if err != nil {
return nil, err
}
nodeCanID[n.Canonical] = n.ID
links := make(map[string][]string)
body, ok := rv.Config.(*hclsyntax.Body)
if ok {
links = getBodyLinks(body)
cfg := getBodyJSON(body)
cfg[provider.HCLCanonicalKey] = rk
resourcesRawConfig[n.ID] = cfg
} else {
// If it's not a hclsyntax.Body normally
// means it's from a JSON file, the only
// way to work with them is with rv.Config.JustAttributes()
// and manually deal with them. For what I've tested
// the Blocks (so egess an ingress for example) do not
// work and fail so we should find a workaround.
return nil, errcode.ErrGenerateFromJSON
}
for _, resources := range links {
nodeIDEdges[n.ID] = append(nodeIDEdges[n.ID], resources...)
}
}
for nid, resources := range nodeIDEdges {
for _, rkattr := range resources {
keys := strings.Split(rkattr, ".")
rk := rkattr
// If the values are like 'aws_security_group.front.id'
// and we do not need the 'id' as it's not on the key
// so we subtract it from the key
// But it's not always the case
if len(keys) == 3 {
rk = strings.Join(keys[:len(keys)-1], ".")
}
tnid, ok := nodeCanID[rk]
if !ok {
continue
}
err := g.AddEdge(&graph.Edge{
ID: uuid.NewV4().String(),
Source: nid,
Target: tnid,
})
if err != nil {
// If the edge already exists we can ignore it
if errors.Is(err, errcode.ErrGraphAlreadyExistsEdge) {
continue
}
return nil, err
}
}
}
// call the preprocess method for each
// TF provider in the file
if err := preprocess(g, resourcesRawConfig, opt); err != nil {
return nil, err
}
if opt.Clean {
g.Clean()
}
err = fixEdges(g, resourcesRawConfig, opt)
if err != nil {
return nil, err
}
if opt.Connections {
err = mutate(g, opt)
if err != nil {
return nil, err
}
}
if opt.Clean {
err = cleanHangingEdges(g, opt)
if err != nil {
return nil, err
}
}
return g, nil
}
// getBodyLinks gets all the variables used and the key in which
// they where used
func getBodyLinks(b *hclsyntax.Body) map[string][]string {
links := make(map[string][]string)
for attrk, attrv := range b.Attributes {
for _, vr := range attrv.Expr.Variables() {
links[attrk] = append(links[attrk], string(hclwrite.TokensForTraversal(vr).Bytes()))
}
}
for _, block := range b.Blocks {
for attrk, attrv := range getBodyLinks(block.Body) {
key := fmt.Sprintf("%s.%s", block.Type, attrk)
links[key] = append(links[key], attrv...)
}
}
return links
}
// getBodyJSON gets all the variables in a JSON format
// of the actual representation
func getBodyJSON(b *hclsyntax.Body) map[string]interface{} {
links := make(map[string]interface{})
for attrk, attrv := range b.Attributes {
v, _ := attrv.Expr.Value(nil)
t := v.Type().FriendlyName()
switch t {
case "string", "bool", "number", "dynamic":
for i, vr := range attrv.Expr.Variables() {
if i > 0 {
continue
}
links[attrk] = fmt.Sprintf("${%s}", string(hclwrite.TokensForTraversal(vr).Bytes()))
}
if _, ok := links[attrk]; !ok {
links[attrk] = hcl2shim.ConfigValueFromHCL2(v)
}
case "tuple":
aux := make([]interface{}, 0)
for _, vr := range attrv.Expr.Variables() {
aux = append(aux, fmt.Sprintf("${%s}", string(hclwrite.TokensForTraversal(vr).Bytes())))
}
if len(aux) == 0 {
i := hcl2shim.ConfigValueFromHCL2(v)
islice, ok := i.([]interface{})
if !ok {
// We continue to not add empty information to the config
// so it's clean and only has required information
continue
}
aux = islice
}
links[attrk] = aux
}
}
for _, block := range b.Blocks {
cfg := getBodyJSON(block.Body)
// We continue to not add empty information to the config
// so it's clean and only has required information
if len(cfg) == 0 {
continue
}
if _, ok := links[block.Type]; !ok {
links[block.Type] = make([]interface{}, 0)
}
links[block.Type] = append(links[block.Type].([]interface{}), cfg)
}
return links
}
// checkHCLProviders checks if we support any of the Providers from f, if not it'll set
// the opt.Raw to true so it can be used with Raw instead of returning an empty Graph
func checkHCLProviders(mod *configs.Module, opt Options) (Options, error) {
for rk := range mod.ManagedResources {
_, _, err := getProviderAndResource(rk, opt)
if err != nil {
if errors.Is(err, errcode.ErrProviderNotFound) {
continue
}
return opt, err
}
// If we find a resource that we support the Provider
// then we use it
return opt, nil
}
// If we reach here means the we do not support the providers
// of the HCL, so Raw has to be used
opt.Raw = true
return opt, nil
}