-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_cors_add.go
152 lines (123 loc) · 3.86 KB
/
storage_cors_add.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
package cmd
import (
"errors"
"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/aws/smithy-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
storageCORSAddCmdFlagAllowedOrigin = "allowed-origin"
storageCORSAddCmdFlagAllowedMethod = "allowed-method"
storageCORSAddCmdFlagAllowedHeader = "allowed-header"
)
// storageCORSRuleFromCmdFlags returns a non-nil pointer to a storageCORSRule struct if at least
// one of the CORS-related command flags is set.
func storageCORSRuleFromCmdFlags(flags *pflag.FlagSet) *storageCORSRule {
var cors *storageCORSRule
flags.VisitAll(func(flag *pflag.Flag) {
switch flag.Name {
case storageCORSAddCmdFlagAllowedOrigin:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedOrigin); len(v) > 0 {
if cors == nil {
cors = &storageCORSRule{}
}
cors.AllowedOrigins = v
}
case storageCORSAddCmdFlagAllowedMethod:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedMethod); len(v) > 0 {
if cors == nil {
cors = &storageCORSRule{}
}
cors.AllowedMethods = v
}
case storageCORSAddCmdFlagAllowedHeader:
if v, _ := flags.GetStringSlice(storageCORSAddCmdFlagAllowedHeader); len(v) > 0 {
if cors == nil {
cors = &storageCORSRule{}
}
cors.AllowedHeaders = v
}
default:
return
}
})
return cors
}
var storageCORSAddCmd = &cobra.Command{
Use: "add sos://BUCKET",
Short: "Add a CORS configuration rule to a bucket",
Long: `This command adds a new rule to the current bucket CORS
configuration.
Example:
exo storage cors add sos://my-bucket \
--allowed-origin "https://my-website.net" \
--allowed-method "*"
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
return cmdCheckRequiredFlags(cmd, []string{
storageCORSAddCmdFlagAllowedOrigin,
storageCORSAddCmdFlagAllowedMethod,
})
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptZoneFromBucket(bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %v", err)
}
cors := storageCORSRuleFromCmdFlags(cmd.Flags())
if err := storage.addBucketCORSRule(bucket, cors); err != nil {
return fmt.Errorf("unable to add rule to the bucket CORS configuration: %s", err)
}
if !gQuiet {
return output(storage.showBucket(bucket))
}
return nil
},
}
func init() {
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedOrigin, nil,
"allowed origin (can be repeated multiple times)")
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedMethod, nil,
"allowed method (can be repeated multiple times)")
storageCORSAddCmd.Flags().StringSlice(storageCORSAddCmdFlagAllowedHeader, nil,
"allowed header (can be repeated multiple times)")
storageCORSCmd.AddCommand(storageCORSAddCmd)
}
func (c *storageClient) addBucketCORSRule(bucket string, cors *storageCORSRule) error {
curCORS, err := c.GetBucketCors(gContext, &s3.GetBucketCorsInput{Bucket: aws.String(bucket)})
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
if apiErr.ErrorCode() == "NoSuchCORSConfiguration" {
curCORS = &s3.GetBucketCorsOutput{}
}
}
if cors == nil {
return fmt.Errorf("unable to retrieve bucket CORS configuration: %s", err)
}
}
_, err = c.PutBucketCors(gContext, &s3.PutBucketCorsInput{
Bucket: &bucket,
CORSConfiguration: &s3types.CORSConfiguration{
CORSRules: append(curCORS.CORSRules, cors.toS3()),
},
})
return err
}