/
query_params.go
519 lines (467 loc) · 13.7 KB
/
query_params.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package sas
import (
"errors"
"net"
"net/url"
"strings"
"time"
)
// TimeFormat represents the format of a SAS start or expiry time. Use it when formatting/parsing a time.Time.
const (
TimeFormat = "2006-01-02T15:04:05Z" // "2017-07-27T00:00:00Z" // ISO 8601
)
var (
// Version is the default version encoded in the SAS token.
Version = "2020-12-06"
)
// TimeFormats ISO 8601 format.
// Please refer to https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas for more details.
var timeFormats = []string{"2006-01-02T15:04:05.0000000Z", TimeFormat, "2006-01-02T15:04Z", "2006-01-02"}
// Protocol indicates the http/https.
type Protocol string
const (
// ProtocolHTTPS can be specified for a SAS protocol.
ProtocolHTTPS Protocol = "https"
// ProtocolHTTPSandHTTP can be specified for a SAS protocol.
ProtocolHTTPSandHTTP Protocol = "https,http"
)
// FormatTimesForSigning converts a time.Time to a snapshotTimeFormat string suitable for a
// Field's StartTime or ExpiryTime fields. Returns "" if value.IsZero().
func formatTimesForSigning(startTime, expiryTime time.Time) (string, string) {
ss := ""
if !startTime.IsZero() {
ss = formatTimeWithDefaultFormat(&startTime)
}
se := ""
if !expiryTime.IsZero() {
se = formatTimeWithDefaultFormat(&expiryTime)
}
return ss, se
}
// formatTimeWithDefaultFormat format time with ISO 8601 in "yyyy-MM-ddTHH:mm:ssZ".
func formatTimeWithDefaultFormat(t *time.Time) string {
return formatTime(t, TimeFormat) // By default, "yyyy-MM-ddTHH:mm:ssZ" is used
}
// formatTime format time with given format, use ISO 8601 in "yyyy-MM-ddTHH:mm:ssZ" by default.
func formatTime(t *time.Time, format string) string {
if format != "" {
return t.Format(format)
}
return t.Format(TimeFormat) // By default, "yyyy-MM-ddTHH:mm:ssZ" is used
}
// ParseTime try to parse a SAS time string.
func parseTime(val string) (t time.Time, timeFormat string, err error) {
for _, sasTimeFormat := range timeFormats {
t, err = time.Parse(sasTimeFormat, val)
if err == nil {
timeFormat = sasTimeFormat
break
}
}
if err != nil {
err = errors.New("fail to parse time with IOS 8601 formats, please refer to https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas for more details")
}
return
}
// IPRange represents a SAS IP range's start IP and (optionally) end IP.
type IPRange struct {
Start net.IP // Not specified if length = 0
End net.IP // Not specified if length = 0
}
// String returns a string representation of an IPRange.
func (ipr *IPRange) String() string {
if len(ipr.Start) == 0 {
return ""
}
start := ipr.Start.String()
if len(ipr.End) == 0 {
return start
}
return start + "-" + ipr.End.String()
}
// https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas
// QueryParameters object represents the components that make up an Azure Storage SAS' query parameters.
// You parse a map of query parameters into its fields by calling NewQueryParameters(). You add the components
// to a query parameter map by calling AddToValues().
// NOTE: Changing any field requires computing a new SAS signature using a XxxSASSignatureValues type.
// This type defines the components used by all Azure Storage resources (Containers, Blobs, Files, & Queues).
type QueryParameters struct {
// All members are immutable or values so copies of this struct are goroutine-safe.
version string `param:"sv"`
services string `param:"ss"`
resourceTypes string `param:"srt"`
protocol Protocol `param:"spr"`
startTime time.Time `param:"st"`
expiryTime time.Time `param:"se"`
snapshotTime time.Time `param:"snapshot"`
ipRange IPRange `param:"sip"`
identifier string `param:"si"`
resource string `param:"sr"`
permissions string `param:"sp"`
signature string `param:"sig"`
cacheControl string `param:"rscc"`
contentDisposition string `param:"rscd"`
contentEncoding string `param:"rsce"`
contentLanguage string `param:"rscl"`
contentType string `param:"rsct"`
signedOID string `param:"skoid"`
signedTID string `param:"sktid"`
signedStart time.Time `param:"skt"`
signedService string `param:"sks"`
signedExpiry time.Time `param:"ske"`
signedVersion string `param:"skv"`
signedDirectoryDepth string `param:"sdd"`
authorizedObjectID string `param:"saoid"`
unauthorizedObjectID string `param:"suoid"`
correlationID string `param:"scid"`
encryptionScope string `param:"ses"`
// private member used for startTime and expiryTime formatting.
stTimeFormat string
seTimeFormat string
}
// AuthorizedObjectID returns authorizedObjectID.
func (p *QueryParameters) AuthorizedObjectID() string {
return p.authorizedObjectID
}
// UnauthorizedObjectID returns unauthorizedObjectID.
func (p *QueryParameters) UnauthorizedObjectID() string {
return p.unauthorizedObjectID
}
// SignedCorrelationID returns signedCorrelationID.
func (p *QueryParameters) SignedCorrelationID() string {
return p.correlationID
}
// SignedOID returns signedOID.
func (p *QueryParameters) SignedOID() string {
return p.signedOID
}
// SignedTID returns signedTID.
func (p *QueryParameters) SignedTID() string {
return p.signedTID
}
// SignedStart returns signedStart.
func (p *QueryParameters) SignedStart() time.Time {
return p.signedStart
}
// SignedExpiry returns signedExpiry.
func (p *QueryParameters) SignedExpiry() time.Time {
return p.signedExpiry
}
// SignedService returns signedService.
func (p *QueryParameters) SignedService() string {
return p.signedService
}
// SignedVersion returns signedVersion.
func (p *QueryParameters) SignedVersion() string {
return p.signedVersion
}
// SnapshotTime returns snapshotTime.
func (p *QueryParameters) SnapshotTime() time.Time {
return p.snapshotTime
}
// Version returns version.
func (p *QueryParameters) Version() string {
return p.version
}
// Services returns services.
func (p *QueryParameters) Services() string {
return p.services
}
// ResourceTypes returns resourceTypes.
func (p *QueryParameters) ResourceTypes() string {
return p.resourceTypes
}
// Protocol returns protocol.
func (p *QueryParameters) Protocol() Protocol {
return p.protocol
}
// StartTime returns startTime.
func (p *QueryParameters) StartTime() time.Time {
return p.startTime
}
// ExpiryTime returns expiryTime.
func (p *QueryParameters) ExpiryTime() time.Time {
return p.expiryTime
}
// IPRange returns ipRange.
func (p *QueryParameters) IPRange() IPRange {
return p.ipRange
}
// Identifier returns identifier.
func (p *QueryParameters) Identifier() string {
return p.identifier
}
// Resource returns resource.
func (p *QueryParameters) Resource() string {
return p.resource
}
// Permissions returns permissions.
func (p *QueryParameters) Permissions() string {
return p.permissions
}
// Signature returns signature.
func (p *QueryParameters) Signature() string {
return p.signature
}
// CacheControl returns cacheControl.
func (p *QueryParameters) CacheControl() string {
return p.cacheControl
}
// ContentDisposition returns contentDisposition.
func (p *QueryParameters) ContentDisposition() string {
return p.contentDisposition
}
// ContentEncoding returns contentEncoding.
func (p *QueryParameters) ContentEncoding() string {
return p.contentEncoding
}
// ContentLanguage returns contentLanguage.
func (p *QueryParameters) ContentLanguage() string {
return p.contentLanguage
}
// ContentType returns contentType.
func (p *QueryParameters) ContentType() string {
return p.contentType
}
// SignedDirectoryDepth returns signedDirectoryDepth.
func (p *QueryParameters) SignedDirectoryDepth() string {
return p.signedDirectoryDepth
}
// SignedEncryptionScope returns encryptionScope.
func (p *QueryParameters) SignedEncryptionScope() string {
return p.encryptionScope
}
// Encode encodes the SAS query parameters into URL encoded form sorted by key.
func (p *QueryParameters) Encode() string {
v := url.Values{}
if p.version != "" {
v.Add("sv", p.version)
}
if p.services != "" {
v.Add("ss", p.services)
}
if p.resourceTypes != "" {
v.Add("srt", p.resourceTypes)
}
if p.protocol != "" {
v.Add("spr", string(p.protocol))
}
if !p.startTime.IsZero() {
v.Add("st", formatTime(&(p.startTime), p.stTimeFormat))
}
if !p.expiryTime.IsZero() {
v.Add("se", formatTime(&(p.expiryTime), p.seTimeFormat))
}
if len(p.ipRange.Start) > 0 {
v.Add("sip", p.ipRange.String())
}
if p.identifier != "" {
v.Add("si", p.identifier)
}
if p.resource != "" {
v.Add("sr", p.resource)
}
if p.permissions != "" {
v.Add("sp", p.permissions)
}
if p.signedOID != "" {
v.Add("skoid", p.signedOID)
v.Add("sktid", p.signedTID)
v.Add("skt", p.signedStart.Format(TimeFormat))
v.Add("ske", p.signedExpiry.Format(TimeFormat))
v.Add("sks", p.signedService)
v.Add("skv", p.signedVersion)
}
if p.signature != "" {
v.Add("sig", p.signature)
}
if p.cacheControl != "" {
v.Add("rscc", p.cacheControl)
}
if p.contentDisposition != "" {
v.Add("rscd", p.contentDisposition)
}
if p.contentEncoding != "" {
v.Add("rsce", p.contentEncoding)
}
if p.contentLanguage != "" {
v.Add("rscl", p.contentLanguage)
}
if p.contentType != "" {
v.Add("rsct", p.contentType)
}
if p.signedDirectoryDepth != "" {
v.Add("sdd", p.signedDirectoryDepth)
}
if p.authorizedObjectID != "" {
v.Add("saoid", p.authorizedObjectID)
}
if p.unauthorizedObjectID != "" {
v.Add("suoid", p.unauthorizedObjectID)
}
if p.correlationID != "" {
v.Add("scid", p.correlationID)
}
if p.encryptionScope != "" {
v.Add("ses", p.encryptionScope)
}
return v.Encode()
}
// NewQueryParameters creates and initializes a QueryParameters object based on the
// query parameter map's passed-in values. If a key is unrecognized, it is ignored
func NewQueryParameters(values url.Values) QueryParameters {
p := QueryParameters{}
for k, v := range values {
val := v[0]
switch strings.ToLower(k) {
case "sv":
p.version = val
case "ss":
p.services = val
case "srt":
p.resourceTypes = val
case "spr":
p.protocol = Protocol(val)
case "st":
p.startTime, p.stTimeFormat, _ = parseTime(val)
case "se":
p.expiryTime, p.seTimeFormat, _ = parseTime(val)
case "sip":
dashIndex := strings.Index(val, "-")
if dashIndex == -1 {
p.ipRange.Start = net.ParseIP(val)
} else {
p.ipRange.Start = net.ParseIP(val[:dashIndex])
p.ipRange.End = net.ParseIP(val[dashIndex+1:])
}
case "si":
p.identifier = val
case "sr":
p.resource = val
case "sp":
p.permissions = val
case "sig":
p.signature = val
case "rscc":
p.cacheControl = val
case "rscd":
p.contentDisposition = val
case "rsce":
p.contentEncoding = val
case "rscl":
p.contentLanguage = val
case "rsct":
p.contentType = val
case "skoid":
p.signedOID = val
case "sktid":
p.signedTID = val
case "skt":
p.signedStart, _ = time.Parse(TimeFormat, val)
case "ske":
p.signedExpiry, _ = time.Parse(TimeFormat, val)
case "sks":
p.signedService = val
case "skv":
p.signedVersion = val
case "sdd":
p.signedDirectoryDepth = val
case "saoid":
p.authorizedObjectID = val
case "suoid":
p.unauthorizedObjectID = val
case "scid":
p.correlationID = val
case "ses":
p.encryptionScope = val
default:
continue // query param didn't get recognized
}
}
return p
}
// newQueryParameters creates and initializes a QueryParameters object based on the
// query parameter map's passed-in values. If deleteSASParametersFromValues is true,
// all SAS-related query parameters are removed from the passed-in map. If
// deleteSASParametersFromValues is false, the map passed-in map is unaltered.
func newQueryParameters(values url.Values, deleteSASParametersFromValues bool) QueryParameters {
p := QueryParameters{}
for k, v := range values {
val := v[0]
isSASKey := true
switch strings.ToLower(k) {
case "sv":
p.version = val
case "ss":
p.services = val
case "srt":
p.resourceTypes = val
case "spr":
p.protocol = Protocol(val)
case "st":
p.startTime, p.stTimeFormat, _ = parseTime(val)
case "se":
p.expiryTime, p.seTimeFormat, _ = parseTime(val)
case "sip":
dashIndex := strings.Index(val, "-")
if dashIndex == -1 {
p.ipRange.Start = net.ParseIP(val)
} else {
p.ipRange.Start = net.ParseIP(val[:dashIndex])
p.ipRange.End = net.ParseIP(val[dashIndex+1:])
}
case "si":
p.identifier = val
case "sr":
p.resource = val
case "sp":
p.permissions = val
//case "snapshot":
// p.snapshotTime, _ = time.Parse(exported.SnapshotTimeFormat, val)
case "sig":
p.signature = val
case "rscc":
p.cacheControl = val
case "rscd":
p.contentDisposition = val
case "rsce":
p.contentEncoding = val
case "rscl":
p.contentLanguage = val
case "rsct":
p.contentType = val
case "skoid":
p.signedOID = val
case "sktid":
p.signedTID = val
case "skt":
p.signedStart, _ = time.Parse(TimeFormat, val)
case "ske":
p.signedExpiry, _ = time.Parse(TimeFormat, val)
case "sks":
p.signedService = val
case "skv":
p.signedVersion = val
case "sdd":
p.signedDirectoryDepth = val
case "saoid":
p.authorizedObjectID = val
case "suoid":
p.unauthorizedObjectID = val
case "scid":
p.correlationID = val
case "ses":
p.encryptionScope = val
default:
isSASKey = false // We didn't recognize the query parameter
}
if isSASKey && deleteSASParametersFromValues {
delete(values, k)
}
}
return p
}