diff --git a/src/azure-cli/azure/cli/command_modules/acr/_help.py b/src/azure-cli/azure/cli/command_modules/acr/_help.py index c90c30683f9..5acb90af949 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/_help.py +++ b/src/azure-cli/azure/cli/command_modules/acr/_help.py @@ -257,11 +257,14 @@ helps['acr login'] = """ type: command short-summary: Log in to an Azure Container Registry through the Docker CLI. -long-summary: Docker must be installed on your machine. Once done, use 'docker logout ' to log out. +long-summary: Docker must be installed on your machine. Once done, use 'docker logout ' to log out. (If you only need an access token and do not want to install Docker, specify '--expose-token') examples: - name: Log in to an Azure Container Registry text: > az acr login -n MyRegistry + - name: Get an Azure Container Registry access token + text: > + az acr login -n MyRegistry --expose-token """ helps['acr network-rule'] = """ diff --git a/src/azure-cli/azure/cli/command_modules/acr/_params.py b/src/azure-cli/azure/cli/command_modules/acr/_params.py index 88de5dec4e1..7b49e6e21ec 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/_params.py +++ b/src/azure-cli/azure/cli/command_modules/acr/_params.py @@ -93,6 +93,7 @@ def load_arguments(self, _): # pylint: disable=too-many-statements with self.argument_context('acr login') as c: c.argument('resource_group_name', deprecate_info=c.deprecate(hide=True)) + c.argument('expose_token', options_list=['--expose-token', '-t'], help='Expose access token instead of automatically logging in through Docker CLI', action='store_true', is_preview=True) with self.argument_context('acr repository') as c: c.argument('resource_group_name', deprecate_info=c.deprecate(hide=True)) diff --git a/src/azure-cli/azure/cli/command_modules/acr/custom.py b/src/azure-cli/azure/cli/command_modules/acr/custom.py index 43171e7888f..ae327b4b0ad 100644 --- a/src/azure-cli/azure/cli/command_modules/acr/custom.py +++ b/src/azure-cli/azure/cli/command_modules/acr/custom.py @@ -14,7 +14,7 @@ get_resource_group_name_by_registry_name, user_confirmation ) -from ._docker_utils import get_login_credentials +from ._docker_utils import get_login_credentials, EMPTY_GUID from .network_rule import NETWORK_RULE_NOT_SUPPORTED logger = get_logger(__name__) @@ -129,12 +129,39 @@ def acr_login(cmd, resource_group_name=None, # pylint: disable=unused-argument tenant_suffix=None, username=None, - password=None): + password=None, + expose_token=False): + if expose_token: + login_server, _, password = get_login_credentials( + cmd=cmd, + registry_name=registry_name, + tenant_suffix=tenant_suffix, + username=username, + password=password) + + logger.warning("You can perform manual login using the provided access token below, " + "for example: 'docker login loginServer -u %s -p accessToken'", EMPTY_GUID) + + token_info = { + "loginServer": login_server, + "accessToken": password + } + + return token_info + + tips = "You may want to use 'az acr login -n {} --expose-token' to get an access token, " \ + "which does not require Docker to be installed.".format(registry_name) + from azure.cli.core.util import in_cloud_console if in_cloud_console(): - raise CLIError('This command requires running the docker daemon, which is not supported in Azure Cloud Shell.') + raise CLIError("This command requires running the docker daemon, " + "which is not supported in Azure Cloud Shell. " + tips) - docker_command, _ = get_docker_command() + try: + docker_command, _ = get_docker_command() + except CLIError as e: + logger.warning(tips) + raise e login_server, username, password = get_login_credentials( cmd=cmd, @@ -174,6 +201,8 @@ def acr_login(cmd, output = getattr(sys.stderr, 'buffer', sys.stderr) output.write(stderr) + return None + def acr_show_usage(cmd, client, registry_name, resource_group_name=None): _, resource_group_name = validate_managed_registry(cmd,