-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput.go
195 lines (170 loc) · 4.88 KB
/
input.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
package s3
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
)
type inputModel struct {
credentials []awsCredentials
method string
endpoint string
region string
bucket string
key string
now time.Time
expireTime time.Time
expiresIn time.Duration
etag string
content io.ReadSeeker
contentType string
contentEncoding string
contentMD5 string
contentLength int64
serverSideEncryption ServerSideEncryptionValue
}
func newInput(method string, options []Option) *inputModel {
return new(inputModel).applyOptions(append(options, method_(method)))
}
func (this *inputModel) applyOptions(options []Option) *inputModel {
for _, option := range options {
if option != nil {
option(this)
}
}
if len(this.credentials) == 0 {
AmbientCredentials()(this)
}
if len(this.region) == 0 {
Region("us-east-1")(this)
}
if this.now.IsZero() {
Timestamp(time.Now().UTC())(this)
}
return this
}
func (this *inputModel) validate() error {
if this.method != HEAD && this.method != GET && this.method != PUT {
return ErrInvalidRequestMethod
}
if len(this.bucket) == 0 {
return ErrBucketMissing
}
if len(this.key) == 0 {
return ErrKeyMissing
}
if this.method == PUT && this.content == nil {
return ErrContentMissing
}
return nil
}
func (this *inputModel) buildAndSignRequest() (request *http.Request, err error) {
request, err = http.NewRequest(this.method, this.buildURL(), this.content)
if err != nil {
return nil, err
}
this.prepareRequestForSigning(request)
signature := calculateAWSv4Signature(this.region, request, this.credential())
request.Header.Set("Authorization", signature)
return request, nil
}
func (this *inputModel) prepareRequestForSigning(request *http.Request) {
if request.URL.Path == "" {
request.URL.Path += "/"
}
if this.contentLength > 0 {
request.ContentLength = this.contentLength
}
if len(this.contentType) == 0 {
this.contentType = "application/x-www-form-urlencoded; charset=utf-8"
}
setHeader(request, "Host", request.Host) // This must be included in range of headers to sign
setHeader(request, "Content-Length", formatInt64(this.contentLength))
setHeader(request, "Content-Encoding", this.contentEncoding)
setHeader(request, "Content-Type", this.contentType)
setHeader(request, "Content-MD5", this.contentMD5)
setHeader(request, "If-None-Match", this.etag)
setHeader(request, "X-Amz-Server-Side-Encryption", string(this.serverSideEncryption))
setHeader(request, "X-Amz-Security-Token", this.credential().SecurityToken)
setHeader(request, "X-Amz-Content-Sha256", hashSHA256(readAndReplaceBody(request)))
setHeader(request, "X-Amz-Expires", formatUnixTimeStamp(this.expireTime))
setHeader(request, "X-Amz-Date", this.timestampV4())
}
func (this *inputModel) buildVirtualHostname() string {
builder := new(strings.Builder)
builder.WriteString(this.bucket)
builder.WriteString(".s3")
if this.region != "us-east-1" {
builder.WriteString("-")
builder.WriteString(this.region)
}
builder.WriteString(".amazonaws.com")
return builder.String()
}
// https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
func (this *inputModel) buildVirtualHostingURL() string {
builder := new(strings.Builder)
if len(this.endpoint) > 0 {
builder.WriteString(this.endpoint)
} else {
builder.WriteString("https://")
builder.WriteString(this.buildVirtualHostname())
builder.WriteString("/")
}
if len(this.endpoint) > 0 {
if !strings.HasSuffix(builder.String(), "/") {
builder.WriteString("/")
}
builder.WriteString(this.bucket)
builder.WriteString("/")
}
builder.WriteString(this.key)
return builder.String()
}
func (this *inputModel) buildURL() string {
builder := new(strings.Builder)
if len(this.endpoint) > 0 {
builder.WriteString(this.endpoint)
} else {
builder.WriteString("https://s3")
if len(this.region) > 0 && this.region != "us-east-1" {
builder.WriteString("-")
builder.WriteString(this.region)
}
builder.WriteString(".amazonaws.com")
}
if !strings.HasSuffix(builder.String(), "/") {
builder.WriteString("/")
}
builder.WriteString(this.bucket)
builder.WriteString("/")
builder.WriteString(this.key)
return builder.String()
}
func (this *inputModel) timestampV4() string {
return this.now.Format(timeFormatV4)
}
func (this *inputModel) expiresInSeconds() string {
return strconv.Itoa(int(this.expiresIn.Seconds()))
}
func (this *inputModel) fullCredentialScope() string {
return fmt.Sprintf("%s/%s",
this.credential().AccessKeyID,
this.credentialScope(),
)
}
func (this *inputModel) credential() (credentials awsCredentials) {
if len(this.credentials) > 0 {
return this.credentials[0]
} else {
return credentials
}
}
func (this *inputModel) credentialScope() string {
return fmt.Sprintf("%s/%s/%s/%s",
timestampDateV4(this.timestampV4()), this.region,
"s3", awsV4CredentialScopeTerminationString,
) // YYYYMMDD/us-east-1/s3/aws4_request
}