2021-03-31 14:33:25 +00:00
|
|
|
package pkg_test
|
|
|
|
|
|
|
|
import (
|
2021-04-07 15:02:52 +00:00
|
|
|
"reflect"
|
2021-03-31 14:33:25 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/alerter"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/analyser"
|
2021-04-14 15:04:33 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/filter"
|
2021-05-03 16:41:52 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/output"
|
2021-03-31 14:33:25 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
2021-04-07 15:02:52 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-03-26 08:44:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/github"
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
2021-03-31 14:33:25 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test"
|
|
|
|
testresource "github.com/cloudskiff/driftctl/test/resource"
|
2021-05-21 14:09:45 +00:00
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
"github.com/stretchr/testify/mock"
|
2021-03-31 14:33:25 +00:00
|
|
|
)
|
|
|
|
|
2021-03-26 08:44:55 +00:00
|
|
|
type TestProvider struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:25 +00:00
|
|
|
type TestCase struct {
|
|
|
|
name string
|
2021-03-26 08:44:55 +00:00
|
|
|
provider *TestProvider
|
2021-03-31 14:33:25 +00:00
|
|
|
stateResources []resource.Resource
|
|
|
|
remoteResources []resource.Resource
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface)
|
2021-03-31 14:33:25 +00:00
|
|
|
assert func(result *test.ScanResult, err error)
|
2021-04-30 11:46:31 +00:00
|
|
|
options *pkg.ScanOptions
|
2021-03-31 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestCases []TestCase
|
|
|
|
|
|
|
|
func runTest(t *testing.T, cases TestCases) {
|
|
|
|
for _, c := range cases {
|
2021-03-26 08:44:55 +00:00
|
|
|
if c.provider == nil {
|
|
|
|
c.provider = &TestProvider{
|
|
|
|
Name: "aws",
|
|
|
|
Version: "3.19.0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repo := testresource.InitFakeSchemaRepository(c.provider.Name, c.provider.Version)
|
|
|
|
aws.InitResourcesMetadata(repo)
|
2021-05-07 15:57:31 +00:00
|
|
|
github.InitResourcesMetadata(repo)
|
2021-03-31 14:33:25 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
testAlerter := alerter.NewAlerter()
|
|
|
|
|
|
|
|
if c.stateResources == nil {
|
|
|
|
c.stateResources = []resource.Resource{}
|
|
|
|
}
|
2021-03-26 08:44:55 +00:00
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
for _, res := range c.stateResources {
|
|
|
|
abstractResource, ok := res.(*resource.AbstractResource)
|
|
|
|
if ok {
|
|
|
|
schema, _ := repo.GetSchema(abstractResource.TerraformType())
|
|
|
|
abstractResource.Sch = schema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:25 +00:00
|
|
|
stateSupplier := &resource.MockSupplier{}
|
|
|
|
stateSupplier.On("Resources").Return(c.stateResources, nil)
|
|
|
|
|
|
|
|
if c.remoteResources == nil {
|
|
|
|
c.remoteResources = []resource.Resource{}
|
|
|
|
}
|
2021-03-26 08:44:55 +00:00
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
for _, res := range c.remoteResources {
|
|
|
|
abstractResource, ok := res.(*resource.AbstractResource)
|
|
|
|
if ok {
|
|
|
|
schema, _ := repo.GetSchema(abstractResource.TerraformType())
|
|
|
|
abstractResource.Sch = schema
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 14:33:25 +00:00
|
|
|
remoteSupplier := &resource.MockSupplier{}
|
|
|
|
remoteSupplier.On("Resources").Return(c.remoteResources, nil)
|
|
|
|
|
2021-03-29 16:10:50 +00:00
|
|
|
resourceFactory := &terraform.MockResourceFactory{}
|
|
|
|
|
|
|
|
if c.mocks != nil {
|
2021-05-21 14:09:45 +00:00
|
|
|
c.mocks(resourceFactory, repo)
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 15:47:53 +00:00
|
|
|
if c.options == nil {
|
|
|
|
c.options = &pkg.ScanOptions{}
|
|
|
|
}
|
|
|
|
|
2021-05-03 16:41:52 +00:00
|
|
|
scanProgress := &output.MockProgress{}
|
|
|
|
scanProgress.On("Start").Return().Once()
|
|
|
|
scanProgress.On("Stop").Return().Once()
|
|
|
|
|
|
|
|
iacProgress := &output.MockProgress{}
|
|
|
|
iacProgress.On("Start").Return().Once()
|
|
|
|
iacProgress.On("Stop").Return().Once()
|
|
|
|
|
|
|
|
driftctl := pkg.NewDriftCTL(remoteSupplier, stateSupplier, testAlerter, resourceFactory, c.options, scanProgress, iacProgress, repo)
|
2021-03-31 14:33:25 +00:00
|
|
|
|
|
|
|
analysis, err := driftctl.Run()
|
|
|
|
|
|
|
|
c.assert(test.NewScanResult(t, analysis), err)
|
2021-05-03 16:41:52 +00:00
|
|
|
scanProgress.AssertExpectations(t)
|
2021-03-31 14:33:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 15:02:52 +00:00
|
|
|
func matchByAttributes(input, attrs map[string]interface{}) bool {
|
|
|
|
for k, v := range attrs {
|
|
|
|
if value, ok := input[k]; !ok || !reflect.DeepEqual(value, v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:25 +00:00
|
|
|
func TestDriftctlRun_BasicBehavior(t *testing.T) {
|
|
|
|
|
|
|
|
cases := TestCases{
|
2021-04-28 10:42:06 +00:00
|
|
|
{
|
|
|
|
name: "analysis duration is set",
|
|
|
|
stateResources: []resource.Resource{},
|
|
|
|
remoteResources: []resource.Resource{},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.NotZero(result.Duration)
|
|
|
|
},
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
{
|
|
|
|
name: "infrastructure should be in sync",
|
|
|
|
stateResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertInfrastructureIsInSync()
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should have deleted resource",
|
|
|
|
stateResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertDeletedCount(1)
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should have unmanaged resource",
|
|
|
|
stateResources: []resource.Resource{},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertUnmanagedCount(1)
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should have changes of field update",
|
|
|
|
stateResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-05-21 14:09:45 +00:00
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"foobar": "barfoo",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-05-21 14:09:45 +00:00
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"foobar": "foobar",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("fake", "FakeResource", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-05-21 14:09:45 +00:00
|
|
|
Path: []string{"foobar"},
|
2021-03-31 14:33:25 +00:00
|
|
|
From: "barfoo",
|
|
|
|
To: "foobar",
|
|
|
|
},
|
|
|
|
Computed: false,
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should have changes on computed field",
|
|
|
|
stateResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "fake",
|
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "barfoo",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "fake",
|
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "foobar",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
2021-05-21 14:09:45 +00:00
|
|
|
result.AssertResourceHasDrift("fake", aws.AwsAmiResourceType, analyser.Change{
|
2021-03-31 14:33:25 +00:00
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-05-21 14:09:45 +00:00
|
|
|
Path: []string{"arn"},
|
2021-03-31 14:33:25 +00:00
|
|
|
From: "barfoo",
|
|
|
|
To: "foobar",
|
|
|
|
},
|
|
|
|
Computed: true,
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
2021-05-21 14:09:45 +00:00
|
|
|
name: "we should have changes on deleted field",
|
2021-03-31 14:33:25 +00:00
|
|
|
stateResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-03-31 14:33:25 +00:00
|
|
|
Id: "fake",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"tags": map[string]string{
|
|
|
|
"tag1": "deleted",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-03-31 14:33:25 +00:00
|
|
|
Id: "fake",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"tags": map[string]string{},
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("fake", "FakeResource", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.DELETE,
|
2021-05-21 14:09:45 +00:00
|
|
|
Path: []string{"tags", "tag1"},
|
2021-03-31 14:33:25 +00:00
|
|
|
From: "deleted",
|
|
|
|
To: nil,
|
|
|
|
},
|
|
|
|
Computed: false,
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should have changes of added field",
|
|
|
|
stateResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-03-31 14:33:25 +00:00
|
|
|
Id: "fake",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"tags": map[string]string{},
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-03-26 08:44:55 +00:00
|
|
|
&testresource.FakeResource{
|
2021-03-31 14:33:25 +00:00
|
|
|
Id: "fake",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"tags": map[string]string{
|
|
|
|
"tag1": "added",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("fake", "FakeResource", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.CREATE,
|
2021-05-21 14:09:45 +00:00
|
|
|
Path: []string{"tags", "tag1"},
|
2021-03-31 14:33:25 +00:00
|
|
|
From: nil,
|
|
|
|
To: "added",
|
|
|
|
},
|
|
|
|
Computed: false,
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should ignore default AWS IAM role when strict mode is disabled",
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-07 15:47:53 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
"role-test-1-policy-test-1",
|
|
|
|
map[string]interface{}{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "role-test-1-policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
stateResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "role-policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/aws-service-role/test",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-policy-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRolePolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"role": "role-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "role-policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-attachment-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy-test-1",
|
2021-05-12 09:35:02 +00:00
|
|
|
"users": []interface{}{},
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-test-2",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/not-aws-service-role/test",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(2)
|
2021-05-10 13:50:57 +00:00
|
|
|
result.AssertUnmanagedCount(2)
|
2021-04-14 15:04:33 +00:00
|
|
|
result.AssertDeletedCount(0)
|
|
|
|
result.AssertDriftCountTotal(0)
|
|
|
|
},
|
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{
|
|
|
|
StrictMode: false,
|
|
|
|
}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "we should not ignore default AWS IAM role when strict mode is enabled",
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-07 15:47:53 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
"role-test-1-policy-test-1",
|
|
|
|
map[string]interface{}{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "role-test-1-policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
stateResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/aws-service-role/test",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-policy-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRolePolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"role": "role-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-attachment-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy-test-1",
|
2021-05-12 09:35:02 +00:00
|
|
|
"users": []interface{}{},
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-14 15:04:33 +00:00
|
|
|
Id: "role-test-2",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/not-aws-service-role/test",
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(2)
|
|
|
|
result.AssertUnmanagedCount(4)
|
|
|
|
result.AssertDeletedCount(0)
|
|
|
|
result.AssertDriftCountTotal(0)
|
|
|
|
},
|
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
return &pkg.ScanOptions{
|
|
|
|
StrictMode: true,
|
|
|
|
}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
{
|
|
|
|
name: "we should not ignore default AWS IAM role when strict mode is enabled and a filter is specified",
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-07 15:47:53 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
"role-test-1-policy-test-1",
|
|
|
|
map[string]interface{}{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "role-test-1-policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
2021-05-21 14:09:45 +00:00
|
|
|
"policy_arn": "policy-test-1",
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
stateResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "fake",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-19 13:42:10 +00:00
|
|
|
Id: "role-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/aws-service-role/test",
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-19 13:42:10 +00:00
|
|
|
Id: "role-policy-test-1",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRolePolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"role": "role-test-1",
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-test-1",
|
|
|
|
Type: aws.AwsIamPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "policy-test-1",
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "policy-attachment-test-1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy-test-1",
|
2021-05-12 09:35:02 +00:00
|
|
|
"users": []interface{}{},
|
|
|
|
"roles": []interface{}{"role-test-1"},
|
2021-05-07 15:47:53 +00:00
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-05-07 15:47:53 +00:00
|
|
|
&resource.AbstractResource{
|
2021-04-19 13:42:10 +00:00
|
|
|
Id: "role-test-2",
|
2021-05-07 15:47:53 +00:00
|
|
|
Type: aws.AwsIamRoleResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"path": "/not-aws-service-role/test",
|
|
|
|
},
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertCoverage(0)
|
|
|
|
result.AssertInfrastructureIsNotSync()
|
|
|
|
result.AssertManagedCount(0)
|
|
|
|
result.AssertUnmanagedCount(1)
|
|
|
|
result.AssertDeletedCount(0)
|
|
|
|
result.AssertDriftCountTotal(0)
|
|
|
|
},
|
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Id=='role-test-1'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{
|
|
|
|
Filter: f,
|
|
|
|
StrictMode: true,
|
|
|
|
}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-19 13:42:10 +00:00
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runTest(t, cases)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDriftctlRun_BasicFilter(t *testing.T) {
|
|
|
|
cases := TestCases{
|
|
|
|
{
|
|
|
|
name: "test filtering on Type",
|
|
|
|
stateResources: []resource.Resource{},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "res1",
|
|
|
|
Type: "not-filtered",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "res2",
|
|
|
|
Type: "filtered",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertUnmanagedCount(1)
|
|
|
|
result.AssertResourceUnmanaged("res2", "filtered")
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='filtered'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test filtering on Id",
|
|
|
|
stateResources: []resource.Resource{},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "res1",
|
|
|
|
Type: "not-filtered",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "res2",
|
|
|
|
Type: "filtered",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertUnmanagedCount(1)
|
|
|
|
result.AssertResourceUnmanaged("res2", "filtered")
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Id=='res2'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test filtering on attribute",
|
|
|
|
stateResources: []resource.Resource{},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
2021-03-31 14:33:25 +00:00
|
|
|
Id: "res1",
|
|
|
|
Type: "filtered",
|
2021-05-21 14:09:45 +00:00
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"test_field": "value to filter on",
|
|
|
|
},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
&testresource.FakeResource{
|
|
|
|
Id: "res2",
|
|
|
|
Type: "not-filtered",
|
|
|
|
Attrs: &resource.Attributes{},
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertUnmanagedCount(1)
|
|
|
|
result.AssertResourceUnmanaged("res1", "filtered")
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Attr.test_field=='value to filter on'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-03-31 14:33:25 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runTest(t, cases)
|
|
|
|
}
|
2021-04-07 15:02:52 +00:00
|
|
|
|
|
|
|
func TestDriftctlRun_Middlewares(t *testing.T) {
|
|
|
|
cases := TestCases{
|
|
|
|
{
|
|
|
|
name: "test bucket policy expander middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"foo\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"bar\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-04 07:51:03 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsS3BucketPolicyResourceType,
|
|
|
|
"foo",
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": "foo",
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"foo\"}",
|
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"foo\"}",
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
Sch: getSchema(repo, aws.AwsS3BucketPolicyResourceType),
|
2021-04-07 15:02:52 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("foo", "aws_s3_bucket_policy", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-05-04 07:51:03 +00:00
|
|
|
Path: []string{"policy"},
|
2021-04-07 15:02:52 +00:00
|
|
|
From: "{\"Id\":\"foo\"}",
|
|
|
|
To: "{\"Id\":\"bar\"}",
|
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
Computed: false,
|
|
|
|
JsonString: true,
|
2021-04-07 15:02:52 +00:00
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_s3_bucket_policy' && Attr.bucket=='foo'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test instance block device middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-04-29 10:32:02 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "dummy-instance",
|
|
|
|
Type: "aws_instance",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"availability_zone": "us-east-1",
|
2021-05-06 10:55:19 +00:00
|
|
|
"root_block_device": []interface{}{
|
|
|
|
map[string]interface{}{
|
2021-04-29 10:32:02 +00:00
|
|
|
"volume_id": "vol-02862d9b39045a3a4",
|
|
|
|
"volume_type": "gp2",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-06 10:55:19 +00:00
|
|
|
"ebs_block_device": []interface{}{
|
|
|
|
map[string]interface{}{
|
2021-04-29 10:32:02 +00:00
|
|
|
"volume_id": "vol-018c5ae89895aca4c",
|
|
|
|
"encrypted": true,
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-04-29 15:17:55 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "vol-018c5ae89895aca4c",
|
|
|
|
Type: "aws_ebs_volume",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"encrypted": false,
|
|
|
|
"multi_attach_enabled": false,
|
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-04-29 15:17:55 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "vol-02862d9b39045a3a4",
|
|
|
|
Type: "aws_ebs_volume",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "gp3",
|
|
|
|
"multi_attach_enabled": false,
|
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-04-29 15:17:55 +00:00
|
|
|
foo := resource.AbstractResource{
|
|
|
|
Id: "vol-018c5ae89895aca4c",
|
|
|
|
Type: "aws_ebs_volume",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"encrypted": true,
|
|
|
|
"multi_attach_enabled": false,
|
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
Sch: getSchema(repo, "aws_ebs_volume"),
|
2021-04-29 15:17:55 +00:00
|
|
|
}
|
2021-05-04 13:03:17 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
2021-04-07 15:02:52 +00:00
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "vol-018c5ae89895aca4c",
|
2021-04-29 10:32:02 +00:00
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
"encrypted": true,
|
2021-04-07 15:02:52 +00:00
|
|
|
"multi_attach_enabled": false,
|
|
|
|
})
|
2021-05-04 13:03:17 +00:00
|
|
|
})).Times(1).Return(&foo, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
|
2021-04-29 15:17:55 +00:00
|
|
|
bar := resource.AbstractResource{
|
|
|
|
Id: "vol-02862d9b39045a3a4",
|
|
|
|
Type: "aws_ebs_volume",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"type": "gp2",
|
|
|
|
"multi_attach_enabled": false,
|
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
Sch: getSchema(repo, "aws_ebs_volume"),
|
2021-04-29 15:17:55 +00:00
|
|
|
}
|
2021-05-04 13:03:17 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_ebs_volume", mock.Anything, mock.MatchedBy(func(input map[string]interface{}) bool {
|
2021-04-07 15:02:52 +00:00
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "vol-02862d9b39045a3a4",
|
2021-04-29 10:32:02 +00:00
|
|
|
"availability_zone": "us-east-1",
|
|
|
|
"type": "gp2",
|
2021-04-07 15:02:52 +00:00
|
|
|
"multi_attach_enabled": false,
|
|
|
|
})
|
2021-05-04 13:03:17 +00:00
|
|
|
})).Times(1).Return(&bar, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(2)
|
|
|
|
result.AssertResourceHasDrift("vol-02862d9b39045a3a4", "aws_ebs_volume", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-04-29 15:17:55 +00:00
|
|
|
Path: []string{"type"},
|
2021-04-07 15:02:52 +00:00
|
|
|
From: "gp2",
|
|
|
|
To: "gp3",
|
|
|
|
},
|
|
|
|
Computed: true,
|
|
|
|
})
|
|
|
|
result.AssertResourceHasDrift("vol-018c5ae89895aca4c", "aws_ebs_volume", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-04-29 15:17:55 +00:00
|
|
|
Path: []string{"encrypted"},
|
2021-04-07 15:02:52 +00:00
|
|
|
From: true,
|
|
|
|
To: false,
|
|
|
|
},
|
|
|
|
Computed: true,
|
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_ebs_volume' && Attr.availability_zone=='us-east-1'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test route table expander middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "table",
|
|
|
|
Type: "aws_route_table",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route": []interface{}{
|
|
|
|
map[string]interface{}{
|
2021-05-10 14:50:49 +00:00
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "0.0.0.0/0",
|
2021-05-07 18:55:49 +00:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"cidr_block": "",
|
|
|
|
"ipv6_cidr_block": "::/0",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
2021-05-10 14:50:49 +00:00
|
|
|
"route_table_id": "table",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
2021-05-07 18:55:49 +00:00
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-07 18:55:49 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "r-table2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-07 18:55:49 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_route", "r-table1080289494", mock.MatchedBy(func(input map[string]interface{}) bool {
|
2021-04-07 15:02:52 +00:00
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
2021-05-07 18:55:49 +00:00
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
2021-04-07 15:02:52 +00:00
|
|
|
"origin": "CreateRoute",
|
|
|
|
"route_table_id": "table",
|
|
|
|
"state": "active",
|
|
|
|
})
|
2021-05-07 18:55:49 +00:00
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-table1080289494",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
2021-05-10 14:50:49 +00:00
|
|
|
"route_table_id": "table",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_cidr_block": "0.0.0.0/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
2021-05-07 18:55:49 +00:00
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_route", "r-table2750132062", mock.MatchedBy(func(input map[string]interface{}) bool {
|
2021-04-07 15:02:52 +00:00
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
2021-05-07 18:55:49 +00:00
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
2021-04-07 15:02:52 +00:00
|
|
|
"origin": "CreateRoute",
|
|
|
|
"route_table_id": "table",
|
|
|
|
"state": "active",
|
|
|
|
})
|
2021-05-07 18:55:49 +00:00
|
|
|
})).Times(1).Return(&resource.AbstractResource{
|
|
|
|
Id: "r-table2750132062",
|
|
|
|
Type: aws.AwsRouteResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"route_table_id": "table",
|
|
|
|
"origin": "CreateRoute",
|
|
|
|
"destination_ipv6_cidr_block": "::/0",
|
|
|
|
"gateway_id": "igw-07b7844a8fd17a638",
|
|
|
|
"state": "active",
|
|
|
|
},
|
|
|
|
}, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(2)
|
|
|
|
result.AssertInfrastructureIsInSync()
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_route' && Attr.gateway_id=='igw-07b7844a8fd17a638'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test sns topic policy expander middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-05-03 17:22:36 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSnsTopicResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"arn": "arn",
|
|
|
|
"id": "foo",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-03 17:15:14 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSnsTopicPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"arn": "arn",
|
|
|
|
"policy": "{\"policy\":\"baz\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-18 15:06:11 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_sns_topic_policy", "foo", map[string]interface{}{
|
2021-05-11 10:14:36 +00:00
|
|
|
"id": "foo",
|
|
|
|
"arn": "arn",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
}).Times(1).Return(&resource.AbstractResource{
|
2021-05-03 17:22:36 +00:00
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSnsTopicPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"arn": "arn",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
Sch: getSchema(repo, aws.AwsSnsTopicPolicyResourceType),
|
2021-05-03 17:22:36 +00:00
|
|
|
}, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("foo", "aws_sns_topic_policy", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-05-03 17:15:14 +00:00
|
|
|
Path: []string{"policy"},
|
2021-04-07 15:02:52 +00:00
|
|
|
From: "{\"policy\":\"bar\"}",
|
|
|
|
To: "{\"policy\":\"baz\"}",
|
|
|
|
},
|
2021-05-03 17:15:14 +00:00
|
|
|
Computed: false,
|
|
|
|
JsonString: true,
|
2021-04-07 15:02:52 +00:00
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_sns_topic_policy' && Attr.arn=='arn'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test sqs queue policy expander middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-04-30 17:29:04 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSqsQueueResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-04-30 17:19:26 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSqsQueuePolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"queue_url": "foo",
|
|
|
|
"policy": "{\"policy\":\"baz\"}",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-11 10:22:02 +00:00
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_sqs_queue_policy", "foo", map[string]interface{}{
|
|
|
|
"id": "foo",
|
|
|
|
"queue_url": "foo",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
}).Times(1).Return(&resource.AbstractResource{
|
2021-04-30 17:29:04 +00:00
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsSqsQueuePolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "foo",
|
|
|
|
"queue_url": "foo",
|
|
|
|
"policy": "{\"policy\":\"bar\"}",
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
Sch: getSchema(repo, aws.AwsSqsQueuePolicyResourceType),
|
2021-04-30 17:29:04 +00:00
|
|
|
}, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(1)
|
|
|
|
result.AssertResourceHasDrift("foo", "aws_sqs_queue_policy", analyser.Change{
|
|
|
|
Change: diff.Change{
|
|
|
|
Type: diff.UPDATE,
|
2021-04-30 17:19:26 +00:00
|
|
|
Path: []string{"policy"},
|
2021-04-07 15:02:52 +00:00
|
|
|
From: "{\"policy\":\"bar\"}",
|
|
|
|
To: "{\"policy\":\"baz\"}",
|
|
|
|
},
|
2021-04-30 17:19:26 +00:00
|
|
|
Computed: false,
|
|
|
|
JsonString: true,
|
2021-04-07 15:02:52 +00:00
|
|
|
})
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_sqs_queue_policy' && Attr.queue_url=='foo'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test security group rule sanitizer middleware",
|
|
|
|
stateResources: []resource.Resource{
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3970541193",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3970541193",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
2021-05-10 16:02:57 +00:00
|
|
|
"self": true,
|
|
|
|
"source_security_group_id": "sg-0254c038e32f25530",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-845917806",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-845917806",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-294318973",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-294318973",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"1.2.0.0/16", "5.6.7.0/24"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2471889226",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2471889226",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"prefix_list_ids": []interface{}{"pl-abb451c2"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3587309474",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3587309474",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
2021-05-10 16:02:57 +00:00
|
|
|
"source_security_group_id": "sg-9e0204ff",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3970541193",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3970541193",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
2021-05-10 16:02:57 +00:00
|
|
|
"self": true,
|
|
|
|
"source_security_group_id": "sg-0254c038e32f25530",
|
|
|
|
},
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
2021-05-10 16:02:57 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-1707973622",
|
|
|
|
Attrs: &resource.Attributes{
|
2021-04-07 15:02:52 +00:00
|
|
|
"id": "sgrule-1707973622",
|
2021-05-10 16:02:57 +00:00
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2821752134",
|
|
|
|
Attrs: &resource.Attributes{
|
2021-04-07 15:02:52 +00:00
|
|
|
"id": "sgrule-2821752134",
|
2021-05-10 16:02:57 +00:00
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2165103420",
|
|
|
|
Attrs: &resource.Attributes{
|
2021-04-07 15:02:52 +00:00
|
|
|
"id": "sgrule-2165103420",
|
2021-05-10 16:02:57 +00:00
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"5.6.7.0/24"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2582518759",
|
|
|
|
Attrs: &resource.Attributes{
|
2021-04-07 15:02:52 +00:00
|
|
|
"id": "sgrule-2582518759",
|
2021-05-10 16:02:57 +00:00
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"1.2.0.0/16"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2471889226",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2471889226",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"prefix_list_ids": []interface{}{"pl-abb451c2"},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3587309474",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3587309474",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
2021-05-10 16:02:57 +00:00
|
|
|
"source_security_group_id": "sg-9e0204ff",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-21 14:09:45 +00:00
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
2021-05-10 16:02:57 +00:00
|
|
|
rule1 := resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-1707973622",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-1707973622",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{
|
2021-05-10 16:02:57 +00:00
|
|
|
"0.0.0.0/0",
|
|
|
|
},
|
2021-05-24 15:19:06 +00:00
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_security_group_rule", rule1.Id,
|
|
|
|
mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "sgrule-1707973622",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"0.0.0.0/0"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
})
|
|
|
|
})).Times(1).Return(&rule1, nil)
|
|
|
|
|
|
|
|
rule2 := resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2821752134",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2821752134",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{
|
2021-05-10 16:02:57 +00:00
|
|
|
"::/0",
|
|
|
|
},
|
2021-05-24 15:19:06 +00:00
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_security_group_rule", rule2.Id,
|
|
|
|
mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "sgrule-2821752134",
|
|
|
|
"type": "egress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{"::/0"},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
})
|
|
|
|
})).Times(1).Return(&rule2, nil)
|
|
|
|
|
|
|
|
rule3 := resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2165103420",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2165103420",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{
|
2021-05-10 16:02:57 +00:00
|
|
|
"5.6.7.0/24",
|
|
|
|
},
|
2021-05-24 15:19:06 +00:00
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_security_group_rule", rule3.Id,
|
|
|
|
mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "sgrule-2165103420",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"5.6.7.0/24"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
})
|
|
|
|
})).Times(1).Return(&rule3, nil)
|
|
|
|
|
|
|
|
rule4 := resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-2582518759",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-2582518759",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{
|
2021-05-10 16:02:57 +00:00
|
|
|
"1.2.0.0/16",
|
|
|
|
},
|
2021-05-24 15:19:06 +00:00
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", "aws_security_group_rule", rule4.Id,
|
|
|
|
mock.MatchedBy(func(input map[string]interface{}) bool {
|
|
|
|
return matchByAttributes(input, map[string]interface{}{
|
|
|
|
"id": "sgrule-2582518759",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "-1",
|
2021-05-24 15:19:06 +00:00
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(0),
|
|
|
|
"cidr_blocks": []interface{}{"1.2.0.0/16"},
|
|
|
|
"ipv6_cidr_blocks": []interface{}{},
|
|
|
|
"prefix_list_ids": []interface{}{},
|
2021-05-10 16:02:57 +00:00
|
|
|
})
|
|
|
|
})).Times(1).Return(&rule4, nil)
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(7)
|
|
|
|
result.AssertInfrastructureIsInSync()
|
|
|
|
},
|
2021-04-14 15:04:33 +00:00
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_security_group_rule' && Attr.security_group_id=='sg-0254c038e32f25530'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return &pkg.ScanOptions{Filter: f}
|
|
|
|
}(t),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test iam_policy_attachment_transformer & iam_policy_attachment_expander middleware",
|
|
|
|
stateResources: []resource.Resource{
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3970541193",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3970541193",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
|
|
|
"self": true,
|
|
|
|
"source_security_group_id": "sg-0254c038e32f25530",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "iduser1",
|
|
|
|
Type: aws.AwsIamUserPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"user": "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "idrole1",
|
|
|
|
Type: aws.AwsIamRolePolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"role": "role1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
remoteResources: []resource.Resource{
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Type: aws.AwsSecurityGroupRuleResourceType,
|
|
|
|
Id: "sgrule-3970541193",
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "sgrule-3970541193",
|
|
|
|
"type": "ingress",
|
|
|
|
"security_group_id": "sg-0254c038e32f25530",
|
|
|
|
"protocol": "tcp",
|
|
|
|
"from_port": float64(0),
|
|
|
|
"to_port": float64(65535),
|
|
|
|
"self": true,
|
|
|
|
"source_security_group_id": "sg-0254c038e32f25530",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "iduser1",
|
|
|
|
Type: aws.AwsIamUserPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"user": "user1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "idrole1",
|
|
|
|
Type: aws.AwsIamRolePolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"role": "role1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mocks: func(factory resource.ResourceFactory, repo resource.SchemaRepositoryInterface) {
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", aws.AwsIamPolicyAttachmentResourceType, "iduser1", map[string]interface{}{
|
|
|
|
"id": "iduser1",
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{"user1"},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{},
|
|
|
|
}).Twice().Return(&resource.AbstractResource{
|
|
|
|
Id: "id1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "iduser1",
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{"user1"},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", aws.AwsIamPolicyAttachmentResourceType, "user1-policy_arn1", map[string]interface{}{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{"user1"},
|
|
|
|
}).Twice().Return(&resource.AbstractResource{
|
|
|
|
Id: "user1-policy_arn1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{"user1"},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", aws.AwsIamPolicyAttachmentResourceType, "idrole1", map[string]interface{}{
|
|
|
|
"id": "idrole1",
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{"role1"},
|
|
|
|
}).Twice().Return(&resource.AbstractResource{
|
|
|
|
Id: "idrole1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"id": "idrole1",
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"users": []interface{}{},
|
|
|
|
"groups": []interface{}{},
|
|
|
|
"roles": []interface{}{"role1"},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
factory.(*terraform.MockResourceFactory).On("CreateAbstractResource", aws.AwsIamPolicyAttachmentResourceType, "role1-policy_arn1", map[string]interface{}{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"roles": []interface{}{"role1"},
|
|
|
|
}).Twice().Return(&resource.AbstractResource{
|
|
|
|
Id: "role1-policy_arn1",
|
|
|
|
Type: aws.AwsIamPolicyAttachmentResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"policy_arn": "policy_arn1",
|
|
|
|
"roles": []interface{}{"role1"},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
},
|
|
|
|
assert: func(result *test.ScanResult, err error) {
|
|
|
|
result.AssertManagedCount(2)
|
|
|
|
result.AssertInfrastructureIsInSync()
|
|
|
|
},
|
|
|
|
options: func(t *testing.T) *pkg.ScanOptions {
|
|
|
|
filterStr := "Type=='aws_iam_policy_attachment'"
|
|
|
|
f, err := filter.BuildExpression(filterStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
|
|
|
|
}
|
|
|
|
|
2021-04-14 15:04:33 +00:00
|
|
|
return &pkg.ScanOptions{Filter: f}
|
2021-04-30 11:46:31 +00:00
|
|
|
}(t),
|
2021-04-07 15:02:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runTest(t, cases)
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
|
|
|
|
func getSchema(repo resource.SchemaRepositoryInterface, resourceType string) *resource.Schema {
|
|
|
|
sch, _ := repo.GetSchema(resourceType)
|
|
|
|
return sch
|
|
|
|
}
|