forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathsignature_v4_test.go
193 lines (176 loc) · 6.1 KB
/
signature_v4_test.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
package sls
import (
"crypto/md5"
"fmt"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type SignerV4Suite struct {
suite.Suite
mockAKID string
mockAKSec string
uri string
method string
region string
body string
dateTime string
urlParams map[string]string
headers map[string]string
signer Signer
}
func toUriWithQuery(uri string, urlParams map[string]string) string {
vals := url.Values{}
for k, v := range urlParams {
vals.Add(k, v)
}
return fmt.Sprintf("%s?%s", uri, vals.Encode())
}
func (s *SignerV4Suite) SetupTest() {
s.mockAKID = "acsddda21dsd"
s.mockAKSec = "zxasdasdasw2"
s.uri = "/logstores"
s.method = "POST"
s.region = "cn-hangzhou"
s.body = "adasd= -asd zcas"
s.headers = map[string]string{
"hello": "world",
"hello-Text": "a12X- ",
" Ko ": "",
"": "AA",
"x-log-test": "het123",
"x-acs-ppp": "dds",
}
s.urlParams = map[string]string{
" abc": "efg",
" agc ": "",
"": "efg",
"A-bc": "eFg",
}
s.dateTime = "20220808T032330Z"
// Set dateTime for debugging
s.headers[HTTPHeaderLogDate] = s.dateTime
s.signer = &SignerV4{
accessKeyID: s.mockAKID,
accessKeySecret: s.mockAKSec,
region: s.region,
}
}
func (s *SignerV4Suite) TestSignV4Case1() {
assert.Nil(s.T(), s.signer.Sign(s.method, toUriWithQuery(s.uri, s.urlParams), s.headers, []byte(s.body)))
auth := s.headers[HTTPHeaderAuthorization]
exp := "SLS4-HMAC-SHA256 " +
"Credential=acsddda21dsd/20220808/cn-hangzhou/sls/aliyun_v4_request," +
"Signature=a98f5632e93836e63839cd836a54055f480020a9364ca944e2d34f2eb9bf1bed"
assert.Equal(s.T(), exp, auth)
}
// Empty urlParams, empty headers, region cn-shanghai
func (s *SignerV4Suite) TestSignV4Case2() {
s.region = "cn-shanghai"
s.signer = &SignerV4{
accessKeyID: s.mockAKID,
accessKeySecret: s.mockAKSec,
region: s.region,
}
s.headers = make(map[string]string)
s.headers[HTTPHeaderLogDate] = s.dateTime
assert.Nil(s.T(), s.signer.Sign(s.method, s.uri, s.headers, []byte(s.body)))
auth := s.headers[HTTPHeaderAuthorization]
exp := "SLS4-HMAC-SHA256 " +
"Credential=acsddda21dsd/20220808/cn-shanghai/sls/aliyun_v4_request," +
"Signature=8a10a5e723cb2e75964816de660b2c16a58af8bc0261f7f0722d832468c76ce8"
assert.Equal(s.T(), exp, auth)
}
// Empty body
func (s *SignerV4Suite) TestSignV4Case3() {
s.body = ""
assert.Nil(s.T(), s.signer.Sign(s.method, toUriWithQuery(s.uri, s.urlParams), s.headers, []byte(s.body)))
auth := s.headers[HTTPHeaderAuthorization]
exp := "SLS4-HMAC-SHA256 " +
"Credential=acsddda21dsd/20220808/cn-hangzhou/sls/aliyun_v4_request," +
"Signature=5a66d8f8051983e0e9d08e0f960ef9252ef971eead5bb5c7acec8617a2eb2701"
assert.Equal(s.T(), exp, auth)
}
// Empty body and method get
func (s *SignerV4Suite) TestSignV4Case4() {
s.body = ""
s.method = "GET"
assert.Nil(s.T(), s.signer.Sign(s.method, toUriWithQuery(s.uri, s.urlParams), s.headers, []byte(s.body)))
auth := s.headers[HTTPHeaderAuthorization]
exp := "SLS4-HMAC-SHA256 " +
"Credential=acsddda21dsd/20220808/cn-hangzhou/sls/aliyun_v4_request," +
"Signature=d92741852500791d662a8d469ff61627c0559ecd86c3f59b7bf6772b6c62666a"
assert.Equal(s.T(), exp, auth)
}
// Complex uri and urlParams
func (s *SignerV4Suite) TestSignV4Case5() {
s.uri = "/logstores/hello/a+*~bb/cc"
s.urlParams["abs-ij*asd/vc"] = "a~js+d ada"
s.urlParams["a abAas123/vc"] = "a~jdad a2ADFs+d ada"
assert.Nil(s.T(), s.signer.Sign(s.method, toUriWithQuery(s.uri, s.urlParams), s.headers, []byte(s.body)))
auth := s.headers[HTTPHeaderAuthorization]
exp := "SLS4-HMAC-SHA256 " +
"Credential=acsddda21dsd/20220808/cn-hangzhou/sls/aliyun_v4_request," +
"Signature=2c204068e961a8813a6bcf7ac422f7fa6e9bf9a5da493e0165dfe100854d18ff"
assert.Equal(s.T(), exp, auth)
}
func (s *SignerV4Suite) TestSignV4Case6() {
mockAKID := "mockAccessKeyID"
mockAKSec := "mockAccessKeySecret"
signer := SignerV4{accessKeyID: mockAKID, accessKeySecret: mockAKSec, region: "cn-hangzhou"}
encoded := signer.percentEncode("123abc!@#$%^&*()-=_+ ~|\\/")
assert.Equal(s.T(), "123abc%21%40%23%24%25%5E%26%2A%28%29-%3D_%2B%20~%7C%5C%2F", encoded)
encoded = signer.percentEncode("!@#$%^&*()=-+ ~./_[()]%20你好\000\u0111❤😓")
assert.Equal(s.T(), "%21%40%23%24%25%5E%26%2A%28%29%3D-%2B%20~.%2F_%5B%28%29%5D%2520%E4%BD%A0%E5%A5%BD%00%C4%91%E2%9D%A4%F0%9F%98%93", encoded)
}
func (s *SignerV4Suite) TestSignV1Case1() {
headers := map[string]string{
"x-log-apiversion": "0.6.0",
"x-log-signaturemethod": "hmac-sha1",
"x-log-bodyrawsize": "0",
"Date": "Mon, 3 Jan 2010 08:33:47 GMT",
}
mockAKID := "mockAccessKeyID"
mockAKSec := "mockAccessKeySecret"
expSign := "Rwm6cTKzoti4HWoe+GKcb6Kv07E="
expAuth := fmt.Sprintf("SLS %s:%s", mockAKID, expSign)
v1 := SignerV1{accessKeyID: mockAKID, accessKeySecret: mockAKSec}
err := v1.Sign("GET", "/logstores", headers, nil)
assert.Nil(s.T(), err)
auth := headers[HTTPHeaderAuthorization]
assert.Equal(s.T(), expAuth, auth)
}
// Protobuf content
func (s *SignerV4Suite) TestSignV1Case2() {
body := []byte{10, 50, 10, 30, 8, 248, 178, 147,
158, 5, 18, 22, 10, 7, 84, 101, 115, 116, 75,
101, 121, 18, 11, 84, 101, 115, 116, 67, 111,
110, 116, 101, 110, 116, 26, 0, 34, 14, 49,
48, 46, 50, 51, 48, 46, 50, 48, 49, 46, 49, 49, 55}
md5Sum := fmt.Sprintf("%X", md5.Sum(body))
headers := map[string]string{
"x-log-apiversion": "0.6.0",
"x-log-signaturemethod": "hmac-sha1",
"x-log-bodyrawsize": "50",
"Content-MD5": md5Sum,
"Content-Type": "application/x-protobuf",
"Content-Length": "50",
"Date": "Mon, 3 Jan 2010 08:33:47 GMT",
}
mockAKID := "mockAccessKeyID"
mockAKSec := "mockAccessKeySecret"
expSign := "87xQWqFaOSewqRIma8kPjGYlXHc="
expAuth := fmt.Sprintf("SLS %s:%s", mockAKID, expSign)
v1 := SignerV1{
accessKeyID: mockAKID,
accessKeySecret: mockAKSec,
}
err := v1.Sign("GET", "/logstores/app_log", headers, body)
assert.Nil(s.T(), err)
auth := headers[HTTPHeaderAuthorization]
assert.Equal(s.T(), expAuth, auth)
}
func TestSignerV4Suite(t *testing.T) {
suite.Run(t, new(SignerV4Suite))
}