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"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
2021-02-16 17:42:46 +00:00
|
|
|
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"
|
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
"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
|
|
|
|
deserializer deserializer.CTYDeserializer
|
|
|
|
repository repository.S3Repository
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
providerConfig tf.TerraformProviderConfig
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func NewS3BucketPolicySupplier(provider *AWSTerraformProvider, repository repository.S3Repository) *S3BucketPolicySupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &S3BucketPolicySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewS3BucketPolicyDeserializer(),
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializedValues, err := s.deserializer.Deserialize(ctyVals)
|
|
|
|
results := make([]resource.Resource, 0, len(deserializedValues))
|
|
|
|
if err != nil {
|
|
|
|
return deserializedValues, err
|
|
|
|
}
|
|
|
|
for _, val := range deserializedValues {
|
|
|
|
res, ok := val.(*aws.AwsS3BucketPolicy)
|
|
|
|
if ok {
|
|
|
|
if res.Policy != nil && *res.Policy != "" {
|
|
|
|
results = append(results, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|