From c7919e74e7c210e0264534471774a9e2a9e71fcf Mon Sep 17 00:00:00 2001 From: Sergii Golovatiuk Date: Mon, 5 Aug 2019 15:21:16 +0200 Subject: [PATCH] Suppress output for ssh-keygen ssh-keygen has private key which shouldn't be visible in output. This patch change the behaviour to print only exit code on failure. Also this patch closes file descriptor once key is read. Conflicts: tripleoclient/tests/workflows/test_deployment.py tripleoclient/workflows/deployment.py Change-Id: I5a286cca6e48b0abc9cec3f15b2648977ae838c2 Related-Bug: rhbz#1734356 (cherry picked from commit b40c9e3b1ca9ed74dd86d002451005faabda89b9) (cherry picked from commit 6055817d2528b5cc3012465121ac59777605b628) --- tripleoclient/tests/workflows/test_deployment.py | 9 ++++++--- tripleoclient/workflows/deployment.py | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tripleoclient/tests/workflows/test_deployment.py b/tripleoclient/tests/workflows/test_deployment.py index 7a6630352..abed6f742 100644 --- a/tripleoclient/tests/workflows/test_deployment.py +++ b/tripleoclient/tests/workflows/test_deployment.py @@ -51,9 +51,8 @@ class TestDeploymentWorkflows(utils.TestCommand): @mock.patch('tripleoclient.workflows.deployment.open') @mock.patch('tripleoclient.workflows.deployment.tempfile') @mock.patch('tripleoclient.workflows.deployment.subprocess.check_call') - def test_enable_ssh_admin(self, mock_check_call, mock_tempfile, - mock_open, mock_rmtree, mock_sleep, - mock_wait_for_ssh_port): + def test_enable_ssh_admin(self, mock_check_call, mock_tempfile, mock_open, + mock_rmtree, mock_sleep, mock_wait_for_ssh_port): log = mock.Mock() hosts = 'a', 'b', 'c' ssh_user = 'test-user' @@ -62,6 +61,8 @@ class TestDeploymentWorkflows(utils.TestCommand): mock_tempfile.mkdtemp.return_value = '/foo' mock_read = mock.Mock() mock_read.read.return_value = 'key' + mock_read.__enter__ = mock.Mock() + mock_read.__exit__ = mock.Mock() mock_open.return_value = mock_read mock_state = mock.Mock() mock_state.state = 'SUCCESS' @@ -104,6 +105,8 @@ class TestDeploymentWorkflows(utils.TestCommand): mock_tempfile.mkdtemp.return_value = '/foo' mock_read = mock.Mock() mock_read.read.return_value = 'key' + mock_read.__enter__ = mock.Mock() + mock_read.__exit__ = mock.Mock() mock_open.return_value = mock_read mock_state = mock.Mock() mock_state.state = 'ERROR' diff --git a/tripleoclient/workflows/deployment.py b/tripleoclient/workflows/deployment.py index 7a8f15c07..3a642cf40 100644 --- a/tripleoclient/workflows/deployment.py +++ b/tripleoclient/workflows/deployment.py @@ -164,8 +164,19 @@ def enable_ssh_admin(log, clients, hosts, ssh_user, ssh_key): try: tmp_key_command = ["ssh-keygen", "-N", "", "-t", "rsa", "-b", "4096", "-f", tmp_key_private, "-C", tmp_key_comment] - subprocess.check_call(tmp_key_command, stderr=subprocess.STDOUT) - tmp_key_public_contents = open(tmp_key_public).read() + DEVNULL = open(os.devnull, 'w') + try: + subprocess.check_call(tmp_key_command, stdout=DEVNULL, + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as exc: + log.error("ssh-keygen has failed with return code {0}". + format(exc.returncode)) + else: + log.info("ssh-keygen has been run successfully") + DEVNULL.close() + + with open(tmp_key_public) as pubkey: + tmp_key_public_contents = pubkey.read() for host in hosts: wait_for_ssh_port(host)