def get_sas_token(resourceGroupName, storageAccount):
    #expiry=datetime.utcnow() + relativedelta(years=10)
    account_key = get_storage_account_keys(resourceGroupName, storageAccount)
    sas_service_client = SharedAccessSignature(storageAccount,
                                               account_key,
                                               x_ms_version='2018-03-28')
    protocol = "https"
    resource_types = ResourceTypes(service=True, container=True, object=True)
    account_permissions = AccountPermissions(read=True,
                                             write=True,
                                             delete=True,
                                             list=True,
                                             add=True,
                                             create=True,
                                             update=True,
                                             process=True,
                                             _str=None)
    expiry = datetime.utcnow() + timedelta(weeks=520)
    start = None
    ip = None
    services = Services(blob=True,
                        queue=True,
                        file=True,
                        table=True,
                        _str=None)
    sas_token = sas_service_client.generate_account(services, resource_types,
                                                    account_permissions,
                                                    expiry, start, ip,
                                                    protocol)
    return sas_token
Esempio n. 2
0
    def create_account_sas_definition(self):
        """
        Creates an account sas definition, to manage storage account and its entities.
        """
        from azure.storage.common import SharedAccessSignature, CloudStorageAccount
        from azure.keyvault.models import SasTokenType, SasDefinitionAttributes
        from azure.keyvault import SecretId

        # To create an account sas definition in the vault we must first create the template. The
        # template_uri for an account sas definition is the intended account sas token signed with an arbitrary key.
        # Use the SharedAccessSignature class from azure.storage.common to create a account sas token
        sas = SharedAccessSignature(
            account_name=self.config.storage_account_name,
            # don't sign the template with the storage account key use key 00000000
            account_key='00000000')
        account_sas_template = sas.generate_account(
            services='bfqt',  # all services blob, file, queue and table
            resource_types='sco',  # all resources service, template, object
            permission='acdlpruw',
            # all permissions add, create, list, process, read, update, write
            expiry='2020-01-01'
        )  # expiry will be ignored and validity period will determine token expiry

        # use the created template to create a sas definition in the vault
        attributes = SasDefinitionAttributes(enabled=True)
        sas_def = self.keyvault_client.set_sas_definition(
            vault_base_url=self.sample_vault_url,
            storage_account_name=self.config.storage_account_name,
            sas_definition_name='acctall',
            template_uri=account_sas_template,
            sas_type=SasTokenType.account,
            validity_period='PT2H',
            sas_definition_attributes=attributes)

        # When the sas definition is created a corresponding managed secret is also created in the vault, the. This
        # secret is used to provision sas tokens according to the sas definition.  Users retrieve the sas token
        # via the get_secret method.

        # get the secret id from the returned SasDefinitionBundle
        sas_secret_id = SecretId(uri=sas_def.secret_id)
        # call get_secret and the value of the returned SecretBundle will be a newly issued sas token
        acct_sas_token = self.keyvault_client.get_secret(
            vault_base_url=sas_secret_id.vault,
            secret_name=sas_secret_id.name,
            secret_version=sas_secret_id.version).value

        # create the cloud storage account object
        cloud_storage_account = CloudStorageAccount(
            account_name=self.config.storage_account_name,
            sas_token=acct_sas_token)

        # create a blob with the account sas token
        blob_service = cloud_storage_account.create_block_blob_service()
        blob_service.create_container('blobcontainer')
        blob_service.create_blob_from_text(container_name='blobcontainer',
                                           blob_name='blob1',
                                           text=u'test blob1 data')
Esempio n. 3
0
    def create_account_sas_definition(self, storage_account_name, vault_url):
        """
        Creates an account sas definition, to manage storage account and its entities.
        """
        from azure.storage.common import SharedAccessSignature, CloudStorageAccount
        from azure.keyvault.models import SasTokenType, SasDefinitionAttributes
        from azure.keyvault import SecretId

        # To create an account sas definition in the vault we must first create the template. The
        # template_uri for an account sas definition is the intended account sas token signed with an arbitrary key.
        # Use the SharedAccessSignature class from azure.storage.common to create a account sas token
        sas = SharedAccessSignature(account_name=storage_account_name,
                                    # don't sign the template with the storage account key use key 00000000
                                    account_key='00000000')
        account_sas_template = sas.generate_account(services='bfqt',  # all services blob, file, queue and table
                                                    resource_types='sco',  # all resources service, template, object
                                                    permission='acdlpruw',
                                                    # all permissions add, create, list, process, read, update, write
                                                    expiry='2020-01-01')  # expiry will be ignored and validity period will determine token expiry

        # use the created template to create a sas definition in the vault
        attributes = SasDefinitionAttributes(enabled=True)
        sas_def = self.client.set_sas_definition(vault_base_url=vault_url,
                                                          storage_account_name=storage_account_name,
                                                          sas_definition_name='acctall',
                                                          template_uri=account_sas_template,
                                                          sas_type=SasTokenType.account,
                                                          validity_period='PT2H',
                                                          sas_definition_attributes=attributes)

        # When the sas definition is created a corresponding managed secret is also created in the vault, the. This
        # secret is used to provision sas tokens according to the sas definition.  Users retrieve the sas token
        # via the get_secret method.

        # get the secret id from the returned SasDefinitionBundle
        sas_secret_id = SecretId(uri=sas_def.secret_id)
        # call get_secret and the value of the returned SecretBundle will be a newly issued sas token
        acct_sas_token = self.client.get_secret(vault_base_url=sas_secret_id.vault,
                                                         secret_name=sas_secret_id.name,
                                                         secret_version=sas_secret_id.version).value

        # create the cloud storage account object
        cloud_storage_account = CloudStorageAccount(account_name=storage_account_name,
                                                    sas_token=acct_sas_token)

        # create a blob with the account sas token
        blob_service = cloud_storage_account.create_block_blob_service()
        blob_service.create_container('blobcontainer')
        blob_service.create_blob_from_text(container_name='blobcontainer',
                                           blob_name='blob1',
                                           text=u'test blob1 data')
Esempio n. 4
0
 def create_account_admin_sas(storageName, key):
     return SharedAccessSignature(account_name=storageName, account_key=key).generate_account(services="bfqt", \
                                                                                              resource_types=ResourceTypes(object=True,service=True,container=True),
                                                                                              permission=AccountPermissions(read=True,list=True,write=True,delete=True,add=True,update=True,process=True,create=True),\
                                                                                              start=datetime.utcnow(), expiry=datetime.utcnow() + timedelta(days=365))