def test_sas_signed_identifier(self):
        # Arrange
        si = SignedIdentifier()
        si.id = "testid"
        si.access_policy.start = "2011-10-11"
        si.access_policy.expiry = "2018-10-12"
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        self.qs.put_message(self.test_queues[0], "message1")

        token = self.qs.generate_shared_access_signature(
            self.test_queues[0], SharedAccessPolicy(signed_identifier=si.id)
        )

        # Act
        service = QueueService(credentials.getStorageServicesName(), sas_token=token)
        set_service_options(service)
        result = service.peek_messages(self.test_queues[0])

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual("", message.message_id)
        self.assertEqual("message1", message.message_text)
    def test_sas_signed_identifier(self):
        # Arrange
        si = SignedIdentifier()
        si.id = 'testid'
        si.access_policy.start = '2011-10-11'
        si.access_policy.expiry = '2018-10-12'
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        self.qs.put_message(self.test_queues[0], 'message1')

        token = self.qs.generate_shared_access_signature(
            self.test_queues[0],
            SharedAccessPolicy(signed_identifier=si.id),
        )

        # Act
        service = QueueService(credentials.getStorageServicesName(),
                               sas_token=token)
        set_service_options(service)
        result = service.peek_messages(self.test_queues[0])

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.message_id)
        self.assertEqual('message1', message.message_text)
Beispiel #3
0
def get_identifier(id, hours, days, permission):
    date_format = "%Y-%m-%dT%H:%M:%SZ"
    start = datetime.datetime.utcnow() - datetime.timedelta(minutes=1)
    expiry = start + datetime.timedelta(hours=hours, days=days)
    si = SignedIdentifier()
    si.id = id
    si.access_policy.start = start.strftime(date_format)
    si.access_policy.expiry = expiry.strftime(date_format)
    si.access_policy.permission = permission
    return si
    def test_set_queue_acl_with_signed_identifiers(self):
        # Arrange

        # Act
        si = SignedIdentifier()
        si.id = 'testid'
        si.access_policy.start = '2011-10-11'
        si.access_policy.expiry = '2011-10-12'
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        # Assert
        self.assertIsNone(resp)
        acl = self.qs.get_queue_acl(self.test_queues[0])
        self.assertIsNotNone(acl)
        self.assertEqual(len(acl.signed_identifiers), 1)
        self.assertEqual(len(acl), 1)
        self.assertEqual(acl.signed_identifiers[0].id, 'testid')
        self.assertEqual(acl[0].id, 'testid')
    def test_set_queue_acl_with_signed_identifiers(self):
        # Arrange

        # Act
        si = SignedIdentifier()
        si.id = 'testid'
        si.access_policy.start = '2011-10-11'
        si.access_policy.expiry = '2011-10-12'
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        # Assert
        self.assertIsNone(resp)
        acl = self.qs.get_queue_acl(self.test_queues[0])
        self.assertIsNotNone(acl)
        self.assertEqual(len(acl.signed_identifiers), 1)
        self.assertEqual(len(acl), 1)
        self.assertEqual(acl.signed_identifiers[0].id, 'testid')
        self.assertEqual(acl[0].id, 'testid')
    def test_sas_signed_identifier(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recordingfile(self.test_mode):
            return

        # Arrange
        si = SignedIdentifier()
        si.id = 'testid'
        si.access_policy.start = '2011-10-11'
        si.access_policy.expiry = '2018-10-12'
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        self.qs.put_message(self.test_queues[0], 'message1')

        token = self.qs.generate_shared_access_signature(
            self.test_queues[0],
            SharedAccessPolicy(signed_identifier=si.id),
        )

        # Act
        service = QueueService(
            account_name=self.settings.STORAGE_ACCOUNT_NAME,
            sas_token=token,
        )
        self._set_service_options(service, self.settings)
        result = service.peek_messages(self.test_queues[0])

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.message_id)
        self.assertEqual('message1', message.message_text)
    def test_sas_signed_identifier(self):
        # SAS URL is calculated from storage key, so this test runs live only
        if TestMode.need_recordingfile(self.test_mode):
            return

        # Arrange
        si = SignedIdentifier()
        si.id = 'testid'
        si.access_policy.start = '2011-10-11'
        si.access_policy.expiry = '2018-10-12'
        si.access_policy.permission = QueueSharedAccessPermissions.READ
        identifiers = SignedIdentifiers()
        identifiers.signed_identifiers.append(si)

        resp = self.qs.set_queue_acl(self.test_queues[0], identifiers)

        self.qs.put_message(self.test_queues[0], 'message1')

        token = self.qs.generate_shared_access_signature(
            self.test_queues[0],
            SharedAccessPolicy(signed_identifier=si.id),
        )

        # Act
        service = QueueService(
            account_name=self.settings.STORAGE_ACCOUNT_NAME,
            sas_token=token,
        )
        self._set_service_options(service, self.settings)
        result = service.peek_messages(self.test_queues[0])

        # Assert
        self.assertIsNotNone(result)
        self.assertEqual(1, len(result))
        message = result[0]
        self.assertIsNotNone(message)
        self.assertNotEqual('', message.message_id)
        self.assertEqual('message1', message.message_text)
# Create the blob service, using the name and key for your Azure Storage account
blob_service = BlobService(storage_account_name, storage_account_key)

# Create the container, if it does not already exist
blob_service.create_container(storage_container_name)

# Upload an example file to the container
blob_service.put_block_blob_from_path(
    storage_container_name,
    'sample.log',
    example_file_path,
)

# Create a new signed identifier (policy)
si = SignedIdentifier()
# Set the name
si.id = policy_name
# Set the expiration date
si.access_policy.expiry = '2016-01-01'
# Set the permissions. Read and List in this example
si.access_policy.permission = ContainerSharedAccessPermissions.READ + ContainerSharedAccessPermissions.LIST

# Get the existing signed identifiers (policies) for the container
identifiers = blob_service.get_container_acl(storage_container_name)
# And append the new one ot the list
identifiers.signed_identifiers.append(si)

# Set the container to the updated list of signed identifiers (policies)
blob_service.set_container_acl(
    container_name=storage_container_name,