forked from wal-g/storages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
174 lines (155 loc) · 5.38 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
package s3
import (
"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/pkg/errors"
"github.com/wal-g/tracelog"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"strings"
)
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)
} else {
// 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 := ioutil.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 getDefaultConfig(settings map[string]string) *aws.Config {
// 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.
config := defaults.Get().Config.WithRegion(settings[RegionSetting])
config = request.WithRetryer(config, client.DefaultRetryer{NumMaxRetries: MaxRetries})
provider := &credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: getFirstSettingOf(settings, []string{AccessKeyIdSetting, AccessKeySetting}),
SecretAccessKey: getFirstSettingOf(settings, []string{SecretAccessKeySetting, SecretKeySetting}),
SessionToken: settings[SessionTokenSetting],
}}
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 endpoint, ok := settings[EndpointSetting]; ok {
config = config.WithEndpoint(endpoint)
}
return config
}
// TODO : unit tests
func createSession(bucket string, settings map[string]string) (*session.Session, error) {
config := getDefaultConfig(settings)
config.MaxRetries = &MaxRetries
if _, err := config.Credentials.Get(); err != nil {
return nil, errors.Wrapf(err, "failed to get AWS credentials; please specify %s and %s", AccessKeyIdSetting, SecretAccessKeySetting)
}
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)
filePath := settings[s3CertFile]
if filePath != "" {
if file, err := os.Open(filePath); err == nil {
defer file.Close()
s, err := session.NewSessionWithOptions(session.Options{Config: *config, CustomCABundle: file})
return s, err
} else {
return nil, err
}
}
s, err := session.NewSession(config)
if err != nil {
return nil, err
}
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(*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", *config.Endpoint)
}
})
}
return s, err
}
func getEndpointPort(settings map[string]string) string {
if port, ok := settings[EndpointPortSetting]; ok {
return port
}
return DefaultPort
}