Skip to content

Commit 02d5404

Browse files
authored
feat(misconf): add metadata to Cloud schema (#6831)
1 parent 8dd076a commit 02d5404

File tree

4 files changed

+1253
-26
lines changed

4 files changed

+1253
-26
lines changed

pkg/iac/rego/convert/struct.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,21 @@ func StructToRego(inputValue reflect.Value) map[string]any {
3232
field := inputValue.Field(i)
3333
typ := inputValue.Type().Field(i)
3434
name := typ.Name
35-
if !typ.IsExported() {
35+
36+
if !typ.IsExported() || field.Interface() == nil {
3637
continue
3738
}
38-
if field.Interface() == nil {
39+
40+
if _, ok := field.Interface().(types.Metadata); ok && name == "Metadata" {
3941
continue
4042
}
43+
4144
val := anonymousToRego(reflect.ValueOf(field.Interface()))
45+
4246
if val == nil {
4347
continue
4448
}
45-
key := strings.ToLower(name)
46-
if _, ok := field.Interface().(types.Metadata); key == "metadata" && ok {
47-
continue
48-
}
49+
4950
output[strings.ToLower(name)] = val
5051
}
5152

pkg/iac/rego/convert/struct_test.go

+49-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,56 @@ import (
55
"testing"
66

77
"github.com/stretchr/testify/assert"
8+
9+
"github.com/aquasecurity/trivy/pkg/iac/types"
810
)
911

1012
func Test_StructConversion(t *testing.T) {
11-
input := struct {
12-
X string
13-
Y int
14-
Z struct {
15-
A float64
16-
}
17-
}{}
18-
input.Z.A = 123
19-
converted := StructToRego(reflect.ValueOf(input))
20-
assert.Equal(t, map[string]any{"z": make(map[string]any)}, converted)
13+
tests := []struct {
14+
name string
15+
inp any
16+
expected any
17+
}{
18+
{
19+
name: "struct with nested struct",
20+
inp: struct {
21+
X string
22+
Y int
23+
Z struct {
24+
A float64
25+
}
26+
}{
27+
X: "test",
28+
Z: struct {
29+
A float64
30+
}{
31+
A: 123,
32+
},
33+
},
34+
expected: map[string]any{"z": make(map[string]any)},
35+
},
36+
{
37+
name: "struct with metadata",
38+
inp: struct {
39+
X string
40+
Metadata types.Metadata
41+
}{
42+
X: "test",
43+
Metadata: types.NewTestMetadata(),
44+
},
45+
expected: map[string]any{
46+
"__defsec_metadata": func() any {
47+
meta := types.NewTestMetadata().GetMetadata()
48+
return meta.ToRego()
49+
}(),
50+
},
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
converted := StructToRego(reflect.ValueOf(tt.inp))
57+
assert.Equal(t, tt.expected, converted)
58+
})
59+
}
2160
}

pkg/iac/rego/schemas/builder.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ func sanitize(s string) string {
7575
}
7676

7777
func (b *builder) readProperty(name string, parent, inputType reflect.Type, indent int) (*Property, error) {
78-
7978
if inputType.Kind() == reflect.Ptr {
8079
inputType = inputType.Elem()
8180
}
8281

8382
switch inputType.String() {
84-
case "types.Metadata", "types.Range", "types.Reference":
83+
case "types.Range", "types.Reference":
8584
return nil, nil
8685
}
8786

@@ -181,7 +180,8 @@ func (b *builder) readStruct(name string, parent, inputType reflect.Type, indent
181180
b.schema.Defs[refName(name, parent, inputType)] = def
182181
}
183182

184-
if inputType.Implements(converterInterface) {
183+
if inputType.Implements(converterInterface) ||
184+
inputType.String() == "types.Metadata" {
185185
if inputType.Kind() == reflect.Ptr {
186186
inputType = inputType.Elem()
187187
}
@@ -192,6 +192,7 @@ func (b *builder) readStruct(name string, parent, inputType reflect.Type, indent
192192
} else {
193193

194194
for i := 0; i < inputType.NumField(); i++ {
195+
195196
field := inputType.Field(i)
196197
prop, err := b.readProperty(field.Name, inputType, field.Type, indent+1)
197198
if err != nil {
@@ -201,9 +202,12 @@ func (b *builder) readStruct(name string, parent, inputType reflect.Type, indent
201202
continue
202203
}
203204
key := strings.ToLower(field.Name)
205+
206+
// metadata exported as "__defsec_metadata"
204207
if key == "metadata" {
205-
continue
208+
key = "__defsec_metadata"
206209
}
210+
207211
def.Properties[key] = *prop
208212
}
209213
}

0 commit comments

Comments
 (0)