Is it possible to add a shared access policy to a queue in Azure Service Bus using python? I'm trying the following:
from azure.servicebus.management import ServiceBusAdministrationClient, AccessRights, AuthorizationRule
.....
queue_properties = servicebus_mgmt_client.get_queue(queue_name)
authorization_rules = queue_properties.authorization_rules
if any(rule.key_name == client_name for rule in authorization_rules):
print(f'SAS policy for {client_name} already exists on queue {queue_name}')
else:
print(f'Creating SAS policy for {client_name} on queue {queue_name}')
rule = AuthorizationRule(
claim_type="SharedAccessKey",
claim_value="None",
rights=[AccessRights.Listen, AccessRights.Send],
key_name=client_name
)
queue_properties.authorization_rules.append(rule)
servicebus_mgmt_client.update_queue(queue_properties)
After using get_queue and checking the existing policies, I'm trying to add one if it's not there. Whenever i run update_queue(), i'm getting a 400 bad request back from Azure without much more info.
Any ideas?
This is a bug in the AuthorizationRule model, and the fix will be going out in the upcoming release of the azure-servicebus package.
Once this is out, you can update the AuthorizationRule to the following and it will update the access rights:
import base64
import secrets
def generate_random_key():
key256 = secrets.token_bytes(32)
return base64.b64encode(key256).decode('utf-8')
auth_rule = AuthorizationRule(
type="SharedAccessAuthorizationRule",
key_name=client_name,
claim_type="SharedAccessKey",
claim_value="None",
rights=[AccessRights.LISTEN, AccessRights.SEND],
primary_key=generate_random_key(),
secondary_key=generate_random_key(),
)