2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/resource/aws"
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/resource"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestS3BucketAcl_Execute(t *testing.T) {
|
|
|
|
type args struct {
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources *[]*resource.Resource
|
|
|
|
resourcesFromState *[]*resource.Resource
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
2021-08-09 14:03:04 +00:00
|
|
|
assert func(assert *assert.Assertions, remoteResources, resourcesFromState *[]*resource.Resource)
|
2020-12-09 15:31:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "grant field on remote resource must be reset if acl != private in state resource",
|
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"grant": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-05-21 14:09:45 +00:00
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"acl": "public-read",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
assert: func(assert *assert.Assertions, remoteResources, resourcesFromState *[]*resource.Resource) {
|
|
|
|
remoteRes := (*remoteResources)[0]
|
|
|
|
stateRes := (*resourcesFromState)[1]
|
2021-04-29 14:36:05 +00:00
|
|
|
_, exist := remoteRes.Attrs.Get("grant")
|
|
|
|
_, stateAclExist := stateRes.Attrs.Get("acl")
|
|
|
|
_, remoteAclExist := remoteRes.Attrs.Get("acl")
|
|
|
|
assert.False(exist)
|
|
|
|
assert.False(stateAclExist)
|
|
|
|
assert.False(remoteAclExist)
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "does not modify grant field on remote resource if acl field is private",
|
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"grant": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-05-21 14:09:45 +00:00
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"acl": "private",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
assert: func(assert *assert.Assertions, remoteResources, resourcesFromState *[]*resource.Resource) {
|
|
|
|
s3Bucket := (*remoteResources)[0]
|
2021-04-29 14:36:05 +00:00
|
|
|
grantAttr, exist := s3Bucket.Attrs.Get("grant")
|
|
|
|
grant := grantAttr.([]map[string]interface{})
|
|
|
|
assert.True(exist)
|
|
|
|
assert.Len(grant, 1)
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-04-29 14:36:05 +00:00
|
|
|
assert.Equal(expected, grant[0])
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "does not modify grant field on remote resource if acl field is undefined",
|
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"grant": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-05-21 14:09:45 +00:00
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
assert: func(assert *assert.Assertions, remoteResources, resourcesFromState *[]*resource.Resource) {
|
|
|
|
s3Bucket := (*remoteResources)[0]
|
2021-04-29 14:36:05 +00:00
|
|
|
grantAttr, exist := s3Bucket.Attrs.Get("grant")
|
|
|
|
grant := grantAttr.([]map[string]interface{})
|
|
|
|
assert.True(exist)
|
|
|
|
assert.Len(grant, 1)
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
|
|
|
}
|
|
|
|
assert.Equal(expected, grant[0])
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "does not modify grant field on remote resource if acl field is empty",
|
|
|
|
args: args{
|
2021-08-09 14:03:04 +00:00
|
|
|
remoteResources: &[]*resource.Resource{
|
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"grant": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
resourcesFromState: &[]*resource.Resource{
|
|
|
|
{
|
2021-05-21 14:09:45 +00:00
|
|
|
Type: aws.AwsAmiResourceType,
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
{
|
2021-04-29 14:36:05 +00:00
|
|
|
Id: "testgrant",
|
|
|
|
Type: aws.AwsS3BucketResourceType,
|
|
|
|
Attrs: &resource.Attributes{
|
|
|
|
"acl": "",
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 14:03:04 +00:00
|
|
|
assert: func(assert *assert.Assertions, remoteResources, resourcesFromState *[]*resource.Resource) {
|
|
|
|
s3Bucket := (*remoteResources)[0]
|
2021-04-29 14:36:05 +00:00
|
|
|
grantAttr, exist := s3Bucket.Attrs.Get("grant")
|
|
|
|
grant := grantAttr.([]map[string]interface{})
|
|
|
|
assert.True(exist)
|
|
|
|
assert.Len(grant, 1)
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"id": "356616ba70ebbea29732c95eef24f9ea326b9018c167651705348b5af406a6db",
|
|
|
|
"permissions": []string{"FULL_CONTROL"},
|
|
|
|
"type": "CanonicalUser",
|
|
|
|
"uri": "",
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-04-29 14:36:05 +00:00
|
|
|
assert.Equal(expected, grant[0])
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range tests {
|
|
|
|
t.Run(c.name, func(tt *testing.T) {
|
|
|
|
assert := assert.New(tt)
|
|
|
|
m := S3BucketAcl{}
|
|
|
|
if err := m.Execute(c.args.remoteResources, c.args.resourcesFromState); err != nil {
|
|
|
|
tt.Error(err)
|
|
|
|
}
|
|
|
|
c.assert(assert, c.args.remoteResources, c.args.resourcesFromState)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|