Browse Source

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.

Change-Id: I5a286cca6e48b0abc9cec3f15b2648977ae838c2
Related-Bug: rhbz#1734356
(cherry picked from commit b40c9e3b1c)
tags/11.5.1
Sergii Golovatiuk 1 month ago
parent
commit
6055817d25

+ 6
- 5
tripleoclient/tests/workflows/test_deployment.py View File

@@ -52,16 +52,16 @@ class TestDeploymentWorkflows(utils.TestCommand):
52 52
     @mock.patch('tripleoclient.workflows.deployment.open')
53 53
     @mock.patch('tripleoclient.workflows.deployment.tempfile')
54 54
     @mock.patch('tripleoclient.workflows.deployment.subprocess.check_call')
55
-    def test_enable_ssh_admin(self, mock_check_call, mock_tempfile,
56
-                              mock_open, mock_rmtree, mock_sleep,
57
-                              mock_wait_for_ssh_port):
55
+    def test_enable_ssh_admin(self, mock_check_call, mock_tempfile, mock_open,
56
+                              mock_rmtree, mock_sleep, mock_wait_for_ssh_port):
58 57
         log = mock.Mock()
59 58
         hosts = 'a', 'b', 'c'
60 59
         ssh_user = 'test-user'
61 60
         ssh_key = 'test-key'
62 61
 
63 62
         mock_tempfile.mkdtemp.return_value = '/foo'
64
-        mock_open.return_value = FakeFile('key')
63
+        mock_open.side_effect = [FakeFile('DEVNULL'), FakeFile('pubkey'),
64
+                                 FakeFile('key')]
65 65
         mock_state = mock.Mock()
66 66
         mock_state.state = 'SUCCESS'
67 67
         self.workflow.executions.get.return_value = mock_state
@@ -101,7 +101,8 @@ class TestDeploymentWorkflows(utils.TestCommand):
101 101
         ssh_key = 'test-key'
102 102
 
103 103
         mock_tempfile.mkdtemp.return_value = '/foo'
104
-        mock_open.side_effect = [FakeFile('pubkey'), FakeFile('privkey')]
104
+        mock_open.side_effect = [FakeFile('DEVNULL'), FakeFile('pubkey'),
105
+                                 FakeFile('privkey')]
105 106
         mock_state = mock.Mock()
106 107
         mock_state.state = 'ERROR'
107 108
         mock_state.to_dict.return_value = dict(state_info='an error')

+ 11
- 1
tripleoclient/workflows/deployment.py View File

@@ -225,7 +225,17 @@ def enable_ssh_admin(log, clients, plan_name, hosts, ssh_user, ssh_key):
225 225
     try:
226 226
         tmp_key_command = ["ssh-keygen", "-N", "", "-t", "rsa", "-b", "4096",
227 227
                            "-f", tmp_key_private, "-C", tmp_key_comment]
228
-        subprocess.check_call(tmp_key_command, stderr=subprocess.STDOUT)
228
+        DEVNULL = open(os.devnull, 'w')
229
+        try:
230
+            subprocess.check_call(tmp_key_command, stdout=DEVNULL,
231
+                                  stderr=subprocess.STDOUT)
232
+        except subprocess.CalledProcessError as exc:
233
+            log.error("ssh-keygen has failed with return code {0}".
234
+                      format(exc.returncode))
235
+        else:
236
+            log.info("ssh-keygen has been run successfully")
237
+        DEVNULL.close()
238
+
229 239
         with open(tmp_key_public) as pubkey:
230 240
             tmp_key_public_contents = pubkey.read()
231 241
         with open(tmp_key_private) as privkey:

Loading…
Cancel
Save