Skip to content

Commit 0d586fd

Browse files
committed
Unmark values in count before go conversion
When evaluating count values, we need to unmark the cty value before passing the value for conversion into a go int value.
1 parent 30126ba commit 0d586fd

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎terraform/eval_count.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func evaluateCountExpressionValue(expr hcl.Expression, ctx EvalContext) (cty.Val
6060
return nullCount, diags
6161
}
6262

63+
// Unmark the count value, sensitive values are allowed in count but not for_each,
64+
// as using it here will not disclose the sensitive value
65+
countVal, _ = countVal.Unmark()
66+
6367
switch {
6468
case countVal.IsNull():
6569
diags = diags.Append(&hcl.Diagnostic{

‎terraform/eval_count_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package terraform
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/davecgh/go-spew/spew"
8+
"github.com/hashicorp/hcl/v2"
9+
"github.com/hashicorp/hcl/v2/hcltest"
10+
"github.com/zclconf/go-cty/cty"
11+
)
12+
13+
func TestEvaluateCountExpression(t *testing.T) {
14+
tests := map[string]struct {
15+
Expr hcl.Expression
16+
Count int
17+
}{
18+
"zero": {
19+
hcltest.MockExprLiteral(cty.NumberIntVal(0)),
20+
0,
21+
},
22+
"expression with marked value": {
23+
hcltest.MockExprLiteral(cty.NumberIntVal(8).Mark("sensitive")),
24+
8,
25+
},
26+
}
27+
for name, test := range tests {
28+
t.Run(name, func(t *testing.T) {
29+
ctx := &MockEvalContext{}
30+
ctx.installSimpleEval()
31+
countVal, diags := evaluateCountExpression(test.Expr, ctx)
32+
33+
if len(diags) != 0 {
34+
t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
35+
}
36+
37+
if !reflect.DeepEqual(countVal, test.Count) {
38+
t.Errorf(
39+
"wrong map value\ngot: %swant: %s",
40+
spew.Sdump(countVal), spew.Sdump(test.Count),
41+
)
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)