-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(misconf): variable support for Terraform Plan (#7228)
Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
- Loading branch information
Showing
16 changed files
with
446 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,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") | ||
} | ||
|
||
plan.variableValues[k] = DynamicValue(v.Msgpack) | ||
} | ||
|
||
return &plan, nil | ||
} |
Oops, something went wrong.