@@ -5,12 +5,15 @@ import (
55 "fmt"
66 "io"
77 "io/fs"
8+ "log"
9+ "strings"
810
911 "github.com/aquasecurity/trivy/pkg/iac/scanners/terraformplan/tfjson/parser"
1012 "github.com/aquasecurity/trivy/pkg/iac/terraform"
1113 tfcontext "github.com/aquasecurity/trivy/pkg/iac/terraform/context"
1214 tfjson "github.com/hashicorp/terraform-json"
1315 "github.com/zclconf/go-cty/cty"
16+ "github.com/zclconf/go-cty/cty/gocty"
1417)
1518
1619func PlanJSONHook (dfs fs.FS , input Input ) (func (ctx * tfcontext.Context , blocks terraform.Blocks , inputVars map [string ]cty.Value ), error ) {
@@ -33,12 +36,47 @@ func PlanJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
3336 return func (ctx * tfcontext.Context , blocks terraform.Blocks , inputVars map [string ]cty.Value ) {
3437 // 'data' blocks are loaded into prior state
3538 //plan.PriorState.Values.RootModule.Resources
39+ for _ , resource := range plan .PriorState .Values .RootModule .Resources {
40+ // TODO: Do index references exist here too?
41+ // TODO: Handle submodule nested resources
42+
43+ parts := strings .Split (resource .Address , "." )
44+ if len (parts ) < 2 {
45+ continue
46+ }
47+
48+ if parts [0 ] == "data" && ! strings .Contains (resource .Type , "coder" ) {
49+ continue
50+ }
51+
52+ val , err := attributeCtyVal (resource .AttributeValues )
53+ if err != nil {
54+ // TODO: Remove log
55+ log .Printf ("unable to determine value of resource %q: %v" , resource .Address , err )
56+ continue
57+ }
58+
59+ ctx .Set (val , parts ... )
60+ }
3661
3762 }, nil
3863}
3964
40- func extract (parents []string , mod * tfjson.StateModule ) {
65+ func attributeCtyVal (attr map [string ]interface {}) (cty.Value , error ) {
66+ mv := make (map [string ]cty.Value )
67+ for k , v := range attr {
68+ ty , err := gocty .ImpliedType (v )
69+ if err != nil {
70+ return cty .NilVal , fmt .Errorf ("implied type for %q: %w" , k , err )
71+ }
72+
73+ mv [k ], err = gocty .ToCtyValue (v , ty )
74+ if err != nil {
75+ return cty .NilVal , fmt .Errorf ("implied value for %q: %w" , k , err )
76+ }
77+ }
4178
79+ return cty .ObjectVal (mv ), nil
4280}
4381
4482// ParsePlanJSON can parse the JSON output of a Terraform plan.
0 commit comments