1

I would like to check if an Azure Storage Queue exists by calling the Azure.Storage.Queues.ExistsAsync(CancellationToken cancellationToken = default) method within the Azure Storage v12 API. I am trying to do this via SAS tokens as per business requirements, but I am getting an authorization error as below:

Azure.RequestFailedException: This request is not authorized to perform this operation.
Status: 403 
ErrorCode: AuthorizationFailure

In the stack trace I can see that this line throws the error within the API:

Azure.Storage.Queues.QueueRestClient.GetPropertiesAsync(Nullable`1 timeout, CancellationToken cancellationToken)

I am using the API as shown below:

var serviceQueueClient = new QueueClient(GetServiceSasToken());
return await serviceQueueClient.ExistsAsync(); // this line throws the exception

private Uri GetServiceSasToken()
{
    var queueSasBuilder = new QueueSasBuilder()
    {
        ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(15),
        QueueName = _queueName, // Name of my queue, which exists on Azure Portal
        Protocol = SasProtocol.Https
    };

    queueSasBuilder.SetPermissions(QueueSasPermissions.Read | QueueSasPermissions.Add | QueueSasPermissions.Process);
    StorageSharedKeyCredential storageSharedKeyCredential = new(_accountName, _accountKey); // storage account name, and the access key
    var sasQueryParams = queueSasBuilder.ToSasQueryParameters(storageSharedKeyCredential).ToString();
    return new UriBuilder()
    {
        Scheme = "https",
        Host = $"{_accountName}.queue.core.windows.net",
        Path = _queueName,
        Query = sasQueryParams
    }.Uri;
}

I have also tried using an Account SAS token as below, but this also fails with the same error.

var serviceQueueClient = new QueueClient(GetAccountSasToken());
return await serviceQueueClient.ExistsAsync(); // this line throws the exception

private Uri GetAccountSasToken()
{
    var queueSasBuilder = new AccountSasBuilder()
    {
        Services = AccountSasServices.Queues,
        ResourceTypes = AccountSasResourceTypes.Service | AccountSasResourceTypes.Container | AccountSasResourceTypes.Object,
        ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(15),
        Protocol = SasProtocol.Https
    };

    queueSasBuilder.SetPermissions(AccountSasPermissions.Create | AccountSasPermissions.List | AccountSasPermissions.Read);
    StorageSharedKeyCredential storageSharedKeyCredential = new(_accountName, _accountKey); // storage account name, and the access key
    var sasQueryParams = queueSasBuilder.ToSasQueryParameters(storageSharedKeyCredential).ToString();
    return new UriBuilder()
    {
        Scheme = "https",
        Host = $"{_accountName}.queue.core.windows.net",
        Path = _queueName,
        Query = sasQueryParams
    }.Uri;
}

I managed to manipulate Azure Blobs in a similar way within the same kubernetes pod, so the private endpoint should be working okay in my environment. What am I missing?

6
  • What happens if you use QueueSasBuilder.SetPermissions( "rwlaup" )?
    – Dai
    Commented Mar 24, 2023 at 11:19
  • Then the error turns into Make sure the authorization header is formed correctly including the signature. When I call queueSasBuilder.SetPermissions(QueueSasPermissions.All) (which also didn't work) the Permissions property end up being raup, so I think rwlaup is an invalid permission setting for queues. Commented Mar 24, 2023 at 11:33
  • What's the output of GetServiceSasToken() method? Basically what's the URI that gets passed to QueueClient constructor? Commented Mar 24, 2023 at 11:47
  • The resulting uri is https://<accountName>.queue.core.windows.net/<queueName>?sv=2021-12-02&spr=https&se=2023-03-24T12%3A38%3A00Z&sp=raup&sig=<signature> Commented Mar 24, 2023 at 12:26
  • Your SAS URL looks ok. Can you try to perform some other operation using this? May be peek messages from the queue? Commented Mar 24, 2023 at 13:30

1 Answer 1

1

After reproducing from my end, I received the same error.

enter image description here

You are facing this issue because the storage account has Firewall enabled.

enter image description here

Make sure you add your IP address ranges to allow access from the internet or your on-premises networks.

enter image description here

After following the above I could get the expected result.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.