Skip to content

Commit c00f251

Browse files
authored
Add support sensitive variables in TestRunner (#337)
1 parent 63caa05 commit c00f251

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

helper/runner.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"reflect"
88

99
"github.com/hashicorp/hcl/v2"
10+
"github.com/hashicorp/hcl/v2/gohcl"
1011
"github.com/hashicorp/hcl/v2/hclsyntax"
1112
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
1213
"github.com/terraform-linters/tflint-plugin-sdk/internal"
1314
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
15+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
1416
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1517
"github.com/zclconf/go-cty/cty"
1618
"github.com/zclconf/go-cty/cty/convert"
@@ -410,6 +412,9 @@ func decodeVariableBlock(block *hcl.Block) (*Variable, hcl.Diagnostics) {
410412
{
411413
Name: "default",
412414
},
415+
{
416+
Name: "sensitive",
417+
},
413418
},
414419
})
415420
if diags.HasErrors() {
@@ -424,6 +429,15 @@ func decodeVariableBlock(block *hcl.Block) (*Variable, hcl.Diagnostics) {
424429

425430
v.Default = val
426431
}
432+
if attr, exists := content.Attributes["sensitive"]; exists {
433+
var sensitive bool
434+
diags := gohcl.DecodeExpression(attr.Expr, nil, &sensitive)
435+
if diags.HasErrors() {
436+
return v, diags
437+
}
438+
439+
v.Default = v.Default.Mark(marks.Sensitive)
440+
}
427441

428442
return v, nil
429443
}

helper/runner_test.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/hcl/v2/hclsyntax"
1111
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
1212
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
13+
"github.com/zclconf/go-cty/cty"
1314
)
1415

1516
func Test_GetResourceContent(t *testing.T) {
@@ -536,7 +537,7 @@ func Test_DecodeRuleConfig_config_not_found(t *testing.T) {
536537
}
537538
}
538539

539-
func Test_EvaluateExpr(t *testing.T) {
540+
func Test_EvaluateExpr_string(t *testing.T) {
540541
tests := []struct {
541542
Name string
542543
Src string
@@ -601,6 +602,64 @@ resource "aws_instance" "foo" {
601602
}
602603
}
603604

605+
func Test_EvaluateExpr_value(t *testing.T) {
606+
tests := []struct {
607+
Name string
608+
Src string
609+
Want string
610+
}{
611+
{
612+
Name: "sensitive variable",
613+
Src: `
614+
variable "instance_type" {
615+
type = string
616+
default = "secret"
617+
sensitive = true
618+
}
619+
620+
resource "aws_instance" "foo" {
621+
instance_type = var.instance_type
622+
}`,
623+
Want: `cty.StringVal("secret").Mark(marks.Sensitive)`,
624+
},
625+
}
626+
627+
for _, test := range tests {
628+
t.Run(test.Name, func(t *testing.T) {
629+
runner := TestRunner(t, map[string]string{"main.tf": test.Src})
630+
631+
resources, err := runner.GetResourceContent("aws_instance", &hclext.BodySchema{
632+
Attributes: []hclext.AttributeSchema{{Name: "instance_type"}},
633+
}, nil)
634+
if err != nil {
635+
t.Fatal(err)
636+
}
637+
638+
for _, resource := range resources.Blocks {
639+
// raw value
640+
var instanceType cty.Value
641+
if err := runner.EvaluateExpr(resource.Body.Attributes["instance_type"].Expr, &instanceType, nil); err != nil {
642+
t.Fatal(err)
643+
}
644+
645+
if instanceType.GoString() != test.Want {
646+
t.Fatalf(`"%s" is expected, but got "%s"`, test.Want, instanceType.GoString())
647+
}
648+
649+
// callback
650+
if err := runner.EvaluateExpr(resource.Body.Attributes["instance_type"].Expr, func(val cty.Value) error {
651+
if instanceType.GoString() != test.Want {
652+
t.Fatalf(`"%s" is expected, but got "%s"`, test.Want, instanceType.GoString())
653+
}
654+
return nil
655+
}, nil); err != nil {
656+
t.Fatal(err)
657+
}
658+
}
659+
})
660+
}
661+
}
662+
604663
type dummyRule struct {
605664
tflint.DefaultRule
606665
}

0 commit comments

Comments
 (0)