forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 9
/
s3-handlers.go
371 lines (332 loc) · 11.7 KB
/
s3-handlers.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package bucket
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/kube-object-storage/lib-bucket-provisioner/pkg/provisioner/api/errors"
"k8s.io/apimachinery/pkg/util/json"
)
// S3Agent wraps the s3.S3 structure to allow for wrapper methods
type S3Agent struct {
client *s3.S3
}
func NewS3Agent(accessKey, secretKey, endpoint string) (*S3Agent, error) {
const cephRegion = "us-east-1"
sess, err := session.NewSession(
aws.NewConfig().
WithRegion(cephRegion).
WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, "")).
WithEndpoint(endpoint).
WithS3ForcePathStyle(true).
WithMaxRetries(20).
WithDisableSSL(true))
if err != nil {
return nil, err
}
svc := s3.New(sess)
return &S3Agent{
client: svc,
}, nil
}
// CreateBucket creates a bucket with the given name
func (s S3Agent) CreateBucket(name string) error {
logger.Infof("creating bucket %q", name)
bucketInput := &s3.CreateBucketInput{
Bucket: &name,
}
_, err := s.client.CreateBucket(bucketInput)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
logger.Infof("DEBUG: after s3 call, ok=%v, aerr=%v", ok, aerr)
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
msg := fmt.Sprintf("Bucket %q already exists", name)
logger.Errorf(msg)
return errors.NewBucketExistsError(msg)
case s3.ErrCodeBucketAlreadyOwnedByYou:
msg := fmt.Sprintf("Bucket %q already owned by you", name)
logger.Errorf(msg)
return errors.NewBucketExistsError(msg)
}
}
return fmt.Errorf("Bucket %q could not be created: %v", name, err)
}
logger.Infof("successfully created bucket %q", name)
return nil
}
// PutBucketPolicy applies the policy to the bucket
func (s S3Agent) PutBucketPolicy(bucket string, policy BucketPolicy) (*s3.PutBucketPolicyOutput, error) {
confirmRemoveSelfBucketAccess := false
serializedPolicy, _ := json.Marshal(policy)
consumablePolicy := string(serializedPolicy)
p := &s3.PutBucketPolicyInput{
Bucket: &bucket,
ConfirmRemoveSelfBucketAccess: &confirmRemoveSelfBucketAccess,
Policy: &consumablePolicy,
}
out, err := s.client.PutBucketPolicy(p)
if err != nil {
return out, err
}
return out, nil
}
func (s S3Agent) GetBucketPolicy(bucket string) (*BucketPolicy, error) {
out, err := s.client.GetBucketPolicy(&s3.GetBucketPolicyInput{
Bucket: &bucket,
})
if err != nil {
return nil, err
}
policy := &BucketPolicy{}
err = json.Unmarshal([]byte(*out.Policy), policy)
if err != nil {
return nil, err
}
return policy, nil
}
// //////////////
// Policy
// //////////////
type action string
const (
All action = "s3:*"
AbortMultipartUpload action = "s3:AbortMultipartUpload"
CreateBucket action = "s3:CreateBucket"
DeleteBucketPolicy action = "s3:DeleteBucketPolicy"
DeleteBucket action = "s3:DeleteBucket"
DeleteBucketWebsite action = "s3:DeleteBucketWebsite"
DeleteObject action = "s3:DeleteObject"
DeleteObjectVersion action = "s3:DeleteObjectVersion"
DeleteReplicationConfiguration action = "s3:DeleteReplicationConfiguration"
GetAccelerateConfiguration action = "s3:GetAccelerateConfiguration"
GetBucketAcl action = "s3:GetBucketAcl"
GetBucketCORS action = "s3:GetBucketCORS"
GetBucketLocation action = "s3:GetBucketLocation"
GetBucketLogging action = "s3:GetBucketLogging"
GetBucketNotification action = "s3:GetBucketNotification"
GetBucketPolicy action = "s3:GetBucketPolicy"
GetBucketRequestPayment action = "s3:GetBucketRequestPayment"
GetBucketTagging action = "s3:GetBucketTagging"
GetBucketVersioning action = "s3:GetBucketVersioning"
GetBucketWebsite action = "s3:GetBucketWebsite"
GetLifecycleConfiguration action = "s3:GetLifecycleConfiguration"
GetObjectAcl action = "s3:GetObjectAcl"
GetObject action = "s3:GetObject"
GetObjectTorrent action = "s3:GetObjectTorrent"
GetObjectVersionAcl action = "s3:GetObjectVersionAcl"
GetObjectVersion action = "s3:GetObjectVersion"
GetObjectVersionTorrent action = "s3:GetObjectVersionTorrent"
GetReplicationConfiguration action = "s3:GetReplicationConfiguration"
ListAllMyBuckets action = "s3:ListAllMyBuckets"
ListBucketMultiPartUploads action = "s3:ListBucketMultiPartUploads"
ListBucket action = "s3:ListBucket"
ListBucketVersions action = "s3:ListBucketVersions"
ListMultipartUploadParts action = "s3:ListMultipartUploadParts"
PutAccelerateConfiguration action = "s3:PutAccelerateConfiguration"
PutBucketAcl action = "s3:PutBucketAcl"
PutBucketCORS action = "s3:PutBucketCORS"
PutBucketLogging action = "s3:PutBucketLogging"
PutBucketNotification action = "s3:PutBucketNotification"
PutBucketPolicy action = "s3:PutBucketPolicy"
PutBucketRequestPayment action = "s3:PutBucketRequestPayment"
PutBucketTagging action = "s3:PutBucketTagging"
PutBucketVersioning action = "s3:PutBucketVersioning"
PutBucketWebsite action = "s3:PutBucketWebsite"
PutLifecycleConfiguration action = "s3:PutLifecycleConfiguration"
PutObjectAcl action = "s3:PutObjectAcl"
PutObject action = "s3:PutObject"
PutObjectVersionAcl action = "s3:PutObjectVersionAcl"
PutReplicationConfiguration action = "s3:PutReplicationConfiguration"
RestoreObject action = "s3:RestoreObject"
)
// AllowedActions is a lenient default list of actions
var AllowedActions = []action{
DeleteObject,
DeleteObjectVersion,
GetBucketAcl,
GetBucketCORS,
GetBucketLocation,
GetBucketLogging,
GetBucketNotification,
GetBucketPolicy,
GetBucketTagging,
GetBucketVersioning,
GetBucketWebsite,
GetObject,
GetObjectAcl,
GetObjectTorrent,
GetObjectVersion,
GetObjectVersionAcl,
GetObjectVersionTorrent,
ListAllMyBuckets,
ListBucket,
ListBucketMultiPartUploads,
ListBucketVersions,
ListMultipartUploadParts,
PutBucketTagging,
PutBucketVersioning,
PutBucketWebsite,
PutBucketVersioning,
PutLifecycleConfiguration,
PutObjectAcl,
PutObjectVersionAcl,
PutReplicationConfiguration,
RestoreObject,
}
type effect string
// effectAllow and effectDeny values are expected by the S3 API to be 'Allow' or 'Deny' explicitly
const (
effectAllow effect = "Allow"
effectDeny effect = "Deny"
)
// PolicyStatment is the Go representation of a PolicyStatement json struct
// it defines what Actions that a Principle can or cannot perform on a Resource
type PolicyStatement struct {
// Sid (optional) is the PolicyStatement's unique identifier
Sid string `json:"Sid"`
// Effect determins whether the Action(s) are 'Allow'ed or 'Deny'ed.
Effect effect `json:"Effect"`
// Principle is/are the Ceph user names affected by this PolicyStatement
Principal map[string][]string `json:"Principal"`
// Action is a list of s3:* actions
Action []action `json:"Action"`
// Resource is the ARN identifier for the S3 resource (bucket)
// Must be in the format of 'arn:aws:s3:::<bucket>
Resource []string `json:"Resource"`
}
// BucketPolicy represents set of policy statements for a single bucket.
type BucketPolicy struct {
// Id (optional) identifies the bucket policy
Id string `json:"Id"`
// Version is the version of the BucketPolicy data structure
// should always be '2012-10-17'
Version string `json:"Version"`
Statement []PolicyStatement `json:"Statement"`
}
// the version of the BucketPolicy json structure
const version = "2012-10-17"
// NewBucketPolicy obviously returns a new BucketPolicy. PolicyStatements may be passed in at creation
// or added after the fact. BucketPolicies should be passed to PutBucketPolicy().
func NewBucketPolicy(ps ...PolicyStatement) *BucketPolicy {
bp := &BucketPolicy{
Version: version,
Statement: append([]PolicyStatement{}, ps...),
}
return bp
}
// ModifyBucketPolicy new and old statement SIDs and overwrites on a match.
// This allows users to Get, modify, and Replace existing statements as well as
// add new ones.
func (bp *BucketPolicy) ModifyBucketPolicy(ps ...PolicyStatement) *BucketPolicy {
for _, newP := range ps {
var match bool
for j, oldP := range bp.Statement {
if newP.Sid == oldP.Sid {
bp.Statement[j] = newP
}
}
if !match {
bp.Statement = append(bp.Statement, newP)
}
}
return bp
}
func (bp *BucketPolicy) DropPolicyStatements(sid ...string) *BucketPolicy {
for _, s := range sid {
for i, stmt := range bp.Statement {
if stmt.Sid == s {
bp.Statement = append(bp.Statement[:i], bp.Statement[i+1:]...)
break
}
}
}
return bp
}
func (bp *BucketPolicy) EjectPrincipals(users ...string) *BucketPolicy {
statements := bp.Statement
for _, s := range statements {
s.EjectPrincipals(users...)
}
bp.Statement = statements
return bp
}
// NewPolicyStatement generates a new PolicyStatement. PolicyStatment methods are designed to
// be chain called with dot notation to allow for easy configuration at creation. This is preferable
// to a long parameter list.
func NewPolicyStatement() *PolicyStatement {
return &PolicyStatement{
Sid: "",
Effect: "",
Principal: map[string][]string{},
Action: []action{},
Resource: []string{},
}
}
func (ps *PolicyStatement) WithSID(sid string) *PolicyStatement {
ps.Sid = sid
return ps
}
const awsPrinciple = "AWS"
// ForPrincipals adds users to the PolicyStatement
func (ps *PolicyStatement) ForPrincipals(users ...string) *PolicyStatement {
ps.Principal[awsPrinciple] = users
return ps
}
// ForResources adds resources (buckets) to the PolicyStatement with the appropriate ARN prefix
func (ps *PolicyStatement) ForResources(resources ...string) *PolicyStatement {
const arnPrefix = "arn:aws:s3:::%s"
for _, v := range resources {
ps.Resource = append(ps.Resource, fmt.Sprintf(arnPrefix, v))
}
return ps
}
// Allows sets the effect of the PolicyStatement to allow PolicyStatement's Actions
func (ps *PolicyStatement) Allows() *PolicyStatement {
if ps.Effect != "" {
return ps
}
ps.Effect = effectAllow
return ps
}
// Denies sets the effect of the PolicyStatement to deny the PolicyStatement's Actions
func (ps *PolicyStatement) Denies() *PolicyStatement {
if ps.Effect != "" {
return ps
}
ps.Effect = effectDeny
return ps
}
// Actions is the set of "s3:*" actions for the PolicyStatement is concerned
func (ps *PolicyStatement) Actions(actions ...action) *PolicyStatement {
ps.Action = actions
return ps
}
func (ps *PolicyStatement) EjectPrincipals(users ...string) {
principals := ps.Principal[awsPrinciple]
for _, u := range users {
for j, v := range principals {
if u == v {
principals = append(principals[:j], principals[:j+1]...)
}
}
}
ps.Principal[awsPrinciple] = principals
}
// //////////////
// End Policy
// //////////////