-
Notifications
You must be signed in to change notification settings - Fork 20
/
sos_acl.go
483 lines (398 loc) · 10.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
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"
//Manual edit ACLs
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"
)
// aclCmd represents the acl command
var sosACLCmd = &cobra.Command{
Use: "acl <bucket name> <object name> [object name] ...",
Short: "Object(s) ACLs management",
}
func init() {
sosCmd.AddCommand(sosACLCmd)
}
// aclCmd represents the acl command
var sosAddACLCmd = &cobra.Command{
Use: "add <bucket name> <object name> [object name] ...",
Short: "Add ACL(s) to object(s)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
meta, err := getACL(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")
}
minioClient, err := newMinioClient(sosZone)
if err != nil {
return err
}
location, err := minioClient.GetBucketLocation(args[0])
if err != nil {
return err
}
minioClient, err = newMinioClient(location)
if err != nil {
return err
}
objInfo, err := minioClient.GetObjectACL(args[0], args[1])
if err != nil {
return err
}
objInfo.Metadata.Add("content-type", objInfo.ContentType)
src := minio.NewSourceInfo(args[0], args[1], nil)
_, okMeta := meta["X-Amz-Acl"]
_, okHeader := objInfo.Metadata["X-Amz-Acl"]
if okHeader && !okMeta {
objInfo.Metadata.Del("X-Amz-Acl")
objInfo.Metadata.Add(manualFullControl, "id="+gCurrentAccount.AccountName())
}
mergeHeader(src.Headers, objInfo.Metadata)
// Destination object
dst, err := minio.NewDestinationInfo(args[0], args[1], nil, meta)
if err != nil {
return err
}
// Copy object call
return minioClient.CopyObject(dst, src)
},
}
//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
//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> [object name] ...",
Short: "Remove ACL(s) from object(s)",
Aliases: gRemoveAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return cmd.Usage()
}
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")
}
minioClient, err := newMinioClient(sosZone)
if err != nil {
return err
}
location, err := minioClient.GetBucketLocation(args[0])
if err != nil {
return err
}
minioClient, err = newMinioClient(location)
if err != nil {
return err
}
objInfo, err := minioClient.GetObjectACL(args[0], args[1])
if err != nil {
return err
}
objInfo.Metadata.Add("content-type", objInfo.ContentType)
objInfo.Metadata.Add("x-amz-metadata-directive", "REPLACE")
src := minio.NewSourceInfo(args[0], args[1], 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(args[0], args[1], nil, nil)
if err != nil {
return err
}
// Copy object call
return minioClient.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()
}
minioClient, err := newMinioClient(sosZone)
if err != nil {
return err
}
location, err := minioClient.GetBucketLocation(args[0])
if err != nil {
return err
}
minioClient, err = newMinioClient(location)
if err != nil {
return err
}
objInfo, err := minioClient.GetObjectACL(args[0], args[1])
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]})
}
for k, v := range objInfo.Metadata {
if len(v) > 0 {
if isGrantACL(k) {
s := getGrantValue(v)
table.Append([]string{objInfo.Key, formatGrantKey(k), s})
}
}
}
table.Render()
return nil
},
}
func getGrantValue(values []string) string {
for i, v := range values {
values[i] = v[len("id="):]
}
return strings.Join(values, ", ")
}
func formatGrantKey(k string) string {
var res string
switch {
case k == manualRead:
res = "Read"
case k == manualWrite:
res = "Write"
case k == manualReadACP:
res = "Read ACP"
case k == manualWriteACP:
res = "Write ACP"
case k == manualFullControl:
res = "Full Control"
}
return res
}
func isGrantACL(key string) bool {
key = strings.ToLower(key)
return strings.HasPrefix(key, "x-amz-grant-")
}
func init() {
sosACLCmd.AddCommand(sosShowACLCmd)
}