aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-blob-go/2018-03-28/azblob/zc_sas_query_params.go
blob: db10171e7535fc9c982bc816ee14b22fc24e48ea (plain) (blame)
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
package azblob

import (
    "net"
    "net/url"
    "strings"
    "time"
)

// SASVersion indicates the SAS version.
const SASVersion = ServiceVersion

type SASProtocol string

const (
    // SASProtocolHTTPS can be specified for a SAS protocol
    SASProtocolHTTPS SASProtocol = "https"

    // SASProtocolHTTPSandHTTP can be specified for a SAS protocol
    SASProtocolHTTPSandHTTP SASProtocol = "https,http"
)

// FormatTimesForSASSigning converts a time.Time to a snapshotTimeFormat string suitable for a
// SASField's StartTime or ExpiryTime fields. Returns "" if value.IsZero().
func FormatTimesForSASSigning(startTime, expiryTime time.Time) (string, string) {
    ss := ""
    if !startTime.IsZero() {
        ss = startTime.Format(SASTimeFormat) // "yyyy-MM-ddTHH:mm:ssZ"
    }
    se := ""
    if !expiryTime.IsZero() {
        se = expiryTime.Format(SASTimeFormat) // "yyyy-MM-ddTHH:mm:ssZ"
    }
    return ss, se
}

// SASTimeFormat represents the format of a SAS start or expiry time. Use it when formatting/parsing a time.Time.
const SASTimeFormat = "2006-01-02T15:04:05Z" //"2017-07-27T00:00:00Z" // ISO 8601

// https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas

// A SASQueryParameters 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 NewSASQueryParameters(). 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 SASQueryParameters 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      SASProtocol `param:"spr"`
    startTime     time.Time   `param:"st"`
    expiryTime    time.Time   `param:"se"`
    ipRange       IPRange     `param:"sip"`
    identifier    string      `param:"si"`
    resource      string      `param:"sr"`
    permissions   string      `param:"sp"`
    signature     string      `param:"sig"`
}

func (p *SASQueryParameters) Version() string {
    return p.version
}

func (p *SASQueryParameters) Services() string {
    return p.services
}
func (p *SASQueryParameters) ResourceTypes() string {
    return p.resourceTypes
}
func (p *SASQueryParameters) Protocol() SASProtocol {
    return p.protocol
}
func (p *SASQueryParameters) StartTime() time.Time {
    return p.startTime
}
func (p *SASQueryParameters) ExpiryTime() time.Time {
    return p.expiryTime
}

func (p *SASQueryParameters) IPRange() IPRange {
    return p.ipRange
}

func (p *SASQueryParameters) Identifier() string {
    return p.identifier
}

func (p *SASQueryParameters) Resource() string {
    return p.resource
}
func (p *SASQueryParameters) Permissions() string {
    return p.permissions
}

func (p *SASQueryParameters) Signature() string {
    return p.signature
}

// 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()
}

// NewSASQueryParameters creates and initializes a SASQueryParameters 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 newSASQueryParameters(values url.Values, deleteSASParametersFromValues bool) SASQueryParameters {
    p := SASQueryParameters{}
    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 = SASProtocol(val)
        case "st":
            p.startTime, _ = time.Parse(SASTimeFormat, val)
        case "se":
            p.expiryTime, _ = time.Parse(SASTimeFormat, 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
        default:
            isSASKey = false // We didn't recognize the query parameter
        }
        if isSASKey && deleteSASParametersFromValues {
            delete(values, k)
        }
    }
    return p
}

// AddToValues adds the SAS components to the specified query parameters map.
func (p *SASQueryParameters) addToValues(v url.Values) 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", p.startTime.Format(SASTimeFormat))
    }
    if !p.expiryTime.IsZero() {
        v.Add("se", p.expiryTime.Format(SASTimeFormat))
    }
    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.signature != "" {
        v.Add("sig", p.signature)
    }
    return v
}

// Encode encodes the SAS query parameters into URL encoded form sorted by key.
func (p *SASQueryParameters) Encode() string {
    v := url.Values{}
    p.addToValues(v)
    return v.Encode()
}