Skip to content

Commit 400a79c

Browse files
authored
fix(misconf): do not use cty.NilVal for non-nil values (#8567)
Signed-off-by: nikpivkin <[email protected]>
1 parent fe400ea commit 400a79c

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

pkg/iac/scanners/terraform/executor/executor.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,16 @@ func writeBlock(tfBlock *terraform.Block, block *hclwrite.Block, causeRng types.
130130
var found bool
131131

132132
for _, attr := range tfBlock.Attributes() {
133-
if attr.GetMetadata().Range().Covers(causeRng) && !attr.IsLiteral() {
134-
block.Body().SetAttributeValue(attr.Name(), attr.Value())
135-
found = true
133+
if !attr.GetMetadata().Range().Covers(causeRng) || attr.IsLiteral() {
134+
continue
135+
}
136+
137+
value := attr.Value()
138+
if !value.IsKnown() || value.IsNull() {
139+
continue
136140
}
141+
block.Body().SetAttributeValue(attr.Name(), value)
142+
found = true
137143
}
138144

139145
for _, child := range tfBlock.AllBlocks() {

pkg/iac/scanners/terraform/parser/parser_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -2418,3 +2418,39 @@ func TestConfigWithEphemeralBlock(t *testing.T) {
24182418
_, err := parser.Load(t.Context())
24192419
require.NoError(t, err)
24202420
}
2421+
2422+
func TestConvertObjectWithUnknownAndNullValuesToMap(t *testing.T) {
2423+
fsys := fstest.MapFS{
2424+
"main.tf": &fstest.MapFile{Data: []byte(`module "foo" {
2425+
source = "./modules/foo"
2426+
}
2427+
2428+
locals {
2429+
known = "test"
2430+
}
2431+
2432+
module "bar" {
2433+
source = "./modules/bar"
2434+
outputs = {
2435+
"key1" : { "Value" : module.foo.test },
2436+
"key2" : { "Value" : local.known },
2437+
"key3" : { "Value" : local.unknown },
2438+
}
2439+
}`)},
2440+
"modules/foo/main.tf": &fstest.MapFile{Data: []byte(`output "test" {
2441+
value = ref_to_unknown
2442+
}`)},
2443+
"modules/bar/main.tf": &fstest.MapFile{Data: []byte(`variable "outputs" {
2444+
type = map(any)
2445+
}`)},
2446+
}
2447+
2448+
parser := New(fsys, "", OptionStopOnHCLError(true))
2449+
require.NoError(t, parser.ParseFS(t.Context(), "."))
2450+
2451+
_, err := parser.Load(t.Context())
2452+
require.NoError(t, err)
2453+
2454+
_, _, err = parser.EvaluateAll(t.Context())
2455+
require.NoError(t, err)
2456+
}

pkg/iac/terraform/attribute.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func (a *Attribute) Value() (ctyVal cty.Value) {
288288
}()
289289
ctyVal, _ = a.hclAttribute.Expr.Value(a.ctx.Inner())
290290
if !ctyVal.IsKnown() || ctyVal.IsNull() {
291-
return cty.NilVal
291+
return cty.DynamicVal
292292
}
293293
return ctyVal
294294
}
@@ -304,8 +304,8 @@ func (a *Attribute) NullableValue() (ctyVal cty.Value) {
304304
}
305305
}()
306306
ctyVal, _ = a.hclAttribute.Expr.Value(a.ctx.Inner())
307-
if !ctyVal.IsKnown() {
308-
return cty.NilVal
307+
if !ctyVal.IsKnown() || ctyVal.IsNull() {
308+
return cty.NullVal(cty.DynamicPseudoType)
309309
}
310310
return ctyVal
311311
}

0 commit comments

Comments
 (0)