-
Notifications
You must be signed in to change notification settings - Fork 43
/
sas_token.go
226 lines (189 loc) · 7.1 KB
/
sas_token.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package storage
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/url"
"strings"
"github.com/Azure/go-autorest/autorest/azure"
)
const (
connStringAccountKeyKey = "AccountKey"
connStringAccountNameKey = "AccountName"
blobContainerSignedVersion = "2018-11-09"
)
// ComputeAccountSASToken computes the SAS Token for a Storage Account based on the
// access key & given permissions
// See: https://docs.microsoft.com/en-us/rest/api/storageservices/create-account-sas
func ComputeAccountSASToken(accountName string,
accountKey string,
permissions string,
services string,
resourceTypes string,
start string,
expiry string,
signedProtocol string,
signedIp string, // nolint: unparam
signedVersion string, // nolint: unparam
signedEncryptionScope string, // nolint: unparam
) (string, error) {
// UTF-8 by default...
stringToSign := accountName + "\n"
stringToSign += permissions + "\n"
stringToSign += services + "\n"
stringToSign += resourceTypes + "\n"
stringToSign += start + "\n"
stringToSign += expiry + "\n"
stringToSign += signedIp + "\n"
stringToSign += signedProtocol + "\n"
stringToSign += signedVersion + "\n"
if signedVersion >= "2020-12-06" {
stringToSign += signedEncryptionScope + "\n"
}
binaryKey, err := base64.StdEncoding.DecodeString(accountKey)
if err != nil {
return "", err
}
hasher := hmac.New(sha256.New, binaryKey)
hasher.Write([]byte(stringToSign))
signature := hasher.Sum(nil)
// Trial and error to determine which fields the Azure portal
// URL encodes for a query string and which it does not.
sasToken := "?sv=" + url.QueryEscape(signedVersion)
sasToken += "&ss=" + url.QueryEscape(services)
sasToken += "&srt=" + url.QueryEscape(resourceTypes)
sasToken += "&sp=" + url.QueryEscape(permissions)
sasToken += "&se=" + (expiry)
sasToken += "&st=" + (start)
sasToken += "&spr=" + (signedProtocol)
// this is consistent with how the Azure portal builds these.
if len(signedIp) > 0 {
sasToken += "&sip=" + signedIp
}
sasToken += "&sig=" + url.QueryEscape(base64.StdEncoding.EncodeToString(signature))
return sasToken, nil
}
// ComputeAccountSASConnectionString computes the composed SAS Connection String for a Storage Account based on the
// sas token
func ComputeAccountSASConnectionString(env *azure.Environment, accountName string, sasToken string) string {
return fmt.Sprintf(
"BlobEndpoint=https://%[1]s.blob.%[2]s/;"+
"FileEndpoint=https://%[1]s.file.%[2]s/;"+
"QueueEndpoint=https://%[1]s.queue.%[2]s/;"+
"TableEndpoint=https://%[1]s.table.%[2]s/;"+
"SharedAccessSignature=%[3]s", accountName, env.StorageEndpointSuffix, sasToken[1:]) // need to cut the first character '?' from the sas token
}
// ComputeAccountSASConnectionUrlForType computes the SAS Connection String for a Storage Account based on the
// sas token and the storage type
func ComputeAccountSASConnectionUrlForType(env *azure.Environment, accountName string, sasToken string, storageType string) (*string, error) {
if !strings.EqualFold(storageType, "blob") && !strings.EqualFold(storageType, "file") && !strings.EqualFold(storageType, "queue") && !strings.EqualFold(storageType, "table") {
return nil, fmt.Errorf("Unexpected storage type %s!", storageType)
}
url := fmt.Sprintf("https://%s.%s.%s%s", accountName, strings.ToLower(storageType), env.StorageEndpointSuffix, sasToken)
return &url, nil
}
func ComputeContainerSASToken(signedPermissions string,
signedStart string,
signedExpiry string,
accountName string,
accountKey string,
containerName string,
signedIdentifier string,
signedIp string,
signedProtocol string,
signedSnapshotTime string,
cacheControl string,
contentDisposition string,
contentEncoding string,
contentLanguage string,
contentType string,
) (string, error) {
canonicalizedResource := "/blob/" + accountName + "/" + containerName
signedVersion := blobContainerSignedVersion
signedResource := "c" // c for container
// UTF-8 by default...
stringToSign := signedPermissions + "\n"
stringToSign += signedStart + "\n"
stringToSign += signedExpiry + "\n"
stringToSign += canonicalizedResource + "\n"
stringToSign += signedIdentifier + "\n"
stringToSign += signedIp + "\n"
stringToSign += signedProtocol + "\n"
stringToSign += signedVersion + "\n"
stringToSign += signedResource + "\n"
stringToSign += signedSnapshotTime + "\n"
stringToSign += cacheControl + "\n"
stringToSign += contentDisposition + "\n"
stringToSign += contentEncoding + "\n"
stringToSign += contentLanguage + "\n"
stringToSign += contentType
binaryKey, err := base64.StdEncoding.DecodeString(accountKey)
if err != nil {
return "", err
}
hasher := hmac.New(sha256.New, binaryKey)
hasher.Write([]byte(stringToSign))
signature := hasher.Sum(nil)
sasToken := "?sv=" + signedVersion
sasToken += "&sr=" + signedResource
sasToken += "&st=" + signedStart
sasToken += "&se=" + signedExpiry
sasToken += "&sp=" + signedPermissions
if len(signedIp) > 0 {
sasToken += "&sip=" + signedIp
}
if len(signedProtocol) > 0 {
sasToken += "&spr=" + signedProtocol
}
if len(signedIdentifier) > 0 {
sasToken += "&si=" + signedIdentifier
}
if len(cacheControl) > 0 {
sasToken += "&rscc=" + url.QueryEscape(cacheControl)
}
if len(contentDisposition) > 0 {
sasToken += "&rscd=" + url.QueryEscape(contentDisposition)
}
if len(contentEncoding) > 0 {
sasToken += "&rsce=" + url.QueryEscape(contentEncoding)
}
if len(contentLanguage) > 0 {
sasToken += "&rscl=" + url.QueryEscape(contentLanguage)
}
if len(contentType) > 0 {
sasToken += "&rsct=" + url.QueryEscape(contentType)
}
sasToken += "&sig=" + url.QueryEscape(base64.StdEncoding.EncodeToString(signature))
return sasToken, nil
}
// ParseAccountSASConnectionString parses the Connection String for a Storage Account
func ParseAccountSASConnectionString(connString string) (map[string]string, error) {
// This connection string was for a real storage account which has been deleted
// so its safe to include here for reference to understand the format.
// DefaultEndpointsProtocol=https;AccountName=azurermtestsa0;AccountKey=2vJrjEyL4re2nxCEg590wJUUC7PiqqrDHjAN5RU304FNUQieiEwS2bfp83O0v28iSfWjvYhkGmjYQAdd9x+6nw==;EndpointSuffix=core.windows.net
validKeys := map[string]bool{"DefaultEndpointsProtocol": true, "BlobEndpoint": true,
"AccountName": true, "AccountKey": true, "EndpointSuffix": true}
// The k-v pairs are separated with semi-colons
tokens := strings.Split(connString, ";")
kvp := make(map[string]string)
for _, atoken := range tokens {
// The individual k-v are separated by an equals sign.
kv := strings.SplitN(atoken, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("[ERROR] token `%s` is an invalid key=pair (connection string %s)", atoken, connString)
}
key := kv[0]
val := kv[1]
if _, present := validKeys[key]; !present {
return nil, fmt.Errorf("[ERROR] Unknown Key `%s` in connection string %s", key, connString)
}
kvp[key] = val
}
if _, present := kvp[connStringAccountKeyKey]; !present {
return nil, fmt.Errorf("[ERROR] Storage Account Key not found in connection string: %s", connString)
}
return kvp, nil
}