-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths3.go
246 lines (220 loc) · 7.45 KB
/
s3.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Package model contains the definitions of domain models and business logic.
package model
import (
"fmt"
"strings"
"time"
"github.com/nao1215/rainbow/utils/errfmt"
"github.com/nao1215/rainbow/utils/xregex"
)
// Region is the name of the AWS region.
type Region string
const (
// RegionUSEast1 US East (N. Virginia)
RegionUSEast1 Region = "us-east-1"
// RegionUSEast2 US East (Ohio)
RegionUSEast2 Region = "us-east-2"
// RegionUSWest1 US West (N. California)
RegionUSWest1 Region = "us-west-1"
// RegionUSWest2 US West (Oregon)
RegionUSWest2 Region = "us-west-2"
// RegionAFSouth1 Africa (Cape Town)
RegionAFSouth1 Region = "af-south-1"
// RegionAPEast1 Asia Pacific (Hong Kong)
RegionAPEast1 Region = "ap-east-1"
// RegionAPSouth1 Asia Pacific (Mumbai)
RegionAPSouth1 Region = "ap-south-1"
// RegionAPNortheast1 Asia Pacific (Tokyo)
RegionAPNortheast1 Region = "ap-northeast-1"
// RegionAPNortheast2 Asia Pacific (Seoul)
RegionAPNortheast2 Region = "ap-northeast-2"
// RegionAPNortheast3 Asia Pacific (Osaka-Local)
RegionAPNortheast3 Region = "ap-northeast-3"
// RegionAPSoutheast1 Asia Pacific (Singapore)
RegionAPSoutheast1 Region = "ap-southeast-1"
// RegionAPSoutheast2 Asia Pacific (Sydney)
RegionAPSoutheast2 Region = "ap-southeast-2"
// RegionCACentral1 Canada (Central)
RegionCACentral1 Region = "ca-central-1"
// RegionCNNorth1 China (Beijing)
RegionCNNorth1 Region = "cn-north-1"
// RegionCNNorthwest1 China (Ningxia)
RegionCNNorthwest1 Region = "cn-northwest-1"
// RegionEUCentral1 Europe (Frankfurt)
RegionEUCentral1 Region = "eu-central-1"
// RegionEUNorth1 Europe (Stockholm)
RegionEUNorth1 Region = "eu-north-1"
// RegionEUSouth1 Europe (Milan)
RegionEUSouth1 Region = "eu-south-1"
// RegionEUWest1 Europe (Ireland)
RegionEUWest1 Region = "eu-west-1"
// RegionEUWest2 Europe (London)
RegionEUWest2 Region = "eu-west-2"
// RegionEUWest3 Europe (Paris)
RegionEUWest3 Region = "eu-west-3"
// RegionMESouth1 Middle East (Bahrain)
RegionMESouth1 Region = "me-south-1"
// RegionSASouth1 South America (São Paulo)
RegionSASouth1 Region = "sa-south-1"
// RegionUSGovEast1 AWS GovCloud (US-East)
RegionUSGovEast1 Region = "us-gov-east-1"
// RegionUSGovWest1 AWS GovCloud (US)
RegionUSGovWest1 Region = "us-gov-west-1"
)
var regions = []Region{
RegionUSEast1, RegionUSEast2, RegionUSWest1, RegionUSWest2, RegionAFSouth1, RegionAPEast1,
RegionAPSouth1, RegionAPNortheast1, RegionAPNortheast2, RegionAPNortheast3, RegionAPSoutheast1,
RegionAPSoutheast2, RegionCACentral1, RegionCNNorth1, RegionCNNorthwest1, RegionEUCentral1,
RegionEUNorth1, RegionEUSouth1, RegionEUWest1, RegionEUWest2, RegionEUWest3, RegionMESouth1,
RegionSASouth1, RegionUSGovEast1, RegionUSGovWest1,
}
// Validate returns true if the Region exists.
func (r Region) Validate() error {
switch r {
case
RegionUSEast1, RegionUSEast2, RegionUSWest1, RegionUSWest2, RegionAFSouth1,
RegionAPEast1, RegionAPSouth1, RegionAPNortheast1, RegionAPNortheast2,
RegionAPNortheast3, RegionAPSoutheast1, RegionAPSoutheast2, RegionCACentral1,
RegionCNNorth1, RegionCNNorthwest1, RegionEUCentral1, RegionEUNorth1,
RegionEUSouth1, RegionEUWest1, RegionEUWest2, RegionEUWest3, RegionMESouth1,
RegionSASouth1, RegionUSGovEast1, RegionUSGovWest1:
return nil
case Region(""):
return ErrEmptyRegion
default:
return ErrInvalidRegion
}
}
// String returns the string representation of the Region.
func (r Region) String() string {
return string(r)
}
// Next returns the next region.
// If the region is the last one, it returns the first region.
// If the region is invalid, it returns "ap-northeast-1".
func (r Region) Next() Region {
for i, region := range regions {
if r == region {
if i == len(regions)-1 {
return regions[0]
}
return regions[i+1]
}
}
return RegionAPNortheast1
}
// Prev returns the previous region.
// If the region is the first one, it returns the last region.
// If the region is invalid, it returns "ap-northeast-1".
func (r Region) Prev() Region {
for i, region := range regions {
if r == region {
if i == 0 {
return regions[len(regions)-1]
}
return regions[i-1]
}
}
return RegionAPNortheast1
}
// Bucket is the name of the S3 bucket.
type Bucket string
// String returns the string representation of the Bucket.
func (b Bucket) String() string {
return string(b)
}
// Empty is whether bucket name is empty
func (b Bucket) Empty() bool {
return b == ""
}
// Domain returns the domain name of the Bucket.
func (b Bucket) Domain() string {
return fmt.Sprintf("%s.s3.amazonaws.com", b.String())
}
// Validate returns true if the Bucket is valid.
// Bucket naming rules: https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
func (b Bucket) Validate() error {
if b.Empty() {
return errfmt.Wrap(ErrInvalidBucketName, "s3 bucket name is empty")
}
validators := []func() error{
b.validateLength,
b.validatePattern,
b.validatePrefix,
b.validateSuffix,
b.validateCharSequence,
}
for _, v := range validators {
if err := v(); err != nil {
return err
}
}
return nil
}
const (
// BucketMinLength is the minimum length of the bucket name.
BucketMinLength = 3
// BucketMaxLength is the maximum length of the bucket name.
BucketMaxLength = 63
)
// validateLength validates the length of the bucket name.
func (b Bucket) validateLength() error {
if len(b) < 3 || len(b) > 63 {
return fmt.Errorf("s3 bucket name must be between 3 and 63 characters long")
}
return nil
}
var s3RegexPattern xregex.Regex //nolint:gochecknoglobals
// validatePattern validates the pattern of the bucket name.
func (b Bucket) validatePattern() error {
s3RegexPattern.InitOnce(`^[a-z0-9][a-z0-9.-]*[a-z0-9]$`)
if err := s3RegexPattern.MatchString(string(b)); err != nil {
return errfmt.Wrap(ErrInvalidBucketName, "s3 bucket name must use only lowercase letters, numbers, periods, and hyphens")
}
return nil
}
// validatePrefix validates the prefix of the bucket name.
func (b Bucket) validatePrefix() error {
for _, prefix := range []string{"xn--", "sthree-", "sthree-configurator"} {
if strings.HasPrefix(string(b), prefix) {
return errfmt.Wrap(ErrInvalidBucketName, "s3 bucket name must not start with \"xn--\", \"sthree-\", or \"sthree-configurator\"")
}
}
return nil
}
// validateSuffix validates the suffix of the bucket name.
func (b Bucket) validateSuffix() error {
for _, suffix := range []string{"-s3alias", "--ol-s3"} {
if strings.HasSuffix(string(b), suffix) {
return errfmt.Wrap(ErrInvalidBucketName, "s3 bucket name must not end with \"-s3alias\" or \"--ol-s3\"")
}
}
return nil
}
// validateCharSequence validates the character sequence of the bucket name.
func (b Bucket) validateCharSequence() error {
if strings.Contains(string(b), "..") || strings.Contains(string(b), "--") {
return errfmt.Wrap(ErrInvalidBucketName, "s3 bucket name must not contain consecutive periods or hyphens")
}
return nil
}
// BucketSets is the set of the BucketSet.
type BucketSets []BucketSet
// BucketSet is the set of the Bucket and the Region.
type BucketSet struct {
// Bucket is the name of the S3 bucket.
Bucket Bucket
// Region is the name of the AWS region.
Region Region
// CreationDate is date the bucket was created.
// This date can change when making changes to your bucket, such as editing its bucket policy.
CreationDate time.Time
}
// Len returns the length of the BucketSets.
func (b BucketSets) Len() int {
return len(b)
}
// Empty returns true if the BucketSets is empty.
func (b BucketSets) Empty() bool {
return b.Len() == 0
}