-
Notifications
You must be signed in to change notification settings - Fork 24
/
config.go
210 lines (178 loc) · 5.46 KB
/
config.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
package config
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
)
// The S3Cli represents configuration for the s3cli
type S3Cli struct {
AccessKeyID string `json:"access_key_id"`
SecretAccessKey string `json:"secret_access_key"`
BucketName string `json:"bucket_name"`
FolderName string `json:"folder_name"`
CredentialsSource string `json:"credentials_source"`
Host string `json:"host"`
Port int `json:"port"` // 0 means no custom port
Region string `json:"region"`
SSLVerifyPeer bool `json:"ssl_verify_peer"`
UseSSL bool `json:"use_ssl"`
SignatureVersion int `json:"signature_version,string"`
ServerSideEncryption string `json:"server_side_encryption"`
SSEKMSKeyID string `json:"sse_kms_key_id"`
AssumeRoleArn string `json:"assume_role_arn"`
MultipartUpload bool `json:"multipart_upload"`
UseV2SigningMethod bool
HostStyle bool `json:"host_style"`
SwiftAuthAccount string `json:"swift_auth_account"`
SwiftTempURLKey string `json:"swift_temp_url_key"`
}
// EmptyRegion is required to allow us to use the AWS SDK against S3 compatible blobstores which do not have
// the concept of a region
const EmptyRegion = " "
const (
defaultRegion = "us-east-1" //nolint:unused
)
// StaticCredentialsSource specifies that credentials will be supplied using access_key_id and secret_access_key
const StaticCredentialsSource = "static"
// NoneCredentialsSource specifies that credentials will be empty. The blobstore client operates in read only mode.
const NoneCredentialsSource = "none"
const credentialsSourceEnvOrProfile = "env_or_profile"
// Nothing was provided in configuration
const noCredentialsSourceProvided = ""
var errorStaticCredentialsMissing = errors.New("access_key_id and secret_access_key must be provided")
type errorStaticCredentialsPresent struct {
credentialsSource string
}
func (e errorStaticCredentialsPresent) Error() string {
return fmt.Sprintf("can't use access_key_id and secret_access_key with %s credentials_source", e.credentialsSource)
}
func newStaticCredentialsPresentError(desiredSource string) error {
return errorStaticCredentialsPresent{credentialsSource: desiredSource}
}
// NewFromReader returns a new s3cli configuration struct from the contents of reader.
// reader.Read() is expected to return valid JSON
func NewFromReader(reader io.Reader) (S3Cli, error) {
bytes, err := io.ReadAll(reader)
if err != nil {
return S3Cli{}, err
}
c := S3Cli{
SSLVerifyPeer: true,
UseSSL: true,
MultipartUpload: true,
}
err = json.Unmarshal(bytes, &c)
if err != nil {
return S3Cli{}, err
}
if c.BucketName == "" {
return S3Cli{}, errors.New("bucket_name must be set")
}
switch c.CredentialsSource {
case StaticCredentialsSource:
if c.AccessKeyID == "" || c.SecretAccessKey == "" {
return S3Cli{}, errorStaticCredentialsMissing
}
case credentialsSourceEnvOrProfile:
if c.AccessKeyID != "" || c.SecretAccessKey != "" {
return S3Cli{}, newStaticCredentialsPresentError(credentialsSourceEnvOrProfile)
}
case NoneCredentialsSource:
if c.AccessKeyID != "" || c.SecretAccessKey != "" {
return S3Cli{}, newStaticCredentialsPresentError(NoneCredentialsSource)
}
case noCredentialsSourceProvided:
if c.SecretAccessKey != "" && c.AccessKeyID != "" {
c.CredentialsSource = StaticCredentialsSource
} else if c.SecretAccessKey == "" && c.AccessKeyID == "" {
c.CredentialsSource = NoneCredentialsSource
} else {
return S3Cli{}, errorStaticCredentialsMissing
}
default:
return S3Cli{}, fmt.Errorf("invalid credentials_source: %s", c.CredentialsSource)
}
switch Provider(c.Host) {
case "aws":
c.configureAWS()
case "alicloud":
c.configureAlicloud()
case "google":
c.configureGoogle()
default:
c.configureDefault()
}
return c, nil
}
// Provider returns one of (aws, alicloud, google) based on a host name.
// Returns "" if a known provider cannot be detected.
func Provider(host string) string {
for provider, regex := range providerRegex {
if regex.MatchString(host) {
return provider
}
}
return ""
}
func (c *S3Cli) configureAWS() {
c.MultipartUpload = true
if c.Region == "" {
c.Region = AWSHostToRegion(c.Host)
}
switch c.SignatureVersion {
case 2:
c.UseV2SigningMethod = true
case 4:
c.UseV2SigningMethod = false
default:
c.UseV2SigningMethod = false
}
}
func (c *S3Cli) configureAlicloud() {
c.MultipartUpload = true
c.configureDefaultSigningMethod()
c.HostStyle = true
c.Host = strings.Split(c.Host, ":")[0]
if c.Region == "" {
c.Region = AlicloudHostToRegion(c.Host)
}
}
func (c *S3Cli) configureGoogle() {
c.MultipartUpload = false
c.configureDefaultSigningMethod()
}
func (c *S3Cli) configureDefault() {
c.configureDefaultSigningMethod()
}
func (c *S3Cli) configureDefaultSigningMethod() {
switch c.SignatureVersion {
case 2:
c.UseV2SigningMethod = true
case 4:
c.UseV2SigningMethod = false
default:
c.UseV2SigningMethod = true
}
}
// UseRegion signals to users of the S3Cli whether to use Region information
func (c *S3Cli) UseRegion() bool {
return c.Region != ""
}
// S3Endpoint returns the S3 URI to use if custom host information has been provided
func (c *S3Cli) S3Endpoint() string {
if c.Host == "" {
return ""
}
if c.Port == 80 && !c.UseSSL {
return c.Host
}
if c.Port == 443 && c.UseSSL {
return c.Host
}
if c.Port != 0 {
return fmt.Sprintf("%s:%d", c.Host, c.Port)
}
return c.Host
}