-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathnewe2e_sas_generation.go
253 lines (224 loc) · 8.79 KB
/
newe2e_sas_generation.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
package e2etest
import (
blobsas "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas"
datalakesas "github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake/sas"
filesas "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/sas"
"github.com/Azure/azure-storage-azcopy/v10/cmd"
"github.com/Azure/azure-storage-azcopy/v10/common"
"time"
)
// BlobSignatureValues makes both account/service signature values generic for output from GenericSignatureValues
type BlobSignatureValues interface {
SignWithSharedKey(credential *blobsas.SharedKeyCredential) (blobsas.QueryParameters, error)
}
// FileSignatureValues makes both account/service signature values generic for output from GenericSignatureValues
type FileSignatureValues interface {
SignWithSharedKey(credential *filesas.SharedKeyCredential) (filesas.QueryParameters, error)
}
// DatalakeSignatureValues makes both account/service signature values generic for output from GenericSignatureValues
type DatalakeSignatureValues interface {
SignWithSharedKey(credential *datalakesas.SharedKeyCredential) (datalakesas.QueryParameters, error)
}
// GenericSignatureValues are a set of signature values that are portable to all 3 Azure storage services and both service and account level SAS.
// Distinct limitations of these generic values are documented in the struct.
type GenericSignatureValues interface {
AsBlob() BlobSignatureValues
AsFile() FileSignatureValues
AsDatalake() DatalakeSignatureValues
}
// GenericServiceSignatureValues is a generic struct encompassing the possible values for Blob, Files, and Datalake service SAS tokens.
// Check the comments within the struct for info about defaults or valid values for each service.
type GenericServiceSignatureValues struct {
// Gets defaulted by the relevant SDK to the SDK version
Version string
// Protocol defaults to HTTPS.
Protocol blobsas.Protocol
// StartTime, if unspecified, is this moment.
StartTime time.Time
// ExpiryTime, if unspecified, is 24 hours from StartTime.
ExpiryTime time.Time
// SnapshotTime is unused on datalake, but refers to the blob snapshot time or the file share snapshot time.
SnapshotTime time.Time
// BlobVersion is unique to Blob.
BlobVersion string
// Permissions should be supplied by calling .String() on any of the below structs.
// Blob and Datalake overlap nicely
// blobsas.ContainerPermissions blobsas.BlobPermissions
// filesas.SharePermissions filesas.FilePermissions
// datalakesas.FileSystemPermissions datalakesas.FilePermissions datalakesas.DirectoryPermissions
// If zero, defaults to racwdl (read, add, create, write, delete, list
Permissions string
IPRange blobsas.IPRange
Identifier string
ContainerName string
ObjectName string
// DirectoryPath is used on Blob & Datalake, and is intended to limit to a particular subdirectory.
// On Files, replaces ObjectName if present.
DirectoryPath string
CacheControl string
ContentDisposition string
ContentEncoding string
ContentLanguage string
ContentType string
AuthorizedObjectID string
UnauthorizedObjectID string
// CorrelationID is used on Blob & Datalake
CorrelationID string
Level *cmd.LocationLevel
}
// withDefaults will never have to be called by
func (vals GenericServiceSignatureValues) withDefaults() GenericServiceSignatureValues {
out := vals
SetIfZero(&out.Protocol, blobsas.ProtocolHTTPS)
SetIfZero(&out.StartTime, time.Now())
SetIfZero(&out.ExpiryTime, out.StartTime.Add(time.Hour*24))
SetIfZero(&out.Permissions, (&blobsas.ContainerPermissions{
Read: true, Add: true, Create: true, Write: true, Delete: true, List: true,
}).String())
return out
}
func (vals GenericServiceSignatureValues) AsBlob() BlobSignatureValues {
s := vals.withDefaults()
blobName := s.ObjectName
directoryName := s.DirectoryPath
if s.Level != nil && *s.Level != cmd.ELocationLevel.Object() {
blobName = ""
directoryName = ""
}
return &blobsas.BlobSignatureValues{
Version: s.Version,
Protocol: s.Protocol,
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
SnapshotTime: s.SnapshotTime,
Permissions: s.Permissions,
IPRange: s.IPRange,
Identifier: s.Identifier,
ContainerName: s.ContainerName,
BlobName: blobName,
Directory: directoryName,
CacheControl: s.CacheControl,
ContentDisposition: s.ContentDisposition,
ContentEncoding: s.ContentEncoding,
ContentLanguage: s.ContentLanguage,
ContentType: s.ContentType,
BlobVersion: s.BlobVersion,
AuthorizedObjectID: s.AuthorizedObjectID,
UnauthorizedObjectID: s.UnauthorizedObjectID,
CorrelationID: s.CorrelationID,
}
}
func (vals GenericServiceSignatureValues) AsFile() FileSignatureValues {
s := vals.withDefaults()
filePath := common.Iff(s.DirectoryPath != "", s.DirectoryPath, s.ObjectName)
if s.Level != nil && *s.Level != cmd.ELocationLevel.Object() {
filePath = ""
}
return &filesas.SignatureValues{
Version: s.Version,
Protocol: filesas.Protocol(s.Protocol),
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
SnapshotTime: s.SnapshotTime,
Permissions: s.Permissions,
IPRange: filesas.IPRange(s.IPRange),
Identifier: s.Identifier,
ShareName: s.ContainerName,
FilePath: filePath,
CacheControl: s.CacheControl,
ContentDisposition: s.ContentDisposition,
ContentEncoding: s.ContentEncoding,
ContentLanguage: s.ContentLanguage,
ContentType: s.ContentType,
}
}
func (vals GenericServiceSignatureValues) AsDatalake() DatalakeSignatureValues {
s := vals.withDefaults()
filePath := s.ObjectName
directoryPath := s.DirectoryPath
if s.Level != nil && *s.Level != cmd.ELocationLevel.Object() {
filePath = ""
directoryPath = ""
}
return &datalakesas.DatalakeSignatureValues{
Version: s.Version,
Protocol: datalakesas.Protocol(s.Protocol),
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
Permissions: s.Permissions,
IPRange: datalakesas.IPRange(s.IPRange),
Identifier: s.Identifier,
FileSystemName: s.ContainerName,
FilePath: filePath,
DirectoryPath: directoryPath,
CacheControl: s.CacheControl,
ContentDisposition: s.ContentDisposition,
ContentEncoding: s.ContentEncoding,
ContentLanguage: s.ContentLanguage,
ContentType: s.ContentType,
AuthorizedObjectID: s.AuthorizedObjectID,
UnauthorizedObjectID: s.UnauthorizedObjectID,
CorrelationID: s.CorrelationID,
}
}
type GenericAccountSignatureValues struct {
Version string
// Defaults to HTTPS
Protocol blobsas.Protocol
// Defaults to now
StartTime time.Time
// Defaults to 24hr past StartTime
ExpiryTime time.Time
// Defaults to racwdl, uses blobsas.AccountPermissions, filesas.AccountPermissions, or datalakesas.AccountPermissions
Permissions string
IPRange blobsas.IPRange
// defaults to sco, uses blobsas.AccountResourceTypes, filesas.AccountResourceTypes, or datalakesas.AccountResourceTypes
ResourceTypes string
}
func (vals GenericAccountSignatureValues) withDefaults() GenericAccountSignatureValues {
out := vals
SetIfZero(&out.Protocol, blobsas.ProtocolHTTPS)
SetIfZero(&out.StartTime, time.Now())
SetIfZero(&out.ExpiryTime, out.StartTime.Add(time.Hour*24))
SetIfZero(&out.Permissions, (&blobsas.AccountPermissions{
Read: true, Add: true, Create: true, Write: true, Delete: true, List: true,
}).String())
SetIfZero(&out.ResourceTypes, (&blobsas.AccountResourceTypes{Service: true, Container: true, Object: true}).String())
return out
}
func (vals GenericAccountSignatureValues) AsBlob() BlobSignatureValues {
s := vals.withDefaults()
return blobsas.AccountSignatureValues{
Version: s.Version,
Protocol: s.Protocol,
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
Permissions: s.Permissions,
IPRange: s.IPRange,
ResourceTypes: s.ResourceTypes,
}
}
func (vals GenericAccountSignatureValues) AsFile() FileSignatureValues {
s := vals.withDefaults()
return filesas.AccountSignatureValues{
Version: s.Version,
Protocol: filesas.Protocol(s.Protocol),
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
Permissions: s.Permissions,
IPRange: filesas.IPRange(s.IPRange),
ResourceTypes: s.ResourceTypes,
}
}
func (vals GenericAccountSignatureValues) AsDatalake() DatalakeSignatureValues {
s := vals.withDefaults()
return datalakesas.AccountSignatureValues{
Version: s.Version,
Protocol: datalakesas.Protocol(s.Protocol),
StartTime: s.StartTime,
ExpiryTime: s.ExpiryTime,
Permissions: s.Permissions,
IPRange: datalakesas.IPRange(s.IPRange),
ResourceTypes: s.ResourceTypes,
}
}