-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ServiceBus] Small stress test and sample touchups (#14304)
* Emplace sample generated for customer issue on the side for future reference. (generate sas token and authenticate) * Adjust stress test that was using push receive instead of pull. * Add async version of non-session to session send failure test, make both cached to improve perf.
- Loading branch information
1 parent
e261627
commit ddece48
Showing
4 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...cebus/azure-servicebus/samples/sync_samples/generate_sas_token_and_authenticate_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python | ||
|
||
# -------------------------------------------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. See License.txt in the project root for license information. | ||
# -------------------------------------------------------------------------------------------- | ||
""" | ||
An example to show authentication using a SAS token generated from a SAS key, as well as the generation process. | ||
""" | ||
|
||
import os | ||
import time | ||
import hmac | ||
import hashlib | ||
import base64 | ||
try: | ||
from urllib.parse import quote as url_parse_quote | ||
except ImportError: | ||
from urllib import pathname2url as url_parse_quote | ||
from azure.core.credentials import AccessToken | ||
|
||
from azure.servicebus import ServiceBusClient | ||
|
||
def generate_sas_token(uri, sas_name, sas_value, token_ttl): | ||
"""Performs the signing and encoding needed to generate a sas token from a sas key.""" | ||
sas = sas_value.encode('utf-8') | ||
expiry = str(int(time.time() + token_ttl)) | ||
string_to_sign = (uri + '\n' + expiry).encode('utf-8') | ||
signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256) | ||
signature = url_parse_quote(base64.b64encode(signed_hmac_sha256.digest())) | ||
return 'SharedAccessSignature sr={}&sig={}&se={}&skn={}'.format(uri, signature, expiry, sas_name) | ||
|
||
class CustomizedSASCredential(object): | ||
def __init__(self, token, expiry): | ||
""" | ||
:param str token: The token string | ||
:param float expiry: The epoch timestamp | ||
""" | ||
self.token = token | ||
self.expiry = expiry | ||
self.token_type = b"servicebus.windows.net:sastoken" | ||
|
||
def get_token(self, *scopes, **kwargs): | ||
""" | ||
This method is automatically called when token is about to expire. | ||
""" | ||
return AccessToken(self.token, self.expiry) | ||
|
||
FULLY_QUALIFIED_NAMESPACE = os.environ['SERVICE_BUS_NAMESPACE'] | ||
SAS_POLICY = os.environ['SERVICE_BUS_SAS_POLICY'] | ||
SAS_KEY = os.environ['SERVICE_BUS_SAS_KEY'] | ||
|
||
auth_uri = "sb://{}/{}".format(FULLY_QUALIFIED_NAMESPACE, SESSION_QUEUE_NAME) | ||
token_ttl = 3000 # seconds | ||
|
||
sas_token = generate_sas_token(auth_uri, SAS_POLICY, SAS_KEY, token_ttl) | ||
|
||
credential=CustomizedSASCredential(sas_token, time.time() + token_ttl) | ||
|
||
with ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential) as client: | ||
pass # client now connected, your logic goes here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters