2020-12-09 15:31:34 +00:00
|
|
|
package output
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
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-02-12 21:38:23 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote"
|
2020-12-09 15:31:34 +00:00
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func fakeAnalysis() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddUnmanaged(
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "unmanaged-id-1",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
},
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "unmanaged-id-2",
|
|
|
|
Type: "aws_unmanaged_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddDeleted(
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "deleted-id-1",
|
|
|
|
Type: "aws_deleted_resource",
|
2020-12-22 11:17:04 +00:00
|
|
|
}, &testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "deleted-id-2",
|
|
|
|
Type: "aws_deleted_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "no-diff-id-1",
|
|
|
|
Type: "aws_no_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
2020-12-22 11:17:04 +00:00
|
|
|
a.AddDifference(analyser.Difference{Res: &testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
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{"updated", "field"},
|
|
|
|
From: "foobar",
|
|
|
|
To: "barfoo",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.CREATE,
|
|
|
|
Path: []string{"new", "field"},
|
|
|
|
From: nil,
|
|
|
|
To: "newValue",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.DELETE,
|
|
|
|
Path: []string{"a"},
|
|
|
|
From: "oldValue",
|
|
|
|
To: nil,
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}})
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisNoDrift() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
for i := 0; i < 5; i++ {
|
2020-12-22 11:17:04 +00:00
|
|
|
a.AddManaged(&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "managed-id-" + fmt.Sprintf("%d", i),
|
|
|
|
Type: "aws_managed_resource",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithJsonFields() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddManaged(
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
2020-12-22 11:17:04 +00:00
|
|
|
&testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-2",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
2020-12-22 11:17:04 +00:00
|
|
|
a.AddDifference(analyser.Difference{Res: &testresource.FakeResource{
|
2020-12-09 15:31:34 +00:00
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
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{"Json"},
|
|
|
|
From: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Removed\":\"Added\",\"Changed\":[\"ec2:DescribeInstances\"],\"Effect\":\"Allow\",\"Resource\":\"*\"}]}",
|
|
|
|
To: "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Changed\":[\"ec2:*\"],\"NewField\":[\"foobar\"],\"Effect\":\"Allow\",\"Resource\":\"*\"}]}",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}})
|
|
|
|
a.AddDifference(analyser.Difference{Res: &testresource.FakeResource{
|
|
|
|
Id: "diff-id-2",
|
|
|
|
Type: "aws_diff_resource",
|
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{"Json"},
|
|
|
|
From: "{\"foo\":\"bar\"}",
|
|
|
|
To: "{\"bar\":\"foo\"}",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
}})
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithStringerResources() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddDeleted(
|
|
|
|
&testresource.FakeResourceStringer{
|
|
|
|
Id: "dfjkgnbsgj",
|
|
|
|
Name: "deleted resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddManaged(
|
|
|
|
&testresource.FakeResourceStringer{
|
|
|
|
Id: "usqyfsdbgjsdgjkdfg",
|
|
|
|
Name: "managed resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddUnmanaged(
|
|
|
|
&testresource.FakeResourceStringer{
|
|
|
|
Id: "duysgkfdjfdgfhd",
|
|
|
|
Name: "unmanaged resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddDifference(analyser.Difference{Res: &testresource.FakeResourceStringer{
|
|
|
|
Id: "gdsfhgkbn",
|
|
|
|
Name: "resource with diff",
|
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
|
|
|
},
|
|
|
|
}})
|
|
|
|
return &a
|
|
|
|
}
|
2020-12-16 12:02:02 +00:00
|
|
|
|
|
|
|
func fakeAnalysisWithComputedFields() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.AddManaged(
|
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
a.AddDifference(analyser.Difference{Res: testresource.FakeResource{
|
|
|
|
Id: "diff-id-1",
|
|
|
|
Type: "aws_diff_resource",
|
2021-01-08 17:14:26 +00:00
|
|
|
}, Changelog: []analyser.Change{
|
2020-12-16 12:02:02 +00:00
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
Path: []string{"updated", "field"},
|
|
|
|
From: "foobar",
|
|
|
|
To: "barfoo",
|
|
|
|
},
|
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.CREATE,
|
|
|
|
Path: []string{"new", "field"},
|
|
|
|
From: nil,
|
|
|
|
To: "newValue",
|
|
|
|
},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.DELETE,
|
|
|
|
Path: []string{"a"},
|
|
|
|
From: "oldValue",
|
|
|
|
To: nil,
|
|
|
|
},
|
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
From: "foo",
|
|
|
|
To: "oof",
|
|
|
|
Path: []string{
|
|
|
|
"struct",
|
|
|
|
"0",
|
|
|
|
"array",
|
|
|
|
"0",
|
|
|
|
},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-08 17:14:26 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
|
|
|
From: "one",
|
|
|
|
To: "two",
|
|
|
|
Path: []string{
|
|
|
|
"struct",
|
|
|
|
"0",
|
|
|
|
"string",
|
|
|
|
},
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
2021-01-08 17:14:26 +00:00
|
|
|
Computed: true,
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
}})
|
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(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return &a
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeAnalysisWithEnumerationError() *analyser.Analysis {
|
|
|
|
a := analyser.Analysis{}
|
|
|
|
a.SetAlerts(alerter.Alerts{
|
|
|
|
"": []alerter.Alert{
|
|
|
|
remote.NewEnumerationAccessDeniedAlert("aws_vpc", "aws_vpc"),
|
|
|
|
remote.NewEnumerationAccessDeniedAlert("aws_sqs", "aws_sqs"),
|
|
|
|
remote.NewEnumerationAccessDeniedAlert("aws_sns", "aws_sns"),
|
2020-12-16 12:02:02 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return &a
|
|
|
|
}
|