-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_setacl.go
296 lines (235 loc) · 8.04 KB
/
storage_setacl.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
package cmd
import (
"fmt"
"strings"
"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"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var storageSetACLCmd = &cobra.Command{
Use: "setacl sos://BUCKET/[OBJECT|PREFIX/] [CANNED-ACL]",
Short: "Set a bucket/objects ACL",
Long: fmt.Sprintf(`This command sets bucket/objects ACL.
It can be used in 2 (mutually exclusive) forms:
* With a "canned" ACL:
exo storage setacl sos://my-bucket public-read
* With explicit Access Control Policy (ACP) grantees:
exo storage setacl sos://my-bucket \
--full-control alice@example.net \
--read bob@example.net
Supported canned ACLs:
* For buckets: %s
* For objects: %s
In ACP mode, it is possible to use the following special values to reference
pre-defined groups:
* ALL_USERS (as in "public-read" canned ACL)
* AUTHENTICATED_USERS (as in "authenticated-read" canned ACL)
For more information on ACL, please refer to the Exoscale Storage
documentation:
https://community.exoscale.com/documentation/storage/acl/
If you want to target objects under a "directory" prefix, suffix the path
argument with "/":
exo storage setacl sos://my-bucket/ --full-control alice@example.net
exo storage setacl -r sos://my-bucket/public/ public-read
Supported output template annotations:
* When showing a bucket: %s
* When showing an object: %s`,
strings.Join(s3BucketCannedACLToStrings(), ", "),
strings.Join(s3ObjectCannedACLToStrings(), ", "),
strings.Join(outputterTemplateAnnotations(&storageShowBucketOutput{}), ", "),
strings.Join(outputterTemplateAnnotations(&storageShowObjectOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 || len(args) > 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
if (len(args) == 2 && storageACLFromCmdFlags(cmd.Flags()) != nil) ||
(len(args) == 1 && storageACLFromCmdFlags(cmd.Flags()) == nil) {
cmdExitOnUsageError(cmd, "either a canned ACL or ACL grantee options must be specified")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
prefix string
acl *storageACL
)
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
parts := strings.SplitN(args[0], "/", 2)
bucket = parts[0]
if len(parts) > 1 {
prefix = parts[1]
// Special case: the caller wants to target objects at the root of
// the bucket, in this case the prefix is empty so we set it to a
// symbolic value that shall be removed later on.
if prefix == "" {
prefix = "/"
}
}
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptZoneFromBucket(bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %v", err)
}
if acl = storageACLFromCmdFlags(cmd.Flags()); acl == nil {
acl = &storageACL{Canned: args[1]}
}
if prefix == "" {
if err := storage.setBucketACL(bucket, acl); err != nil {
return fmt.Errorf("unable to set ACL: %s", err)
}
if !gQuiet {
return output(storage.showBucket(bucket))
}
return nil
}
if err := storage.setObjectsACL(bucket, prefix, acl, recursive); err != nil {
return fmt.Errorf("unable to set ACL: %s", err)
}
if !gQuiet && !recursive && !strings.HasSuffix(prefix, "/") {
return output(storage.showObject(bucket, prefix))
}
if !gQuiet {
fmt.Println("ACL set successfully")
}
return nil
},
}
func init() {
storageSetACLCmd.Flags().BoolP("recursive", "r", false,
"set ACL recursively (with object prefix only)")
storageSetACLCmd.Flags().String(storageSetACLCmdFlagRead, "", "ACL Read grantee")
storageSetACLCmd.Flags().String(storageSetACLCmdFlagWrite, "", "ACP Write grantee")
storageSetACLCmd.Flags().String(storageSetACLCmdFlagReadACP, "", "ACP Read ACP grantee")
storageSetACLCmd.Flags().String(storageSetACLCmdFlagWriteACP, "", "ACP Write ACP grantee")
storageSetACLCmd.Flags().String(storageSetACLCmdFlagFullControl, "", "ACP Full Control grantee")
storageCmd.AddCommand(storageSetACLCmd)
}
func (c *storageClient) setBucketACL(bucket string, acl *storageACL) error {
s3ACL := s3.PutBucketAclInput{Bucket: aws.String(bucket)}
if acl.Canned != "" {
if !isInList(s3BucketCannedACLToStrings(), acl.Canned) {
return fmt.Errorf("invalid canned ACL %q, supported values are: %s",
acl.Canned,
strings.Join(s3BucketCannedACLToStrings(), ", "))
}
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.GetBucketAcl(gContext, &s3.GetBucketAclInput{Bucket: aws.String(bucket)})
if err != nil {
return fmt.Errorf("unable to retrieve current ACL: %s", 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.PutBucketAcl(gContext, &s3ACL); err != nil {
return err
}
return nil
}
func (c *storageClient) setObjectACL(bucket, key string, acl *storageACL) error {
s3ACL := s3.PutObjectAclInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
if acl.Canned != "" {
if !isInList(s3ObjectCannedACLToStrings(), acl.Canned) {
return fmt.Errorf("invalid canned ACL %q, supported values are: %s",
acl.Canned,
strings.Join(s3ObjectCannedACLToStrings(), ", "))
}
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.GetObjectAcl(gContext, &s3.GetObjectAclInput{
Bucket: s3ACL.Bucket,
Key: s3ACL.Key,
})
if err != nil {
return fmt.Errorf("unable to retrieve current ACL: %s", 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.PutObjectAcl(gContext, &s3ACL); err != nil {
return err
}
return nil
}
func (c *storageClient) setObjectsACL(bucket, prefix string, acl *storageACL, recursive bool) error {
return c.forEachObject(bucket, prefix, recursive, func(o *s3types.Object) error {
return c.setObjectACL(bucket, aws.ToString(o.Key), acl)
})
}
// storageACLFromCmdFlags returns a non-nil pointer to a storageACL struct if at least
// one of the ACL-related command flags is set.
func storageACLFromCmdFlags(flags *pflag.FlagSet) *storageACL {
var acl *storageACL
flags.VisitAll(func(flag *pflag.Flag) {
switch flag.Name {
case storageSetACLCmdFlagRead:
if v := flag.Value.String(); v != "" {
if acl == nil {
acl = &storageACL{}
}
acl.Read = v
}
case storageSetACLCmdFlagWrite:
if v := flag.Value.String(); v != "" {
if acl == nil {
acl = &storageACL{}
}
acl.Write = v
}
case storageSetACLCmdFlagReadACP:
if v := flag.Value.String(); v != "" {
if acl == nil {
acl = &storageACL{}
}
acl.ReadACP = v
}
case storageSetACLCmdFlagWriteACP:
if v := flag.Value.String(); v != "" {
if acl == nil {
acl = &storageACL{}
}
acl.WriteACP = v
}
case storageSetACLCmdFlagFullControl:
if v := flag.Value.String(); v != "" {
if acl == nil {
acl = &storageACL{}
}
acl.FullControl = v
}
default:
return
}
})
return acl
}