diff --git a/awscli/customizations/codeartifact/login.py b/awscli/customizations/codeartifact/login.py index 50f2d2f51f17..806cf75ac9a6 100644 --- a/awscli/customizations/codeartifact/login.py +++ b/awscli/customizations/codeartifact/login.py @@ -42,10 +42,10 @@ class CommandFailedError(Exception): def __init__(self, called_process_error, auth_token): msg = str(called_process_error).replace(auth_token, '******') if called_process_error.stderr is not None: - msg +=( - f' Stderr from command:\n' - f'{called_process_error.stderr.decode(get_stderr_encoding())}' - ) + stderr = called_process_error.stderr.decode( + get_stderr_encoding() + ).replace(auth_token, '******') + msg += f' Stderr from command:\n{stderr}' Exception.__init__(self, msg) diff --git a/tests/unit/customizations/codeartifact/test_adapter_login.py b/tests/unit/customizations/codeartifact/test_adapter_login.py index d481c442ea5e..088a2386916a 100644 --- a/tests/unit/customizations/codeartifact/test_adapter_login.py +++ b/tests/unit/customizations/codeartifact/test_adapter_login.py @@ -83,6 +83,21 @@ def test_run_commands_command_failed_redact_auth_token(self): ): self.test_subject._run_commands('tool', ['cmd']) + def test_run_commands_command_failed_redact_auth_token_in_stderr(self): + error_to_be_caught = subprocess.CalledProcessError( + returncode=1, + cmd=['cmd'], + output=None, + stderr=b'Command error message containing auth-token here.' + ) + self.subprocess_utils.run.side_effect = error_to_be_caught + with self.assertRaisesRegex( + CommandFailedError, + rf"(?=.*cmd)(?!.*auth-token)" + rf"(?=.*Stderr from command:\nCommand error message containing \*\*\*\*\*\* here.)" + ): + self.test_subject._run_commands('tool', ['cmd']) + def test_run_commands_nonexistent_command(self): self.subprocess_utils.run.side_effect = OSError( errno.ENOENT, 'not found error'