async def test_account_sas(self, resource_group, location, storage_account,
                               storage_account_key):
        # SAS URL is calculated from storage key, so this test runs live only
        qsc = QueueServiceClient(self.account_url(storage_account, "queue"),
                                 storage_account_key,
                                 transport=AiohttpTestTransport())

        # Arrange
        queue_client = await self._create_queue(qsc)
        await queue_client.send_message(u'message1')
        token = generate_account_sas(qsc.account_name,
                                     qsc.credential.account_key,
                                     ResourceTypes(object=True),
                                     AccountSasPermissions(read=True),
                                     datetime.utcnow() + timedelta(hours=1),
                                     datetime.utcnow() - timedelta(minutes=5))

        # Act
        service = QueueServiceClient(
            account_url=qsc.url,
            credential=token,
        )
        new_queue_client = service.get_queue_client(queue_client.queue_name)
        result = await new_queue_client.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)
Beispiel #2
0
    def test_account_sas(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
        qsc = QueueServiceClient(self._account_url(storage_account.name),
                                 storage_account_key)
        queue_client = self._get_queue_reference(qsc)
        queue_client.create_queue()
        queue_client.enqueue_message(u'message1')
        token = qsc.generate_shared_access_signature(
            ResourceTypes.OBJECT, AccountSasPermissions(read=True),
            datetime.utcnow() + timedelta(hours=1),
            datetime.utcnow() - timedelta(minutes=5))

        # Act
        service = QueueServiceClient(
            account_url=qsc.url,
            credential=token,
        )
        new_queue_client = service.get_queue_client(queue_client.queue_name)
        result = new_queue_client.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 test_account_sas(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()
        queue_client.send_message(u'message1')
        token = generate_account_sas(qsc.account_name,
                                     qsc.credential.account_key,
                                     ResourceTypes(object=True),
                                     AccountSasPermissions(read=True),
                                     datetime.utcnow() + timedelta(hours=1),
                                     datetime.utcnow() - timedelta(minutes=5))

        # Act
        for credential in [token, AzureSasCredential(token)]:
            service = QueueServiceClient(
                account_url=qsc.url,
                credential=credential,
            )
            new_queue_client = service.get_queue_client(
                queue_client.queue_name)
            result = new_queue_client.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 generate_sas_token(self):
        fake_key = 'a' * 30 + 'b' * 30

        return '?' + generate_account_sas(
            account_name='test',  # name of the storage account
            account_key=fake_key,  # key for the storage account
            resource_types=ResourceTypes(object=True),
            permission=AccountSasPermissions(read=True, list=True),
            start=datetime.now() - timedelta(hours=24),
            expiry=datetime.now() + timedelta(days=8))
Beispiel #5
0
    def authentication_by_shared_access_signature(self):
        # Instantiate a QueueServiceClient using a connection string
        from azure.storage.queue import QueueServiceClient
        queue_service = QueueServiceClient.from_connection_string(
            conn_str=self.connection_string)

        # Create a SAS token to use for authentication of a client
        from azure.storage.queue import generate_account_sas, ResourceTypes, AccountSasPermissions

        sas_token = generate_account_sas(
            self.account_name,
            self.access_key,
            resource_types=ResourceTypes(service=True),
            permission=AccountSasPermissions(read=True),
            expiry=datetime.utcnow() + timedelta(hours=1))

        token_auth_queue_service = QueueServiceClient(
            account_url=self.account_url, credential=sas_token)

        # Get information for the Queue Service
        properties = token_auth_queue_service.get_service_properties()