Skip to content

Commit ec68c9a

Browse files
authored
fix(misconf): fix parsing of engine links and frameworks (#6937)
1 parent bc3741a commit ec68c9a

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

pkg/iac/rego/metadata.go

+35-11
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,7 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
9090
if raw, ok := meta["url"]; ok {
9191
sm.References = append(sm.References, fmt.Sprintf("%s", raw))
9292
}
93-
if raw, ok := meta["frameworks"]; ok {
94-
frameworks, ok := raw.(map[string][]string)
95-
if !ok {
96-
return fmt.Errorf("failed to parse framework metadata: not an object")
97-
}
98-
for fw, sections := range frameworks {
99-
sm.Frameworks[framework.Framework(fw)] = sections
100-
}
101-
}
93+
10294
if raw, ok := meta["related_resources"]; ok {
10395
switch relatedResources := raw.(type) {
10496
case []map[string]any:
@@ -112,6 +104,9 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
112104
}
113105
}
114106

107+
if err := sm.updateFrameworks(meta); err != nil {
108+
return fmt.Errorf("failed to update frameworks: %w", err)
109+
}
115110
sm.updateAliases(meta)
116111

117112
var err error
@@ -126,6 +121,28 @@ func (sm *StaticMetadata) Update(meta map[string]any) error {
126121
return nil
127122
}
128123

124+
func (sm *StaticMetadata) updateFrameworks(meta map[string]any) error {
125+
if raw, ok := meta["frameworks"]; ok {
126+
frameworks, ok := raw.(map[string]any)
127+
if !ok {
128+
return fmt.Errorf("frameworks metadata is not an object, got %T", raw)
129+
}
130+
for fw, rawIDs := range frameworks {
131+
ids, ok := rawIDs.([]any)
132+
if !ok {
133+
return fmt.Errorf("framework ids is not an array, got %T", rawIDs)
134+
}
135+
fr := framework.Framework(fw)
136+
for _, id := range ids {
137+
if str, ok := id.(string); ok {
138+
sm.Frameworks[fr] = append(sm.Frameworks[fr], str)
139+
}
140+
}
141+
}
142+
}
143+
return nil
144+
}
145+
129146
func (sm *StaticMetadata) updateAliases(meta map[string]any) {
130147
if raw, ok := meta["aliases"]; ok {
131148
if aliases, ok := raw.([]any); ok {
@@ -172,8 +189,15 @@ func NewEngineMetadata(schema string, meta map[string]any) (*scan.EngineMetadata
172189
if val, ok := sMap["bad_examples"].(string); ok {
173190
em.BadExamples = []string{val}
174191
}
175-
if val, ok := sMap["links"].(string); ok {
176-
em.Links = []string{val}
192+
switch links := sMap["links"].(type) {
193+
case string:
194+
em.Links = []string{links}
195+
case []any:
196+
for _, v := range links {
197+
if str, ok := v.(string); ok {
198+
em.Links = append(em.Links, str)
199+
}
200+
}
177201
}
178202
if val, ok := sMap["remediation_markdown"].(string); ok {
179203
em.RemediationMarkdown = val

pkg/iac/rego/metadata_test.go

+28-11
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func Test_UpdateStaticMetadata(t *testing.T) {
4646
"severity": "s_n",
4747
"library": true,
4848
"url": "r_n",
49-
"frameworks": map[string][]string{
50-
"all": {"aa"},
49+
"frameworks": map[string]any{
50+
"all": []any{"aa"},
5151
},
5252
},
5353
))
@@ -137,7 +137,7 @@ func Test_UpdateStaticMetadata(t *testing.T) {
137137
})
138138
}
139139

140-
func Test_getEngineMetadata(t *testing.T) {
140+
func Test_NewEngineMetadata(t *testing.T) {
141141
inputSchema := map[string]any{
142142
"terraform": map[string]any{
143143
"good_examples": `resource "aws_cloudtrail" "good_example" {
@@ -153,8 +153,11 @@ func Test_getEngineMetadata(t *testing.T) {
153153
}
154154
}
155155
}`,
156+
157+
"links": "https://avd.aquasec.com/avd/183",
156158
},
157-
"cloud_formation": map[string]any{"good_examples": `---
159+
"cloud_formation": map[string]any{
160+
"good_examples": `---
158161
Resources:
159162
GoodExample:
160163
Type: AWS::CloudTrail::Trail
@@ -164,15 +167,19 @@ Resources:
164167
S3BucketName: "CloudtrailBucket"
165168
S3KeyPrefix: "/trailing"
166169
TrailName: "Cloudtrail"`,
167-
}}
170+
"links": []any{"https://avd.aquasec.com/avd/183"},
171+
},
172+
}
168173

169174
var testCases = []struct {
170175
schema string
171-
want string
176+
want *scan.EngineMetadata
172177
}{
173178
{
174179
schema: "terraform",
175-
want: `resource "aws_cloudtrail" "good_example" {
180+
want: &scan.EngineMetadata{
181+
GoodExamples: []string{
182+
`resource "aws_cloudtrail" "good_example" {
176183
is_multi_region_trail = true
177184
178185
event_selector {
@@ -185,9 +192,15 @@ Resources:
185192
}
186193
}
187194
}`,
195+
},
196+
Links: []string{"https://avd.aquasec.com/avd/183"},
197+
},
188198
},
189-
{schema: "cloud_formation",
190-
want: `---
199+
{
200+
schema: "cloud_formation",
201+
want: &scan.EngineMetadata{
202+
GoodExamples: []string{
203+
`---
191204
Resources:
192205
GoodExample:
193206
Type: AWS::CloudTrail::Trail
@@ -196,14 +209,18 @@ Resources:
196209
IsMultiRegionTrail: true
197210
S3BucketName: "CloudtrailBucket"
198211
S3KeyPrefix: "/trailing"
199-
TrailName: "Cloudtrail"`},
212+
TrailName: "Cloudtrail"`,
213+
},
214+
Links: []string{"https://avd.aquasec.com/avd/183"},
215+
},
216+
},
200217
}
201218

202219
for _, tc := range testCases {
203220
t.Run(tc.schema, func(t *testing.T) {
204221
em, err := NewEngineMetadata(tc.schema, inputSchema)
205222
require.NoError(t, err)
206-
assert.Equal(t, tc.want, em.GoodExamples[0])
223+
assert.Equal(t, tc.want, em)
207224
})
208225
}
209226
}

0 commit comments

Comments
 (0)