| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/hmac" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "fmt" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | // NOTE: this all came from the Azure SDK for Go, but has been refactored to aid creating a Preparer. |
| 17 | |
| 18 | // buildCanonicalizedHeader builds the Canonicalized Header required to sign Storage Requests |
| 19 | func buildCanonicalizedHeader(headers http.Header) string { |
| 20 | cm := make(map[string]string) |
| 21 | |
| 22 | for k, v := range headers { |
| 23 | headerName := strings.TrimSpace(strings.ToLower(k)) |
| 24 | if strings.HasPrefix(headerName, "x-ms-") { |
| 25 | cm[headerName] = v[0] |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if len(cm) == 0 { |
| 30 | return "" |
| 31 | } |
| 32 | |
| 33 | var keys []string |
| 34 | for key := range cm { |
| 35 | keys = append(keys, key) |
| 36 | } |
| 37 | |
| 38 | sort.Strings(keys) |
| 39 | |
| 40 | ch := bytes.NewBufferString("") |
| 41 | |
| 42 | for _, key := range keys { |
| 43 | ch.WriteString(key) |
| 44 | ch.WriteRune(':') |
| 45 | ch.WriteString(cm[key]) |
| 46 | ch.WriteRune('\n') |
| 47 | } |
| 48 | |
| 49 | return strings.TrimSuffix(string(ch.Bytes()), "\n") |
| 50 | } |
| 51 | |
| 52 | // buildCanonicalizedResource builds the Canonical Resource required for to sign Storage Account requests |
| 53 | func buildCanonicalizedResource(uri, accountName string) (*string, error) { |
| 54 | u, err := url.Parse(uri) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | cr := bytes.NewBufferString("") |
| 60 | if accountName != StorageEmulatorAccountName { |
| 61 | cr.WriteString("/") |
| 62 | cr.WriteString(primaryStorageAccountName(accountName)) |
| 63 | } |
| 64 | |
| 65 | if len(u.Path) > 0 { |
| 66 | // Any portion of the CanonicalizedResource string that is derived from |
| 67 | // the resource's URI should be encoded exactly as it is in the URI. |
| 68 | // -- https://msdn.microsoft.com/en-gb/library/azure/dd179428.aspx |
| 69 | cr.WriteString(u.EscapedPath()) |
| 70 | } |
| 71 | |
| 72 | // TODO: replace this with less of a hack |
| 73 | if comp := u.Query().Get("comp"); comp != "" { |
| 74 | cr.WriteString(fmt.Sprintf("?comp=%s", comp)) |
| 75 | } |
| 76 | |
| 77 | out := string(cr.Bytes()) |
| 78 | return &out, nil |
| 79 | } |
| 80 | |
| 81 | func formatSharedKeyLiteAuthorizationHeader(accountName, key string) string { |
| 82 | canonicalizedAccountName := primaryStorageAccountName(accountName) |
| 83 | return fmt.Sprintf("SharedKeyLite %s:%s", canonicalizedAccountName, key) |
| 84 | } |
| 85 | |
| 86 | // hmacValue base-64 decodes the storageAccountKey, then signs the string with it |
| 87 | // as outlined here: https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key |
| 88 | func hmacValue(storageAccountKey, canonicalizedString string) string { |
| 89 | key, err := base64.StdEncoding.DecodeString(storageAccountKey) |
| 90 | if err != nil { |
| 91 | return "" |
| 92 | } |
| 93 | |
| 94 | encr := hmac.New(sha256.New, []byte(key)) |
| 95 | encr.Write([]byte(canonicalizedString)) |
| 96 | return base64.StdEncoding.EncodeToString(encr.Sum(nil)) |
| 97 | } |
| 98 | |
| 99 | // prepareHeadersForRequest prepares a request so that it can be signed |
| 100 | // by ensuring the `date` and `x-ms-date` headers are set |
| 101 | func prepareHeadersForRequest(r *http.Request) { |
| 102 | if r.Header == nil { |
| 103 | r.Header = http.Header{} |
| 104 | } |
| 105 | |
| 106 | date := time.Now().UTC().Format(http.TimeFormat) |
| 107 | |
| 108 | // a date must be set, X-Ms-Date should be used when both are set; but let's set both for completeness |
| 109 | r.Header.Set("date", date) |
| 110 | r.Header.Set("x-ms-date", date) |
| 111 | } |
| 112 | |
| 113 | // primaryStorageAccountName returns the name of the primary for a given Storage Account |
| 114 | func primaryStorageAccountName(input string) string { |
| 115 | // from https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key |
| 116 | // If you are accessing the secondary location in a storage account for which |
| 117 | // read-access geo-replication (RA-GRS) is enabled, do not include the |
| 118 | // -secondary designation in the authorization header. |
| 119 | // For authorization purposes, the account name is always the name of the primary location, |
| 120 | // even for secondary access. |
| 121 | return strings.TrimSuffix(input, "-secondary") |
| 122 | } |