def authentication_by_shared_access_signature(self): # Instantiate a TableServiceClient using a connection string # [START auth_from_sas] from azure.data.tables import TableServiceClient from azure.core.credentials import AzureNamedKeyCredential, AzureSasCredential # Create a SAS token to use for authentication of a client from azure.data.tables import generate_account_sas, ResourceTypes, AccountSasPermissions print("Account name: {}".format(self.account_name)) credential = AzureNamedKeyCredential(self.account_name, self.access_key) sas_token = generate_account_sas( credential, resource_types=ResourceTypes(service=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), ) with TableServiceClient(endpoint=self.endpoint, credential=AzureSasCredential( sas_token)) as token_auth_table_service: properties = token_auth_table_service.get_service_properties() print("Shared Access Signature: {}".format(properties))
def generate_sas_token(self): fake_key = 'a' * 30 + 'b' * 30 return '?' + generate_account_sas( credential=AzureNamedKeyCredential(name="fakename", key=fake_key), resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True, list=True), start=datetime.now() - timedelta(hours=24), expiry=datetime.now() + timedelta(days=8))
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))
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 # Arrange account_url = self.account_url(storage_account, "table") if 'cosmos' in account_url: pytest.skip("Cosmos Tables does not yet support sas") tsc = self.create_client_from_credential(TableServiceClient, storage_account_key, account_url=account_url) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_entity(entity=entity) entity['RowKey'] = 'test2' await table.upsert_entity(entity=entity) token = generate_account_sas( storage_account.name, storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(storage_account, "table") service = self.create_client_from_credential( TableServiceClient, token, account_url=account_url) # Act # service = TableServiceClient( # self.account_url(storage_account, "table"), # credential=token, # ) sas_table = service.get_table_client(table.table_name) entities = [] async for e in sas_table.list_entities(): entities.append(e) # Assert assert len(entities) == 2 assert entities[0].text.value == u'hello' assert entities[1].text.value == u'hello' finally: await self._delete_table(table=table, ts=tsc)
def test_account_sas(self, tables_storage_account_name, tables_primary_storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange account_url = self.account_url(tables_storage_account_name, "table") tsc = self.create_client_from_credential( TableServiceClient, tables_primary_storage_account_key, account_url=account_url) table = self._create_table(tsc) try: entity = { 'PartitionKey': u'test', 'RowKey': u'test1', 'text': u'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = u'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( tables_storage_account_name, tables_primary_storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(tables_storage_account_name, "table") service = self.create_client_from_credential( TableServiceClient, token, account_url=account_url) # Act # service = TableServiceClient( # self.account_url(tables_storage_account_name, "table"), # credential=token, # ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert assert len(entities) == 2 assert entities[0].text == u'hello' assert entities[1].text == u'hello' finally: self._delete_table(table=table, ts=tsc)
async def test_account_sas(self, resource_group, location, cosmos_account, cosmos_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(cosmos_account, "cosmos") if 'cosmos' in url: pytest.skip("Cosmos Tables does not yet support sas") tsc = TableServiceClient(url, cosmos_account_key) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_insert_merge_entity( table_entity_properties=entity) entity['RowKey'] = 'test2' await table.upsert_insert_merge_entity( table_entity_properties=entity) token = generate_account_sas( cosmos_account.name, cosmos_account_key, resource_types=ResourceTypes(container=True), permission=AccountSasPermissions(list=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(cosmos_account, "cosmos"), credential=token, ) entities = [] async for e in service.list_tables(): entities.append(e) # Assert self.assertEqual(len(entities), 1) # self.assertEqual(entities[0].text, 'hello') # self.assertEqual(entities[1].text, 'hello') finally: await self._delete_table(table=table, ts=tsc) if self.is_live: sleep(SLEEP_DELAY)
async def test_account_sas(self, tables_storage_account_name, tables_primary_storage_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange account_url = self.account_url(tables_storage_account_name, "table") tsc = self.create_client_from_credential( TableServiceClient, tables_primary_storage_account_key, endpoint=account_url) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_entity(entity=entity) entity['RowKey'] = 'test2' await table.upsert_entity(entity=entity) token = generate_account_sas( tables_primary_storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(tables_storage_account_name, "table") service = self.create_client_from_credential(TableServiceClient, token, endpoint=account_url) # Act sas_table = service.get_table_client(table.table_name) entities = [] async for e in sas_table.list_entities(): entities.append(e) # Assert assert len(entities) == 2 assert entities[0]['text'] == u'hello' assert entities[1]['text'] == u'hello' finally: await self._delete_table(table=table, ts=tsc)
def test_account_sas(self, tables_storage_account_name, tables_primary_storage_account_key): account_url = self.account_url(tables_storage_account_name, "table") tsc = self.create_client_from_credential( TableServiceClient, tables_primary_storage_account_key, endpoint=account_url) table = self._create_table(tsc) try: entity = { 'PartitionKey': u'test', 'RowKey': u'test1', 'text': u'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = u'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = self.generate_sas( generate_account_sas, tables_primary_storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) account_url = self.account_url(tables_storage_account_name, "table") service = self.create_client_from_credential(TableServiceClient, token, endpoint=account_url) # Act sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert assert len(entities) == 2 assert entities[0]['text'] == u'hello' assert entities[1]['text'] == u'hello' finally: tsc.delete_table(table.table_name)
async def test_account_sas(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # SAS URL is calculated from storage key, so this test runs live only # Arrange url = self.account_url(tables_cosmos_account_name, "cosmos") tsc = TableServiceClient(url, tables_primary_cosmos_account_key) table = await self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } await table.upsert_insert_merge_entity( table_entity_properties=entity) entity['RowKey'] = 'test2' await table.upsert_insert_merge_entity( table_entity_properties=entity) token = generate_account_sas( tables_cosmos_account_name, tables_primary_cosmos_account_key, resource_types=ResourceTypes(container=True), permission=AccountSasPermissions(list=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), credential=token, ) entities = [] async for e in service.list_tables(): entities.append(e) # Assert assert len(entities) == 1 finally: await self._delete_table(table=table, ts=tsc) if self.is_live: sleep(SLEEP_DELAY)
def sas_token_auth(self): from azure.data.tables import (TableServiceClient, ResourceTypes, AccountSasPermissions, generate_account_sas) import datetime import timedelta token = generate_account_sas( account_name=self.account_name, account_key=self.account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) table_service_client = TableServiceClient(account_url=self.account_url, credential=token)
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 # Arrange url = self.account_url(storage_account, "table") if 'cosmos' in url: pytest.skip("Cosmos Tables does not yet support sas") tsc = TableServiceClient(url, storage_account_key) table = self._create_table(tsc) try: entity = { 'PartitionKey': 'test', 'RowKey': 'test1', 'text': 'hello', } table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) entity['RowKey'] = 'test2' table.upsert_entity(mode=UpdateMode.MERGE, entity=entity) token = generate_account_sas( storage_account.name, storage_account_key, resource_types=ResourceTypes(object=True), permission=AccountSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) # Act service = TableServiceClient( self.account_url(storage_account, "table"), credential=token, ) sas_table = service.get_table_client(table.table_name) entities = list(sas_table.list_entities()) # Assert self.assertEqual(len(entities), 2) self.assertEqual(entities[0].text, 'hello') self.assertEqual(entities[1].text, 'hello') finally: self._delete_table(table=table, ts=tsc)
async def authentication_by_shared_access_signature(self): # Instantiate a TableServiceClient using a connection string # [START auth_by_sas] from azure.data.tables.aio import TableServiceClient # Create a SAS token to use for authentication of a client from azure.data.tables 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)) async with TableServiceClient( account_url=self.account_url, credential=sas_token) as token_auth_table_service: properties = await table_service.get_service_properties() print("Shared Access Signature: {}".format(properties))
def authentication_by_shared_access_signature(self): # Instantiate a TableServiceClient using a connection string from azure.data.tables import TableServiceClient table_service = TableServiceClient.from_connection_string(conn_str=self.connection_string) # Create a SAS token to use for authentication of a client from azure.data.tables 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_table_service = TableServiceClient(account_url=self.account_url, credential=sas_token) # Get information for the Table Service properties = token_auth_table_service.get_service_properties()