2020-12-09 15:31:34 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-03 16:20:25 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2020-12-16 12:02:02 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
2021-03-03 16:20:25 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/output"
|
2021-08-03 13:01:20 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/alerts"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
2021-09-06 15:00:38 +00:00
|
|
|
remoteerr "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-05-21 14:09:45 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
2021-09-06 15:00:38 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
)
|
|
|
|
|
2021-11-25 14:12:14 +00:00
|
|
|
func fakeAnalysis(opts analyser.AnalyzerOptions) *analyser.Analysis {
|
2021-11-25 15:15:01 +00:00
|
|
|
if opts == (analyser.AnalyzerOptions{}) {
|
|
|
|
opts = analyser.AnalyzerOptions{Deep: true}
|
|
|
|
}
|
2021-11-25 14:12:14 +00:00
|
|
|
a := analyser.NewAnalysis(opts)
|
2020-12-09 15:31:34 +00:00
|
|
|
a.AddUnmanaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "unmanaged-id-1",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "unmanaged-id-2",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddDeleted(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "deleted-id-1",
|
|
|
|
Type: "aws_deleted_resource",
|
2021-08-26 02:32:15 +00:00
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://delete_state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
}, &resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "deleted-id-2",
|
|
|
|
Type: "aws_deleted_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "no-diff-id-1",
|
|
|
|
Type: "aws_no_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
2021-09-07 11:47:13 +00:00
|
|
|
// Cover the case when a diff occur on a resource without a source
|
|
|
|
a.AddDifference(analyser.Difference{
|
|
|
|
Res: &resource.Resource{
|
|
|
|
Id: "diff-id-2",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
Changelog: []analyser.Change{
|
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"updated", "field"},
|
|
|
|
From: "foobar",
|
|
|
|
To: "barfoo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-08-09 14:03:04 +00:00
|
|
|
a.AddDifference(analyser.Difference{Res: &resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
2021-07-30 14:13:36 +00:00
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
},
|
|
|
|
Changelog: []analyser.Change{
|
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"updated", "field"},
|
|
|
|
From: "foobar",
|
|
|
|
To: "barfoo",
|
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.CREATE,
|
|
|
|
Path: []string{"new", "field"},
|
|
|
|
From: nil,
|
|
|
|
To: "newValue",
|
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.DELETE,
|
|
|
|
Path: []string{"a"},
|
|
|
|
From: "oldValue",
|
|
|
|
To: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2020-12-09 15:31:34 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
2021-04-22 16:21:00 +00:00
|
|
|
func fakeAnalysisWithAlerts() *analyser.Analysis {
|
2021-11-25 14:12:14 +00:00
|
|
|
a := fakeAnalysis(analyser.AnalyzerOptions{})
|
2021-04-22 16:21:00 +00:00
|
|
|
a.SetAlerts(alerter.Alerts{
|
|
|
|
"": []alerter.Alert{
|
2021-09-06 15:00:38 +00:00
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_vpc", "aws_vpc"), alerts.EnumerationPhase),
|
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_sqs", "aws_sqs"), alerts.EnumerationPhase),
|
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_sns", "aws_sns"), alerts.EnumerationPhase),
|
2021-04-22 16:21:00 +00:00
|
|
|
},
|
|
|
|
})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderVersion = "3.19.0"
|
2021-04-22 16:21:00 +00:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
func fakeAnalysisNoDrift() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
for i := 0; i < 5; i++ {
|
2021-08-09 14:03:04 +00:00
|
|
|
a.AddManaged(&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "managed-id-" + fmt.Sprintf("%d", i),
|
|
|
|
Type: "aws_managed_resource",
|
|
|
|
})
|
|
|
|
}
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2020-12-09 15:31:34 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithJsonFields() *analyser.Analysis {
|
2021-11-25 15:15:01 +00:00
|
|
|
a := analyser.NewAnalysis(analyser.AnalyzerOptions{Deep: true})
|
2020-12-09 15:31:34 +00:00
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-2",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
2021-07-30 14:13:36 +00:00
|
|
|
a.AddDifference(analyser.Difference{
|
2021-08-09 14:03:04 +00:00
|
|
|
Res: &resource.Resource{
|
2021-07-30 14:13:36 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
Changelog: []analyser.Change{
|
|
|
|
{
|
|
|
|
JsonString: true,
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"Json"},
|
|
|
|
From: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Removed\":\"Added\",\"Changed\":[\"oldValue1\", \"oldValue2\"],\"Effect\":\"Allow\",\"Resource\":\"*\"}]}",
|
|
|
|
To: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Changed\":\"newValue\",\"NewField\":[\"foobar\"],\"Effect\":\"Allow\",\"Resource\":\"*\"}]}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}})
|
|
|
|
a.AddDifference(analyser.Difference{
|
2021-08-09 14:03:04 +00:00
|
|
|
Res: &resource.Resource{
|
2021-07-30 14:13:36 +00:00
|
|
|
Id: "diff-id-2",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
Changelog: []analyser.Change{
|
|
|
|
{
|
|
|
|
JsonString: true,
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"Json"},
|
|
|
|
From: "{\"foo\":\"bar\"}",
|
|
|
|
To: "{\"bar\":\"foo\"}",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2020-12-09 15:31:34 +00:00
|
|
|
return &a
|
2021-05-27 12:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithoutAttrs() *analyser.Analysis {
|
2021-11-25 15:15:01 +00:00
|
|
|
a := analyser.NewAnalysis(analyser.AnalyzerOptions{Deep: true})
|
2021-05-27 12:50:39 +00:00
|
|
|
a.AddDeleted(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
Id: "dfjkgnbsgj",
|
2021-08-09 14:03:04 +00:00
|
|
|
Type: "FakeResourceStringer",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-08-26 02:32:15 +00:00
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
|
|
|
},
|
2021-05-27 12:50:39 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
Id: "usqyfsdbgjsdgjkdfg",
|
2021-08-09 14:03:04 +00:00
|
|
|
Type: "FakeResourceStringer",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-27 12:50:39 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddUnmanaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
Id: "duysgkfdjfdgfhd",
|
2021-08-09 14:03:04 +00:00
|
|
|
Type: "FakeResourceStringer",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{},
|
2021-05-27 12:50:39 +00:00
|
|
|
},
|
|
|
|
)
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2021-05-27 12:50:39 +00:00
|
|
|
return &a
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithStringerResources() *analyser.Analysis {
|
2021-11-25 15:15:01 +00:00
|
|
|
a := analyser.NewAnalysis(analyser.AnalyzerOptions{Deep: true})
|
2021-08-09 14:03:04 +00:00
|
|
|
schema := &resource.Schema{HumanReadableAttributesFunc: func(res *resource.Resource) map[string]string {
|
2021-06-02 11:56:18 +00:00
|
|
|
return map[string]string{
|
|
|
|
"Name": (*res.Attrs)["name"].(string),
|
|
|
|
}
|
|
|
|
}}
|
2020-12-09 15:31:34 +00:00
|
|
|
a.AddDeleted(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-02 11:56:18 +00:00
|
|
|
Id: "dfjkgnbsgj",
|
|
|
|
Type: "FakeResourceStringer",
|
2021-08-09 14:03:04 +00:00
|
|
|
Sch: schema,
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "deleted resource",
|
|
|
|
},
|
2021-08-26 02:32:15 +00:00
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-02 11:56:18 +00:00
|
|
|
Id: "usqyfsdbgjsdgjkdfg",
|
|
|
|
Type: "FakeResourceStringer",
|
2021-08-09 14:03:04 +00:00
|
|
|
Sch: schema,
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "managed resource",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddUnmanaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-02 11:56:18 +00:00
|
|
|
Id: "duysgkfdjfdgfhd",
|
|
|
|
Type: "FakeResourceStringer",
|
2021-08-09 14:03:04 +00:00
|
|
|
Sch: schema,
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "unmanaged resource",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
)
|
2021-08-09 14:03:04 +00:00
|
|
|
a.AddDifference(analyser.Difference{Res: &resource.Resource{
|
2021-06-02 11:56:18 +00:00
|
|
|
Id: "gdsfhgkbn",
|
|
|
|
Type: "FakeResourceStringer",
|
2021-08-09 14:03:04 +00:00
|
|
|
Sch: schema,
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "resource with diff",
|
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
}, Changelog: []analyser.Change{
|
2020-12-09 15:31:34 +00:00
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"Name"},
|
|
|
|
From: "",
|
|
|
|
To: "resource with diff",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2020-12-09 15:31:34 +00:00
|
|
|
return &a
|
|
|
|
}
|
2020-12-16 12:02:02 +00:00
|
|
|
|
|
|
|
func fakeAnalysisWithComputedFields() *analyser.Analysis {
|
2021-11-25 15:15:01 +00:00
|
|
|
a := analyser.NewAnalysis(analyser.AnalyzerOptions{Deep: true})
|
2020-12-16 12:02:02 +00:00
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2020-12-16 12:02:02 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
2021-07-30 14:13:36 +00:00
|
|
|
a.AddDifference(analyser.Difference{
|
2021-08-09 14:03:04 +00:00
|
|
|
Res: &resource.Resource{
|
2021-07-30 14:13:36 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
Source: &resource.TerraformStateSource{
|
|
|
|
State: "tfstate://state.tfstate",
|
|
|
|
Module: "module",
|
|
|
|
Name: "name",
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
}, Changelog: []analyser.Change{
|
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"updated", "field"},
|
|
|
|
From: "foobar",
|
|
|
|
To: "barfoo",
|
|
|
|
},
|
|
|
|
Computed: true,
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.CREATE,
|
|
|
|
Path: []string{"new", "field"},
|
|
|
|
From: nil,
|
|
|
|
To: "newValue",
|
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.DELETE,
|
|
|
|
Path: []string{"a"},
|
|
|
|
From: "oldValue",
|
|
|
|
To: nil,
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
From: "foo",
|
|
|
|
To: "oof",
|
|
|
|
Path: []string{
|
|
|
|
"struct",
|
|
|
|
"0",
|
|
|
|
"array",
|
|
|
|
"0",
|
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
2021-07-30 14:13:36 +00:00
|
|
|
{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
From: "one",
|
|
|
|
To: "two",
|
|
|
|
Path: []string{
|
|
|
|
"struct",
|
|
|
|
"0",
|
|
|
|
"string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
}})
|
2021-01-11 16:33:23 +00:00
|
|
|
a.SetAlerts(alerter.Alerts{
|
2021-01-21 17:05:29 +00:00
|
|
|
"": []alerter.Alert{
|
2021-02-12 21:38:23 +00:00
|
|
|
analyser.NewComputedDiffAlert(),
|
|
|
|
},
|
|
|
|
})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2021-02-12 21:38:23 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:39:14 +00:00
|
|
|
func fakeAnalysisWithAWSEnumerationError() *analyser.Analysis {
|
2021-02-12 21:38:23 +00:00
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.SetAlerts(alerter.Alerts{
|
|
|
|
"": []alerter.Alert{
|
2021-09-06 15:00:38 +00:00
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_vpc", "aws_vpc"), alerts.EnumerationPhase),
|
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_sqs", "aws_sqs"), alerts.EnumerationPhase),
|
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteAWSTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "aws_sns", "aws_sns"), alerts.EnumerationPhase),
|
2021-03-02 10:39:14 +00:00
|
|
|
},
|
|
|
|
})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2021-03-02 10:39:14 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithGithubEnumerationError() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.SetAlerts(alerter.Alerts{
|
|
|
|
"": []alerter.Alert{
|
2021-09-06 15:00:38 +00:00
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteGithubTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "github_team", "github_team"), alerts.EnumerationPhase),
|
|
|
|
alerts.NewRemoteAccessDeniedAlert(common.RemoteGithubTerraform, remoteerr.NewResourceListingErrorWithType(errors.New("dummy error"), "github_team_membership", "github_team"), alerts.EnumerationPhase),
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
})
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2020-12-16 12:02:02 +00:00
|
|
|
return &a
|
|
|
|
}
|
2021-03-03 16:20:25 +00:00
|
|
|
|
2021-06-09 14:06:21 +00:00
|
|
|
func fakeAnalysisForJSONPlan() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddUnmanaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-09 14:06:21 +00:00
|
|
|
Id: "unmanaged-id-1",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "First unmanaged resource",
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-09 14:06:21 +00:00
|
|
|
Id: "unmanaged-id-2",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "Second unmanaged resource",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-09 14:06:21 +00:00
|
|
|
Id: "managed-id-1",
|
|
|
|
Type: "aws_managed_resource",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "First managed resource",
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
&resource.Resource{
|
2021-06-09 14:06:21 +00:00
|
|
|
Id: "managed-id-2",
|
|
|
|
Type: "aws_managed_resource",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "Second managed resource",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2021-07-29 09:50:35 +00:00
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
2021-06-09 14:06:21 +00:00
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:56:00 +00:00
|
|
|
func fakeAnalysisWithoutDeep() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddUnmanaged(
|
|
|
|
&resource.Resource{
|
|
|
|
Id: "unmanaged-id-1",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"name": "First unmanaged resource",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.ProviderName = "AWS"
|
|
|
|
a.ProviderVersion = "3.19.0"
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
2021-03-03 16:20:25 +00:00
|
|
|
func TestGetPrinter(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-03-15 17:30:18 +00:00
|
|
|
name string
|
|
|
|
path string
|
|
|
|
key string
|
|
|
|
quiet bool
|
|
|
|
want output.Printer
|
2021-03-03 16:20:25 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "json file output",
|
|
|
|
path: "/path/to/file",
|
|
|
|
key: JSONOutputType,
|
|
|
|
want: output.NewConsolePrinter(),
|
|
|
|
},
|
2021-03-15 17:30:18 +00:00
|
|
|
{
|
|
|
|
name: "json file output quiet",
|
|
|
|
path: "/path/to/file",
|
|
|
|
key: JSONOutputType,
|
|
|
|
quiet: true,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
2021-03-03 16:20:25 +00:00
|
|
|
{
|
|
|
|
name: "json stdout output",
|
|
|
|
path: "stdout",
|
|
|
|
key: JSONOutputType,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "json /dev/stdout output",
|
|
|
|
path: "/dev/stdout",
|
|
|
|
key: JSONOutputType,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "console stdout output",
|
|
|
|
path: "stdout",
|
|
|
|
key: ConsoleOutputType,
|
|
|
|
want: output.NewConsolePrinter(),
|
|
|
|
},
|
2021-03-15 17:30:18 +00:00
|
|
|
{
|
|
|
|
name: "quiet console stdout output",
|
|
|
|
path: "stdout",
|
|
|
|
quiet: true,
|
|
|
|
key: ConsoleOutputType,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
2021-06-09 14:06:21 +00:00
|
|
|
{
|
|
|
|
name: "jsonplan file output",
|
|
|
|
path: "/path/to/file",
|
|
|
|
key: PlanOutputType,
|
|
|
|
want: output.NewConsolePrinter(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jsonplan stdout output",
|
|
|
|
path: "stdout",
|
|
|
|
key: PlanOutputType,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "jsonplan /dev/stdout output",
|
|
|
|
path: "/dev/stdout",
|
|
|
|
key: PlanOutputType,
|
|
|
|
want: &output.VoidPrinter{},
|
|
|
|
},
|
2021-03-03 16:20:25 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := GetPrinter(OutputConfig{
|
2021-09-01 08:39:17 +00:00
|
|
|
Key: tt.key,
|
|
|
|
Path: tt.path,
|
2021-03-15 17:30:18 +00:00
|
|
|
}, tt.quiet); !reflect.DeepEqual(got, tt.want) {
|
2021-03-03 16:20:25 +00:00
|
|
|
t.Errorf("GetPrinter() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 16:58:11 +00:00
|
|
|
|
|
|
|
func TestShouldPrint(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
outputs []OutputConfig
|
|
|
|
quiet bool
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test stdout prevents printing",
|
|
|
|
outputs: []OutputConfig{
|
|
|
|
{
|
|
|
|
Path: "stdout",
|
|
|
|
Key: JSONOutputType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test output to file doesn't prevent printing",
|
|
|
|
outputs: []OutputConfig{
|
|
|
|
{
|
|
|
|
Path: "result.json",
|
|
|
|
Key: JSONOutputType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test stdout prevents printing",
|
|
|
|
outputs: []OutputConfig{
|
|
|
|
{
|
|
|
|
Path: "result.json",
|
|
|
|
Key: JSONOutputType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Path: "stdout",
|
|
|
|
Key: PlanOutputType,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := ShouldPrint(tt.outputs, tt.quiet); got != tt.want {
|
|
|
|
t.Errorf("ShouldPrint() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|