async def test_sas_process(self, resource_group, location, storage_account, storage_account_key): qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key, transport=AiohttpTestTransport()) # SAS URL is calculated from storage key, so this test runs live only # Arrange queue_client = await self._create_queue(qsc) await queue_client.send_message(u'message1') token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(process=True), datetime.utcnow() + timedelta(hours=1), ) # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) messages = [] async for m in queue_client.receive_messages(): messages.append(m) message = messages[0] # Assert self.assertIsNotNone(message) self.assertNotEqual('', message.id) self.assertEqual(u'message1', message.content)
async def test_sas_read(self, resource_group, location, storage_account, storage_account_key): qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key, transport=AiohttpTestTransport()) # SAS URL is calculated from storage key, so this test runs live only # Arrange queue_client = await self._create_queue(qsc) await queue_client.send_message(u'message1') token = generate_queue_sas(queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(read=True), datetime.utcnow() + timedelta(hours=1), datetime.utcnow() - timedelta(minutes=5)) # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) result = await service.peek_messages() # Assert self.assertIsNotNone(result) self.assertEqual(1, len(result)) message = result[0] self.assertIsNotNone(message) self.assertNotEqual('', message.id) self.assertEqual(u'message1', message.content)
def get_queue_sas( queue: QueueNameType, *, account_id: str, read: bool = False, add: bool = False, update: bool = False, process: bool = False, ) -> str: logging.debug("getting queue sas %s (account_id: %s)", queue, account_id) name, key = get_storage_account_name_key(account_id) expiry = datetime.datetime.utcnow() + datetime.timedelta(days=30) token = generate_queue_sas( name, str(queue), key, permission=QueueSasPermissions(read=read, add=add, update=update, process=process), expiry=expiry, ) url = "https://%s.queue.core.windows.net/%s?%s" % (name, queue, token) return url
def test_sas_update(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key) queue_client = self._get_queue_reference(qsc) queue_client.create_queue() queue_client.send_message(u'message1') token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(update=True), datetime.utcnow() + timedelta(hours=1), ) messages = queue_client.receive_messages() result = next(messages) # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) service.update_message( result.id, pop_receipt=result.pop_receipt, visibility_timeout=0, content=u'updatedmessage1', ) # Assert result = next(messages) self.assertEqual(u'updatedmessage1', result.content)
def test_sas_process(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key) queue_client = self._get_queue_reference(qsc) queue_client.create_queue() queue_client.send_message(u'message1') token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(process=True), datetime.utcnow() + timedelta(hours=1), ) # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) message = next(service.receive_messages()) # Assert self.assertIsNotNone(message) self.assertNotEqual('', message.id) self.assertEqual(u'message1', message.content)
def test_sas_add(self, storage_account_name, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange qsc = QueueServiceClient( self.account_url(storage_account_name, "queue"), storage_account_key) queue_client = self._get_queue_reference(qsc) queue_client.create_queue() token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(add=True), datetime.utcnow() + timedelta(hours=1), ) # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) result = service.send_message(u'addedmessage') # Assert result = next(queue_client.receive_messages()) self.assertEqual(u'addedmessage', result.content)
def get_queue_sas( queue: QueueNameType, storage_type: StorageType, *, read: bool = False, add: bool = False, update: bool = False, process: bool = False, duration: Optional[datetime.timedelta] = None, ) -> str: if duration is None: duration = DEFAULT_DURATION account_id = get_primary_account(storage_type) logging.debug("getting queue sas %s (account_id: %s)", queue, account_id) name, key = get_storage_account_name_key(account_id) expiry = datetime.datetime.utcnow() + duration token = generate_queue_sas( name, str(queue), key, permission=QueueSasPermissions( read=read, add=add, update=update, process=process ), expiry=expiry, ) url = "https://%s.queue.core.windows.net/%s?%s" % (name, queue, token) return url
def test_set_access_policy(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return # [START create_queue_client_from_connection_string] from azure.storage.queue import QueueClient queue_client = QueueClient.from_connection_string(self.connection_string(storage_account, storage_account_key), "queuetest") # [END create_queue_client_from_connection_string] # Create the queue try: queue_client.create_queue() except ResourceExistsError: pass queue_client.send_message(u"hello world") try: # [START set_access_policy] # Create an access policy from azure.storage.queue import AccessPolicy, QueueSasPermissions access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = QueueSasPermissions(read=True) identifiers = {'my-access-policy-id': access_policy} # Set the access policy queue_client.set_queue_access_policy(identifiers) # [END set_access_policy] # Use the access policy to generate a SAS token # [START queue_client_sas_token] from azure.storage.queue import generate_queue_sas sas_token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, policy_id='my-access-policy-id' ) # [END queue_client_sas_token] # Authenticate with the sas token # [START create_queue_client] q = QueueClient.from_queue_url( queue_url=queue_client.url, credential=sas_token ) # [END create_queue_client] # Use the newly authenticated client to receive messages my_message = q.receive_messages() assert my_message is not None finally: # Delete the queue queue_client.delete_queue()
async def test_set_access_policy(self, resource_group, location, storage_account, storage_account_key): connection_string = self.connection_string(storage_account, storage_account_key) storage_url = self._account_url(storage_account.name) # [START async_create_queue_client_from_connection_string] from azure.storage.queue.aio import QueueClient queue_client = QueueClient.from_connection_string( self.connection_string(storage_account, storage_account_key), "asyncqueuetest") # [END async_create_queue_client_from_connection_string] # Create the queue try: await queue_client.create_queue() except ResourceExistsError: pass await queue_client.send_message(u"hello world") try: # [START async_set_access_policy] # Create an access policy from azure.storage.queue import AccessPolicy, QueueSasPermissions access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = QueueSasPermissions(read=True) identifiers = {'my-access-policy-id': access_policy} # Set the access policy await queue_client.set_queue_access_policy(identifiers) # [END async_set_access_policy] # Use the access policy to generate a SAS token from azure.storage.queue import generate_queue_sas sas_token = generate_queue_sas(queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, policy_id='my-access-policy-id') # [END async_set_access_policy] # Authenticate with the sas token # [START async_create_queue_client] from azure.storage.queue.aio import QueueClient q = QueueClient.from_queue_url(queue_url=queue_client.url, credential=sas_token) # [END async_create_queue_client] # Use the newly authenticated client to receive messages my_messages = q.receive_messages() finally: # Delete the queue await queue_client.delete_queue()
def set_access_policy(self): # [START create_queue_client_from_connection_string] from azure.storage.queue import QueueClient queue = QueueClient.from_connection_string(self.connection_string, "myqueue1") # [END create_queue_client_from_connection_string] # Create the queue queue.create_queue() # Send a message queue.send_message(u"hello world") try: # [START set_access_policy] # Create an access policy from azure.storage.queue import AccessPolicy, QueueSasPermissions access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = QueueSasPermissions(read=True) identifiers = {'my-access-policy-id': access_policy} # Set the access policy queue.set_queue_access_policy(identifiers) # [END set_access_policy] # Use the access policy to generate a SAS token # [START queue_client_sas_token] from azure.storage.queue import generate_queue_sas sas_token = generate_queue_sas( queue.account_name, queue.queue_name, queue.credential.account_key, policy_id='my-access-policy-id' ) # [END queue_client_sas_token] # Authenticate with the sas token # [START create_queue_client] token_auth_queue = QueueClient.from_queue_url( queue_url=queue.url, credential=sas_token ) # [END create_queue_client] # Use the newly authenticated client to receive messages my_message = token_auth_queue.receive_messages() finally: # Delete the queue queue.delete_queue()
def test_sas_signed_identifier(self, resource_group, location, storage_account, storage_account_key): # SAS URL is calculated from storage key, so this test runs live only if not self.is_live: return # Arrange access_policy = AccessPolicy() access_policy.start = datetime.utcnow() - timedelta(hours=1) access_policy.expiry = datetime.utcnow() + timedelta(hours=1) access_policy.permission = QueueSasPermissions(read=True) identifiers = {'testid': access_policy} qsc = QueueServiceClient(self._account_url(storage_account.name), storage_account_key) queue_client = self._get_queue_reference(qsc) queue_client.create_queue() resp = queue_client.set_queue_access_policy(identifiers) queue_client.send_message(u'message1') token = generate_queue_sas(queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, policy_id='testid') # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) result = service.peek_messages() # Assert self.assertIsNotNone(result) self.assertEqual(1, len(result)) message = result[0] self.assertIsNotNone(message) self.assertNotEqual('', message.id) self.assertEqual(u'message1', message.content)
async def test_sas_update(self, resource_group, location, storage_account, storage_account_key): qsc = QueueServiceClient(self.account_url(storage_account, "queue"), storage_account_key, transport=AiohttpTestTransport()) # SAS URL is calculated from storage key, so this test runs live only # Arrange queue_client = await self._create_queue(qsc) await queue_client.send_message(u'message1') token = generate_queue_sas( queue_client.account_name, queue_client.queue_name, queue_client.credential.account_key, QueueSasPermissions(update=True), datetime.utcnow() + timedelta(hours=1), ) messages = [] async for m in queue_client.receive_messages(): messages.append(m) result = messages[0] # Act service = QueueClient.from_queue_url( queue_url=queue_client.url, credential=token, ) await service.update_message( result.id, pop_receipt=result.pop_receipt, visibility_timeout=0, content=u'updatedmessage1', ) # Assert messages = [] async for m in queue_client.receive_messages(): messages.append(m) result = messages[0] self.assertEqual(u'updatedmessage1', result.content)