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

hclsyntax: Pass marks through template expressions #404

Merged
merged 1 commit into from
Sep 24, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions hclsyntax/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
var diags hcl.Diagnostics
isKnown := true

// Maintain a set of marks for values used in the template
marks := make(cty.ValueMarks)

for _, part := range e.Parts {
partVal, partDiags := part.Value(ctx)
diags = append(diags, partDiags...)
Expand Down Expand Up @@ -71,14 +74,24 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
continue
}

buf.WriteString(strVal.AsString())
// Unmark the part and merge its marks into the set
unmarked, partMarks := strVal.Unmark()
for k, v := range partMarks {
marks[k] = v
}

buf.WriteString(unmarked.AsString())
}

var ret cty.Value
if !isKnown {
return cty.UnknownVal(cty.String), diags
ret = cty.UnknownVal(cty.String)
} else {
ret = cty.StringVal(buf.String())
}

return cty.StringVal(buf.String()), diags
// Apply the full set of marks to the returned value
return ret.WithMarks(marks), diags
}

func (e *TemplateExpr) Range() hcl.Range {
Expand Down
21 changes: 21 additions & 0 deletions hclsyntax/expression_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ trim`,
cty.UnknownVal(cty.String),
1, // Unexpected endfor directive
},
{ // marks from uninterpolated values are ignored
`hello%{ if false } ${target}%{ endif }`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"target": cty.StringVal("world").WithMarks(cty.NewValueMarks("sensitive")),
Copy link

@azr azr Sep 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Howdy ! I was looking at this and nice !!! We are probably going to use that for Packer too :D.

Question: can an object or a list be sensitive ? I couldn't tell from the test here ex:

variable "foo" {
  default = { 
    key = "secret"
    value = "secret"
  }
  sensitive = true
}

( maybe it would be worth it to tests that too if that should work )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's our hope, although it's very much work in progress. Applying marks to complex values currently doesn't work reliably, and this specific case (assuming your interpolation is like "key=${var.foo.key}") will currently panic.

I'll be opening a follow-up PR for that issue, and should be able to add an interpolation test case then. Thanks!

},
},
cty.StringVal("hello"),
0,
},
{ // marks from interpolated values are passed through
`${greeting} ${target}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"greeting": cty.StringVal("hello").WithMarks(cty.NewValueMarks("english")),
"target": cty.StringVal("world").WithMarks(cty.NewValueMarks("sensitive")),
},
},
cty.StringVal("hello world").WithMarks(cty.NewValueMarks("english", "sensitive")),
0,
},
}

for _, test := range tests {
Expand Down