Skip to content

Commit 8969480

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 8969480

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

awscli/customizations/ecr.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818

1919

2020
def register_ecr_commands(cli):
21-
cli.register('building-command-table.ecr', _inject_get_login)
21+
cli.register('building-command-table.ecr', _inject_commands)
2222

2323

24-
def _inject_get_login(command_table, session, **kwargs):
24+
def _inject_commands(command_table, session, **kwargs):
2525
command_table['get-login'] = ECRLogin(session)
26+
command_table['get-login-password'] = ECRGetLoginPassword(session)
2627

2728

2829
class ECRLogin(BasicCommand):
29-
"""Log in with docker login"""
30+
"""Log in with 'docker login'"""
3031
NAME = 'get-login'
3132

3233
DESCRIPTION = BasicCommand.FROM_FILE('ecr/get-login_description.rst')
@@ -49,8 +50,8 @@ class ECRLogin(BasicCommand):
4950
'help_text': (
5051
"Specify if the '-e' flag should be included in the "
5152
"'docker login' command. The '-e' option has been deprecated "
52-
"and is removed in docker version 17.06 and later. You must "
53-
"specify --no-include-email if you're using docker version "
53+
"and is removed in Docker version 17.06 and later. You must "
54+
"specify --no-include-email if you're using Docker version "
5455
"17.06 or later. The default behavior is to include the "
5556
"'-e' flag in the 'docker login' output."),
5657
},
@@ -83,3 +84,23 @@ def _run_main(self, parsed_args, parsed_globals):
8384
sys.stdout.write(' '.join(command))
8485
sys.stdout.write('\n')
8586
return 0
87+
88+
89+
class ECRGetLoginPassword(BasicCommand):
90+
"""Get a password to be used with container clients such as Docker"""
91+
NAME = 'get-login-password'
92+
93+
DESCRIPTION = BasicCommand.FROM_FILE(
94+
'ecr/get-login-password_description.rst')
95+
96+
def _run_main(self, parsed_args, parsed_globals):
97+
ecr_client = create_client_from_parsed_globals(
98+
self._session,
99+
'ecr',
100+
parsed_globals)
101+
result = ecr_client.get_authorization_token()
102+
auth = result['authorizationData'][0]
103+
auth_token = b64decode(auth['authorizationToken']).decode()
104+
_, password = auth_token.split(':')
105+
sys.stdout.write(password)
106+
return 0
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 with Docker::
15+
16+
aws ecr get-login-password | docker login --username AWS --password-stdin https://<aws_account_id>.dkr.ecr.<region>.amazonaws.com
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,35 @@
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))

0 commit comments

Comments
 (0)