Skip to content

Commit 5d5d0c3

Browse files
authored
Merge pull request #8870 from hssyoo/codeartifact-v1
2 parents 9c73169 + 8667ec4 commit 5d5d0c3

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "enhancement",
3+
"category": "``codeartifact``",
4+
"description": "Update login command error message."
5+
}

awscli/customizations/codeartifact/login.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def get_relative_expiration_time(remaining):
3838

3939

4040
class CommandFailedError(Exception):
41-
def __init__(self, called_process_error):
42-
msg = str(called_process_error)
41+
def __init__(self, called_process_error, auth_token):
42+
msg = str(called_process_error).replace(auth_token, '******')
4343
if called_process_error.stderr is not None:
4444
msg +=(
4545
f' Stderr from command:\n'
@@ -105,7 +105,7 @@ def _run_command(self, tool, command, *, ignore_errors=False):
105105
)
106106
except subprocess.CalledProcessError as ex:
107107
if not ignore_errors:
108-
raise CommandFailedError(ex)
108+
raise CommandFailedError(ex, self.auth_token)
109109
except OSError as ex:
110110
if ex.errno == errno.ENOENT:
111111
raise ValueError(
@@ -305,7 +305,7 @@ def login(self, dry_run=False):
305305
)
306306
except subprocess.CalledProcessError as e:
307307
uni_print('Failed to update the NuGet.Config\n')
308-
raise CommandFailedError(e)
308+
raise CommandFailedError(e, self.auth_token)
309309

310310
uni_print(source_configured_message % source_name)
311311
self._write_success_message('nuget')
@@ -725,7 +725,8 @@ class CodeArtifactLogin(BasicCommand):
725725
'action': 'store_true',
726726
'help_text': 'Only print the commands that would be executed '
727727
'to connect your tool with your repository without '
728-
'making any changes to your configuration',
728+
'making any changes to your configuration. Note that '
729+
'this prints the unredacted auth token as part of the output',
729730
'required': False,
730731
'default': False
731732
},

tests/functional/codeartifact/test_codeartifact_login.py

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import platform
44
import subprocess
55
import time
6+
import re
67

78
from botocore.utils import parse_timestamp
89

@@ -962,6 +963,24 @@ def test_pip_login_with_namespace_dry_run(self):
962963
'Argument --namespace is not supported for pip', result.stderr
963964
)
964965

966+
def test_pip_login_command_failed_auth_token_redacted(self):
967+
def side_effect(command, capture_output, check):
968+
raise subprocess.CalledProcessError(
969+
returncode=1,
970+
cmd=command
971+
)
972+
973+
self.subprocess_mock.side_effect = side_effect
974+
cmdline = self._setup_cmd(tool='pip')
975+
result = self.cli_runner.run(cmdline)
976+
self.assertEqual(result.rc, 255)
977+
self.assertIn(
978+
"Command '['pip', 'config', 'set', 'global.index-url',"
979+
" 'https://aws:******@domain-domain-owner.codeartifact.aws.a2z.com/pypi/repository/simple/']'"
980+
" returned non-zero exit status 1.",
981+
result.stderr
982+
)
983+
965984
def test_twine_login_without_domain_owner(self):
966985
cmdline = self._setup_cmd(tool='twine')
967986
result = self.cli_runner.run(cmdline)

tests/unit/customizations/codeartifact/test_adapter_login.py

+15
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ def test_run_commands_command_failed(self):
6868
):
6969
self.test_subject._run_commands('tool', ['cmd'])
7070

71+
def test_run_commands_command_failed_redact_auth_token(self):
72+
error_to_be_caught = subprocess.CalledProcessError(
73+
returncode=1,
74+
cmd=['cmd', 'with', 'auth-token', 'present'],
75+
output=None,
76+
stderr=b'Command error message.'
77+
)
78+
self.subprocess_utils.run.side_effect = error_to_be_caught
79+
with self.assertRaisesRegex(
80+
CommandFailedError,
81+
(rf"(?=.*cmd)(?=.*with)(?!.*auth-token)(?=.*present)"
82+
rf"(?=.*Stderr from command:\nCommand error message.)")
83+
):
84+
self.test_subject._run_commands('tool', ['cmd'])
85+
7186
def test_run_commands_nonexistent_command(self):
7287
self.subprocess_utils.run.side_effect = OSError(
7388
errno.ENOENT, 'not found error'

0 commit comments

Comments
 (0)