-
Notifications
You must be signed in to change notification settings - Fork 1
feat: parse and extract tf variables #168
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
Open
Emyrk
wants to merge
6
commits into
main
Choose a base branch
from
stevenmasley/variables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1af8373
feat: parse and extract tf variables
Emyrk 8154589
fixup some complex variables
Emyrk 50dedf6
treat unknown variables as nil
Emyrk a9bb1e3
add extra defense
Emyrk 28466ba
fix e2e
Emyrk 6d83d05
Update extract/variable.go
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package extract | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/hcl/v2/ext/typeexpr" | ||
"github.com/zclconf/go-cty/cty" | ||
"github.com/zclconf/go-cty/cty/convert" | ||
|
||
"github.com/coder/preview/types" | ||
) | ||
|
||
// VariableFromBlock extracts a terraform variable, but not it's final resolved value. | ||
// code taken mostly from https://github.com/aquasecurity/trivy/blob/main/pkg/iac/scanners/terraform/parser/evaluator.go#L479 | ||
func VariableFromBlock(block *terraform.Block) (tfVar types.Variable) { | ||
defer func() { | ||
// Extra safety mechanism to ensure that if a panic occurs, we do not break | ||
// everything else. | ||
if r := recover(); r != nil { | ||
tfVar = types.Variable{ | ||
Name: block.Label(), | ||
Diagnostics: types.Diagnostics{ | ||
{ | ||
Severity: hcl.DiagError, | ||
Summary: "Panic occurred in extracting variable. This should not happen, please report this to Coder.", | ||
Detail: fmt.Sprintf("panic in variable extract: %+v", r), | ||
}, | ||
}, | ||
} | ||
} | ||
}() | ||
|
||
attributes := block.Attributes() | ||
|
||
var valType cty.Type | ||
var defaults *typeexpr.Defaults | ||
|
||
if typeAttr, exists := attributes["type"]; exists { | ||
ty, def, err := typeAttr.DecodeVarType() | ||
if err != nil { | ||
var subject hcl.Range | ||
if typeAttr.HCLAttribute() != nil { | ||
subject = typeAttr.HCLAttribute().Range | ||
} | ||
return types.Variable{ | ||
Name: block.Label(), | ||
Diagnostics: types.Diagnostics{&hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: "Failed to decode variable type for " + block.Label(), | ||
Detail: err.Error(), | ||
Subject: &subject, | ||
}}, | ||
} | ||
} | ||
valType = ty | ||
defaults = def | ||
} | ||
|
||
var val cty.Value | ||
var defSubject hcl.Range | ||
if def, exists := attributes["default"]; exists { | ||
val = def.NullableValue() | ||
defSubject = def.HCLAttribute().Range | ||
} | ||
|
||
if valType != cty.NilType { | ||
// TODO: If this code ever extracts the actual value of the variable, | ||
// then we need to source the value from that, rather than the default. | ||
if defaults != nil { | ||
val = defaults.Apply(val) | ||
} | ||
|
||
canConvert := !val.IsNull() && val.IsWhollyKnown() && valType != cty.NilType | ||
|
||
if canConvert { | ||
typedVal, err := convert.Convert(val, valType) | ||
if err != nil { | ||
return types.Variable{ | ||
Name: block.Label(), | ||
Diagnostics: types.Diagnostics{&hcl.Diagnostic{ | ||
Severity: hcl.DiagError, | ||
Summary: fmt.Sprintf("Failed to convert variable default value to type %q for %q", | ||
valType.FriendlyNameForConstraint(), block.Label()), | ||
Detail: err.Error(), | ||
Subject: &defSubject, | ||
}}, | ||
} | ||
} | ||
val = typedVal | ||
} | ||
} else { | ||
valType = val.Type() | ||
} | ||
Comment on lines
+40
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is taken from trivy. Unfortunately it is not exported, so I had to copy paste it. |
||
return types.Variable{ | ||
Name: block.Label(), | ||
Default: val, | ||
Type: valType, | ||
Description: optionalString(block, "description"), | ||
Nullable: optionalBoolean(block, "nullable"), | ||
Sensitive: optionalBoolean(block, "sensitive"), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.