-
Notifications
You must be signed in to change notification settings - Fork 1
/
badbucket.go
120 lines (106 loc) · 2.86 KB
/
badbucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package badbucket
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
//IsBucketListable checks if a bucket is listable
func IsBucketListable(s *s3.S3, bucketName string) (bool, error) {
params := &s3.ListObjectsInput{Bucket: aws.String(bucketName)}
_, err := s.ListObjects(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Get error details
if awsErr.Code() == "NoSuchBucket" {
return false, err
} else {
return false, nil
}
}
}
return true, nil
}
//AreFilesReadable lists objects in an s3 bucket and tries to download the first one
func AreFilesReadable(s *session.Session, bucketName string) (bool, error) {
svc := s3.New(s)
downloader := s3manager.NewDownloader(s)
params := &s3.ListObjectsInput{Bucket: aws.String(bucketName)}
resp, err := svc.ListObjects(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Get error details
if awsErr.Code() == "NoSuchBucket" {
return false, err
} else {
return false, nil
}
}
}
if resp.Contents == nil {
return false, fmt.Errorf("No files in bucket")
}
testKey := resp.Contents[0].Key
file, err := os.Create(*testKey)
if err != nil {
log.Panicf("Unable to open file %q, %v", "testfile", err)
}
defer file.Close()
_, err = downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(*testKey),
})
if err != nil {
log.Printf("Unable to download item %q, %v", *testKey, err)
return false, nil
}
return true, nil
}
func IsBucketWriteable(s *session.Session, bucketName string) (bool, error) {
// Create an uploader with the session and default options
uploader := s3manager.NewUploader(s)
d1 := []byte("thiswasgeneratedbybadbycket\n")
err := ioutil.WriteFile("/tmp/buckettest.txt", d1, 0644)
f, err := os.Open("/tmp/buckettest.txt")
defer f.Close()
if err != nil {
return false, fmt.Errorf("failed to open file %q, %v", f, err)
}
// Upload the file to S3.
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucketName),
Key: aws.String("badbucketpoc.txt"),
Body: f,
})
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Get error details
if awsErr.Code() == "AccessDenied" {
return false, nil
} else {
return false, err
}
}
}
return true, nil
}
func DetectBucketRegion(bucketName string) string {
var bucketURL bytes.Buffer
bucketURL.WriteString("http://")
bucketURL.WriteString(bucketName)
bucketURL.WriteString(".s3.amazonaws.com/")
resp, err := http.Get(bucketURL.String())
if err != nil {
log.Fatalf("Unable to determine region. Exiting...")
}
region := resp.Header["X-Amz-Bucket-Region"][0]
return region
}