Skip to content

Commit f02a46f

Browse files
committed
ignore coder data
1 parent 596505d commit f02a46f

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

cli/root.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ type RootCmd struct {
2121

2222
func (r *RootCmd) Root() *serpent.Command {
2323
var (
24-
dir string
25-
vars []string
24+
dir string
25+
vars []string
26+
planJSON string
2627
)
2728
cmd := &serpent.Command{
2829
Use: "codertf",
@@ -36,6 +37,14 @@ func (r *RootCmd) Root() *serpent.Command {
3637
Default: ".",
3738
Value: serpent.StringOf(&dir),
3839
},
40+
{
41+
Name: "plan",
42+
Description: "Terraform plan file as json.",
43+
Flag: "plan",
44+
FlagShorthand: "p",
45+
Default: "",
46+
Value: serpent.StringOf(&planJSON),
47+
},
3948
{
4049
Name: "vars",
4150
Description: "Variables.",
@@ -61,6 +70,7 @@ func (r *RootCmd) Root() *serpent.Command {
6170

6271
input := preview.Input{
6372
ParameterValues: rvars,
73+
PlanJSONPath: planJSON,
6474
}
6575

6676
ctx := i.Context()

plan.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

1619
func 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

Comments
 (0)