2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-03-18 10:59:52 +00:00
|
|
|
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3BucketPolicySupplier struct {
|
2021-03-18 10:59:52 +00:00
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-03-18 10:59:52 +00:00
|
|
|
repository repository.S3Repository
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
providerConfig tf.TerraformProviderConfig
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
func NewS3BucketPolicySupplier(provider *AWSTerraformProvider, repository repository.S3Repository, deserializer *resource.Deserializer) *S3BucketPolicySupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &S3BucketPolicySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-02-16 17:42:46 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-03-18 10:59:52 +00:00
|
|
|
provider.Config,
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3BucketPolicySupplier) Resources() ([]resource.Resource, error) {
|
2021-02-16 17:42:46 +00:00
|
|
|
buckets, err := s.repository.ListAllBuckets()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, aws.AwsS3BucketPolicyResourceType, aws.AwsS3BucketResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
for _, bucket := range buckets {
|
|
|
|
bucket := *bucket
|
|
|
|
region, err := s.repository.GetBucketLocation(&bucket)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 10:59:52 +00:00
|
|
|
if region == "" || region != s.providerConfig.DefaultAlias {
|
2020-12-09 15:31:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-16 17:42:46 +00:00
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
s3BucketNotification, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: aws.AwsS3BucketPolicyResourceType,
|
|
|
|
ID: *bucket.Name,
|
|
|
|
Attributes: map[string]string{
|
|
|
|
"alias": region,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *s3BucketNotification, err
|
|
|
|
})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
ctyVals, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializedValues, err := s.deserializer.Deserialize(aws.AwsS3BucketPolicyResourceType, ctyVals)
|
2020-12-09 15:31:34 +00:00
|
|
|
results := make([]resource.Resource, 0, len(deserializedValues))
|
|
|
|
if err != nil {
|
|
|
|
return deserializedValues, err
|
|
|
|
}
|
|
|
|
for _, val := range deserializedValues {
|
2021-05-21 14:09:45 +00:00
|
|
|
res, _ := val.(*resource.AbstractResource)
|
|
|
|
policy, _ := res.Attrs.Get("policy")
|
|
|
|
if policy != nil && policy != "" {
|
|
|
|
results = append(results, res)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|