2021-01-27 22:47:39 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
awssdk "github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
2021-01-27 22:47:39 +00:00
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAwsBucketPolicyExpander_Execute(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
resourcesFromState []resource.Resource
|
2021-05-04 07:51:03 +00:00
|
|
|
mocks func(*terraform.MockResourceFactory)
|
2021-01-27 22:47:39 +00:00
|
|
|
expected []resource.Resource
|
|
|
|
}{
|
|
|
|
{
|
2021-05-04 07:51:03 +00:00
|
|
|
name: "Inline policy, no aws_s3_bucket_policy attached",
|
|
|
|
mocks: func(factory *terraform.MockResourceFactory) {
|
|
|
|
factory.On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsS3BucketPolicyResourceType,
|
|
|
|
"foo",
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": "foo",
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"MYINLINEBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}",
|
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
resourcesFromState: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"MYINLINEBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
expected: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-05-04 07:51:03 +00:00
|
|
|
name: "No inline policy, aws_s3_bucket_policy attached",
|
|
|
|
mocks: func(factory *terraform.MockResourceFactory) {
|
|
|
|
factory.On(
|
|
|
|
"CreateAbstractResource",
|
|
|
|
aws.AwsS3BucketPolicyResourceType,
|
|
|
|
"foo",
|
|
|
|
map[string]interface{}{
|
|
|
|
"id": "foo",
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"MYBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}",
|
|
|
|
},
|
|
|
|
).Once().Return(&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
resourcesFromState: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
expected: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-05-04 07:51:03 +00:00
|
|
|
name: "Inline policy and aws_s3_bucket_policy",
|
|
|
|
resourcesFromState: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": awssdk.String("{\"Id\":\"MYINLINEBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}"),
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"MYBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
expected: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "{\"Id\":\"MYBUCKETPOLICY\",\"Statement\":[{\"Action\":\"s3:*\",\"Condition\":{\"IpAddress\":{\"aws:SourceIp\":\"8.8.8.8/32\"}},\"Effect\":\"Deny\",\"Principal\":\"*\",\"Resource\":\"arn:aws:s3:::bucket-test-policy-like-sqs/*\",\"Sid\":\"IPAllow\"}],\"Version\":\"2012-10-17\"}",
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-04-29 14:36:05 +00:00
|
|
|
{
|
2021-05-04 07:51:03 +00:00
|
|
|
name: "empty policy ",
|
|
|
|
resourcesFromState: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-04 07:51:03 +00:00
|
|
|
expected: []resource.Resource{
|
2021-04-29 14:36:05 +00:00
|
|
|
&resource.AbstractResource{
|
|
|
|
Id: "foo",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"bucket": "foo",
|
|
|
|
"policy": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-01-27 22:47:39 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
factory := &terraform.MockResourceFactory{}
|
2021-05-04 07:51:03 +00:00
|
|
|
if tt.mocks != nil {
|
|
|
|
tt.mocks(factory)
|
|
|
|
}
|
2021-03-29 16:10:50 +00:00
|
|
|
|
|
|
|
m := NewAwsBucketPolicyExpander(factory)
|
2021-01-27 22:47:39 +00:00
|
|
|
err := m.Execute(&[]resource.Resource{}, &tt.resourcesFromState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
changelog, err := diff.Diff(tt.expected, tt.resourcesFromState)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(changelog) > 0 {
|
|
|
|
for _, change := range changelog {
|
|
|
|
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|