Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
414ae4d
{Doc} Update track2 migration roadmap (#16871)
jsntcy Feb 10, 2021
68d30cc
hotfix: pin version for azure-keyvault-administration (#16887)
houk-ms Feb 10, 2021
8aedd2d
added start/end time in show-log-chain table output (#16753)
akshayneema Feb 10, 2021
f86f327
Merge branch 'release' of github.com:Azure/azure-cli into dev
fengzhou-msft Feb 10, 2021
39fe66a
[SQL] Update az sql mi examples (#16852)
kukislav Feb 18, 2021
c803a54
Use MSIFASTINSTALL (#16903)
Petermarcu Feb 18, 2021
45c185b
{ServiceFabric} Refine code to unblock CI Lint (#16970)
evelyn-ys Feb 19, 2021
f742dbf
[SQL] az sql db/elastic-pool create/update: Added maint-config-id arg…
Feb 19, 2021
bc4d70c
[AppService]: fix ##8743- az webapp deploy (#16715)
Kotasudhakarreddy Feb 19, 2021
58a2ba2
[AKS] Add support for SGX addon 'confcom' (#16869)
Pengpeng-Microsoft Feb 19, 2021
4b6a6c8
{ARM} ts: show command bug fixed, improved error handling for export …
detienne20 Feb 19, 2021
a896091
[Storage] Fix #16872: az storage blob now (2.19) requires login even …
Juliehzl Feb 19, 2021
3739c5e
[Storage] Fix #16959: az storage copy crashes: ValidationError: local…
Juliehzl Feb 19, 2021
1f87589
{Packaging} Bump cryptography from 2.8 to 3.2 (#15687)
dependabot[bot] Feb 19, 2021
61fcd29
[Compute] az vm (extension) image list: Make it more robust (#16992)
qwordy Feb 20, 2021
da146f2
{Packaging} Bump ADAL to 1.2.6 (#16671)
jiasli Feb 20, 2021
69c0684
roleDefinitionName should be roleName (#16781)
houk-ms Feb 20, 2021
0f56b1b
Update _help.py (#17015)
Feb 22, 2021
109f495
expose real exception while getting sdk (#16993)
evelyn-ys Feb 22, 2021
247a1ad
[Compute] vmss create: Fix a license type issue (#17007)
qwordy Feb 22, 2021
ada4878
{Network} Revert deprecation for network route table (#17004)
yonzhan Feb 22, 2021
3221fe2
[Storage] Fix #16679: `az storage blob download` fails with "Permissi…
evelyn-ys Feb 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Storage] Fix Azure#16872: az storage blob now (2.19) requires login …
…even if connection-string is provided (Azure#16885)

* test_storage_blob_azcopy_sync_oauth pass

* refine code

* test pass

* fix style

* remove

* revert
  • Loading branch information
Juliehzl authored Feb 19, 2021
commit a89609125772d15a7a39ab2c2a267717c7198d1b
16 changes: 10 additions & 6 deletions src/azure-cli/azure/cli/command_modules/storage/azcopy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ def client_auth_for_azcopy(cmd, client, service='blob'):
return azcopy_creds

# oauth mode
token_info = Profile(cli_ctx=cmd.cli_ctx).get_raw_token(resource=STORAGE_RESOURCE_ENDPOINT)[0][2]
try:
token_info = _unserialize_non_msi_token_payload(token_info)
except KeyError: # unserialized MSI token payload
raise Exception('MSI auth not yet supported.')
return AzCopyCredentials(token_info=token_info)
if client.token_credential:
token_info = Profile(cli_ctx=cmd.cli_ctx).get_raw_token(resource=STORAGE_RESOURCE_ENDPOINT)[0][2]
try:
token_info = _unserialize_non_msi_token_payload(token_info)
except KeyError as ex: # unserialized token payload
from azure.cli.core.azclierror import ValidationError
raise ValidationError('No {}. MSI auth and service principal are not yet supported.'.format(ex))
return AzCopyCredentials(token_info=token_info)

return None


def storage_client_auth_for_azcopy(client, service):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ def storage_remove(cmd, client, service, target, recursive=None, exclude_pattern
flags.append('--include-path=' + include_path)
if exclude_path is not None:
flags.append('--exclude-path=' + exclude_path)
sas_token = _generate_sas_token(cmd, client.account_name, client.account_key, service=service,
resource_types='co',
permissions='rdl')

sas_token = client.sas_token

if not sas_token and client.account_key:
sas_token = _generate_sas_token(cmd, client.account_name, client.account_key, service=service,
resource_types='co',
permissions='rdl')

azcopy.remove(_add_url_sas(target, sas_token), flags=flags)


Expand All @@ -70,8 +75,14 @@ def storage_blob_sync(cmd, client, source, destination, exclude_pattern=None, in
flags.append('--exclude-pattern=' + exclude_pattern)
if exclude_path is not None:
flags.append('--exclude-path=' + exclude_path)
sas_token = _generate_sas_token(cmd, client.account_name, client.account_key, service='blob', resource_types='co',
permissions='rwdlac')

sas_token = client.sas_token

if not sas_token and client.account_key:
sas_token = _generate_sas_token(cmd, client.account_name, client.account_key, service='blob',
resource_types='co',
permissions='rwdlac')

azcopy.sync(source, _add_url_sas(destination, sas_token), flags=flags)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StorageAzcopyTests(StorageScenarioMixin, LiveScenarioTest):
@StorageAccountPreparer()
@StorageTestFilesPreparer()
def test_storage_blob_azcopy_sync(self, resource_group, storage_account_info, test_dir):
storage_account, _ = storage_account_info
storage_account, account_key = storage_account_info
container = self.create_container(storage_account_info)

# sync directory
Expand Down Expand Up @@ -100,6 +100,116 @@ def test_storage_blob_azcopy_sync(self, resource_group, storage_account_info, te
.assert_with_checks(JMESPathCheck("name", "sample.js"),
JMESPathCheck("properties.contentSettings.contentType", "application/javascript"))

@ResourceGroupPreparer()
@StorageAccountPreparer()
@StorageTestFilesPreparer()
def test_storage_blob_azcopy_sync_oauth(self, storage_account, test_dir):
container = self.create_random_name(prefix='container', length=20)

self.oauth_cmd('storage container create -n {} --account-name {} '.format(container, storage_account), checks=[
JMESPathCheck('created', True)])

# sync directory
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 41))

self.oauth_cmd('storage blob delete-batch -s {} --account-name {}'.format(
container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 0))

# resync container
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 41))

# update file
with open(os.path.join(test_dir, 'readme'), 'w') as f:
f.write('updated.')
# sync one blob
self.oauth_cmd('storage blob list -c {} --account-name {} --prefix readme'.format(
container, storage_account), checks=JMESPathCheck('[0].properties.contentLength', 87))
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {} -d readme'.format(
os.path.join(test_dir, 'readme'), container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {} --prefix readme'.format(
container, storage_account), checks=JMESPathCheck('[0].properties.contentLength', 8))

# delete one file and sync
os.remove(os.path.join(test_dir, 'readme'))
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 40))

# delete one folder and sync
shutil.rmtree(os.path.join(test_dir, 'apple'))
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 30))

# sync with another folder
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
os.path.join(test_dir, 'butter'), container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 20))

# empty the folder and sync
shutil.rmtree(os.path.join(test_dir, 'butter'))
shutil.rmtree(os.path.join(test_dir, 'duff'))
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 0))

# sync a subset of files in a directory
with open(os.path.join(test_dir, 'test.json'), 'w') as f:
f.write('updated.')
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {} --include-pattern *.json'.format(
test_dir, container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 1))

self.oauth_cmd('storage blob delete-batch -s {} --account-name {}'.format(
container, storage_account))
self.oauth_cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 0))

# sync with guessing content-type
cur = os.path.split(os.path.realpath(__file__))[0]
source = os.path.join(cur, "testdir")
self.oauth_cmd('storage blob sync -s "{}" -c {} --account-name {} ', source, container, storage_account)

self.oauth_cmd('storage blob show -n "sample.js" -c {} --account-name {} ', container, storage_account)\
.assert_with_checks(JMESPathCheck("name", "sample.js"),
JMESPathCheck("properties.contentSettings.contentType", "application/javascript"))

@ResourceGroupPreparer()
@StorageAccountPreparer()
@StorageTestFilesPreparer()
def test_storage_blob_azcopy_sync_sas(self, resource_group, storage_account, test_dir):
from datetime import datetime, timedelta
expiry = (datetime.utcnow() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%MZ')

account_info = self.get_account_info(resource_group, storage_account)
container = self.create_random_name(prefix='container', length=20)

self.oauth_cmd('storage container create -n {} --account-name {} '.format(container, storage_account), checks=[
JMESPathCheck('created', True)])

sas = self.storage_cmd('storage container generate-sas -n {} --https-only --permissions dlrw --expiry {} -otsv',
account_info, container, expiry).output.strip()
# sync directory
self.cmd('storage blob list -c {} --account-name {} --sas-token {} '.format(
container, storage_account, sas), checks=JMESPathCheck('length(@)', 0))
self.cmd('storage blob sync -s "{}" -c {} --account-name {} --sas-token {} '.format(
test_dir, container, storage_account, sas))
self.cmd('storage blob list -c {} --account-name {} --sas-token {}'.format(
container, storage_account, sas), checks=JMESPathCheck('length(@)', 41))

@ResourceGroupPreparer()
@StorageAccountPreparer()
@StorageTestFilesPreparer()
Expand Down Expand Up @@ -225,6 +335,49 @@ def test_storage_file_azcopy_remove(self, resource_group, storage_account):
self.storage_cmd('storage file list -s {}', account_info, s2) \
.assert_with_checks(JMESPathCheck('length(@)', 0))

@ResourceGroupPreparer()
@StorageAccountPreparer()
@StorageTestFilesPreparer()
def test_storage_azcopy_remove_sas(self, resource_group, storage_account, test_dir):
from datetime import datetime, timedelta
expiry = (datetime.utcnow() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%MZ')

account_info = self.get_account_info(resource_group, storage_account)
container = self.create_container(account_info, prefix='container', length=20)

sas = self.storage_cmd(
'storage account generate-sas --https-only --permissions dlr --resource-types co --services bf --expiry {} -otsv',
account_info, expiry).output.strip()
# remove blob
self.cmd('storage blob sync -s "{}" -c {} --account-name {}'.format(
test_dir, container, storage_account))
self.cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 41))

self.cmd('storage remove -c {} -n readme --account-name {} --sas-token {}'.format(
container, storage_account, sas))

self.cmd('storage blob list -c {} --account-name {}'.format(
container, storage_account), checks=JMESPathCheck('length(@)', 40))

# remove file share
s1 = self.create_share(account_info)
d1 = 'dir1'

local_file = self.create_temp_file(512, full_random=False)
src1_file = os.path.join(d1, 'source_file1.txt')

self.storage_cmd('storage directory create --share-name {} -n {}', account_info, s1, d1)
self.storage_cmd('storage file upload -p "{}" --share-name {} --source "{}"', account_info,
src1_file, s1, local_file)
self.storage_cmd('storage file exists -p "{}" -s {}', account_info, src1_file, s1) \
.assert_with_checks(JMESPathCheck('exists', True))

self.storage_cmd('storage remove --share-name {} -p "{}" --sas-token {} ',
account_info, s1, src1_file, sas)
self.storage_cmd('storage file exists -p "{}" -s {}', account_info, src1_file, s1) \
.assert_with_checks(JMESPathCheck('exists', False))

@ResourceGroupPreparer()
@StorageAccountPreparer(parameter_name='first_account')
@StorageAccountPreparer(parameter_name='second_account', sku='Premium_LRS', kind='BlockBlobStorage')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def get_account_info(self, group, name):
return name, self.get_account_key(group, name)

def oauth_cmd(self, cmd, *args, **kwargs):
if args:
cmd = cmd.format(*args)
args = ()
return self.cmd(cmd + ' --auth-mode login', *args, **kwargs)

def storage_cmd(self, cmd, account_info, *args):
Expand Down