-
Notifications
You must be signed in to change notification settings - Fork 2k
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 HCL2 panics when parsing undefined variables #10326
Conversation
Update hcl/v2 to point to https://github.com/hashicorp/hcl/commits/nomad-v2.9.1+tweaks1
With the updated undefined variable code, we attempt to pick the text of `${....}` verbatim from the hcl body. Previously, we'd attempt to regenerate the string from the AST and pray it matches input; the generation is lossy, as the same AST can represent multiple variations (e.g. `${v.0}` and `${v[0]}` have the same HCLv2 AST). In this change, we attempt to go back to the hcl2 source and find the string snippet corresponding to the variable reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
v := "${" + expr + "}" | ||
UndefinedVariable: func(t hcl.Traversal) (cty.Value, hcl.Diagnostics) { | ||
body := c.ParseConfig.Body | ||
start := t.SourceRange().Start.Byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own clarity here, because the the docs on hcl.Traversal
aren't very easy to drop into and come away with a solid understanding... the SourceRange
starts right after the ${
we're evaluating and stops either at the }
or the end of the body? Just want to make sure that's guaranteed so that the slicing below can't panic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imperially, Start.Byte is the offset first non-white space character after ${
, and End.Byte is the offset of the first whitespace or }
.
It's an error if ${
is present without a corresponding }
. I was being a bit more defensive though.
This fixes a regression in #10326, to handle unquoted unknown variables. The HCL job may contain unquoted undefined variable references without ${...} wrapping, e.g. value = meta.node_class. In 1.0.4, this got parsed as value = "${meta.node_class}". This code performs a scan to find the relevant ${ and }, and only tries to find the closest ones with whitespace as the only separator.
Fix HCL2 panics when parsing undefined variables
This fixes a regression in #10326, to handle unquoted unknown variables. The HCL job may contain unquoted undefined variable references without ${...} wrapping, e.g. value = meta.node_class. In 1.0.4, this got parsed as value = "${meta.node_class}". This code performs a scan to find the relevant ${ and }, and only tries to find the closest ones with whitespace as the only separator.
I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions. |
This PR fixes a panic related to parsing undefined "variables" appearing in HCL2 - HCL2 parser is meant to leave unknown variable references intact (e.g.
${VAR}
). However, some attributes can be sophisticated e.g.${attr.something-with-dots["and_indexing"].3}
and the code didn't handle them resulting into a panic.This PR makes two core changes:
First it updates hcl2 to be base on v2.9.1 with some nomad tweaks:
nomad-v2.9.1+tweaks1
. This branch is the same as before, but picks up the feedback we got in hashicorp/hcl#413Secondly, with the updated undefined variable code, we attempt to pick the text of
${....}
verbatim from the hcl body. Previously, we'd attempt to regenerate the string from the AST and pray it matches input; the generation is lossy, as the same AST can represent multiple variations (e.g.${v.0}
and${v[0]}
have the same HCLv2 AST). In this change, we attempt to go back to the hcl2 source and find the string snippet corresponding to the variable reference.Fixes #10316
Fixes #9956