-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
feat(misconf): variable support for Terraform Plan #7228
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package snapshot | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/zclconf/go-cty/cty" | ||
ctymsgpack "github.com/zclconf/go-cty/cty/msgpack" | ||
"google.golang.org/protobuf/proto" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraformplan/snapshot/planproto" | ||
) | ||
|
||
type DynamicValue []byte | ||
|
||
func (v DynamicValue) Decode(ty cty.Type) (cty.Value, error) { | ||
if v == nil { | ||
return cty.NilVal, nil | ||
} | ||
|
||
return ctymsgpack.Unmarshal([]byte(v), ty) | ||
} | ||
|
||
type Plan struct { | ||
variableValues map[string]DynamicValue | ||
} | ||
|
||
func (p Plan) inputVariables() (map[string]cty.Value, error) { | ||
vars := make(map[string]cty.Value) | ||
for k, v := range p.variableValues { | ||
val, err := v.Decode(cty.DynamicPseudoType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
vars[k] = val | ||
} | ||
return vars, nil | ||
} | ||
|
||
func readTfPlan(r io.Reader) (*Plan, error) { | ||
b, err := io.ReadAll(r) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read plan: %w", err) | ||
} | ||
|
||
var rawPlan planproto.Plan | ||
if err := proto.Unmarshal(b, &rawPlan); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal plan: %w", err) | ||
} | ||
|
||
plan := Plan{ | ||
variableValues: make(map[string]DynamicValue), | ||
} | ||
|
||
for k, v := range rawPlan.Variables { | ||
if len(v.Msgpack) == 0 { // len(0) because that's the default value for a "bytes" in protobuf | ||
return nil, fmt.Errorf("dynamic value does not have msgpack serialization") | ||
} | ||
Comment on lines
+56
to
+58
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. I'm not sure if I understand the comment, we check the len to be zero to ensure that what we are unpacking is msgpack right? 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. Terraforom stores cty values in msgpack encoding . If the byte length is 0, it means a invalid value is represented. I looked up the implementation in Terraform. |
||
|
||
plan.variableValues[k] = DynamicValue(v.Msgpack) | ||
} | ||
|
||
return &plan, nil | ||
} |
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.
If it is run in the container, would we need to update https://github.com/aquasecurity/trivy/blob/5e20c115fee9196943a3715c4f85bb3eac9d7d66/Dockerfile.protoc as well?
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.
See the alternate logic here https://github.com/aquasecurity/trivy/pull/7228/files#diff-d7a1577b2d546e34aaf816bdd1b83edef28a66a555f049767bafa83268b7efbbR190-R195
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.
mage:protoc
command builds an image and runs it. The dockerfile does not need to be updated.