Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix internal marshal error of sensitive value #40

Merged
merged 1 commit into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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