Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ACR] Add a new argument '--expose-token' for 'az acr login' #12077

Merged
merged 3 commits into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion src/azure-cli/azure/cli/command_modules/acr/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <registry url>' to log out.
long-summary: Docker must be installed on your machine. Once done, use 'docker logout <registry url>' 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'] = """
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/azure/cli/command_modules/acr/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
37 changes: 33 additions & 4 deletions src/azure-cli/azure/cli/command_modules/acr/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down