From 613f4cf556633fede820c35883f8871d4684d8da Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 4 May 2021 17:08:54 +0200 Subject: [PATCH 1/7] feat: create gen-driftignore --- pkg/analyser/analysis.go | 49 ++++++ pkg/cmd/driftctl.go | 1 + pkg/cmd/gen_driftignore.go | 58 +++++++ pkg/cmd/gen_driftignore_test.go | 152 ++++++++++++++++++ pkg/cmd/testdata/input_stdin_empty.json | 15 ++ pkg/cmd/testdata/input_stdin_invalid.json | 1 + pkg/cmd/testdata/input_stdin_valid.json | 112 +++++++++++++ pkg/cmd/testdata/output_stdin_empty.txt | 0 pkg/cmd/testdata/output_stdin_valid.txt | 17 ++ .../testdata/output_stdin_valid_filter.txt | 5 + .../testdata/output_stdin_valid_filter2.txt | 12 ++ 11 files changed, 422 insertions(+) create mode 100644 pkg/cmd/gen_driftignore.go create mode 100644 pkg/cmd/gen_driftignore_test.go create mode 100644 pkg/cmd/testdata/input_stdin_empty.json create mode 100644 pkg/cmd/testdata/input_stdin_invalid.json create mode 100644 pkg/cmd/testdata/input_stdin_valid.json create mode 100644 pkg/cmd/testdata/output_stdin_empty.txt create mode 100644 pkg/cmd/testdata/output_stdin_valid.txt create mode 100644 pkg/cmd/testdata/output_stdin_valid_filter.txt create mode 100644 pkg/cmd/testdata/output_stdin_valid_filter2.txt diff --git a/pkg/analyser/analysis.go b/pkg/analyser/analysis.go index 80344023..e1d280aa 100644 --- a/pkg/analyser/analysis.go +++ b/pkg/analyser/analysis.go @@ -2,6 +2,7 @@ package analyser import ( "encoding/json" + "fmt" "sort" "strings" "time" @@ -58,6 +59,13 @@ type serializableAnalysis struct { Alerts map[string][]alerter.SerializableAlert `json:"alerts"` } +type GenDriftIgnoreOptions struct { + ExcludeUnmanaged bool + ExcludeDeleted bool + ExcludeDrifted bool + InputPath string +} + func (a Analysis) MarshalJSON() ([]byte, error) { bla := serializableAnalysis{} for _, m := range a.managed { @@ -202,6 +210,40 @@ func (a *Analysis) SortResources() { a.differences = SortDifferences(a.differences) } +func (a *Analysis) DriftIgnoreList(opts GenDriftIgnoreOptions) (int, string) { + var list []string + + resourceCount := 0 + + addResources := func(res ...resource.Resource) { + for _, r := range res { + list = append(list, fmt.Sprintf("%s.%s", r.TerraformType(), escapeKey(r.TerraformId()))) + } + resourceCount += len(res) + } + addDifferences := func(diff ...Difference) { + for _, d := range diff { + addResources(d.Res) + } + resourceCount += len(diff) + } + + if !opts.ExcludeUnmanaged && a.Summary().TotalUnmanaged > 0 { + list = append(list, "# Resources not covered by IaC") + addResources(a.Unmanaged()...) + } + if !opts.ExcludeDeleted && a.Summary().TotalDeleted > 0 { + list = append(list, "# Missing resources") + addResources(a.Deleted()...) + } + if !opts.ExcludeDrifted && a.Summary().TotalDrifted > 0 { + list = append(list, "# Changed resources") + addDifferences(a.Differences()...) + } + + return resourceCount, strings.Join(list, "\n") +} + func SortDifferences(diffs []Difference) []Difference { sort.SliceStable(diffs, func(i, j int) bool { if diffs[i].Res.TerraformType() != diffs[j].Res.TerraformType() { @@ -223,3 +265,10 @@ func SortChanges(changes []Change) []Change { }) return changes } + +func escapeKey(line string) string { + line = strings.ReplaceAll(line, `\`, `\\`) + line = strings.ReplaceAll(line, `.`, `\.`) + + return line +} diff --git a/pkg/cmd/driftctl.go b/pkg/cmd/driftctl.go index 4980e834..939a82de 100644 --- a/pkg/cmd/driftctl.go +++ b/pkg/cmd/driftctl.go @@ -66,6 +66,7 @@ func NewDriftctlCmd(build build.BuildInterface) *DriftctlCmd { cmd.PersistentFlags().BoolP("send-crash-report", "", false, "Enable error reporting. Crash data will be sent to us via Sentry.\nWARNING: may leak sensitive data (please read the documentation for more details)\nThis flag should be used only if an error occurs during execution") cmd.AddCommand(NewScanCmd()) + cmd.AddCommand(NewGenDriftIgnoreCmd()) return cmd } diff --git a/pkg/cmd/gen_driftignore.go b/pkg/cmd/gen_driftignore.go new file mode 100644 index 00000000..57ce35df --- /dev/null +++ b/pkg/cmd/gen_driftignore.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/cloudskiff/driftctl/pkg/analyser" + "github.com/spf13/cobra" +) + +func NewGenDriftIgnoreCmd() *cobra.Command { + opts := &analyser.GenDriftIgnoreOptions{} + + cmd := &cobra.Command{ + Use: "gen-driftignore", + Short: "Generate a .driftignore file based on your scan result", + Long: "This command will generate a new .driftignore file containing your current drifts and send output to /dev/stdout", + Example: "driftctl scan -o json://stdout | driftctl gen-driftignore", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + _, list, err := genDriftIgnore(opts) + if err != nil { + return err + } + + fmt.Print(list) + + return nil + }, + } + + fl := cmd.Flags() + + fl.BoolVar(&opts.ExcludeUnmanaged, "exclude-unmanaged", false, "Exclude resources not managed by IaC") + fl.BoolVar(&opts.ExcludeDeleted, "exclude-missing", false, "Exclude missing resources") + fl.BoolVar(&opts.ExcludeDrifted, "exclude-changed", false, "Exclude resources that changed on cloud provider") + fl.StringVarP(&opts.InputPath, "from", "f", "/dev/stdin", "Input where the JSON should be parsed") + + return cmd +} + +func genDriftIgnore(opts *analyser.GenDriftIgnoreOptions) (int, string, error) { + input, err := os.ReadFile(opts.InputPath) + if err != nil { + return 0, "", err + } + + analysis := &analyser.Analysis{} + err = json.Unmarshal(input, analysis) + if err != nil { + return 0, "", err + } + + n, list := analysis.DriftIgnoreList(*opts) + + return n, list, nil +} diff --git a/pkg/cmd/gen_driftignore_test.go b/pkg/cmd/gen_driftignore_test.go new file mode 100644 index 00000000..4756ef0f --- /dev/null +++ b/pkg/cmd/gen_driftignore_test.go @@ -0,0 +1,152 @@ +package cmd + +import ( + "bytes" + "errors" + "io" + "os" + "testing" + + "github.com/cloudskiff/driftctl/test" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" +) + +func TestGenDriftIgnoreCmd_Input(t *testing.T) { + cases := []struct { + name string + args []string + output string + err error + }{ + { + name: "test error on invalid input", + args: []string{"-f", "./testdata/input_stdin_invalid.json"}, + output: "./testdata/output_stdin_empty.txt", + err: errors.New("invalid character 'i' looking for beginning of value"), + }, + { + name: "test empty driftignore with valid input", + args: []string{"-f", "./testdata/input_stdin_empty.json"}, + output: "./testdata/output_stdin_empty.txt", + err: nil, + }, + { + name: "test driftignore content with valid input", + args: []string{"-f", "./testdata/input_stdin_valid.json"}, + output: "./testdata/output_stdin_valid.txt", + err: nil, + }, + { + name: "test driftignore content with valid input and filter missing & changed only", + args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-unmanaged"}, + output: "./testdata/output_stdin_valid_filter.txt", + err: nil, + }, + { + name: "test driftignore content with valid input and filter unmanaged only", + args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-missing", "--exclude-changed"}, + output: "./testdata/output_stdin_valid_filter2.txt", + err: nil, + }, + { + name: "test error when input file does not exist", + args: []string{"-f", "doesnotexist"}, + output: "./testdata/output_stdin_valid_filter2.txt", + err: errors.New("open doesnotexist: no such file or directory"), + }, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + rootCmd := &cobra.Command{Use: "root"} + rootCmd.AddCommand(NewGenDriftIgnoreCmd()) + + output, err := os.ReadFile(c.output) + if err != nil { + t.Fatal(err) + } + + stdout := os.Stdout // keep backup of the real stdout + r, w, _ := os.Pipe() + os.Stdout = w + + args := append([]string{"gen-driftignore"}, c.args...) + + _, err = test.Execute(rootCmd, args...) + if c.err != nil { + assert.EqualError(t, err, c.err.Error()) + return + } else { + assert.Equal(t, c.err, err) + } + + outC := make(chan []byte) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + _, _ = io.Copy(&buf, r) + outC <- buf.Bytes() + }() + + // back to normal state + w.Close() + os.Stdout = stdout // restoring the real stdout + result := <-outC + + assert.Equal(t, string(output), string(result)) + }) + } +} + +func TestGenDriftIgnoreCmd_ValidFlags(t *testing.T) { + rootCmd := &cobra.Command{Use: "root"} + genDriftIgnoreCmd := NewGenDriftIgnoreCmd() + genDriftIgnoreCmd.RunE = func(_ *cobra.Command, args []string) error { return nil } + rootCmd.AddCommand(genDriftIgnoreCmd) + + cases := []struct { + args []string + }{ + {args: []string{"gen-driftignore", "--exclude-unmanaged"}}, + {args: []string{"gen-driftignore", "--exclude-missing"}}, + {args: []string{"gen-driftignore", "--exclude-changed"}}, + {args: []string{"gen-driftignore", "--exclude-changed=false", "--exclude-missing=false", "--exclude-unmanaged=true"}}, + {args: []string{"gen-driftignore", "--from", "/dev/stdin"}}, + {args: []string{"gen-driftignore", "-f", "/dev/stdout"}}, + } + + for _, tt := range cases { + output, err := test.Execute(rootCmd, tt.args...) + if output != "" { + t.Errorf("Unexpected output: %v", output) + } + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + } +} + +func TestGenDriftIgnoreCmd_InvalidFlags(t *testing.T) { + rootCmd := &cobra.Command{Use: "root"} + genDriftIgnoreCmd := NewGenDriftIgnoreCmd() + genDriftIgnoreCmd.RunE = func(_ *cobra.Command, args []string) error { return nil } + rootCmd.AddCommand(genDriftIgnoreCmd) + + cases := []struct { + args []string + err error + }{ + {args: []string{"gen-driftignore", "--deleted"}, err: errors.New("unknown flag: --deleted")}, + {args: []string{"gen-driftignore", "--drifted"}, err: errors.New("unknown flag: --drifted")}, + {args: []string{"gen-driftignore", "--changed"}, err: errors.New("unknown flag: --changed")}, + {args: []string{"gen-driftignore", "--missing"}, err: errors.New("unknown flag: --missing")}, + {args: []string{"gen-driftignore", "--from"}, err: errors.New("flag needs an argument: --from")}, + {args: []string{"gen-driftignore", "-f"}, err: errors.New("flag needs an argument: 'f' in -f")}, + } + + for _, tt := range cases { + _, err := test.Execute(rootCmd, tt.args...) + assert.EqualError(t, err, tt.err.Error()) + } +} diff --git a/pkg/cmd/testdata/input_stdin_empty.json b/pkg/cmd/testdata/input_stdin_empty.json new file mode 100644 index 00000000..0da4a07a --- /dev/null +++ b/pkg/cmd/testdata/input_stdin_empty.json @@ -0,0 +1,15 @@ +{ + "summary": { + "total_resources": 0, + "total_changed": 0, + "total_unmanaged": 0, + "total_missing": 0, + "total_managed": 0 + }, + "managed": null, + "unmanaged": null, + "missing": null, + "differences": null, + "coverage": 0, + "alerts": null +} \ No newline at end of file diff --git a/pkg/cmd/testdata/input_stdin_invalid.json b/pkg/cmd/testdata/input_stdin_invalid.json new file mode 100644 index 00000000..e466dcbd --- /dev/null +++ b/pkg/cmd/testdata/input_stdin_invalid.json @@ -0,0 +1 @@ +invalid \ No newline at end of file diff --git a/pkg/cmd/testdata/input_stdin_valid.json b/pkg/cmd/testdata/input_stdin_valid.json new file mode 100644 index 00000000..6b44fa55 --- /dev/null +++ b/pkg/cmd/testdata/input_stdin_valid.json @@ -0,0 +1,112 @@ +{ + "summary": { + "total_resources": 12, + "total_changed": 1, + "total_unmanaged": 11, + "total_missing": 0, + "total_managed": 1 + }, + "managed": [ + { + "id": "test-20210416154114486700000001", + "type": "aws_s3_bucket" + } + ], + "unmanaged": [ + { + "id": "driftctl", + "type": "aws_iam_user" + }, + { + "id": "sundowndev", + "type": "aws_iam_user" + }, + { + "id": "test_user", + "type": "aws_iam_user" + }, + { + "id": "OrganizationAccountAccessRole:AdministratorAccess", + "type": "aws_iam_role_policy" + }, + { + "id": "driftctl_assume_role:driftctl_policy.10", + "type": "aws_iam_role_policy" + }, + { + "id": "OrganizationAccountAccessRole", + "type": "aws_iam_role" + }, + { + "id": "driftctl_assume\\_role", + "type": "aws_iam_role" + }, + { + "id": "driftctl:driftctlrole", + "type": "aws_iam_user_policy" + }, + { + "id": "AKIAXYUOJZ3H5YCXF34G", + "type": "aws_iam_access_key" + }, + { + "id": "AKIAXYUOJZ3HV2LTLXD2", + "type": "aws_iam_access_key" + }, + { + "id": "AKIAXYUOJZ3HUSPPQQ4L", + "type": "aws_iam_access_key" + } + ], + "missing": [ + { + "id": "testuser1", + "type": "aws_iam_user" + }, + { + "id": "testrole1", + "type": "aws_iam_role" + } + ], + "differences": [ + { + "res": { + "id": "test-20210416154114486700000001", + "type": "aws_s3_bucket" + }, + "changelog": [ + { + "type": "update", + "path": [ + "BucketPrefix" + ], + "from": "test-", + "to": null, + "computed": false + }, + { + "type": "create", + "path": [ + "Tags", + "tag2" + ], + "from": null, + "to": "value", + "computed": false + }, + { + "type": "update", + "path": [ + "Tags", + "test" + ], + "from": "test", + "to": "test1", + "computed": false + } + ] + } + ], + "coverage": 8, + "alerts": null +} \ No newline at end of file diff --git a/pkg/cmd/testdata/output_stdin_empty.txt b/pkg/cmd/testdata/output_stdin_empty.txt new file mode 100644 index 00000000..e69de29b diff --git a/pkg/cmd/testdata/output_stdin_valid.txt b/pkg/cmd/testdata/output_stdin_valid.txt new file mode 100644 index 00000000..c9dc1193 --- /dev/null +++ b/pkg/cmd/testdata/output_stdin_valid.txt @@ -0,0 +1,17 @@ +# Resources not covered by IaC +aws_iam_user.driftctl +aws_iam_user.sundowndev +aws_iam_user.test_user +aws_iam_role_policy.OrganizationAccountAccessRole:AdministratorAccess +aws_iam_role_policy.driftctl_assume_role:driftctl_policy\.10 +aws_iam_role.OrganizationAccountAccessRole +aws_iam_role.driftctl_assume\\_role +aws_iam_user_policy.driftctl:driftctlrole +aws_iam_access_key.AKIAXYUOJZ3H5YCXF34G +aws_iam_access_key.AKIAXYUOJZ3HV2LTLXD2 +aws_iam_access_key.AKIAXYUOJZ3HUSPPQQ4L +# Missing resources +aws_iam_user.testuser1 +aws_iam_role.testrole1 +# Changed resources +aws_s3_bucket.test-20210416154114486700000001 \ No newline at end of file diff --git a/pkg/cmd/testdata/output_stdin_valid_filter.txt b/pkg/cmd/testdata/output_stdin_valid_filter.txt new file mode 100644 index 00000000..d434ae26 --- /dev/null +++ b/pkg/cmd/testdata/output_stdin_valid_filter.txt @@ -0,0 +1,5 @@ +# Missing resources +aws_iam_user.testuser1 +aws_iam_role.testrole1 +# Changed resources +aws_s3_bucket.test-20210416154114486700000001 \ No newline at end of file diff --git a/pkg/cmd/testdata/output_stdin_valid_filter2.txt b/pkg/cmd/testdata/output_stdin_valid_filter2.txt new file mode 100644 index 00000000..c267e959 --- /dev/null +++ b/pkg/cmd/testdata/output_stdin_valid_filter2.txt @@ -0,0 +1,12 @@ +# Resources not covered by IaC +aws_iam_user.driftctl +aws_iam_user.sundowndev +aws_iam_user.test_user +aws_iam_role_policy.OrganizationAccountAccessRole:AdministratorAccess +aws_iam_role_policy.driftctl_assume_role:driftctl_policy\.10 +aws_iam_role.OrganizationAccountAccessRole +aws_iam_role.driftctl_assume\\_role +aws_iam_user_policy.driftctl:driftctlrole +aws_iam_access_key.AKIAXYUOJZ3H5YCXF34G +aws_iam_access_key.AKIAXYUOJZ3HV2LTLXD2 +aws_iam_access_key.AKIAXYUOJZ3HUSPPQQ4L \ No newline at end of file From fe3d0a14eaa420f2f5be7f103b21e4e94463e744 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Fri, 14 May 2021 11:46:01 +0200 Subject: [PATCH 2/7] docs: improve gen-driftignore help message --- pkg/cmd/gen_driftignore.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/gen_driftignore.go b/pkg/cmd/gen_driftignore.go index 57ce35df..1114d821 100644 --- a/pkg/cmd/gen_driftignore.go +++ b/pkg/cmd/gen_driftignore.go @@ -13,11 +13,10 @@ func NewGenDriftIgnoreCmd() *cobra.Command { opts := &analyser.GenDriftIgnoreOptions{} cmd := &cobra.Command{ - Use: "gen-driftignore", - Short: "Generate a .driftignore file based on your scan result", - Long: "This command will generate a new .driftignore file containing your current drifts and send output to /dev/stdout", - Example: "driftctl scan -o json://stdout | driftctl gen-driftignore", - Args: cobra.NoArgs, + Use: "gen-driftignore", + Short: "Generate a .driftignore file based on your scan result", + Long: "This command will generate a new .driftignore file containing your current drifts and send output to /dev/stdout\n\nExample: driftctl scan -o json://stdout | driftctl gen-driftignore > .driftignore", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { _, list, err := genDriftIgnore(opts) if err != nil { @@ -35,7 +34,7 @@ func NewGenDriftIgnoreCmd() *cobra.Command { fl.BoolVar(&opts.ExcludeUnmanaged, "exclude-unmanaged", false, "Exclude resources not managed by IaC") fl.BoolVar(&opts.ExcludeDeleted, "exclude-missing", false, "Exclude missing resources") fl.BoolVar(&opts.ExcludeDrifted, "exclude-changed", false, "Exclude resources that changed on cloud provider") - fl.StringVarP(&opts.InputPath, "from", "f", "/dev/stdin", "Input where the JSON should be parsed") + fl.StringVarP(&opts.InputPath, "from", "f", "/dev/stdin", "Input where the JSON should be parsed from") return cmd } From ef4430c8eaa0107a6c0a4011b5e88259d790ee8e Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 18 May 2021 12:09:50 +0200 Subject: [PATCH 3/7] refactor: rename from flag to input also add an error when input is not specified --- pkg/cmd/gen_driftignore.go | 7 +++++- pkg/cmd/gen_driftignore_test.go | 42 ++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/pkg/cmd/gen_driftignore.go b/pkg/cmd/gen_driftignore.go index 1114d821..0e7656a6 100644 --- a/pkg/cmd/gen_driftignore.go +++ b/pkg/cmd/gen_driftignore.go @@ -6,6 +6,7 @@ import ( "os" "github.com/cloudskiff/driftctl/pkg/analyser" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -18,6 +19,10 @@ func NewGenDriftIgnoreCmd() *cobra.Command { Long: "This command will generate a new .driftignore file containing your current drifts and send output to /dev/stdout\n\nExample: driftctl scan -o json://stdout | driftctl gen-driftignore > .driftignore", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { + if opts.InputPath == "" { + return errors.New("Error: you need to specify an input to parse JSON from") + } + _, list, err := genDriftIgnore(opts) if err != nil { return err @@ -34,7 +39,7 @@ func NewGenDriftIgnoreCmd() *cobra.Command { fl.BoolVar(&opts.ExcludeUnmanaged, "exclude-unmanaged", false, "Exclude resources not managed by IaC") fl.BoolVar(&opts.ExcludeDeleted, "exclude-missing", false, "Exclude missing resources") fl.BoolVar(&opts.ExcludeDrifted, "exclude-changed", false, "Exclude resources that changed on cloud provider") - fl.StringVarP(&opts.InputPath, "from", "f", "/dev/stdin", "Input where the JSON should be parsed from") + fl.StringVarP(&opts.InputPath, "input", "i", "", "Input where the JSON should be parsed from") return cmd } diff --git a/pkg/cmd/gen_driftignore_test.go b/pkg/cmd/gen_driftignore_test.go index 4756ef0f..c8a9e18d 100644 --- a/pkg/cmd/gen_driftignore_test.go +++ b/pkg/cmd/gen_driftignore_test.go @@ -21,40 +21,46 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { }{ { name: "test error on invalid input", - args: []string{"-f", "./testdata/input_stdin_invalid.json"}, + args: []string{"-i", "./testdata/input_stdin_invalid.json"}, output: "./testdata/output_stdin_empty.txt", err: errors.New("invalid character 'i' looking for beginning of value"), }, { name: "test empty driftignore with valid input", - args: []string{"-f", "./testdata/input_stdin_empty.json"}, + args: []string{"-i", "./testdata/input_stdin_empty.json"}, output: "./testdata/output_stdin_empty.txt", err: nil, }, { name: "test driftignore content with valid input", - args: []string{"-f", "./testdata/input_stdin_valid.json"}, + args: []string{"-i", "./testdata/input_stdin_valid.json"}, output: "./testdata/output_stdin_valid.txt", err: nil, }, { name: "test driftignore content with valid input and filter missing & changed only", - args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-unmanaged"}, + args: []string{"-i", "./testdata/input_stdin_valid.json", "--exclude-unmanaged"}, output: "./testdata/output_stdin_valid_filter.txt", err: nil, }, { name: "test driftignore content with valid input and filter unmanaged only", - args: []string{"-f", "./testdata/input_stdin_valid.json", "--exclude-missing", "--exclude-changed"}, + args: []string{"-i", "./testdata/input_stdin_valid.json", "--exclude-missing", "--exclude-changed"}, output: "./testdata/output_stdin_valid_filter2.txt", err: nil, }, { name: "test error when input file does not exist", - args: []string{"-f", "doesnotexist"}, + args: []string{"-i", "doesnotexist"}, output: "./testdata/output_stdin_valid_filter2.txt", err: errors.New("open doesnotexist: no such file or directory"), }, + { + name: "test error when input flag is not specified", + args: []string{}, + output: "", + err: errors.New("Error: you need to specify an input to parse JSON from"), + }, } for _, c := range cases { @@ -62,18 +68,13 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { rootCmd := &cobra.Command{Use: "root"} rootCmd.AddCommand(NewGenDriftIgnoreCmd()) - output, err := os.ReadFile(c.output) - if err != nil { - t.Fatal(err) - } - stdout := os.Stdout // keep backup of the real stdout r, w, _ := os.Pipe() os.Stdout = w args := append([]string{"gen-driftignore"}, c.args...) - _, err = test.Execute(rootCmd, args...) + _, err := test.Execute(rootCmd, args...) if c.err != nil { assert.EqualError(t, err, c.err.Error()) return @@ -94,7 +95,14 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { os.Stdout = stdout // restoring the real stdout result := <-outC - assert.Equal(t, string(output), string(result)) + if c.output != "" { + output, err := os.ReadFile(c.output) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, string(output), string(result)) + } }) } } @@ -112,8 +120,8 @@ func TestGenDriftIgnoreCmd_ValidFlags(t *testing.T) { {args: []string{"gen-driftignore", "--exclude-missing"}}, {args: []string{"gen-driftignore", "--exclude-changed"}}, {args: []string{"gen-driftignore", "--exclude-changed=false", "--exclude-missing=false", "--exclude-unmanaged=true"}}, - {args: []string{"gen-driftignore", "--from", "/dev/stdin"}}, - {args: []string{"gen-driftignore", "-f", "/dev/stdout"}}, + {args: []string{"gen-driftignore", "--input", "/dev/stdin"}}, + {args: []string{"gen-driftignore", "-i", "/dev/stdout"}}, } for _, tt := range cases { @@ -141,8 +149,8 @@ func TestGenDriftIgnoreCmd_InvalidFlags(t *testing.T) { {args: []string{"gen-driftignore", "--drifted"}, err: errors.New("unknown flag: --drifted")}, {args: []string{"gen-driftignore", "--changed"}, err: errors.New("unknown flag: --changed")}, {args: []string{"gen-driftignore", "--missing"}, err: errors.New("unknown flag: --missing")}, - {args: []string{"gen-driftignore", "--from"}, err: errors.New("flag needs an argument: --from")}, - {args: []string{"gen-driftignore", "-f"}, err: errors.New("flag needs an argument: 'f' in -f")}, + {args: []string{"gen-driftignore", "--input"}, err: errors.New("flag needs an argument: --input")}, + {args: []string{"gen-driftignore", "-i"}, err: errors.New("flag needs an argument: 'i' in -i")}, } for _, tt := range cases { From 1ed957ee64279103c67ebbc46e13685e59f3e80b Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 18 May 2021 15:38:51 +0200 Subject: [PATCH 4/7] refactor: more human readable help messages --- pkg/cmd/gen_driftignore.go | 2 +- pkg/cmd/gen_driftignore_test.go | 2 +- pkg/cmd/scan.go | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/gen_driftignore.go b/pkg/cmd/gen_driftignore.go index 0e7656a6..6d24261d 100644 --- a/pkg/cmd/gen_driftignore.go +++ b/pkg/cmd/gen_driftignore.go @@ -20,7 +20,7 @@ func NewGenDriftIgnoreCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if opts.InputPath == "" { - return errors.New("Error: you need to specify an input to parse JSON from") + return errors.New("Error: you must specify an input to parse JSON from. Use driftctl gen-driftignore -i \nGenerate a JSON file using the output flag: driftctl scan -o json://path/to/drifts.json") } _, list, err := genDriftIgnore(opts) diff --git a/pkg/cmd/gen_driftignore_test.go b/pkg/cmd/gen_driftignore_test.go index c8a9e18d..e7816374 100644 --- a/pkg/cmd/gen_driftignore_test.go +++ b/pkg/cmd/gen_driftignore_test.go @@ -59,7 +59,7 @@ func TestGenDriftIgnoreCmd_Input(t *testing.T) { name: "test error when input flag is not specified", args: []string{}, output: "", - err: errors.New("Error: you need to specify an input to parse JSON from"), + err: errors.New("Error: you must specify an input to parse JSON from. Use driftctl gen-driftignore -i \nGenerate a JSON file using the output flag: driftctl scan -o json://path/to/drifts.json"), }, } diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index f4510f16..9ff1d676 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -203,6 +203,10 @@ func scanRun(opts *pkg.ScanOptions) error { telemetry.SendTelemetry(analysis) } + if analysis.Summary().TotalResources-analysis.Summary().TotalManaged > 0 { + fmt.Println("\nHint: use gen-driftignore command to generate a .driftignore file based on your drifts") + } + if !analysis.IsSync() { return cmderrors.InfrastructureNotInSync{} } From aa101e1731620f04459ebd588bfeabdd17d28f95 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Mon, 24 May 2021 14:59:18 +0200 Subject: [PATCH 5/7] refactor: use globaloutput for hint message --- pkg/cmd/scan.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/scan.go b/pkg/cmd/scan.go index 9ff1d676..b8195a22 100644 --- a/pkg/cmd/scan.go +++ b/pkg/cmd/scan.go @@ -203,11 +203,9 @@ func scanRun(opts *pkg.ScanOptions) error { telemetry.SendTelemetry(analysis) } - if analysis.Summary().TotalResources-analysis.Summary().TotalManaged > 0 { - fmt.Println("\nHint: use gen-driftignore command to generate a .driftignore file based on your drifts") - } - if !analysis.IsSync() { + globaloutput.Printf("\nHint: use gen-driftignore command to generate a .driftignore file based on your drifts\n") + return cmderrors.InfrastructureNotInSync{} } From af08a560e11e1ac1b31e87b6b8c66f6a309d5476 Mon Sep 17 00:00:00 2001 From: Louis TOUSSAINT Date: Mon, 24 May 2021 17:44:17 +0200 Subject: [PATCH 6/7] Issue 543: Fix false positive on aws_db_instance --- pkg/resource/aws/aws_db_instance_ext.go | 6 ++++++ .../testdata/acc/aws_db_instance/.terraform.lock.hcl | 1 + .../aws/testdata/acc/aws_db_instance/db_instance.tf | 11 +++++++++++ 3 files changed, 18 insertions(+) create mode 100644 pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf diff --git a/pkg/resource/aws/aws_db_instance_ext.go b/pkg/resource/aws/aws_db_instance_ext.go index b066956e..a7a5228d 100644 --- a/pkg/resource/aws/aws_db_instance_ext.go +++ b/pkg/resource/aws/aws_db_instance_ext.go @@ -14,9 +14,15 @@ func (r *AwsDbInstance) NormalizeForState() (resource.Resource, error) { if r.ApplyImmediately != nil && !*r.ApplyImmediately { r.ApplyImmediately = nil } + if r.CharacterSetName != nil && *r.CharacterSetName == "" { + r.CharacterSetName = nil + } return r, nil } func (r *AwsDbInstance) NormalizeForProvider() (resource.Resource, error) { + if r.CharacterSetName != nil && *r.CharacterSetName == "" { + r.CharacterSetName = nil + } return r, nil } diff --git a/pkg/resource/aws/testdata/acc/aws_db_instance/.terraform.lock.hcl b/pkg/resource/aws/testdata/acc/aws_db_instance/.terraform.lock.hcl index 4c3c17a7..8076e21f 100644 --- a/pkg/resource/aws/testdata/acc/aws_db_instance/.terraform.lock.hcl +++ b/pkg/resource/aws/testdata/acc/aws_db_instance/.terraform.lock.hcl @@ -5,6 +5,7 @@ provider "registry.terraform.io/hashicorp/aws" { version = "3.19.0" constraints = "3.19.0" hashes = [ + "h1:+7Vi7p13+cnrxjXbfJiTimGSFR97xCaQwkkvWcreLns=", "h1:xur9tF49NgsovNnmwmBR8RdpN8Fcg1TD4CKQPJD6n1A=", "zh:185a5259153eb9ee4699d4be43b3d509386b473683392034319beee97d470c3b", "zh:2d9a0a01f93e8d16539d835c02b8b6e1927b7685f4076e96cb07f7dd6944bc6c", diff --git a/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf b/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf new file mode 100644 index 00000000..a43f5015 --- /dev/null +++ b/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf @@ -0,0 +1,11 @@ +resource "aws_db_instance" "default" { + allocated_storage = 10 + engine = "mysql" + engine_version = "5.7" + instance_class = "db.t3.micro" + name = "mydb" + username = "foo" + password = "foobarbaz" + parameter_group_name = "default.mysql5.7" + skip_final_snapshot = true +} From b5a65e2565078de71065b3fc2f1a10752af16664 Mon Sep 17 00:00:00 2001 From: Louis TOUSSAINT Date: Tue, 25 May 2021 12:22:32 +0200 Subject: [PATCH 7/7] Issue 542: Add Env var for db_instance Acc test --- .../aws/testdata/acc/aws_db_instance/db_instance.tf | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf b/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf index a43f5015..80a14020 100644 --- a/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf +++ b/pkg/resource/aws/testdata/acc/aws_db_instance/db_instance.tf @@ -1,3 +1,13 @@ +provider "aws" { + region = "us-east-1" +} + +terraform { + required_providers { + aws = "3.19.0" + } +} + resource "aws_db_instance" "default" { allocated_storage = 10 engine = "mysql"