1
1
/*
2
- Package signer providers functions for sign http request before request cloud.
2
+ Copyright 2020 The Kubernetes Authors.
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ /*
18
+ Package aksk providers functions for sign http request before request cloud.
3
19
*/
4
20
package aksk
5
21
@@ -21,7 +37,7 @@ import (
21
37
"time"
22
38
)
23
39
24
- //caseInsencitiveStringArray represents string case insensitive sorting operations
40
+ // caseInsencitiveStringArray represents string case insensitive sorting operations
25
41
type caseInsencitiveStringArray []string
26
42
27
43
// noEscape specifies whether the character should be encoded or not
@@ -86,19 +102,19 @@ type signKeyCacheEntry struct {
86
102
NumberOfDaysSinceEpoch int64 // number of days since epoch
87
103
}
88
104
89
- // The default sign algorithm
105
+ // SignAlgorithmHMACSHA256 The default sign algorithm
90
106
const SignAlgorithmHMACSHA256 = "SDK-HMAC-SHA256"
91
107
92
- // The header key of content hash value
108
+ // ContentSha256HeaderKey The header key of content hash value
93
109
const ContentSha256HeaderKey = "x-sdk-content-sha256"
94
110
95
- //A regular for searching empty string
111
+ // A regular for searching empty string
96
112
var spaceRegexp = regexp .MustCompile (`\s+` )
97
113
98
114
// cache sign key
99
115
var cache = NewCache (300 )
100
116
101
- //Sign manipulates the http.Request instance with some required authentication headers for SK/SK auth.
117
+ // Sign manipulates the http.Request instance with some required authentication headers for SK/SK auth.
102
118
func Sign (req * http.Request , signOptions SignOptions ) {
103
119
signOptions .AccessKey = strings .TrimSpace (signOptions .AccessKey )
104
120
signOptions .SecretKey = strings .TrimSpace (signOptions .SecretKey )
@@ -164,9 +180,9 @@ func deriveSigningKey(signParam reqSignParams) []byte {
164
180
})
165
181
cache .Add (cacheKey , string (signKeyStr ))
166
182
return signKey
167
- } else {
168
- return buildSignKey (signParam )
169
183
}
184
+
185
+ return buildSignKey (signParam )
170
186
}
171
187
172
188
func buildSignKey (signParam reqSignParams ) []byte {
@@ -179,7 +195,7 @@ func buildSignKey(signParam reqSignParams) []byte {
179
195
return computeSignature ("sdk_request" , kService , signParam .SignAlgorithm )
180
196
}
181
197
182
- //HmacSha256 implements the Keyed-Hash Message Authentication Code computation.
198
+ // HmacSha256 implements the Keyed-Hash Message Authentication Code computation.
183
199
func HmacSha256 (data string , key []byte ) []byte {
184
200
mac := hmac .New (sha256 .New , key )
185
201
mac .Write ([]byte (data ))
@@ -215,10 +231,9 @@ func buildAuthorizationHeader(signParam reqSignParams, signature []byte) string
215
231
func computeSignature (signData string , key []byte , algorithm string ) []byte {
216
232
if algorithm == SignAlgorithmHMACSHA256 {
217
233
return HmacSha256 (signData , key )
218
- } else {
219
- log .Fatalf ("Unsupported algorithm %s, please use %s and try again" , algorithm , SignAlgorithmHMACSHA256 )
220
- return nil
221
234
}
235
+ log .Fatalf ("Unsupported algorithm %s, please use %s and try again" , algorithm , SignAlgorithmHMACSHA256 )
236
+ return nil
222
237
}
223
238
224
239
// createStringToSign build the need to be signed string
@@ -278,7 +293,7 @@ func encodeQueryString(queryValues url.Values) string {
278
293
279
294
i := 0
280
295
281
- for k , _ := range queryValues {
296
+ for k := range queryValues {
282
297
keys [i ] = urlEncode (k , false )
283
298
encodedVals [keys [i ]] = k
284
299
i ++
@@ -302,9 +317,8 @@ func encodeQueryString(queryValues url.Values) string {
302
317
func getCanonicalizedQueryString (signParas reqSignParams ) string {
303
318
if usePayloadForQueryParameters (signParas .Req ) {
304
319
return ""
305
- } else {
306
- return encodeQueryString (signParas .Req .URL .Query ())
307
320
}
321
+ return encodeQueryString (signParas .Req .URL .Query ())
308
322
}
309
323
310
324
// createCanonicalRequest builds canonical string depends the official document for signing
@@ -352,7 +366,7 @@ func getCanonicalizedHeaderString(req *http.Request) string {
352
366
var headers StringBuilder
353
367
354
368
keys := make ([]string , 0 )
355
- for k , _ := range req .Header {
369
+ for k := range req .Header {
356
370
keys = append (keys , strings .TrimSpace (k ))
357
371
}
358
372
@@ -379,7 +393,7 @@ func getSignedHeadersString(req *http.Request) string {
379
393
var headers StringBuilder
380
394
381
395
keys := make ([]string , 0 )
382
- for k , _ := range req .Header {
396
+ for k := range req .Header {
383
397
keys = append (keys , strings .TrimSpace (k ))
384
398
}
385
399
@@ -445,15 +459,18 @@ func (signParas *reqSignParams) getScope() string {
445
459
}, "/" )
446
460
}
447
461
462
+ // Write ...
448
463
func (buff * StringBuilder ) Write (s string ) * StringBuilder {
449
464
buff .builder .WriteString (s )
450
465
return buff
451
466
}
452
467
468
+ // ToString ...
453
469
func (buff * StringBuilder ) ToString () string {
454
470
return buff .builder .String ()
455
471
}
456
472
473
+ // GetBytes ...
457
474
func (buff * StringBuilder ) GetBytes () []byte {
458
475
return []byte (buff .ToString ())
459
476
}
0 commit comments