Skip to content

Commit 0cf42e2

Browse files
[ACR] az acr login: Add refreshToken and username fields to the output after using --expose-token parameter (#31091)
1 parent a372c3b commit 0cf42e2

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/azure-cli/azure/cli/command_modules/acr/_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@
342342
helps['acr login'] = """
343343
type: command
344344
short-summary: Log in to an Azure Container Registry through the Docker CLI.
345-
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')
345+
long-summary: Docker must be installed on your machine. Once done, use `docker logout <registry url>` to log out. (If you only need a refresh token and do not want to install Docker, specify '--expose-token')
346346
examples:
347347
- name: Log in to an Azure Container Registry
348348
text: >

src/azure-cli/azure/cli/command_modules/acr/_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def load_arguments(self, _): # pylint: disable=too-many-statements
168168
c.argument('days', type=int, help='The number of days to retain a soft-deleted manifest or tag after which it gets purged (Range: 1 to 90). Default is 7.')
169169

170170
with self.argument_context('acr login') as c:
171-
c.argument('expose_token', options_list=['--expose-token', '-t'], help='Expose access token instead of automatically logging in through Docker CLI', action='store_true')
171+
c.argument('expose_token', options_list=['--expose-token', '-t'], help='Expose refresh token instead of automatically logging in through Docker CLI', action='store_true')
172172

173173
with self.argument_context('acr repository') as c:
174174
c.argument('resource_group_name', deprecate_info=c.deprecate(hide=True))

src/azure-cli/azure/cli/command_modules/acr/custom.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,25 @@ def acr_login(cmd,
299299
password=password,
300300
resource_group_name=resource_group_name)
301301

302-
logger.warning("You can perform manual login using the provided access token below, "
303-
"for example: 'docker login loginServer -u %s -p accessToken'", EMPTY_GUID)
302+
logger.warning("Note: The token in both the accessToken and refreshToken fields is "
303+
"an ACR Refresh Token, not an ACR Access Token. This ACR Refresh Token cannot be used "
304+
"directly to authenticate with registry APIs such as pushing/pulling images and listing "
305+
"repositories/tags. This ACR Refresh Token must be subsequently exchanged for an ACR Access."
306+
"Please see https://aka.ms/acr/auth/oauth")
307+
308+
logger.warning("You can perform manual login using the provided refresh token below, "
309+
"for example: 'docker login loginServer -u %s -p refreshToken'", EMPTY_GUID)
304310

305311
token_info = {
306312
"loginServer": login_server,
307-
"accessToken": password
313+
"username": EMPTY_GUID,
314+
"accessToken": password,
315+
"refreshToken": password
308316
}
309317

310318
return token_info
311319

312-
tips = "You may want to use 'az acr login -n {} --expose-token' to get an access token, " \
320+
tips = "You may want to use 'az acr login -n {} --expose-token' to get a refresh token, " \
313321
"which does not require Docker to be installed.".format(registry_name)
314322

315323
from azure.cli.core.util import in_cloud_console

src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_commands.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from azure.cli.testsdk.scenario_tests import AllowLargeResponse
77
from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer, KeyVaultPreparer, record_only, live_only
8-
from azure.cli.command_modules.acr.custom import DEF_DIAG_SETTINGS_NAME_TEMPLATE
8+
from azure.cli.command_modules.acr.custom import DEF_DIAG_SETTINGS_NAME_TEMPLATE, EMPTY_GUID
99
from azure.cli.core.commands.client_factory import get_subscription_id
1010
import time
1111

@@ -94,7 +94,30 @@ def test_check_name_availability_dnl_scope(self):
9494
self.check('nameAvailable', True),
9595
self.check_pattern('availableLoginServerName',r'{name}-[a-zA-Z0-9]+\.*')
9696
])
97+
98+
@live_only()
99+
@ResourceGroupPreparer()
100+
def test_acr_login_expose_token(self, resource_group):
101+
registry_name = self.create_random_name('clireg', 20)
102+
103+
self.kwargs.update({
104+
'registry_name': registry_name,
105+
'rg': resource_group,
106+
'sku': 'Premium'
107+
})
108+
109+
self.cmd('acr create -n {registry_name} -g {rg} --sku {sku}',
110+
checks=[self.check('name', '{registry_name}'),
111+
self.check('provisioningState', 'Succeeded')])
112+
113+
tokens = self.cmd('acr login -n {} --expose-token'.format(registry_name), checks=[
114+
self.exists('accessToken'),
115+
self.exists('refreshToken'),
116+
self.exists('loginServer'),
117+
self.check('username', EMPTY_GUID)]).get_output_in_json()
97118

119+
self.assertEqual(tokens['accessToken'], tokens['refreshToken'])
120+
98121
@ResourceGroupPreparer()
99122
@live_only()
100123
def test_acr_create_with_managed_registry(self, resource_group, resource_group_location):

0 commit comments

Comments
 (0)