From bf212846e2190c5fc38c94da5eafb4b87ce94b73 Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Fri, 9 Apr 2021 17:02:04 +0800 Subject: [PATCH 1/4] refine error message --- .../command_modules/storage/_validators.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/storage/_validators.py b/src/azure-cli/azure/cli/command_modules/storage/_validators.py index 6406e4ef996..7aa8452ce4e 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/storage/_validators.py @@ -177,14 +177,17 @@ def validate_client_parameters(cmd, namespace): # if account name is specified but no key, attempt to query if n.account_name and not n.account_key and not n.sas_token: - logger.warning('There are no credentials provided in your command and environment, we will query for the ' - 'account key inside your storage account. \nPlease provide --connection-string, ' - '--account-key or --sas-token as credentials, or use `--auth-mode login` if you ' - 'have required RBAC roles in your command. For more information about RBAC roles ' - 'in storage, visit ' - 'https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli. \n' - 'Setting the corresponding environment variables can avoid inputting credentials in ' - 'your command. Please use --help to get more information.') + message = """ +There are no credentials provided in your command and environment, we will query for account key for your storage account. +It is recommended to provide --connection-string, --account-key or --sas-token in your command as credentials. +""" + if hasattr(n, 'auth_mode') or hasattr(n, 'token_credential'): + message += """ +You also can add `--auth-mode login` in your command to use Azure Active Directory (Azure AD) for authorization if your login account is assigned required RBAC roles. +For more information about RBAC roles in storage, visit https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli. +""" + logger.warning('{}In addition, setting the corresponding environment variables can avoid inputting credentials ' + 'in your command. Please use --help to get more information about environment variable usage.'.format(message)) try: n.account_key = _query_account_key(cmd.cli_ctx, n.account_name) except Exception as ex: # pylint: disable=broad-except From 7494b8dc474edd505b952ad2c94d4fdfc3535ddf Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Fri, 9 Apr 2021 17:24:42 +0800 Subject: [PATCH 2/4] fix --- .../azure/cli/command_modules/storage/_validators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/storage/_validators.py b/src/azure-cli/azure/cli/command_modules/storage/_validators.py index 7aa8452ce4e..c096b0b66a6 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/storage/_validators.py @@ -181,12 +181,12 @@ def validate_client_parameters(cmd, namespace): There are no credentials provided in your command and environment, we will query for account key for your storage account. It is recommended to provide --connection-string, --account-key or --sas-token in your command as credentials. """ - if hasattr(n, 'auth_mode') or hasattr(n, 'token_credential'): + if 'auth_mode' in cmd.arguments: message += """ You also can add `--auth-mode login` in your command to use Azure Active Directory (Azure AD) for authorization if your login account is assigned required RBAC roles. For more information about RBAC roles in storage, visit https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli. """ - logger.warning('{}In addition, setting the corresponding environment variables can avoid inputting credentials ' + logger.warning('{}\nIn addition, setting the corresponding environment variables can avoid inputting credentials ' 'in your command. Please use --help to get more information about environment variable usage.'.format(message)) try: n.account_key = _query_account_key(cmd.cli_ctx, n.account_name) From 13e2d4788943f31a715ad91a22037cccc24b7c49 Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Mon, 12 Apr 2021 18:06:52 +0800 Subject: [PATCH 3/4] fix test --- .../storage/tests/latest/test_storage_validators.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_validators.py b/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_validators.py index ad26faadae7..7346dbfcf59 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_validators.py +++ b/src/azure-cli/azure/cli/command_modules/storage/tests/latest/test_storage_validators.py @@ -41,9 +41,10 @@ def get_models(self, *attr_args, **_): class MockCmd(object): - def __init__(self, ctx): + def __init__(self, ctx, arguments={}): self.cli_ctx = ctx self.loader = MockLoader(self.cli_ctx) + self.arguments = arguments def get_models(self, *attr_args, **kwargs): return get_sdk(self.cli_ctx, ResourceType.DATA_STORAGE, *attr_args, **kwargs) From dbdba9e301ec4bb1cdd5d23ff1b07166dd86b76c Mon Sep 17 00:00:00 2001 From: Zunli Hu Date: Mon, 12 Apr 2021 18:33:13 +0800 Subject: [PATCH 4/4] fix style --- .../azure/cli/command_modules/storage/_validators.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/storage/_validators.py b/src/azure-cli/azure/cli/command_modules/storage/_validators.py index c096b0b66a6..b528ac9e7ed 100644 --- a/src/azure-cli/azure/cli/command_modules/storage/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/storage/_validators.py @@ -186,8 +186,9 @@ def validate_client_parameters(cmd, namespace): You also can add `--auth-mode login` in your command to use Azure Active Directory (Azure AD) for authorization if your login account is assigned required RBAC roles. For more information about RBAC roles in storage, visit https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-cli. """ - logger.warning('{}\nIn addition, setting the corresponding environment variables can avoid inputting credentials ' - 'in your command. Please use --help to get more information about environment variable usage.'.format(message)) + logger.warning('%s\nIn addition, setting the corresponding environment variables can avoid inputting ' + 'credentials in your command. Please use --help to get more information about environment ' + 'variable usage.', message) try: n.account_key = _query_account_key(cmd.cli_ctx, n.account_name) except Exception as ex: # pylint: disable=broad-except