Skip to content

Commit

Permalink
Fix internal marshal error of sensitive value (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Apr 9, 2023
1 parent b5fb9a6 commit 5fcaa10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
6 changes: 6 additions & 0 deletions opa/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/open-policy-agent/opa/types"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
Expand Down Expand Up @@ -293,6 +294,11 @@ func exprToJSON(expr hcl.Expression, tyMap map[string]cty.Type, path string, run
}
return ret, err
}
if marks.Contains(value, marks.Sensitive) {
ret["unknown"] = true
ret["sensitive"] = true
return ret, nil
}
if !value.IsWhollyKnown() {
ret["unknown"] = true
return ret, nil
Expand Down
15 changes: 15 additions & 0 deletions opa/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,21 @@ func TestExprToJSON(t *testing.T) {
},
source: `variable "foo" { sensitive = true }`,
},
{
name: "composite sensitive",
input: parse("[var.foo]"),
ty: cty.String,
want: map[string]any{
"unknown": true,
"sensitive": true,
"range": map[string]any{
"filename": "main.tf",
"start": map[string]int{"line": 1, "column": 1, "byte": 0},
"end": map[string]int{"line": 1, "column": 10, "byte": 9},
},
},
source: `variable "foo" { sensitive = true }`,
},
{
name: "invalid type",
input: hcl.StaticExpr(cty.StringVal("foo"), hcl.Range{Filename: "main.tf", Start: hcl.InitialPos, End: hcl.InitialPos}),
Expand Down
6 changes: 2 additions & 4 deletions opa/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand Down Expand Up @@ -97,7 +98,7 @@ func (r *testRunner) GetModuleContent(schema *hclext.BodySchema, _ *tflint.GetMo
return content, nil
}

var sensitiveMark = cty.NewValueMarks("sensitive")
var sensitiveMark = cty.NewValueMarks(marks.Sensitive)

// EvaluateExpr returns a value of the passed expression.
// Not expected to reflect anything other than cty.Value.
Expand All @@ -124,9 +125,6 @@ func (r *testRunner) EvaluateExpr(expr hcl.Expression, ret interface{}, _ *tflin
if diags.HasErrors() {
return diags
}
if val.IsMarked() {
return tflint.ErrSensitive
}

return gocty.FromCtyValue(val, ret)
}
Expand Down
3 changes: 1 addition & 2 deletions opa/test_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
)

Expand Down Expand Up @@ -236,7 +235,7 @@ variable "instance_type" {
sensitive = true
}`,
expr: parse("var.instance_type"),
err: tflint.ErrSensitive,
want: `cty.StringVal("t2.micro").Mark(marks.Sensitive)`,
},
}

Expand Down

0 comments on commit 5fcaa10

Please sign in to comment.