From d63b0048b1e003793d25f6ae3d8c5042919a0d79 Mon Sep 17 00:00:00 2001 From: ajutamangdev Date: Tue, 3 Sep 2024 16:54:34 +0545 Subject: [PATCH] feat: add the support for multi-regions for s3 bucket --- cmd/main.go | 39 ++++++++++++++++++++++++++++++--------- config/common-files.txt | 1 + 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 1383650..bc6b8f8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -24,6 +24,16 @@ type Pattern struct { IsRegex bool } +var regions = []string{ + "us-east-1", "us-east-2", "us-west-1", "us-west-2", + "af-south-1", "ap-east-1", "ap-south-1", "ap-northeast-1", + "ap-northeast-2", "ap-northeast-3", "ap-southeast-1", + "ap-southeast-2", "ca-central-1", "cn-north-1", "cn-northwest-1", + "eu-central-1", "eu-west-1", "eu-west-2", "eu-west-3", + "eu-north-1", "eu-south-1", "me-south-1", "sa-east-1", + "us-gov-east-1", "us-gov-west-1", +} + func createInsecureHTTPClient() *http.Client { // skips certificate verification tr := &http.Transport{ @@ -156,15 +166,26 @@ func runMain() { } if checkBucketPublic(*bucketName) { - cfg, err := config.LoadDefaultConfig(context.TODO(), - config.WithRegion("us-east-1"), - config.WithCredentialsProvider(aws.AnonymousCredentials{}), - ) - if err != nil { - log.Fatalf("failed to load config, %v", err) - } - svc := s3.NewFromConfig(cfg) + for _, region := range regions { + cfg, err := config.LoadDefaultConfig(context.TODO(), + config.WithRegion(region), + config.WithCredentialsProvider(aws.AnonymousCredentials{}), + ) + if err != nil { + log.Printf("failed to load config for region %s, %v", region, err) + continue + } + + svc := s3.NewFromConfig(cfg) - enumerateFiles(svc, *bucketName, commonFilesPatterns) + _, err = svc.HeadBucket(context.TODO(), &s3.HeadBucketInput{ + Bucket: bucketName, + }) + if err == nil { + fmt.Printf("Bucket found in region %s\n", region) + enumerateFiles(svc, *bucketName, commonFilesPatterns) + break + } + } } } diff --git a/config/common-files.txt b/config/common-files.txt index 2801f26..df0ef5a 100644 --- a/config/common-files.txt +++ b/config/common-files.txt @@ -31,3 +31,4 @@ tar.gz .inc tmp temp +.html \ No newline at end of file