4
4
"github.com/aquasecurity/trivy/pkg/iac/terraform"
5
5
tfcontext "github.com/aquasecurity/trivy/pkg/iac/terraform/context"
6
6
"github.com/zclconf/go-cty/cty"
7
+
8
+ "github.com/coder/preview/hclext"
7
9
)
8
10
9
11
// ParameterContextsEvalHook is called in a loop, so if parameters affect
@@ -24,6 +26,21 @@ func ParameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks
24
26
continue // Wow a value exists?!. This feels like a bug.
25
27
}
26
28
29
+ countAttr , countExists := block .Attributes ()["count" ]
30
+ if countExists {
31
+ // Omit count = 0 values!
32
+ countVal := countAttr .Value ()
33
+ if ! countVal .Type ().Equals (cty .Number ) {
34
+ continue // Probably unknown
35
+ }
36
+ v , _ := countVal .AsBigFloat ().Int64 ()
37
+ if v < 1 {
38
+ // Non-one counts are incorrect
39
+ // Zero counts are ignored as the blocks are omitted
40
+ continue
41
+ }
42
+ }
43
+
27
44
nameAttr := block .GetAttribute ("name" )
28
45
nameVal := nameAttr .Value ()
29
46
if ! nameVal .Type ().Equals (cty .String ) {
@@ -48,8 +65,44 @@ func ParameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks
48
65
}
49
66
50
67
// Set the default value as the 'value' attribute
51
- path := []string {"data" }
52
- path = append (path , block .Labels ()... )
68
+ path := []string {
69
+ "data" ,
70
+ "coder_parameter" ,
71
+ block .Reference ().NameLabel (),
72
+ }
73
+ if countExists {
74
+ // Append to the existing tuple
75
+ existing := ctx .Get (path ... )
76
+ if existing .IsNull () {
77
+ continue
78
+ }
79
+
80
+ if ! existing .Type ().IsTupleType () {
81
+ continue
82
+ }
83
+
84
+ if existing .LengthInt () > 1 {
85
+ // coder_parameters can only ever have a count of 0 or 1.
86
+ // More than that is invalid. So ignore invalid blocks.
87
+ continue
88
+ }
89
+
90
+ it := existing .ElementIterator ()
91
+ if ! it .Next () {
92
+ continue
93
+ }
94
+
95
+ _ , v := it .Element ()
96
+ merged := hclext .MergeObjects (v , cty .ObjectVal (map [string ]cty.Value {
97
+ "value" : value ,
98
+ }))
99
+
100
+ // Since our count can only equal 1, we can safely set the
101
+ // value to a tuple of length 1 in all cases.
102
+ ctx .Set (cty .TupleVal ([]cty.Value {merged }), path ... )
103
+ continue
104
+ }
105
+
53
106
path = append (path , "value" )
54
107
// The current context is in the `coder_parameter` block.
55
108
// Use the parent context to "export" the value
@@ -104,5 +157,6 @@ func evaluateCoderParameterDefault(b *terraform.Block) (cty.Value, bool) {
104
157
if diags .HasErrors () {
105
158
return cty .NilVal , false
106
159
}
160
+
107
161
return v , true
108
162
}
0 commit comments