-
Notifications
You must be signed in to change notification settings - Fork 19
/
acl.go
291 lines (237 loc) · 7.8 KB
/
acl.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
package sos
import (
"context"
"fmt"
"strings"
"github.com/exoscale/cli/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
)
const (
ACLGranteeAllUsers = "http://acs.amazonaws.com/groups/global/AllUsers"
ACLGranteeAuthenticatedUsers = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
SetACLCmdFlagRead = "read"
SetACLCmdFlagWrite = "write"
SetACLCmdFlagReadACP = "read-acp"
SetACLCmdFlagWriteACP = "write-acp"
SetACLCmdFlagFullControl = "full-control"
)
type ACL struct {
Canned string `json:"-"`
Read string `json:"read"`
Write string `json:"write"`
ReadACP string `json:"read_acp"`
WriteACP string `json:"write_acp"`
FullControl string `json:"full_control"`
}
func (c *Client) SetBucketACL(ctx context.Context, bucket string, acl *ACL) error {
s3ACL := s3.PutBucketAclInput{Bucket: aws.String(bucket)}
if acl.Canned != "" {
if !utils.IsInList(BucketCannedACLToStrings(), acl.Canned) {
return fmt.Errorf("invalid canned ACL %q, supported values are: %s",
acl.Canned,
strings.Join(BucketCannedACLToStrings(), ", "))
}
s3ACL.ACL = s3types.BucketCannedACL(acl.Canned)
} else {
s3ACL.AccessControlPolicy = &s3types.AccessControlPolicy{Grants: acl.toS3Grants()}
// As a safety precaution, if the caller didn't explicitly set a Grantee
// with the FULL_CONTROL permission we set it to the current bucket owner.
if acl.FullControl == "" {
curACL, err := c.S3Client.GetBucketAcl(ctx, &s3.GetBucketAclInput{Bucket: aws.String(bucket)})
if err != nil {
return fmt.Errorf("unable to retrieve current ACL: %w", err)
}
s3ACL.AccessControlPolicy.Grants = append(s3ACL.AccessControlPolicy.Grants, s3types.Grant{
Grantee: &s3types.Grantee{Type: s3types.TypeCanonicalUser, ID: curACL.Owner.ID},
Permission: s3types.PermissionFullControl,
})
}
}
if _, err := c.S3Client.PutBucketAcl(ctx, &s3ACL); err != nil {
return err
}
return nil
}
func (c *Client) SetObjectACL(ctx context.Context, bucket, key string, acl *ACL) error {
s3ACL := s3.PutObjectAclInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
if acl.Canned != "" {
if !utils.IsInList(ObjectCannedACLToStrings(), acl.Canned) {
return fmt.Errorf("invalid canned ACL %q, supported values are: %s",
acl.Canned,
strings.Join(ObjectCannedACLToStrings(), ", "))
}
s3ACL.ACL = s3types.ObjectCannedACL(acl.Canned)
} else {
s3ACL.AccessControlPolicy = &s3types.AccessControlPolicy{Grants: acl.toS3Grants()}
// As a safety precaution, if the caller didn't explicitly set a Grantee
// with the FULL_CONTROL permission we set it to the current object owner.
if acl.FullControl == "" {
curACL, err := c.S3Client.GetObjectAcl(ctx, &s3.GetObjectAclInput{
Bucket: s3ACL.Bucket,
Key: s3ACL.Key,
})
if err != nil {
return fmt.Errorf("unable to retrieve current ACL: %w", err)
}
s3ACL.AccessControlPolicy.Grants = append(s3ACL.AccessControlPolicy.Grants, s3types.Grant{
Grantee: &s3types.Grantee{Type: s3types.TypeCanonicalUser, ID: curACL.Owner.ID},
Permission: s3types.PermissionFullControl,
})
}
}
if _, err := c.S3Client.PutObjectAcl(ctx, &s3ACL); err != nil {
return err
}
return nil
}
func (c *Client) SetObjectsACL(ctx context.Context, bucket, prefix string, acl *ACL, recursive bool) error {
return c.ForEachObject(ctx, bucket, prefix, recursive, func(o *s3types.Object) error {
return c.SetObjectACL(ctx, bucket, aws.ToString(o.Key), acl)
})
}
func BucketCannedACLToStrings() []string {
s3BucketCannedACLs := s3types.BucketCannedACLPrivate.Values()
list := make([]string, len(s3BucketCannedACLs))
for i, v := range s3BucketCannedACLs {
list[i] = string(v)
}
return list
}
func ObjectCannedACLToStrings() []string {
s3ObjectCannedACLs := s3types.ObjectCannedACLPrivate.Values()
list := make([]string, len(s3ObjectCannedACLs))
for i, v := range s3ObjectCannedACLs {
list[i] = string(v)
}
return list
}
// toS3Grants converts the local ACL representation to the s3types.Grant format.
func (a ACL) toS3Grants() []s3types.Grant {
grants := make([]s3types.Grant, 0)
if a.Read != "" {
grants = append(grants, s3types.Grant{
Grantee: storageACLGranteeToS3(a.Read),
Permission: s3types.PermissionRead,
})
}
if a.Write != "" {
grants = append(grants, s3types.Grant{
Grantee: storageACLGranteeToS3(a.Write),
Permission: s3types.PermissionWrite,
})
}
if a.ReadACP != "" {
grants = append(grants, s3types.Grant{
Grantee: storageACLGranteeToS3(a.ReadACP),
Permission: s3types.PermissionReadAcp,
})
}
if a.WriteACP != "" {
grants = append(grants, s3types.Grant{
Grantee: storageACLGranteeToS3(a.WriteACP),
Permission: s3types.PermissionWriteAcp,
})
}
if a.FullControl != "" {
grants = append(grants, s3types.Grant{
Grantee: storageACLGranteeToS3(a.FullControl),
Permission: s3types.PermissionFullControl,
})
}
return grants
}
// ACLFromS3 converts an S3 ACL Grant to the local ACL representation.
func ACLFromS3(grants []s3types.Grant) ACL {
acl := ACL{
Read: "-",
Write: "-",
ReadACP: "-",
WriteACP: "-",
FullControl: "-",
}
for _, grant := range grants {
switch grant.Permission {
case s3types.PermissionRead:
acl.Read = storageACLGranteeFromS3(grant.Grantee)
case s3types.PermissionWrite:
acl.Write = storageACLGranteeFromS3(grant.Grantee)
case s3types.PermissionReadAcp:
acl.ReadACP = storageACLGranteeFromS3(grant.Grantee)
case s3types.PermissionWriteAcp:
acl.WriteACP = storageACLGranteeFromS3(grant.Grantee)
case s3types.PermissionFullControl:
acl.FullControl = storageACLGranteeFromS3(grant.Grantee)
}
}
return acl
}
// storageACLGranteeFromS3 returns a human-friendly representation of an S3 ACL
// Grantee.
func storageACLGranteeFromS3(v *s3types.Grantee) string {
if v.Type == s3types.TypeCanonicalUser {
return aws.ToString(v.DisplayName)
}
switch aws.ToString(v.URI) {
case ACLGranteeAllUsers:
return "ALL_USERS"
case ACLGranteeAuthenticatedUsers:
return "AUTHENTICATED_USERS"
}
return "-"
}
// storageACLGranteeToS3 converts a human-friendly ACL grantee representation
// to the S3 format.
func storageACLGranteeToS3(v string) *s3types.Grantee {
switch v {
case "ALL_USERS":
return &s3types.Grantee{
Type: s3types.TypeGroup,
URI: aws.String(ACLGranteeAllUsers),
}
case "AUTHENTICATED_USERS":
return &s3types.Grantee{
Type: s3types.TypeGroup,
URI: aws.String(ACLGranteeAuthenticatedUsers),
}
default:
return &s3types.Grantee{
Type: s3types.TypeCanonicalUser,
ID: aws.String(v),
}
}
}
// storageACLToCopyObject updates the object to be copied with S3 ACL information.
func storageACLToCopyObject(acl *s3.GetObjectAclOutput, o *s3.CopyObjectInput) {
s3GranteeToString := func(g *s3types.Grantee) *string {
if g.Type == s3types.TypeCanonicalUser {
return aws.String("id=" + aws.ToString(g.ID))
}
switch aws.ToString(g.URI) {
case ACLGranteeAllUsers:
return aws.String("uri=" + ACLGranteeAllUsers)
case ACLGranteeAuthenticatedUsers:
return aws.String("uri=" + ACLGranteeAuthenticatedUsers)
}
return nil
}
o.GrantFullControl = aws.String("id=" + aws.ToString(acl.Owner.ID))
for _, grant := range acl.Grants {
switch grant.Permission {
case s3types.PermissionRead:
o.GrantRead = s3GranteeToString(grant.Grantee)
// Write permission is not supported on S3 objects:
// https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#permissions
case s3types.PermissionReadAcp:
o.GrantReadACP = s3GranteeToString(grant.Grantee)
case s3types.PermissionWriteAcp:
o.GrantWriteACP = s3GranteeToString(grant.Grantee)
case s3types.PermissionFullControl:
o.GrantFullControl = s3GranteeToString(grant.Grantee)
}
}
}