aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-sdk-for-go/storage/client.go
blob: 2816e03ec6c95cee833e1163fe60c4ef9aeded6e (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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// Package storage provides clients for Microsoft Azure Storage Services.
package storage

import (
    "bytes"
    "encoding/base64"
    "encoding/json"
    "encoding/xml"
    "errors"
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "net/url"
    "regexp"
    "sort"
    "strconv"
    "strings"
)

const (
    // DefaultBaseURL is the domain name used for storage requests when a
    // default client is created.
    DefaultBaseURL = "core.windows.net"

    // DefaultAPIVersion is the  Azure Storage API version string used when a
    // basic client is created.
    DefaultAPIVersion = "2015-02-21"

    defaultUseHTTPS = true

    // StorageEmulatorAccountName is the fixed storage account used by Azure Storage Emulator
    StorageEmulatorAccountName = "devstoreaccount1"

    // StorageEmulatorAccountKey is the the fixed storage account used by Azure Storage Emulator
    StorageEmulatorAccountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="

    blobServiceName  = "blob"
    tableServiceName = "table"
    queueServiceName = "queue"
    fileServiceName  = "file"

    storageEmulatorBlob  = "127.0.0.1:10000"
    storageEmulatorTable = "127.0.0.1:10002"
    storageEmulatorQueue = "127.0.0.1:10001"
)

// Client is the object that needs to be constructed to perform
// operations on the storage account.
type Client struct {
    // HTTPClient is the http.Client used to initiate API
    // requests.  If it is nil, http.DefaultClient is used.
    HTTPClient *http.Client

    accountName string
    accountKey  []byte
    useHTTPS    bool
    baseURL     string
    apiVersion  string
}

type storageResponse struct {
    statusCode int
    headers    http.Header
    body       io.ReadCloser
}

type odataResponse struct {
    storageResponse
    odata odataErrorMessage
}

// AzureStorageServiceError contains fields of the error response from
// Azure Storage Service REST API. See https://msdn.microsoft.com/en-us/library/azure/dd179382.aspx
// Some fields might be specific to certain calls.
type AzureStorageServiceError struct {
    Code                      string `xml:"Code"`
    Message                   string `xml:"Message"`
    AuthenticationErrorDetail string `xml:"AuthenticationErrorDetail"`
    QueryParameterName        string `xml:"QueryParameterName"`
    QueryParameterValue       string `xml:"QueryParameterValue"`
    Reason                    string `xml:"Reason"`
    StatusCode                int
    RequestID                 string
}

type odataErrorMessageMessage struct {
    Lang  string `json:"lang"`
    Value string `json:"value"`
}

type odataErrorMessageInternal struct {
    Code    string                   `json:"code"`
    Message odataErrorMessageMessage `json:"message"`
}

type odataErrorMessage struct {
    Err odataErrorMessageInternal `json:"odata.error"`
}

// UnexpectedStatusCodeError is returned when a storage service responds with neither an error
// nor with an HTTP status code indicating success.
type UnexpectedStatusCodeError struct {
    allowed []int
    got     int
}

func (e UnexpectedStatusCodeError) Error() string {
    s := func(i int) string { return fmt.Sprintf("%d %s", i, http.StatusText(i)) }

    got := s(e.got)
    expected := []string{}
    for _, v := range e.allowed {
        expected = append(expected, s(v))
    }
    return fmt.Sprintf("storage: status code from service response is %s; was expecting %s", got, strings.Join(expected, " or "))
}

// Got is the actual status code returned by Azure.
func (e UnexpectedStatusCodeError) Got() int {
    return e.got
}

// NewBasicClient constructs a Client with given storage service name and
// key.
func NewBasicClient(accountName, accountKey string) (Client, error) {
    if accountName == StorageEmulatorAccountName {
        return NewEmulatorClient()
    }
    return NewClient(accountName, accountKey, DefaultBaseURL, DefaultAPIVersion, defaultUseHTTPS)
}

//NewEmulatorClient contructs a Client intended to only work with Azure
//Storage Emulator
func NewEmulatorClient() (Client, error) {
    return NewClient(StorageEmulatorAccountName, StorageEmulatorAccountKey, DefaultBaseURL, DefaultAPIVersion, false)
}

// NewClient constructs a Client. This should be used if the caller wants
// to specify whether to use HTTPS, a specific REST API version or a custom
// storage endpoint than Azure Public Cloud.
func NewClient(accountName, accountKey, blobServiceBaseURL, apiVersion string, useHTTPS bool) (Client, error) {
    var c Client
    if accountName == "" {
        return c, fmt.Errorf("azure: account name required")
    } else if accountKey == "" {
        return c, fmt.Errorf("azure: account key required")
    } else if blobServiceBaseURL == "" {
        return c, fmt.Errorf("azure: base storage service url required")
    }

    key, err := base64.StdEncoding.DecodeString(accountKey)
    if err != nil {
        return c, fmt.Errorf("azure: malformed storage account key: %v", err)
    }

    return Client{
        accountName: accountName,
        accountKey:  key,
        useHTTPS:    useHTTPS,
        baseURL:     blobServiceBaseURL,
        apiVersion:  apiVersion,
    }, nil
}

func (c Client) getBaseURL(service string) string {
    scheme := "http"
    if c.useHTTPS {
        scheme = "https"
    }
    host := ""
    if c.accountName == StorageEmulatorAccountName {
        switch service {
        case blobServiceName:
            host = storageEmulatorBlob
        case tableServiceName:
            host = storageEmulatorTable
        case queueServiceName:
            host = storageEmulatorQueue
        }
    } else {
        host = fmt.Sprintf("%s.%s.%s", c.accountName, service, c.baseURL)
    }

    u := &url.URL{
        Scheme: scheme,
        Host:   host}
    return u.String()
}

func (c Client) getEndpoint(service, path string, params url.Values) string {
    u, err := url.Parse(c.getBaseURL(service))
    if err != nil {
        // really should not be happening
        panic(err)
    }

    // API doesn't accept path segments not starting with '/'
    if !strings.HasPrefix(path, "/") {
        path = fmt.Sprintf("/%v", path)
    }

    if c.accountName == StorageEmulatorAccountName {
        path = fmt.Sprintf("/%v%v", StorageEmulatorAccountName, path)
    }

    u.Path = path
    u.RawQuery = params.Encode()
    return u.String()
}

// GetBlobService returns a BlobStorageClient which can operate on the blob
// service of the storage account.
func (c Client) GetBlobService() BlobStorageClient {
    return BlobStorageClient{c}
}

// GetQueueService returns a QueueServiceClient which can operate on the queue
// service of the storage account.
func (c Client) GetQueueService() QueueServiceClient {
    return QueueServiceClient{c}
}

// GetTableService returns a TableServiceClient which can operate on the table
// service of the storage account.
func (c Client) GetTableService() TableServiceClient {
    return TableServiceClient{c}
}

// GetFileService returns a FileServiceClient which can operate on the file
// service of the storage account.
func (c Client) GetFileService() FileServiceClient {
    return FileServiceClient{c}
}

func (c Client) createAuthorizationHeader(canonicalizedString string) string {
    signature := c.computeHmac256(canonicalizedString)
    return fmt.Sprintf("%s %s:%s", "SharedKey", c.getCanonicalizedAccountName(), signature)
}

func (c Client) getAuthorizationHeader(verb, url string, headers map[string]string) (string, error) {
    canonicalizedResource, err := c.buildCanonicalizedResource(url)
    if err != nil {
        return "", err
    }

    canonicalizedString := c.buildCanonicalizedString(verb, headers, canonicalizedResource)
    return c.createAuthorizationHeader(canonicalizedString), nil
}

func (c Client) getStandardHeaders() map[string]string {
    return map[string]string{
        "x-ms-version": c.apiVersion,
        "x-ms-date":    currentTimeRfc1123Formatted(),
    }
}

func (c Client) getCanonicalizedAccountName() string {
    // since we may be trying to access a secondary storage account, we need to
    // remove the -secondary part of the storage name
    return strings.TrimSuffix(c.accountName, "-secondary")
}

func (c Client) buildCanonicalizedHeader(headers map[string]string) string {
    cm := make(map[string]string)

    for k, v := range headers {
        headerName := strings.TrimSpace(strings.ToLower(k))
        match, _ := regexp.MatchString("x-ms-", headerName)
        if match {
            cm[headerName] = v
        }
    }

    if len(cm) == 0 {
        return ""
    }

    keys := make([]string, 0, len(cm))
    for key := range cm {
        keys = append(keys, key)
    }

    sort.Strings(keys)

    ch := ""

    for i, key := range keys {
        if i == len(keys)-1 {
            ch += fmt.Sprintf("%s:%s", key, cm[key])
        } else {
            ch += fmt.Sprintf("%s:%s\n", key, cm[key])
        }
    }
    return ch
}

func (c Client) buildCanonicalizedResourceTable(uri string) (string, error) {
    errMsg := "buildCanonicalizedResourceTable error: %s"
    u, err := url.Parse(uri)
    if err != nil {
        return "", fmt.Errorf(errMsg, err.Error())
    }

    cr := "/" + c.getCanonicalizedAccountName()

    if len(u.Path) > 0 {
        cr += u.Path
    }

    return cr, nil
}

func (c Client) buildCanonicalizedResource(uri string) (string, error) {
    errMsg := "buildCanonicalizedResource error: %s"
    u, err := url.Parse(uri)
    if err != nil {
        return "", fmt.Errorf(errMsg, err.Error())
    }

    cr := "/" + c.getCanonicalizedAccountName()

    if len(u.Path) > 0 {
        // Any portion of the CanonicalizedResource string that is derived from
        // the resource's URI should be encoded exactly as it is in the URI.
        // -- https://msdn.microsoft.com/en-gb/library/azure/dd179428.aspx
        cr += u.EscapedPath()
    }

    params, err := url.ParseQuery(u.RawQuery)
    if err != nil {
        return "", fmt.Errorf(errMsg, err.Error())
    }

    if len(params) > 0 {
        cr += "\n"
        keys := make([]string, 0, len(params))
        for key := range params {
            keys = append(keys, key)
        }

        sort.Strings(keys)

        for i, key := range keys {
            if len(params[key]) > 1 {
                sort.Strings(params[key])
            }

            if i == len(keys)-1 {
                cr += fmt.Sprintf("%s:%s", key, strings.Join(params[key], ","))
            } else {
                cr += fmt.Sprintf("%s:%s\n", key, strings.Join(params[key], ","))
            }
        }
    }

    return cr, nil
}

func (c Client) buildCanonicalizedString(verb string, headers map[string]string, canonicalizedResource string) string {
    contentLength := headers["Content-Length"]
    if contentLength == "0" {
        contentLength = ""
    }
    canonicalizedString := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
        verb,
        headers["Content-Encoding"],
        headers["Content-Language"],
        contentLength,
        headers["Content-MD5"],
        headers["Content-Type"],
        headers["Date"],
        headers["If-Modified-Since"],
        headers["If-Match"],
        headers["If-None-Match"],
        headers["If-Unmodified-Since"],
        headers["Range"],
        c.buildCanonicalizedHeader(headers),
        canonicalizedResource)

    return canonicalizedString
}

func (c Client) exec(verb, url string, headers map[string]string, body io.Reader) (*storageResponse, error) {
    authHeader, err := c.getAuthorizationHeader(verb, url, headers)
    if err != nil {
        return nil, err
    }
    headers["Authorization"] = authHeader
    if err != nil {
        return nil, err
    }

    req, err := http.NewRequest(verb, url, body)
    if err != nil {
        return nil, errors.New("azure/storage: error creating request: " + err.Error())
    }

    if clstr, ok := headers["Content-Length"]; ok {
        // content length header is being signed, but completely ignored by golang.
        // instead we have to use the ContentLength property on the request struct
        // (see https://golang.org/src/net/http/request.go?s=18140:18370#L536 and
        // https://golang.org/src/net/http/transfer.go?s=1739:2467#L49)
        req.ContentLength, err = strconv.ParseInt(clstr, 10, 64)
        if err != nil {
            return nil, err
        }
    }
    for k, v := range headers {
        req.Header.Add(k, v)
    }

    httpClient := c.HTTPClient
    if httpClient == nil {
        httpClient = http.DefaultClient
    }
    resp, err := httpClient.Do(req)
    if err != nil {
        return nil, err
    }

    statusCode := resp.StatusCode
    if statusCode >= 400 && statusCode <= 505 {
        var respBody []byte
        respBody, err = readResponseBody(resp)
        if err != nil {
            return nil, err
        }

        if len(respBody) == 0 {
            // no error in response body
            err = fmt.Errorf("storage: service returned without a response body (%s)", resp.Status)
        } else {
            // response contains storage service error object, unmarshal
            storageErr, errIn := serviceErrFromXML(respBody, resp.StatusCode, resp.Header.Get("x-ms-request-id"))
            if err != nil { // error unmarshaling the error response
                err = errIn
            }
            err = storageErr
        }
        return &storageResponse{
            statusCode: resp.StatusCode,
            headers:    resp.Header,
            body:       ioutil.NopCloser(bytes.NewReader(respBody)), /* restore the body */
        }, err
    }

    return &storageResponse{
        statusCode: resp.StatusCode,
        headers:    resp.Header,
        body:       resp.Body}, nil
}

func (c Client) execInternalJSON(verb, url string, headers map[string]string, body io.Reader) (*odataResponse, error) {
    req, err := http.NewRequest(verb, url, body)
    for k, v := range headers {
        req.Header.Add(k, v)
    }

    httpClient := c.HTTPClient
    if httpClient == nil {
        httpClient = http.DefaultClient
    }

    resp, err := httpClient.Do(req)
    if err != nil {
        return nil, err
    }

    respToRet := &odataResponse{}
    respToRet.body = resp.Body
    respToRet.statusCode = resp.StatusCode
    respToRet.headers = resp.Header

    statusCode := resp.StatusCode
    if statusCode >= 400 && statusCode <= 505 {
        var respBody []byte
        respBody, err = readResponseBody(resp)
        if err != nil {
            return nil, err
        }

        if len(respBody) == 0 {
            // no error in response body
            err = fmt.Errorf("storage: service returned without a response body (%d)", resp.StatusCode)
            return respToRet, err
        }
        // try unmarshal as odata.error json
        err = json.Unmarshal(respBody, &respToRet.odata)
        return respToRet, err
    }

    return respToRet, nil
}

func (c Client) createSharedKeyLite(url string, headers map[string]string) (string, error) {
    can, err := c.buildCanonicalizedResourceTable(url)

    if err != nil {
        return "", err
    }
    strToSign := headers["x-ms-date"] + "\n" + can

    hmac := c.computeHmac256(strToSign)
    return fmt.Sprintf("SharedKeyLite %s:%s", c.accountName, hmac), nil
}

func (c Client) execTable(verb, url string, headers map[string]string, body io.Reader) (*odataResponse, error) {
    var err error
    headers["Authorization"], err = c.createSharedKeyLite(url, headers)
    if err != nil {
        return nil, err
    }

    return c.execInternalJSON(verb, url, headers, body)
}

func readResponseBody(resp *http.Response) ([]byte, error) {
    defer resp.Body.Close()
    out, err := ioutil.ReadAll(resp.Body)
    if err == io.EOF {
        err = nil
    }
    return out, err
}

func serviceErrFromXML(body []byte, statusCode int, requestID string) (AzureStorageServiceError, error) {
    var storageErr AzureStorageServiceError
    if err := xml.Unmarshal(body, &storageErr); err != nil {
        return storageErr, err
    }
    storageErr.StatusCode = statusCode
    storageErr.RequestID = requestID
    return storageErr, nil
}

func (e AzureStorageServiceError) Error() string {
    return fmt.Sprintf("storage: service returned error: StatusCode=%d, ErrorCode=%s, ErrorMessage=%s, RequestId=%s, QueryParameterName=%s, QueryParameterValue=%s",
        e.StatusCode, e.Code, e.Message, e.RequestID, e.QueryParameterName, e.QueryParameterValue)
}

// checkRespCode returns UnexpectedStatusError if the given response code is not
// one of the allowed status codes; otherwise nil.
func checkRespCode(respCode int, allowed []int) error {
    for _, v := range allowed {
        if respCode == v {
            return nil
        }
    }
    return UnexpectedStatusCodeError{allowed, respCode}
}