Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ type TfData struct {
Values map[string]any
}

func changingItemQueriesFromPlan(ctx context.Context, lf log.Fields) ([]*sdp.Query, error) {
// read results from `terraform show -json ${tfplan file}`
contents, err := os.ReadFile(viper.GetString("plan-json"))
if err != nil {
return nil, fmt.Errorf("failed to read %v: %w", viper.GetString("plan-json"), err)
}

func changingItemQueriesFromPlan(ctx context.Context, planJSON []byte, lf log.Fields) ([]*sdp.Query, error) {
changing_items_tf := map[string]TfData{}

var parsed map[string]any
err = json.Unmarshal(contents, &parsed)
err := json.Unmarshal(planJSON, &parsed)
if err != nil {
return nil, fmt.Errorf("failed to parse %v: %w", viper.GetString("plan-json"), err)
}
Expand Down Expand Up @@ -102,7 +96,7 @@ func changingItemQueriesFromPlan(ctx context.Context, lf log.Fields) ([]*sdp.Que
mappings, ok := datamaps.AwssourceData[r.Type]
if !ok {
log.WithContext(ctx).WithFields(lf).WithField("terraform-address", r.Address).Warn("skipping unmapped resource")
break
continue
}

for _, mapData := range mappings {
Expand All @@ -112,7 +106,7 @@ func changingItemQueriesFromPlan(ctx context.Context, lf log.Fields) ([]*sdp.Que
WithFields(lf).
WithField("terraform-address", r.Address).
WithField("terraform-query-field", mapData.QueryField).Warn("skipping resource without query field")
break
continue
}

u := uuid.New()
Expand Down Expand Up @@ -180,10 +174,17 @@ func SubmitPlan(signals chan os.Signal, ready chan bool) int {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

// read results from `terraform show -json ${tfplan file}`
contents, err := os.ReadFile(viper.GetString("plan-json"))
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(lf).Error("failed to read terraform file")
return 1
}

log.WithContext(ctx).WithFields(lf).Info("resolving items from terraform plan")
queries, err := changingItemQueriesFromPlan(ctx, lf)
queries, err := changingItemQueriesFromPlan(ctx, contents, lf)
if err != nil {
log.WithContext(ctx).WithError(err).WithFields(lf).Error("failed to read terraform plan")
log.WithContext(ctx).WithError(err).WithFields(lf).Error("parse terraform plan")

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information

[Sensitive data returned by an access to apiKey](1) flows to a logging call.
return 1
}

Expand Down
40 changes: 40 additions & 0 deletions cmd/submitplan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"context"
"os"
"testing"

"github.com/sirupsen/logrus"
)

func TestChangingItemQueriesFromPlan(t *testing.T) {
testFile := "testdata/plan.json"
planJSON, err := os.ReadFile(testFile)

if err != nil {
t.Errorf("Error reading %v: %v", testFile, err)
}

queries, err := changingItemQueriesFromPlan(context.Background(), planJSON, logrus.Fields{})

if err != nil {
t.Error(err)
}

if len(queries) != 1 {
t.Errorf("Expected 1 queries, got %v", len(queries))
}

if queries[0].Type != "iam-policy" {
t.Errorf("Expected query type to be iam-policy, got %v", queries[0].Type)
}

if queries[0].Query != "arn:aws:iam::123456789012:policy/test-alb-ingress" {
t.Errorf("Expected query to be arn:aws:iam::123456789012:policy/test-alb-ingress, got %v", queries[0].Query)
}

if queries[0].Scope != "*" {
t.Errorf("Expected query scope to be *, got %v", queries[0].Scope)
}
}
Loading