Repository: aws/aws-cli

Issue:
Proposal: aws ecr get-login-password
This is a proposal for a new AWS CLI command for ECR

```
$ aws ecr get-login-password
cGFzc3dvcmQ=
```

This command can be used in the following ways:

```
$ aws ecr get-login-password | docker login --username AWS --password-stdin 111111111111.dkr.ecr.us-west-2.amazonaws.com
Login Succeeded

$ docker login --username AWS --password "$(aws ecr get-login-password)" 111111111111.dkr.ecr.us-west-2.amazonaws.com
Login Succeeded
```

This idea has been previously proposed by @theY4Kman https://github.com/aws/aws-cli/issues/2875#issuecomment-433565983 and @kojiromike https://github.com/aws/aws-cli/issues/3687#issue-374397564


Discussion:

Resolving patch:
diff --git a/awscli/customizations/ecr.py b/awscli/customizations/ecr.py
--- a/awscli/customizations/ecr.py
+++ b/awscli/customizations/ecr.py
@@ -18,15 +18,16 @@
 
 
 def register_ecr_commands(cli):
-    cli.register('building-command-table.ecr', _inject_get_login)
+    cli.register('building-command-table.ecr', _inject_commands)
 
 
-def _inject_get_login(command_table, session, **kwargs):
+def _inject_commands(command_table, session, **kwargs):
     command_table['get-login'] = ECRLogin(session)
+    command_table['get-login-password'] = ECRGetLoginPassword(session)
 
 
 class ECRLogin(BasicCommand):
-    """Log in with docker login"""
+    """Log in with 'docker login'"""
     NAME = 'get-login'
 
     DESCRIPTION = BasicCommand.FROM_FILE('ecr/get-login_description.rst')
@@ -49,8 +50,8 @@ class ECRLogin(BasicCommand):
             'help_text': (
                 "Specify if the '-e' flag should be included in the "
                 "'docker login' command.  The '-e' option has been deprecated "
-                "and is removed in docker version 17.06 and later.  You must "
-                "specify --no-include-email if you're using docker version "
+                "and is removed in Docker version 17.06 and later.  You must "
+                "specify --no-include-email if you're using Docker version "
                 "17.06 or later.  The default behavior is to include the "
                 "'-e' flag in the 'docker login' output."),
         },
@@ -83,3 +84,24 @@ def _run_main(self, parsed_args, parsed_globals):
             sys.stdout.write(' '.join(command))
             sys.stdout.write('\n')
         return 0
+
+
+class ECRGetLoginPassword(BasicCommand):
+    """Get a password to be used with container clients such as Docker"""
+    NAME = 'get-login-password'
+
+    DESCRIPTION = BasicCommand.FROM_FILE(
+            'ecr/get-login-password_description.rst')
+
+    def _run_main(self, parsed_args, parsed_globals):
+        ecr_client = create_client_from_parsed_globals(
+                self._session,
+                'ecr',
+                parsed_globals)
+        result = ecr_client.get_authorization_token()
+        auth = result['authorizationData'][0]
+        auth_token = b64decode(auth['authorizationToken']).decode()
+        _, password = auth_token.split(':')
+        sys.stdout.write(password)
+        sys.stdout.write('\n')
+        return 0


Test patch:
diff --git a/tests/functional/ecr/test_get_login_password.py b/tests/functional/ecr/test_get_login_password.py
new file mode 100644
--- /dev/null
+++ b/tests/functional/ecr/test_get_login_password.py
@@ -0,0 +1,35 @@
+# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the 'License'). You
+# may not use this file except in compliance with the License. A copy of
+# the License is located at
+#
+#     http://aws.amazon.com/apache2.0/
+#
+# or in the 'license' file accompanying this file. This file is
+# distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
+# ANY KIND, either express or implied. See the License for the specific
+# language governing permissions and limitations under the License.
+
+from awscli.testutils import BaseAWSCommandParamsTest
+
+
+class TestGetLoginPasswordCommand(BaseAWSCommandParamsTest):
+    def setUp(self):
+        super(TestGetLoginPasswordCommand, self).setUp()
+        self.parsed_responses = [
+            {
+                'authorizationData': [
+                    {
+                        "authorizationToken": "Zm9vOmJhcg==",
+                        "proxyEndpoint": "1235.ecr.us-east-1.io",
+                        "expiresAt": "2015-10-16T00:00:00Z"
+                    }
+                ]
+            },
+        ]
+
+    def test_prints_get_login_command(self):
+        stdout = self.run_cmd("ecr get-login-password")[0]
+        self.assertIn('bar', stdout)
+        self.assertEquals(1, len(self.operations_called))