feat: support Terraform override files in template preview#196
feat: support Terraform override files in template preview#196
Conversation
Implement Terraform's override file semantics (override.tf, *_override.tf) by merging override blocks into primary files before evaluation. Related to: coder/coder#21991
| // Merge override files into primary files before parsing, so Trivy | ||
| // sees post-merge content with no duplicate blocks. This replicates | ||
| // Terraform's override file semantics. | ||
| // | ||
| // TODO: It'd be nice if Trivy did this for us. |
| f, diags := hclwrite.ParseConfig(content, path, hcl.Pos{Line: 1, Column: 1}) | ||
| if diags.HasErrors() { | ||
| return nil, fmt.Errorf("parse file %s: %s", path, diags.Error()) | ||
| } |
There was a problem hiding this comment.
this only works for .hcl files. hclparse can do json. Idk if that is the same format or transferable to hclwrite though.
import "github.com/hashicorp/hcl/v2/hclparse"
p := hclparse.NewParser()
p.ParseJSONFile()As it stands, json files will break this
There was a problem hiding this comment.
That's a great catch!
After some research, supporting .tf.json files is more complex than it looks. hclparse's structs can't be used with hclwrite and there's no straightforward way to convert between the two. And JSON is ambiguous about blocks vs attributes, so there's no way to resolve this without the provider schema. So even Trivy doesn't do that :)
For now I'm thinking of not merging at all if .tf.json files are detected - maybe issue a warning about the lack of support and hope the override stuff is not coder-related. Or only merge the .tf files and warn that .tf.json overrides are not supported.
There was a problem hiding this comment.
If you can issue a warning, do it. Otherwise a silent drop is fine for json imo
Implements https://developer.hashicorp.com/terraform/language/files/override semantics (override.tf, *_override.tf) by merging override blocks into primary .tf files before Trivy evaluation:
How it works
Related to: coder/coder#21991