/// <summary> /// Converts the permissions specified for the shared access policy to a string. /// </summary> /// <param name="permissions">A <see cref="SharedAccessQueuePermissions"/> object.</param> /// <returns>The shared access permissions in string format.</returns> public static string PermissionsToString(SharedAccessQueuePermissions permissions) { // The Queue service supports a fixed order => raup StringBuilder builder = new StringBuilder(); if ((permissions & SharedAccessQueuePermissions.Read) == SharedAccessQueuePermissions.Read) { builder.Append("r"); } if ((permissions & SharedAccessQueuePermissions.Add) == SharedAccessQueuePermissions.Add) { builder.Append("a"); } if ((permissions & SharedAccessQueuePermissions.Update) == SharedAccessQueuePermissions.Update) { builder.Append("u"); } if ((permissions & SharedAccessQueuePermissions.ProcessMessages) == SharedAccessQueuePermissions.ProcessMessages) { builder.Append("p"); } return builder.ToString(); }
/// <summary> /// Converts the permissions specified for the shared access policy to a string. /// </summary> /// <param name="permissions">A <see cref="SharedAccessQueuePermissions"/> object.</param> /// <returns>The shared access permissions in string format.</returns> public static string PermissionsToString(SharedAccessQueuePermissions permissions) { // The Queue service supports a fixed order => raup StringBuilder builder = new StringBuilder(); if ((permissions & SharedAccessQueuePermissions.Read) == SharedAccessQueuePermissions.Read) { builder.Append("r"); } if ((permissions & SharedAccessQueuePermissions.Add) == SharedAccessQueuePermissions.Add) { builder.Append("a"); } if ((permissions & SharedAccessQueuePermissions.Update) == SharedAccessQueuePermissions.Update) { builder.Append("u"); } if ((permissions & SharedAccessQueuePermissions.ProcessMessages) == SharedAccessQueuePermissions.ProcessMessages) { builder.Append("p"); } return(builder.ToString()); }
/// <summary> /// Gets the queue Uri with SAS. /// </summary> /// <param name="permissions">The permissions for a shared access signature associated with this shared access policy.</param> /// <param name="startTime">The start time for a shared access signature associated with this shared access policy.</param> /// <param name="endTime">The expiry time for a shared access signature associated with this shared access policy.</param> /// <returns>The queue Uri with SAS.</returns> public Uri GetQueueUriWithSas(SharedAccessQueuePermissions permissions = SharedAccessQueuePermissions.Read, DateTimeOffset?startTime = null, DateTimeOffset?endTime = null) { var uriBuilder = new UriBuilder(this._cloudQueue.Uri); uriBuilder.Query = this.GetQueueSas(permissions, startTime, endTime).TrimStart('?'); return(uriBuilder.Uri); }
public async Task QueueSASTestAsync() { CloudQueueClient client = GenerateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(GenerateNewQueueName()); await queue.CreateAsync(); string messageContent = Guid.NewGuid().ToString(); CloudQueueMessage message = new CloudQueueMessage(messageContent); await queue.AddMessageAsync(message); // Prepare SAS authentication with full permissions string id = Guid.NewGuid().ToString(); DateTime start = DateTime.UtcNow; DateTime expiry = start.AddMinutes(30); QueuePermissions permissions = new QueuePermissions(); SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update; permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy() { SharedAccessStartTime = start, SharedAccessExpiryTime = expiry, Permissions = queuePerm }); await queue.SetPermissionsAsync(permissions); await Task.Delay(30 * 1000); string sasTokenFromId = queue.GetSharedAccessSignature(null, id); StorageCredentials sasCredsFromId = new StorageCredentials(sasTokenFromId); CloudStorageAccount sasAcc = new CloudStorageAccount(sasCredsFromId, null /* blobEndpoint */, new Uri(TestBase.TargetTenantConfig.QueueServiceEndpoint), null /* tableEndpoint */, null /* fileEndpoint */); CloudQueueClient sasClient = sasAcc.CreateCloudQueueClient(); CloudQueue sasQueueFromSasUri = new CloudQueue(sasClient.Credentials.TransformUri(queue.Uri)); CloudQueueMessage receivedMessage = await sasQueueFromSasUri.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage.AsString); CloudQueue sasQueueFromSasUri1 = new CloudQueue(new Uri(queue.Uri.ToString() + sasTokenFromId)); CloudQueueMessage receivedMessage1 = await sasQueueFromSasUri1.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage1.AsString); CloudQueue sasQueueFromId = new CloudQueue(queue.Uri, sasCredsFromId); CloudQueueMessage receivedMessage2 = await sasQueueFromId.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage2.AsString); string sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null); StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy); CloudQueue sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy); CloudQueueMessage receivedMessage3 = await sasQueueFromPolicy.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage3.AsString); await queue.DeleteAsync(); }
/// <summary> /// Returns a shared access signature for the queue. /// </summary> /// <param name="permissions">The permissions for a shared access signature associated with this shared access policy.</param> /// <param name="startTime">The start time for a shared access signature associated with this shared access policy.</param> /// <param name="endTime">The expiry time for a shared access signature associated with this shared access policy.</param> /// <returns>The query string returned includes the leading question mark.</returns> public string GetQueueSas(SharedAccessQueuePermissions permissions = SharedAccessQueuePermissions.Read, DateTimeOffset?startTime = null, DateTimeOffset?endTime = null) { return(this._cloudQueue.GetSharedAccessSignature(new SharedAccessQueuePolicy { Permissions = permissions, SharedAccessStartTime = startTime, SharedAccessExpiryTime = endTime })); }
/// <summary> /// This method is used get the URI /// </summary> /// <param name="permissions"></param> /// <returns></returns> public Uri GetUri(SharedAccessQueuePermissions permissions) { var policy = new SharedAccessQueuePolicy { SharedAccessExpiryTime = DateTime.UtcNow.AddDays(31.0), Permissions = permissions }; return(new Uri(_queueReference.Uri, _queueReference.GetSharedAccessSignature(policy) + "&comp=list&restype=container")); }
/// <summary> /// Returns a shared access signature for the queue. /// </summary> /// <param name="permissions">The permissions for a shared access signature associated with this shared access policy.</param> /// <param name="startTime">The start time for a shared access signature associated with this shared access policy.</param> /// <param name="endTime">The expiry time for a shared access signature associated with this shared access policy.</param> /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for a task to complete.</param> /// <returns>The query string returned includes the leading question mark.</returns> public Task <string> GetQueueSasAsync(SharedAccessQueuePermissions permissions = SharedAccessQueuePermissions.Read, DateTimeOffset?startTime = null, DateTimeOffset?endTime = null, CancellationToken?cancellationToken = null) { return(Task.Run( () => this._cloudQueue.GetSharedAccessSignature(new SharedAccessQueuePolicy { Permissions = permissions, SharedAccessStartTime = startTime, SharedAccessExpiryTime = endTime }), cancellationToken ?? CancellationToken.None)); }
public async Task QueueRegionalSASTestAsync() { CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = new CultureInfo("it-IT"); CloudQueueClient client = GenerateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(GenerateNewQueueName()); try { await queue.CreateAsync(); string messageContent = Guid.NewGuid().ToString(); CloudQueueMessage message = new CloudQueueMessage(messageContent); await queue.AddMessageAsync(message); // Prepare SAS authentication with full permissions string id = Guid.NewGuid().ToString(); DateTime start = DateTime.UtcNow; DateTime expiry = start.AddMinutes(30); QueuePermissions permissions = new QueuePermissions(); SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update; permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy() { SharedAccessStartTime = start, SharedAccessExpiryTime = expiry, Permissions = queuePerm }); await queue.SetPermissionsAsync(permissions); await Task.Delay(30 * 1000); string sasTokenFromId = queue.GetSharedAccessSignature(null, id); StorageCredentials sasCredsFromId = new StorageCredentials(sasTokenFromId); CloudQueue sasQueueFromId = new CloudQueue(queue.Uri, sasCredsFromId); CloudQueueMessage receivedMessage1 = await sasQueueFromId.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage1.AsString); string sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null); StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy); CloudQueue sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy); CloudQueueMessage receivedMessage2 = await sasQueueFromPolicy.PeekMessageAsync(); Assert.AreEqual(messageContent, receivedMessage2.AsString); } finally { Thread.CurrentThread.CurrentCulture = currentCulture; queue.DeleteAsync().Wait(); } }
public void WillReturnParseableSharedAccessSignature( SharedAccessQueuePermissions permissions, string expectedPermissions) { var queue = new StandaloneAzureQueue(BasePath, QueueName); Assert.Contains(expectedPermissions, queue.GetSharedAccessSignature(new SharedAccessQueuePolicy { Permissions = permissions })); }
/// <summary> /// Gets the queue Uri with SAS. /// </summary> /// <param name="permissions">The permissions for a shared access signature associated with this shared access policy.</param> /// <param name="startTime">The start time for a shared access signature associated with this shared access policy.</param> /// <param name="endTime">The expiry time for a shared access signature associated with this shared access policy.</param> /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for a task to complete.</param> /// <returns>The queue Uri with SAS.</returns> public Task <Uri> GetQueueUriWithSasAsync(SharedAccessQueuePermissions permissions = SharedAccessQueuePermissions.Read, DateTimeOffset?startTime = null, DateTimeOffset?endTime = null, CancellationToken?cancellationToken = null) { return(Task.Run(() => { var uriBuilder = new UriBuilder(this._cloudQueue.Uri); uriBuilder.Query = this.GetQueueSas(permissions, startTime, endTime).TrimStart('?'); return uriBuilder.Uri; }, cancellationToken ?? CancellationToken.None)); }
public void QueueSASTest() { CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName()); try { queue.Create(); string messageContent = Guid.NewGuid().ToString(); CloudQueueMessage message = new CloudQueueMessage(messageContent); queue.AddMessage(message); // Prepare SAS authentication with full permissions string id = Guid.NewGuid().ToString(); DateTime start = DateTime.UtcNow; DateTime expiry = start.AddMinutes(30); QueuePermissions permissions = new QueuePermissions(); SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update; permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy() { SharedAccessStartTime = start, SharedAccessExpiryTime = expiry, Permissions = queuePerm }); queue.SetPermissions(permissions); Thread.Sleep(30 * 1000); string sasTokenFromId = queue.GetSharedAccessSignature(null, id); StorageCredentials sasCredsFromId = new StorageCredentials(sasTokenFromId); CloudQueue sasQueueFromId = new CloudQueue(queue.Uri, sasCredsFromId); CloudQueueMessage receivedMessage1 = sasQueueFromId.PeekMessage(); Assert.AreEqual(messageContent, receivedMessage1.AsString); string sasTokenFromPolicy = queue.GetSharedAccessSignature(permissions.SharedAccessPolicies[id], null); StorageCredentials sasCredsFromPolicy = new StorageCredentials(sasTokenFromPolicy); CloudQueue sasQueueFromPolicy = new CloudQueue(queue.Uri, sasCredsFromPolicy); CloudQueueMessage receivedMessage2 = sasQueueFromPolicy.PeekMessage(); Assert.AreEqual(messageContent, receivedMessage2.AsString); } finally { queue.DeleteIfExists(); } }
public async Task CloudQueueGetSetPermissionsAsync() { CloudQueueClient client = GenerateCloudQueueClient(); CloudQueue queue = client.GetQueueReference(GenerateNewQueueName()); await queue.CreateAsync(); QueuePermissions emptyPermission = await queue.GetPermissionsAsync(); Assert.AreEqual(emptyPermission.SharedAccessPolicies.Count, 0); string id = Guid.NewGuid().ToString(); DateTime start = DateTime.UtcNow; DateTime expiry = start.AddMinutes(30); QueuePermissions permissions = new QueuePermissions(); SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update; permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy() { SharedAccessStartTime = start, SharedAccessExpiryTime = expiry, Permissions = queuePerm }); await queue.SetPermissionsAsync(permissions); await Task.Delay(30 * 1000); await queue.FetchAttributesAsync(); CloudQueue queueToRetrieve = client.GetQueueReference(queue.Name); QueuePermissions permissionsToRetrieve = await queueToRetrieve.GetPermissionsAsync(); Assert.AreEqual(permissions.SharedAccessPolicies.Count, permissionsToRetrieve.SharedAccessPolicies.Count); //Assert.AreEqual(start, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessStartTime.Value.UtcDateTime); //Assert.AreEqual(expiry, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessExpiryTime.Value.UtcDateTime); Assert.AreEqual(permissions.SharedAccessPolicies[id].Permissions, permissionsToRetrieve.SharedAccessPolicies[id].Permissions); await queue.DeleteAsync(); }
/// <summary> /// Constructs a <see cref="SharedAccessQueuePermissions"/> object from a permissions string. /// </summary> /// <param name="input">The shared access permissions in string format.</param> /// <returns>A <see cref="SharedAccessQueuePermissions"/> object.</returns> public static SharedAccessQueuePermissions PermissionsFromString(string input) { CommonUtility.AssertNotNull("input", input); SharedAccessQueuePermissions permissions = 0; foreach (char c in input) { switch (c) { case 'r': permissions |= SharedAccessQueuePermissions.Read; break; case 'p': permissions |= SharedAccessQueuePermissions.ProcessMessages; break; case 'a': permissions |= SharedAccessQueuePermissions.Add; break; case 'u': permissions |= SharedAccessQueuePermissions.Update; break; default: throw new ArgumentOutOfRangeException("input"); } } // Incase we ever change none to be something other than 0 if (permissions == 0) { permissions |= SharedAccessQueuePermissions.None; } return(permissions); }
public static string DeterminePermissionsString(this SharedAccessQueuePermissions permissions) { var permissionsString = "sp="; if ((permissions & SharedAccessQueuePermissions.Read) == SharedAccessQueuePermissions.Read) { permissionsString += "r"; } if ((permissions & SharedAccessQueuePermissions.Add) == SharedAccessQueuePermissions.Add) { permissionsString += "a"; } if ((permissions & SharedAccessQueuePermissions.Update) == SharedAccessQueuePermissions.Update) { permissionsString += "u"; } if ((permissions & SharedAccessQueuePermissions.ProcessMessages) == SharedAccessQueuePermissions.ProcessMessages) { permissionsString += "p"; } return(permissionsString); }
public void CloudQueueGetSetPermissions() { CloudQueue queue = DefaultQueueClient.GetQueueReference(GenerateNewQueueName()); try { queue.Create(); QueuePermissions emptyPermission = queue.GetPermissions(); Assert.AreEqual(emptyPermission.SharedAccessPolicies.Count, 0); string id = Guid.NewGuid().ToString(); DateTime start = DateTime.UtcNow; DateTime expiry = start.AddMinutes(30); QueuePermissions permissions = new QueuePermissions(); SharedAccessQueuePermissions queuePerm = SharedAccessQueuePermissions.Add | SharedAccessQueuePermissions.ProcessMessages | SharedAccessQueuePermissions.Read | SharedAccessQueuePermissions.Update; permissions.SharedAccessPolicies.Add(id, new SharedAccessQueuePolicy() { SharedAccessStartTime = start, SharedAccessExpiryTime = expiry, Permissions = queuePerm }); queue.SetPermissions(permissions); Thread.Sleep(30 * 1000); CloudQueue queueToRetrieve = DefaultQueueClient.GetQueueReference(queue.Name); QueuePermissions permissionsToRetrieve = queueToRetrieve.GetPermissions(); Assert.AreEqual(permissions.SharedAccessPolicies.Count, permissionsToRetrieve.SharedAccessPolicies.Count); //Assert.AreEqual(start, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessStartTime.Value.UtcDateTime); //Assert.AreEqual(expiry, permissionsToRetrieve.SharedAccessPolicies[id].SharedAccessExpiryTime.Value.UtcDateTime); Assert.AreEqual(permissions.SharedAccessPolicies[id].Permissions, permissionsToRetrieve.SharedAccessPolicies[id].Permissions); } finally { queue.DeleteIfExists(); } }
public static string PermissionsToString(SharedAccessQueuePermissions permissions) { throw new System.NotImplementedException(); }