-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
QueueSasBuilder.cs
340 lines (311 loc) · 13.5 KB
/
QueueSasBuilder.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Azure.Core;
using Azure.Storage.Queues;
namespace Azure.Storage.Sas
{
/// <summary>
/// <see cref="QueueSasBuilder"/> is used to generate a Shared Access
/// Signature (SAS) for an Azure Storage queue.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas">
/// Create a Service SAS</see>.
/// </summary>
public class QueueSasBuilder
{
/// <summary>
/// The storage service version to use to authenticate requests made
/// with this shared access signature, and the service version to use
/// when handling requests made with this shared access signature.
/// </summary>
/// <remarks>
/// This property has been deprecated and we will always use the latest
/// storage SAS version of the Storage service supported. This change
/// does not have any impact on how your application generates or makes
/// use of SAS tokens.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public string Version { get; set; }
/// <summary>
/// The optional signed protocol field specifies the protocol
/// permitted for a request made with the SAS. Possible values are
/// <see cref="SasProtocol.HttpsAndHttp"/>,
/// <see cref="SasProtocol.Https"/>, and
/// <see cref="SasProtocol.None"/>.
/// </summary>
public SasProtocol Protocol { get; set; }
/// <summary>
/// Optionally specify the time at which the shared access signature
/// becomes valid. If omitted when DateTimeOffset.MinValue is used,
/// start time for this call is assumed to be the time when the
/// storage service receives the request.
/// </summary>
public DateTimeOffset StartsOn { get; set; }
/// <summary>
/// The time at which the shared access signature becomes invalid.
/// This field must be omitted if it has been specified in an
/// associated stored access policy.
/// </summary>
public DateTimeOffset ExpiresOn { get; set; }
/// <summary>
/// The permissions associated with the shared access signature. The
/// user is restricted to operations allowed by the permissions. This
/// field must be omitted if it has been specified in an associated
/// stored access policy. <see cref="QueueSasPermissions"/> and
/// <see cref="QueueAccountSasPermissions"/> can be used to create the
/// permissions string.
/// </summary>
public string Permissions { get; private set; }
/// <summary>
/// Specifies an IP address or a range of IP addresses from which to
/// accept requests. If the IP address from which the request
/// originates does not match the IP address or address range
/// specified on the SAS token, the request is not authenticated.
/// When specifying a range of IP addresses, note that the range is
/// inclusive.
/// </summary>
public SasIPRange IPRange { get; set; }
/// <summary>
/// An optional unique value up to 64 characters in length that
/// correlates to an access policy specified for the container.
/// </summary>
public string Identifier { get; set; }
/// <summary>
/// The optional name of the blob being made accessible.
/// </summary>
public string QueueName { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="QueueSasBuilder"/>
/// class.
/// </summary>
/// <remarks>
/// This constructor has been deprecated. Please consider using
/// <see cref="QueueSasBuilder(QueueSasPermissions, DateTimeOffset)"/>
/// to create a Service SAS or
/// <see cref="QueueSasBuilder(QueueAccountSasPermissions, DateTimeOffset)"/>
/// to create an Account SAS. This change does not have any impact on how
/// your application generates or makes use of SAS tokens.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public QueueSasBuilder()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="QueueSasBuilder"/>
/// class.
/// </summary>
/// <param name="permissions">
/// The permissions associated with the shared access signature.
/// The user is restricted to operations allowed by the permissions.
/// This field must be omitted if it has been specified in an
/// associated stored access policy.
/// </param>
/// <param name="expiresOn">
/// The time at which the shared access signature becomes invalid.
/// This field must be omitted if it has been specified in an
/// associated stored access policy.
/// </param>
public QueueSasBuilder(QueueSasPermissions permissions, DateTimeOffset expiresOn)
{
ExpiresOn = expiresOn;
SetPermissions(permissions);
}
/// <summary>
/// Initializes a new instance of the <see cref="QueueSasBuilder"/>
/// class.
/// </summary>
/// <param name="permissions">
/// The permissions associated with the shared access signature.
/// The user is restricted to operations allowed by the permissions.
/// This field must be omitted if it has been specified in an
/// associated stored access policy.
/// </param>
/// <param name="expiresOn">
/// The time at which the shared access signature becomes invalid.
/// This field must be omitted if it has been specified in an
/// associated stored access policy.
/// </param>
public QueueSasBuilder(QueueAccountSasPermissions permissions, DateTimeOffset expiresOn)
{
ExpiresOn = expiresOn;
SetPermissions(permissions);
}
/// <summary>
/// Sets the permissions for a queue SAS.
/// </summary>
/// <param name="permissions">
/// <see cref="QueueSasPermissions"/> containing the allowed permissions.
/// </param>
public void SetPermissions(QueueSasPermissions permissions)
{
Permissions = permissions.ToPermissionsString();
}
/// <summary>
/// Sets the permissions for a queue account level SAS.
/// </summary>
/// <param name="permissions">
/// <see cref="QueueAccountSasPermissions"/> containing the allowed permissions.
/// </param>
public void SetPermissions(QueueAccountSasPermissions permissions)
{
Permissions = permissions.ToPermissionsString();
}
/// <summary>
/// Sets the permissions for the SAS using a raw permissions string.
/// </summary>
/// <param name="rawPermissions">
/// Raw permissions string for the SAS.
/// </param>
/// <param name="normalize">
/// If the permissions should be validated and correctly ordered.
/// </param>
public void SetPermissions(
string rawPermissions,
bool normalize = default)
{
if (normalize)
{
rawPermissions = SasExtensions.ValidateAndSanitizeRawPermissions(
permissions: rawPermissions,
validPermissionsInOrder: s_validPermissionsInOrder);
}
SetPermissions(rawPermissions);
}
/// <summary>
/// Sets the permissions for the SAS using a raw permissions string.
/// </summary>
/// <param name="rawPermissions">Raw permissions string for the SAS.</param>
public void SetPermissions(string rawPermissions)
{
Permissions = rawPermissions;
}
private static readonly List<char> s_validPermissionsInOrder = new List<char>
{
Constants.Sas.Permissions.Read,
Constants.Sas.Permissions.Write,
Constants.Sas.Permissions.Delete,
Constants.Sas.Permissions.DeleteBlobVersion,
Constants.Sas.Permissions.List,
Constants.Sas.Permissions.Add,
Constants.Sas.Permissions.Create,
Constants.Sas.Permissions.Update,
Constants.Sas.Permissions.Process,
Constants.Sas.Permissions.Tag,
Constants.Sas.Permissions.FilterByTags,
};
/// <summary>
/// Use an account's <see cref="StorageSharedKeyCredential"/> to sign this
/// shared access signature values to produce the proper SAS query
/// parameters for authenticating requests.
/// </summary>
/// <param name="sharedKeyCredential">
/// The storage account's <see cref="StorageSharedKeyCredential"/>.
/// </param>
/// <returns>
/// The <see cref="SasQueryParameters"/> used for authenticating
/// requests.
/// </returns>
[CallerShouldAudit("https://aka.ms/azsdk/callershouldaudit/storage-queues")]
public SasQueryParameters ToSasQueryParameters(StorageSharedKeyCredential sharedKeyCredential)
{
sharedKeyCredential = sharedKeyCredential ?? throw Errors.ArgumentNull(nameof(sharedKeyCredential));
EnsureState();
var startTime = SasExtensions.FormatTimesForSasSigning(StartsOn);
var expiryTime = SasExtensions.FormatTimesForSasSigning(ExpiresOn);
// String to sign: http://msdn.microsoft.com/en-us/library/azure/dn140255.aspx
var stringToSign = string.Join("\n",
Permissions,
startTime,
expiryTime,
GetCanonicalName(sharedKeyCredential.AccountName, QueueName ?? string.Empty),
Identifier,
IPRange.ToString(),
SasExtensions.ToProtocolString(Protocol),
Version);
var signature = StorageSharedKeyCredentialInternals.ComputeSasSignature(sharedKeyCredential, stringToSign);
var p = SasQueryParametersInternals.Create(
version: Version,
services: default,
resourceTypes: default,
protocol: Protocol,
startsOn: StartsOn,
expiresOn: ExpiresOn,
ipRange: IPRange,
identifier: Identifier,
resource: null,
permissions: Permissions,
signature: signature);
return p;
}
/// <summary>
/// Computes the canonical name for a queue resource for SAS signing.
/// </summary>
/// <param name="account">
/// Account.
/// </param>
/// <param name="queueName">
/// Name of queue.
/// </param>
/// <returns>
/// Canonical name as a string.
/// </returns>
private static string GetCanonicalName(string account, string queueName) =>
// Queue: "/queue/account/queuename"
string.Join("", new[] { "/queue/", account, "/", queueName });
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>A string that represents the current object.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();
/// <summary>
/// Check if two QueueSasBuilder instances are equal.
/// </summary>
/// <param name="obj">The instance to compare to.</param>
/// <returns>True if they're equal, false otherwise.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => base.Equals(obj);
/// <summary>
/// Get a hash code for the QueueSasBuilder.
/// </summary>
/// <returns>Hash code for the QueueSasBuilder.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
/// <summary>
/// Ensure the <see cref="QueueSasBuilder"/>'s properties are in a
/// consistent state.
/// </summary>
private void EnsureState()
{
if (Identifier == default)
{
if (ExpiresOn == default)
{
throw Errors.SasMissingData(nameof(ExpiresOn));
}
if (string.IsNullOrEmpty(Permissions))
{
throw Errors.SasMissingData(nameof(Permissions));
}
}
Version = SasQueryParametersInternals.DefaultSasVersionInternal;
}
internal static QueueSasBuilder DeepCopy(QueueSasBuilder originalQueueSasBuilder)
=> new QueueSasBuilder
{
Version = originalQueueSasBuilder.Version,
Protocol = originalQueueSasBuilder.Protocol,
StartsOn = originalQueueSasBuilder.StartsOn,
ExpiresOn = originalQueueSasBuilder.ExpiresOn,
Permissions = originalQueueSasBuilder.Permissions,
IPRange = originalQueueSasBuilder.IPRange,
Identifier = originalQueueSasBuilder.Identifier,
QueueName = originalQueueSasBuilder.QueueName
};
}
}