/
session.go
242 lines (215 loc) · 7.24 KB
/
session.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
package s3
import (
"io"
"net"
"net/http"
"os"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/pkg/errors"
"github.com/wal-g/tracelog"
)
const DefaultPort = "443"
const HTTP = "http"
// TODO : unit tests
// Given an S3 bucket name, attempt to determine its region
func findBucketRegion(bucket string, config *aws.Config) (string, error) {
input := s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}
sess, err := session.NewSession(config.WithRegion("us-east-1"))
if err != nil {
return "", err
}
output, err := s3.New(sess).GetBucketLocation(&input)
if err != nil {
return "", err
}
if output.LocationConstraint == nil {
// buckets in "US Standard", a.k.a. us-east-1, are returned as a nil region
return "us-east-1", nil
}
// all other regions are strings
return *output.LocationConstraint, nil
}
// TODO : unit tests
func getAWSRegion(s3Bucket string, config *aws.Config, settings map[string]string) (string, error) {
if region, ok := settings[RegionSetting]; ok {
return region, nil
}
if config.Endpoint == nil ||
*config.Endpoint == "" ||
strings.HasSuffix(*config.Endpoint, ".amazonaws.com") {
region, err := findBucketRegion(s3Bucket, config)
return region, errors.Wrapf(err, "%s is not set and s3:GetBucketLocation failed", RegionSetting)
}
// For S3 compatible services like Minio, Ceph etc. use `us-east-1` as region
// ref: https://github.com/minio/cookbook/blob/master/docs/aws-sdk-for-go-with-minio.md
return "us-east-1", nil
}
func setupReqProxy(endpointSource, port string) *string {
resp, err := http.Get(endpointSource)
if err != nil {
tracelog.ErrorLogger.Printf("Endpoint source error: %v ", err)
return nil
}
if resp.StatusCode != 200 {
tracelog.ErrorLogger.Printf("Endpoint source bad status code: %v ", resp.StatusCode)
return nil
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err == nil {
return aws.String(net.JoinHostPort(string(bytes), port))
}
tracelog.ErrorLogger.Println("Endpoint source reading error:", err)
return nil
}
func getFirstSettingOf(settings map[string]string, keys []string) string {
for _, key := range keys {
if value, ok := settings[key]; ok {
return value
}
}
return ""
}
func configWithSettings(s *session.Session, bucket string, settings map[string]string) (*aws.Config, error) {
// DefaultRetryer implements basic retry logic using exponential backoff for
// most services. If you want to implement custom retry logic, you can implement the
// request.Retryer interface.
maxRetriesCount := MaxRetriesDefault
if maxRetriesRaw, ok := settings[MaxRetriesSetting]; ok {
maxRetriesInt, err := strconv.Atoi(maxRetriesRaw)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", MaxRetriesSetting)
}
maxRetriesCount = maxRetriesInt
}
config := s.Config
config = request.WithRetryer(config, NewConnResetRetryer(client.DefaultRetryer{NumMaxRetries: maxRetriesCount}))
accessKeyID := getFirstSettingOf(settings, []string{AccessKeyIDSetting, AccessKeySetting})
secretAccessKey := getFirstSettingOf(settings, []string{SecretAccessKeySetting, SecretKeySetting})
sessionToken := settings[SessionTokenSetting]
roleArn := settings[RoleARN]
sessionName := settings[SessionName]
if roleArn != "" {
stsSession := sts.New(s)
assumedRole, err := stsSession.AssumeRole(&sts.AssumeRoleInput{
RoleArn: aws.String(roleArn),
RoleSessionName: aws.String(sessionName),
})
if err != nil {
return nil, err
}
accessKeyID = *assumedRole.Credentials.AccessKeyId
secretAccessKey = *assumedRole.Credentials.SecretAccessKey
sessionToken = *assumedRole.Credentials.SessionToken
}
if accessKeyID != "" && secretAccessKey != "" {
provider := &credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
SessionToken: sessionToken,
}}
providers := make([]credentials.Provider, 0)
providers = append(providers, provider)
providers = append(providers, defaults.CredProviders(config, defaults.Handlers())...)
newCredentials := credentials.NewCredentials(&credentials.ChainProvider{
VerboseErrors: aws.BoolValue(config.CredentialsChainVerboseErrors),
Providers: providers,
})
config = config.WithCredentials(newCredentials)
}
if logLevel, ok := settings[LogLevel]; ok {
config = config.WithLogLevel(func(s string) aws.LogLevelType {
switch s {
case "DEVEL":
return aws.LogDebug
default:
return aws.LogOff
}
}(logLevel))
}
if endpoint, ok := settings[EndpointSetting]; ok {
config = config.WithEndpoint(endpoint)
}
if s3ForcePathStyleStr, ok := settings[ForcePathStyleSetting]; ok {
s3ForcePathStyle, err := strconv.ParseBool(s3ForcePathStyleStr)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", ForcePathStyleSetting)
}
config.S3ForcePathStyle = aws.Bool(s3ForcePathStyle)
}
region, err := getAWSRegion(bucket, config, settings)
if err != nil {
return nil, err
}
config = config.WithRegion(region)
return config, nil
}
// TODO : unit tests
func createSession(bucket string, settings map[string]string) (*session.Session, error) {
s, err := session.NewSession()
if err != nil {
return nil, err
}
c, err := configWithSettings(s, bucket, settings)
if err != nil {
return nil, err
}
s.Config = c
filePath := settings[s3CertFile]
if filePath != "" {
if file, err := os.Open(filePath); err == nil {
defer file.Close()
s, err := session.NewSessionWithOptions(session.Options{Config: *s.Config, CustomCABundle: file})
return s, err
}
return nil, err
}
if settings[S3UseYcSessionToken] != "" {
useYcSessionToken, err := strconv.ParseBool(settings[S3UseYcSessionToken])
if err != nil {
return nil, NewFolderError(err, "Invalid %s setting", S3UseYcSessionToken)
}
if useYcSessionToken {
// yandex cloud mimic metadata service, so we can use default AWS credentials, but set token to another header
cred := credentials.NewCredentials(defaults.RemoteCredProvider(*defaults.Config(), defaults.Handlers()))
s.Config.WithCredentials(cred)
s.Handlers.Send.PushFront(func(r *request.Request) {
token := r.HTTPRequest.Header.Get("X-Amz-Security-Token")
r.HTTPRequest.Header.Add("X-YaCloud-SubjectToken", token)
})
}
}
if endpointSource, ok := settings[EndpointSourceSetting]; ok {
s.Handlers.Validate.PushBack(func(request *request.Request) {
src := setupReqProxy(endpointSource, getEndpointPort(settings))
if src != nil {
tracelog.DebugLogger.Printf("using endpoint %s", *src)
host := strings.TrimPrefix(*s.Config.Endpoint, "https://")
request.HTTPRequest.Host = host
request.HTTPRequest.Header.Add("Host", host)
request.HTTPRequest.URL.Host = *src
request.HTTPRequest.URL.Scheme = HTTP
} else {
tracelog.DebugLogger.Printf("using endpoint %s", *s.Config.Endpoint)
}
})
}
return s, err
}
func getEndpointPort(settings map[string]string) string {
if port, ok := settings[EndpointPortSetting]; ok {
return port
}
return DefaultPort
}