-
Notifications
You must be signed in to change notification settings - Fork 106
/
cfn.go
260 lines (223 loc) · 6.44 KB
/
cfn.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
// Copyright 2021 Fugue, Inc.
//
// 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 loader
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
var validCfnExts map[string]bool = map[string]bool{
".yaml": true,
".yml": true,
".json": true,
}
type CfnDetector struct{}
func (c *CfnDetector) DetectFile(i InputFile, opts DetectOptions) (IACConfiguration, error) {
if !opts.IgnoreExt && !validCfnExts[i.Ext()] {
return nil, fmt.Errorf("File does not have .yaml, .yml, or .json extension: %v", i.Path())
}
contents, err := i.Contents()
if err != nil {
return nil, err
}
template := &cfnTemplate{}
if err := yaml.Unmarshal(contents, &template); err != nil || template == nil {
return nil, fmt.Errorf("Failed to parse file as YAML or JSON %v: %v", i.Path(), err)
}
_, hasTemplateFormatVersion := template.Contents["AWSTemplateFormatVersion"]
_, hasResources := template.Contents["Resources"]
if !hasTemplateFormatVersion && !hasResources {
return nil, fmt.Errorf("Input file is not a CloudFormation template: %v", i.Path())
}
path := i.Path()
source, err := LoadSourceInfoNode(contents)
if err != nil {
source = nil // Don't consider source code locations essential.
}
return &cfnConfiguration{
path: path,
template: *template,
source: source,
}, nil
}
func (c *CfnDetector) DetectDirectory(i InputDirectory, opts DetectOptions) (IACConfiguration, error) {
return nil, nil
}
type cfnConfiguration struct {
path string
template cfnTemplate
source *SourceInfoNode
}
func (l *cfnConfiguration) RegulaInput() RegulaInput {
return RegulaInput{
"filepath": l.path,
"content": l.template.Contents,
}
}
func (l *cfnConfiguration) Location(path []string) (LocationStack, error) {
if l.source == nil || len(path) < 1 {
return nil, nil
}
resourcePath := []string{"Resources"}
resourcePath = append(resourcePath, path[0])
resource, err := l.source.GetPath(resourcePath)
if err != nil {
return nil, nil
}
resourceLine, resourceColumn := resource.Location()
resourceLocation := Location{
Path: l.path,
Line: resourceLine,
Col: resourceColumn,
}
properties, err := resource.GetKey("Properties")
if err != nil {
return []Location{resourceLocation}, nil
}
attribute, err := properties.GetPath(path[1:])
if attribute != nil {
return []Location{resourceLocation}, nil
}
line, column := attribute.Location()
return []Location{{Path: l.path, Line: line, Col: column}}, nil
}
func (l *cfnConfiguration) LoadedFiles() []string {
return []string{l.path}
}
type cfnTemplate struct {
Contents map[string]interface{}
}
func (t *cfnTemplate) UnmarshalYAML(node *yaml.Node) error {
contents, err := decodeMap(node)
if err != nil {
return err
}
t.Contents = contents
return nil
}
func decodeMap(node *yaml.Node) (map[string]interface{}, error) {
if len(node.Content)%2 != 0 {
return nil, fmt.Errorf("Malformed map at line %v, col %v", node.Line, node.Column)
}
m := map[string]interface{}{}
for i := 0; i < len(node.Content); i += 2 {
keyNode := node.Content[i]
valNode := node.Content[i+1]
if keyNode.Kind != yaml.ScalarNode || keyNode.Tag != "!!str" {
return nil, fmt.Errorf("Malformed map key at line %v, col %v", keyNode.Line, keyNode.Column)
}
var key string
if err := keyNode.Decode(&key); err != nil {
return nil, fmt.Errorf("Failed to decode map key: %v", err)
}
val, err := decodeNode(valNode)
if err != nil {
return nil, fmt.Errorf("Failed to decode map val: %v", err)
}
m[key] = val
}
return m, nil
}
func decodeSeq(node *yaml.Node) ([]interface{}, error) {
s := []interface{}{}
for _, child := range node.Content {
i, err := decodeNode(child)
if err != nil {
return nil, fmt.Errorf("Error decoding sequence item at line %v, col %v", child.Line, child.Column)
}
s = append(s, i)
}
return s, nil
}
var intrinsicFns map[string]string = map[string]string{
"!And": "Fn::And",
"!Base64": "Fn::Base64",
"!Cidr": "Fn::Cidr",
"!Equals": "Fn::Equals",
"!FindInMap": "Fn::FindInMap",
"!GetAtt": "Fn::GetAtt",
"!GetAZs": "Fn::GetAZs",
"!If": "Fn::If",
"!ImportValue": "Fn::ImportValue",
"!Join": "Fn::Join",
"!Not": "Fn::Not",
"!Or": "Fn::Or",
"!Ref": "Ref",
"!Split": "Fn::Split",
"!Sub": "Fn::Sub",
"!Transform": "Fn::Transform",
}
func decodeIntrinsic(node *yaml.Node, name string) (map[string]interface{}, error) {
if name == "" {
name = strings.Replace(node.Tag, "!", "Fn::", 1)
}
intrinsic := map[string]interface{}{}
switch node.Kind {
case yaml.SequenceNode:
val, err := decodeSeq(node)
if err != nil {
return nil, fmt.Errorf("Failed to decode intrinsic containing sequence: %v", err)
}
intrinsic[name] = val
case yaml.MappingNode:
val, err := decodeMap(node)
if err != nil {
return nil, fmt.Errorf("Failed to decode intrinsic containing map: %v", err)
}
intrinsic[name] = val
default:
var val interface{}
if err := node.Decode(&val); err != nil {
return nil, fmt.Errorf("Failed to decode intrinsic: %v", err)
}
// Special case for GetAtt
if name == "Fn::GetAtt" {
if valString, ok := val.(string); ok {
val = strings.Split(valString, ".")
}
}
intrinsic[name] = val
}
return intrinsic, nil
}
func decodeNode(node *yaml.Node) (interface{}, error) {
switch node.Tag {
case "!!seq":
val, err := decodeSeq(node)
if err != nil {
return nil, fmt.Errorf("Failed to decode map val: %v", err)
}
return val, nil
case "!!map":
val, err := decodeMap(node)
if err != nil {
return nil, fmt.Errorf("Failed to decode map val: %v", err)
}
return val, nil
default:
name, isIntrinsic := intrinsicFns[node.Tag]
if isIntrinsic {
val, err := decodeIntrinsic(node, name)
if err != nil {
return nil, fmt.Errorf("Failed to decode map val: %v", err)
}
return val, nil
}
var val interface{}
if err := node.Decode(&val); err != nil {
return nil, fmt.Errorf("Failed to decode map val: %v", err)
}
return val, nil
}
}