Skip to content

Commit

Permalink
feat: add the support for multi-regions for s3 bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
ajutamangdev committed Sep 3, 2024
1 parent ed95fee commit d63b004
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
39 changes: 30 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
}
}
}
1 change: 1 addition & 0 deletions config/common-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ tar.gz
.inc
tmp
temp
.html

0 comments on commit d63b004

Please sign in to comment.