/
custom.go
111 lines (95 loc) · 2.57 KB
/
custom.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
package provider
import (
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/sa7mon/s3scanner/bucket"
"github.com/sa7mon/s3scanner/provider/clientmap"
)
type CustomProvider struct {
regions []string
clients *clientmap.ClientMap
insecure bool
addressStyle int
endpointFormat string
}
func (cp CustomProvider) Insecure() bool {
return cp.insecure
}
func (cp CustomProvider) AddressStyle() int {
return cp.addressStyle
}
func (CustomProvider) Name() string {
return "custom"
}
func (cp CustomProvider) BucketExists(b *bucket.Bucket) (*bucket.Bucket, error) {
b.Provider = cp.Name()
exists, region, err := bucketExists(cp.clients, b)
if err != nil {
return b, err
}
if exists {
b.Exists = bucket.BucketExists
b.Region = region
} else {
b.Exists = bucket.BucketNotExist
}
return b, nil
}
func (cp CustomProvider) Scan(b *bucket.Bucket, doDestructiveChecks bool) error {
client := cp.getRegionClient(b.Region)
return checkPermissions(client, b, doDestructiveChecks)
}
func (cp CustomProvider) Enumerate(b *bucket.Bucket) error {
if b.Exists != bucket.BucketExists {
return errors.New("bucket might not exist")
}
if b.PermAllUsersRead != bucket.PermissionAllowed {
return nil
}
client := cp.getRegionClient(b.Region)
enumErr := enumerateListObjectsV2(client, b)
if enumErr != nil {
return enumErr
}
return nil
}
func (cp *CustomProvider) getRegionClient(region string) *s3.Client {
return cp.clients.Get(region)
}
/*
NewCustomProvider is a constructor which makes a new custom provider with the given options.
addressStyle should either be "path" or "vhost"
*/
func NewCustomProvider(addressStyle string, insecure bool, regions []string, endpointFormat string) (*CustomProvider, error) {
cp := new(CustomProvider)
cp.regions = regions
cp.insecure = insecure
cp.endpointFormat = endpointFormat
if addressStyle == "path" {
cp.addressStyle = PathStyle
} else if addressStyle == "vhost" {
cp.addressStyle = VirtualHostStyle
} else {
return cp, fmt.Errorf("unknown custom provider address style: %s. Expected 'path' or 'vhost'", addressStyle)
}
clients, err := cp.newClients()
if err != nil {
return nil, err
}
cp.clients = clients
return cp, nil
}
func (cp *CustomProvider) newClients() (*clientmap.ClientMap, error) {
clients := clientmap.WithCapacity(len(cp.regions))
for _, r := range cp.regions {
regionUrl := strings.Replace(cp.endpointFormat, "$REGION", r, -1)
client, err := newNonAWSClient(cp, regionUrl)
if err != nil {
return nil, err
}
clients.Set(r, client)
}
return clients, nil
}