forked from kahing/goofys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2signer.go
212 lines (181 loc) · 5.02 KB
/
v2signer.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
// Copyright 2015 - 2017 Ka-Hing Cheung
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/protocol/rest"
)
var (
errInvalidMethod = errors.New("v2 signer does not handle HTTP POST")
)
const (
signatureVersion = "2"
signatureMethod = "HmacSHA1"
timeFormat = "Mon, 2 Jan 2006 15:04:05 +0000"
)
var subresources = []string{
"acl",
"delete",
"lifecycle",
"location",
"logging",
"notification",
"partNumber",
"policy",
"requestPayment",
"torrent",
"uploadId",
"uploads",
"versionId",
"versioning",
"versions",
"website",
}
type signer struct {
// Values that must be populated from the request
Request *http.Request
Time time.Time
Credentials *credentials.Credentials
Debug aws.LogLevelType
Logger aws.Logger
pathStyle bool
bucket string
Query url.Values
stringToSign string
signature string
}
// Sign requests with signature version 2.
//
// Will sign the requests with the service config's Credentials object
// Signing is skipped if the credentials is the credentials.AnonymousCredentials
// object.
func SignV2(req *request.Request) {
// If the request does not need to be signed ignore the signing of the
// request if the AnonymousCredentials object is used.
if req.Config.Credentials == credentials.AnonymousCredentials {
return
}
v2 := signer{
Request: req.HTTPRequest,
Time: req.Time,
Credentials: req.Config.Credentials,
Debug: req.Config.LogLevel.Value(),
Logger: req.Config.Logger,
pathStyle: aws.BoolValue(req.Config.S3ForcePathStyle),
}
req.Error = v2.Sign()
}
func (v2 *signer) Sign() error {
credValue, err := v2.Credentials.Get()
if err != nil {
return err
}
v2.Query = v2.Request.URL.Query()
contentMD5 := v2.Request.Header.Get("Content-MD5")
contentType := v2.Request.Header.Get("Content-Type")
date := v2.Time.UTC().Format(timeFormat)
v2.Request.Header.Set("x-amz-date", date)
if credValue.SessionToken != "" {
v2.Request.Header.Set("x-amz-security-token", credValue.SessionToken)
}
// in case this is a retry, ensure no signature present
v2.Request.Header.Del("Authorization")
method := v2.Request.Method
uri := v2.Request.URL.Opaque
if uri != "" {
if questionMark := strings.Index(uri, "?"); questionMark != -1 {
uri = uri[0:questionMark]
}
uri = "/" + strings.Join(strings.Split(uri, "/")[3:], "/")
} else {
uri = v2.Request.URL.Path
}
path := rest.EscapePath(uri, false)
if !v2.pathStyle {
host := strings.SplitN(v2.Request.URL.Host, ".", 2)[0]
path = "/" + host + uri
}
if path == "" {
path = "/"
}
// build URL-encoded query keys and values
queryKeysAndValues := []string{}
for _, key := range subresources {
if _, ok := v2.Query[key]; ok {
k := strings.Replace(url.QueryEscape(key), "+", "%20", -1)
v := strings.Replace(url.QueryEscape(v2.Query.Get(key)), "+", "%20", -1)
if v != "" {
v = "=" + v
}
queryKeysAndValues = append(queryKeysAndValues, k+v)
}
}
// join into one query string
query := strings.Join(queryKeysAndValues, "&")
if query != "" {
path += "?" + query
}
tmp := []string{
method,
contentMD5,
contentType,
"",
}
var headers []string
for k := range v2.Request.Header {
k = strings.ToLower(k)
if strings.HasPrefix(k, "x-amz-") {
headers = append(headers, k)
}
}
sort.Strings(headers)
for _, k := range headers {
v := strings.Join(v2.Request.Header[http.CanonicalHeaderKey(k)], ",")
tmp = append(tmp, k+":"+v)
}
tmp = append(tmp, path)
// build the canonical string for the V2 signature
v2.stringToSign = strings.Join(tmp, "\n")
hash := hmac.New(sha1.New, []byte(credValue.SecretAccessKey))
hash.Write([]byte(v2.stringToSign))
v2.signature = base64.StdEncoding.EncodeToString(hash.Sum(nil))
v2.Request.Header.Set("Authorization",
"AWS "+credValue.AccessKeyID+":"+v2.signature)
if v2.Debug.Matches(aws.LogDebugWithSigning) {
v2.logSigningInfo()
}
return nil
}
const logSignInfoMsg = `DEBUG: Request Signature:
---[ STRING TO SIGN ]--------------------------------
%s
---[ SIGNATURE ]-------------------------------------
%s
-----------------------------------------------------`
func (v2 *signer) logSigningInfo() {
msg := fmt.Sprintf(logSignInfoMsg, v2.stringToSign, v2.Request.Header.Get("Authorization"))
v2.Logger.Log(msg)
}