driftctl/enumeration/remote/aws/s3_bucket_inventory_enumera...

82 lines
2.6 KiB
Go
Raw Normal View History

2021-06-29 12:29:28 +00:00
package aws
import (
"fmt"
"github.com/snyk/driftctl/enumeration/alerter"
"github.com/snyk/driftctl/enumeration/remote/alerts"
"github.com/snyk/driftctl/enumeration/remote/aws/repository"
"github.com/snyk/driftctl/enumeration/remote/common"
remoteerror "github.com/snyk/driftctl/enumeration/remote/error"
tf "github.com/snyk/driftctl/enumeration/remote/terraform"
2021-06-29 12:29:28 +00:00
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/snyk/driftctl/enumeration/resource/aws"
2021-06-29 12:29:28 +00:00
)
type S3BucketInventoryEnumerator struct {
repository repository.S3Repository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
2021-08-03 10:34:36 +00:00
alerter alerter.AlerterInterface
2021-06-29 12:29:28 +00:00
}
2021-08-03 10:34:36 +00:00
func NewS3BucketInventoryEnumerator(repo repository.S3Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig, alerter alerter.AlerterInterface) *S3BucketInventoryEnumerator {
2021-06-29 12:29:28 +00:00
return &S3BucketInventoryEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
2021-08-03 10:34:36 +00:00
alerter: alerter,
2021-06-29 12:29:28 +00:00
}
}
func (e *S3BucketInventoryEnumerator) SupportedType() resource.ResourceType {
return aws.AwsS3BucketInventoryResourceType
}
2021-08-09 14:03:04 +00:00
func (e *S3BucketInventoryEnumerator) Enumerate() ([]*resource.Resource, error) {
2021-06-29 12:29:28 +00:00
buckets, err := e.repository.ListAllBuckets()
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsS3BucketResourceType)
2021-06-29 12:29:28 +00:00
}
2021-10-27 09:30:47 +00:00
results := make([]*resource.Resource, 0, len(buckets))
2021-06-29 12:29:28 +00:00
for _, bucket := range buckets {
region, err := e.repository.GetBucketLocation(*bucket.Name)
if err != nil {
2021-08-03 10:34:36 +00:00
alerts.SendEnumerationAlert(common.RemoteAWSTerraform, e.alerter, remoteerror.NewResourceScanningError(err, string(e.SupportedType()), *bucket.Name))
continue
2021-06-29 12:29:28 +00:00
}
if region == "" || region != e.providerConfig.DefaultAlias {
logrus.WithFields(logrus.Fields{
"region": region,
"bucket": *bucket.Name,
}).Debug("Skipped bucket inventory")
continue
}
inventoryConfigurations, err := e.repository.ListBucketInventoryConfigurations(bucket, region)
if err != nil {
2021-08-03 10:34:36 +00:00
// TODO: we should think about a way to ignore just one bucket inventory listing
return nil, remoteerror.NewResourceListingError(err, string(e.SupportedType()))
2021-06-29 12:29:28 +00:00
}
for _, config := range inventoryConfigurations {
id := fmt.Sprintf("%s:%s", *bucket.Name, *config.Id)
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
id,
map[string]interface{}{
"region": region,
},
),
)
}
}
return results, err
}