-
Notifications
You must be signed in to change notification settings - Fork 18
/
sos_acl.go
546 lines (453 loc) · 12.3 KB
/
sos_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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package cmd
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/exoscale/cli/table"
minio "github.com/minio/minio-go/v6"
"github.com/spf13/cobra"
)
// nolint
const (
//Canned ACLs
private string = "private"
publicRead string = "public-read"
publicReadWrite string = "public-read-write"
authenticatedRead string = "authenticated-read"
bucketOwnerRead string = "bucket-owner-read"
bucketOwnerFullControl string = "bucket-owner-full-control"
//S3 Grant ACLs response header
manualRead string = "X-Amz-Grant-Read"
manualWrite string = "X-Amz-Grant-Write"
manualReadACP string = "X-Amz-Grant-Read-Acp"
manualWriteACP string = "X-Amz-Grant-Write-Acp"
manualFullControl string = "X-Amz-Grant-Full-Control"
//S3 Grant ACLs response body
sosACLRead string = "READ"
sosACLWrite string = "WRITE"
sosACLReadACP string = "READ_ACP"
sosACLWriteACP string = "WRITE_ACP"
sosACLFullControl string = "FULL_CONTROL"
)
// aclCmd represents the acl command
var sosACLCmd = &cobra.Command{
Use: "acl",
Short: "Object(s) ACLs management",
}
func init() {
sosCmd.AddCommand(sosACLCmd)
}
// aclCmd represents the acl command
var sosAddACLCmd = &cobra.Command{
Use: "add <bucket name> <object|prefix>",
Short: "Add ACL(s) to objects",
Long: `This commands adds ACL(s) to objects in a bucket. It is possible to
set ACLs either on a single object, or recursively from a prefix
using the flag "--recursive". To recurse across the whole bucket,
specify "/" as prefix.`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
bucket := args[0]
path := args[1]
// The "/" path is just a trick for CLI users to signify they mean the bucket root:
// for SOS the actual bucket root is an empty prefix (i.e. ""), however passing
// an empty string as CLI option value has a different meaning.
if path == "/" {
path = ""
}
meta, err := getACL(cmd)
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
if meta == nil {
println("error: You have to choose one flag")
if err = cmd.Usage(); err != nil {
return err
}
return fmt.Errorf("error: You have to choose one flag")
}
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
location, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(location); err != nil {
return err
}
for obj := range sosClient.ListObjects(bucket, path, recursive, gContext.Done()) {
// Skip "folders", as they don't support ACLs.
if strings.HasSuffix(obj.Key, "/") {
continue
}
objInfo, err := sosClient.GetObjectACLWithContext(gContext, bucket, obj.Key)
if err != nil {
return err
}
objInfo.Metadata.Add("content-type", objInfo.ContentType)
src := minio.NewSourceInfo(bucket, objInfo.Key, nil)
// When the Object acl is updated from Canned ACL(X-Amz-Acl) to
// Grant ACL(X-Amz-Grant), we have to remove Canned ACL before.
_, hasNewCannedACL := meta["X-Amz-Acl"]
_, hasCannedACL := objInfo.Metadata["X-Amz-Acl"]
if hasCannedACL && !hasNewCannedACL {
// Remove Canned ACL from the header to let Grant ACL take effect.
objInfo.Metadata.Del("X-Amz-Acl")
// This lets the objInfo full control grantee to keep the control on the objInfo,
// if the flag "--full-control" is not specified.
var fullControl string
for _, g := range objInfo.Grant {
if g.Permission == sosACLFullControl {
fullControl = g.Grantee.ID
}
}
if fullControl == "" {
return fmt.Errorf(`objInfo %q has no "FULL_CONTROL" grantee`, objInfo)
}
objInfo.Metadata.Add(manualFullControl, "id="+fullControl)
}
mergeHeader(src.Headers, objInfo.Metadata)
// Destination objInfo
dst, err := minio.NewDestinationInfo(bucket, objInfo.Key, nil, meta)
if err != nil {
return err
}
// Copy objInfo call
err = sosClient.CopyObject(dst, src)
if err != nil {
return err
}
if !gQuiet && !recursive {
acl, err := getDefaultCannedACL(cmd)
if err != nil {
return err
}
if acl == publicReadWrite || acl == publicRead {
fmt.Printf("https://sos-%s.exo.io/%s/%s\n", location, bucket, objInfo.Key)
}
}
}
return nil
},
}
//merge src header in dst header
func mergeHeader(dst, src http.Header) {
for k, v := range src {
dst[k] = v
}
}
func getACL(cmd *cobra.Command) (map[string]string, error) {
meta := map[string]string{}
defACL, err := getDefaultCannedACL(cmd)
if err != nil {
return nil, err
}
if defACL != "" {
meta["X-Amz-Acl"] = defACL
return meta, nil
}
manualACLs, err := getManualACL(cmd)
if err != nil {
return nil, err
}
if manualACLs == nil {
return nil, nil
}
for k, v := range manualACLs {
for i := range v {
v[i] = fmt.Sprintf("id=%s", v[i])
}
meta[k] = strings.Join(v, ", ")
}
return meta, nil
}
func getDefaultCannedACL(cmd *cobra.Command) (string, error) {
acl, err := cmd.Flags().GetBool(private)
if err != nil {
return "", err
}
if acl {
return private, nil
}
acl, err = cmd.Flags().GetBool(publicRead)
if err != nil {
return "", err
}
if acl {
return publicRead, nil
}
acl, err = cmd.Flags().GetBool(publicReadWrite)
if err != nil {
return "", err
}
if acl {
return publicReadWrite, nil
}
acl, err = cmd.Flags().GetBool(authenticatedRead)
if err != nil {
return "", err
}
if acl {
return authenticatedRead, nil
}
acl, err = cmd.Flags().GetBool(bucketOwnerRead)
if err != nil {
return "", err
}
if acl {
return bucketOwnerRead, nil
}
acl, err = cmd.Flags().GetBool(bucketOwnerFullControl)
if err != nil {
return "", err
}
if acl {
return bucketOwnerFullControl, nil
}
return "", nil
}
func getManualACL(cmd *cobra.Command) (map[string][]string, error) {
res := map[string][]string{}
acl, err := cmd.Flags().GetString("read")
if err != nil {
return nil, err
}
if acl != "" {
res[manualRead] = getCommaflag(acl)
}
acl, err = cmd.Flags().GetString("write")
if err != nil {
return nil, err
}
if acl != "" {
res[manualWrite] = getCommaflag(acl)
}
acl, err = cmd.Flags().GetString("read-acp")
if err != nil {
return nil, err
}
if acl != "" {
res[manualReadACP] = getCommaflag(acl)
}
acl, err = cmd.Flags().GetString("write-acp")
if err != nil {
return nil, err
}
if acl != "" {
res[manualWriteACP] = getCommaflag(acl)
}
acl, err = cmd.Flags().GetString("full-control")
if err != nil {
return nil, err
}
if acl != "" {
res[manualFullControl] = getCommaflag(acl)
}
if len(res) == 0 {
return nil, nil
}
return res, err
}
func init() {
sosACLCmd.AddCommand(sosAddACLCmd)
sosAddACLCmd.Flags().SortFlags = false
sosAddACLCmd.Flags().Bool("recursive", false, "Set ACL recursively")
//Canned ACLs
sosAddACLCmd.Flags().BoolP(private, "p", false, "Canned ACL private")
sosAddACLCmd.Flags().BoolP(publicRead, "r", false, "Canned ACL public read")
sosAddACLCmd.Flags().BoolP(publicReadWrite, "w", false, "Canned ACL public read and write")
sosAddACLCmd.Flags().BoolP(authenticatedRead, "", false, "Canned ACL authenticated read")
sosAddACLCmd.Flags().BoolP(bucketOwnerRead, "", false, "Canned ACL bucket owner read")
sosAddACLCmd.Flags().BoolP(bucketOwnerFullControl, "f", false, "Canned ACL bucket owner full control")
//Manual ACLs
sosAddACLCmd.Flags().StringP("read", "", "", "Manual acl edit grant read e.g(value, value, ...)")
sosAddACLCmd.Flags().StringP("write", "", "", "Manual acl edit grant write e.g(value, value, ...)")
sosAddACLCmd.Flags().StringP("read-acp", "", "", "Manual acl edit grant acp read e.g(value, value, ...)")
sosAddACLCmd.Flags().StringP("write-acp", "", "", "Manual acl edit grant acp write e.g(value, value, ...)")
sosAddACLCmd.Flags().StringP("full-control", "", "", "Manual acl edit grant full control e.g(value, value, ...)")
}
// aclCmd represents the acl command
var sosRemoveACLCmd = &cobra.Command{
Use: "remove <bucket name> <object name>",
Short: "Remove ACL(s) from an object",
Aliases: gRemoveAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
bucket := args[0]
object := args[1]
meta, err := getManualACLBool(cmd)
if err != nil {
return err
}
if meta == nil {
println("error: You have to choose one flag")
if err = cmd.Usage(); err != nil {
return err
}
return fmt.Errorf("error: You have to choose one flag")
}
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
location, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(location); err != nil {
return err
}
objInfo, err := sosClient.GetObjectACLWithContext(gContext, bucket, object)
if err != nil {
return err
}
objInfo.Metadata.Add("content-type", objInfo.ContentType)
objInfo.Metadata.Add("x-amz-metadata-directive", "REPLACE")
src := minio.NewSourceInfo(bucket, object, nil)
_, okHeader := objInfo.Metadata["X-Amz-Acl"]
if okHeader {
return fmt.Errorf("error: No Manual ACL are set")
}
for _, k := range meta {
objInfo.Metadata.Del(k)
}
mergeHeader(src.Headers, objInfo.Metadata)
// Destination object
dst, err := minio.NewDestinationInfo(bucket, object, nil, nil)
if err != nil {
return err
}
// Copy object call
return sosClient.CopyObject(dst, src)
},
}
func init() {
sosACLCmd.AddCommand(sosRemoveACLCmd)
sosRemoveACLCmd.Flags().SortFlags = false
sosRemoveACLCmd.Flags().BoolP("read", "r", false, "Remove grant read ACL")
sosRemoveACLCmd.Flags().BoolP("write", "w", false, "Remove grant write ACL")
sosRemoveACLCmd.Flags().BoolP("read-acp", "", false, "Remove grant acp read ACL")
sosRemoveACLCmd.Flags().BoolP("write-acp", "", false, "Remove grant acp write ACL")
sosRemoveACLCmd.Flags().BoolP("full-control", "f", false, "Remove grant full control ACL")
}
func getManualACLBool(cmd *cobra.Command) ([]string, error) {
var res []string
acl, err := cmd.Flags().GetBool("read")
if err != nil {
return nil, err
}
if acl {
res = append(res, manualRead)
}
acl, err = cmd.Flags().GetBool("write")
if err != nil {
return nil, err
}
if acl {
res = append(res, manualWrite)
}
acl, err = cmd.Flags().GetBool("read-acp")
if err != nil {
return nil, err
}
if acl {
res = append(res, manualReadACP)
}
acl, err = cmd.Flags().GetBool("write-acp")
if err != nil {
return nil, err
}
if acl {
res = append(res, manualWriteACP)
}
acl, err = cmd.Flags().GetBool("full-control")
if err != nil {
return nil, err
}
if acl {
res = append(res, manualFullControl)
}
return res, nil
}
// aclCmd represents the acl command
var sosShowACLCmd = &cobra.Command{
Use: "list <bucket name> <object name>",
Short: "list Object ACLs",
Aliases: gListAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
bucket := args[0]
object := args[1]
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
location, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(location); err != nil {
return err
}
objInfo, err := sosClient.GetObjectACLWithContext(gContext, bucket, object)
if err != nil {
return err
}
cannedACL, okHeader := objInfo.Metadata["X-Amz-Acl"]
table := table.NewTable(os.Stdout)
table.SetHeader([]string{"File Name", "ACL", "Value"})
if okHeader && len(cannedACL) > 0 {
table.Append([]string{objInfo.Key, "Canned", cannedACL[0]})
} else {
for _, g := range objInfo.Grant {
table.Append([]string{objInfo.Key, formatGrantKey(g.Permission), g.Grantee.DisplayName})
}
}
table.Render()
return nil
},
}
func formatGrantKey(k string) string {
var res string
switch k {
case sosACLRead:
res = "Read"
case sosACLWrite:
res = "Write"
case sosACLReadACP:
res = "Read ACP"
case sosACLWriteACP:
res = "Write ACP"
case sosACLFullControl:
res = "Full Control"
}
return res
}
func init() {
sosACLCmd.AddCommand(sosShowACLCmd)
}