Skip to content

Commit a9bbbc0

Browse files
author
Matthew Russo
committed
adds aws ecr get-login-password customization
as proposed in aws#4867, and previously discussed in aws#2875 (comment) aws#3687 (comment) this commit adds a new customization command for ECR that only returns the password to login to a registry. this approach is composable, is compatible with other container clients, allows use of functionality like Docker's --password-stdin flag, and is more resilient to changes in client APIs
1 parent b616864 commit a9bbbc0

File tree

5 files changed

+145
-1
lines changed

5 files changed

+145
-1
lines changed

awscli/customizations/ecr.py

+38
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
def register_ecr_commands(cli):
2121
cli.register('building-command-table.ecr', _inject_get_login)
22+
cli.register('building-command-table.ecr', _inject_get_login_password)
2223

2324

2425
def _inject_get_login(command_table, session, **kwargs):
2526
command_table['get-login'] = ECRLogin(session)
2627

28+
def _inject_get_login_password(command_table, session, **kwargs):
29+
command_table['get-login-password'] = ECRGetLoginPassword(session)
2730

2831
class ECRLogin(BasicCommand):
2932
"""Log in with docker login"""
@@ -83,3 +86,38 @@ def _run_main(self, parsed_args, parsed_globals):
8386
sys.stdout.write(' '.join(command))
8487
sys.stdout.write('\n')
8588
return 0
89+
90+
91+
class ECRGetLoginPassword(BasicCommand):
92+
"""Get a password to be used with container clients such as Docker"""
93+
NAME = 'get-login-password'
94+
95+
DESCRIPTION = BasicCommand.FROM_FILE(
96+
'ecr/get-login-password_description.rst')
97+
98+
ARG_TABLE = [
99+
{
100+
'name': 'registry-ids',
101+
'help_text': 'A list of AWS account IDs that correspond to the '
102+
'Amazon ECR registries that you want to log in to.',
103+
'required': False,
104+
'nargs': '+'
105+
},
106+
]
107+
108+
def _run_main(self, parsed_args, parsed_globals):
109+
ecr_client = create_client_from_parsed_globals(
110+
self._session,
111+
'ecr',
112+
parsed_globals)
113+
if not parsed_args.registry_ids:
114+
result = ecr_client.get_authorization_token()
115+
else:
116+
result = ecr_client.get_authorization_token(
117+
registryIds=parsed_args.registry_ids)
118+
for auth in result['authorizationData']:
119+
auth_token = b64decode(auth['authorizationToken']).decode()
120+
_, password = auth_token.split(':')
121+
sys.stdout.write(password)
122+
sys.stdout.write('\n')
123+
return 0
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
**To retrieve a password to your default registry**
2+
3+
This example prints a password that you can use with a container client of your
4+
choice to log in to your default Amazon ECR registry.
5+
6+
Command::
7+
8+
aws ecr get-login-password
9+
10+
Output::
11+
12+
<password>
13+
14+
Usage::
15+
16+
aws ecr get-login-password | docker login --username AWS --password-stdin 111111111111.dkr.ecr.us-west-2.amazonaws.com
17+
18+
**To get passwords to another account's registry**
19+
20+
This example prints one or more passwords that you can use to log in to
21+
Amazon ECR registries associated with other accounts.
22+
23+
Command::
24+
25+
aws ecr get-login-password --registry-ids 012345678910 023456789012
26+
27+
Output::
28+
29+
<password-for-registry-012345678910>
30+
<password-for-registry-023456789012>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
**To log in to an Amazon ECR registry**
2+
3+
This command retrieves and prints a password that is valid for a specified
4+
registry for 12 hours. You can pass the password to the login command of the
5+
container client of your preference, such as Docker. After you have logged in
6+
to an Amazon ECR registry with this command, you can use the Docker CLI to push
7+
and pull images from that registry until the token expires.
8+
9+
.. note::
10+
11+
This command displays password(s) to stdout with authentication credentials.
12+
Your credentials could be visible by other users on your system in a process
13+
list display or a command history. If you are not on a secure system, you
14+
should consider this risk and login interactively. For more information,
15+
see ``get-authorization-token``.

awscli/examples/ecr/get-login_description.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ token expires.
99

1010
.. note::
1111

12-
This command writes displays ``docker login`` commands to stdout with
12+
This command displays ``docker login`` commands to stdout with
1313
authentication credentials. Your credentials could be visible by other
1414
users on your system in a process list display or a command history. If you
1515
are not on a secure system, you should consider this risk and login
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License'). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the 'license' file accompanying this file. This file is
10+
# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
14+
from awscli.testutils import BaseAWSCommandParamsTest
15+
16+
17+
class TestGetLoginPasswordCommand(BaseAWSCommandParamsTest):
18+
def setUp(self):
19+
super(TestGetLoginPasswordCommand, self).setUp()
20+
self.parsed_responses = [
21+
{
22+
'authorizationData': [
23+
{
24+
"authorizationToken": "Zm9vOmJhcg==",
25+
"proxyEndpoint": "1235.ecr.us-east-1.io",
26+
"expiresAt": "2015-10-16T00:00:00Z"
27+
}
28+
]
29+
},
30+
]
31+
32+
def test_prints_get_login_command(self):
33+
stdout = self.run_cmd("ecr get-login-password")[0]
34+
self.assertIn('bar', stdout)
35+
self.assertEquals(1, len(self.operations_called))
36+
self.assertNotIn('registryIds', self.operations_called[0][1])
37+
38+
def test_prints_multiple_get_login_commands(self):
39+
self.parsed_responses = [
40+
{
41+
'authorizationData': [
42+
{
43+
"authorizationToken": "Zm9vOmJhcg==",
44+
"proxyEndpoint": "1235.ecr.us-east-1.io",
45+
"expiresAt": "2015-10-16T00:00:00Z"
46+
},
47+
{
48+
"authorizationToken": "YWJjOjEyMw==",
49+
"proxyEndpoint": "4567.ecr.us-east-1.io",
50+
"expiresAt": "2015-10-16T00:00:00Z"
51+
}
52+
]
53+
},
54+
]
55+
stdout = self.run_cmd(
56+
"ecr get-login-password --registry-ids 1234 5678")[0]
57+
self.assertIn('bar\n', stdout)
58+
self.assertIn('123\n', stdout)
59+
self.assertEquals(1, len(self.operations_called))
60+
self.assertEquals([u'1234', u'5678'],
61+
self.operations_called[0][1]['registryIds'])

0 commit comments

Comments
 (0)