]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/ssh: add 'ssh generate-key' and 'ssh clear-key' commands
authorSage Weil <sage@redhat.com>
Thu, 21 Nov 2019 18:42:17 +0000 (12:42 -0600)
committerSage Weil <sage@redhat.com>
Thu, 21 Nov 2019 18:57:13 +0000 (12:57 -0600)
Signed-off-by: Sage Weil <sage@redhat.com>
src/pybind/mgr/ssh/module.py

index 170fc071c035c3328829db6adacd99b796190b6c..8be5c2cb2fb3e87b4e84fb3636a4c3d1e850035d 100644 (file)
@@ -9,6 +9,7 @@ import os
 import random
 import tempfile
 import multiprocessing.pool
+import subprocess
 
 from ceph.deployment import inventory
 from mgr_module import MgrModule
@@ -348,6 +349,43 @@ class SSHOrchestrator(MgrModule, orchestrator.OrchestratorClientMixin):
         self.ssh_config_tmp = None
         return 0, "", ""
 
+    @orchestrator._cli_write_command(
+        'ssh generate-key',
+        desc='Generate a cluster SSH key (if not present)')
+    def _generate_key(self):
+        if not self.ssh_pub or not self.ssh_key:
+            self.log.info('Generating ssh key...')
+            tmp_dir = tempfile.TemporaryDirectory()
+            path = tmp_dir.name + '/key'
+            try:
+                subprocess.call([
+                    '/usr/bin/ssh-keygen',
+                    '-C', 'ceph-%s' % self._cluster_fsid,
+                    '-N', '',
+                    '-f', path
+                ])
+                with open(path, 'r') as f:
+                    secret = f.read()
+                with open(path + '.pub', 'r') as f:
+                    pub = f.read()
+            finally:
+                os.unlink(path)
+                os.unlink(path + '.pub')
+                tmp_dir.cleanup()
+            self.set_store('ssh_identity_key', secret)
+            self.set_store('ssh_identity_pub', pub)
+            self._reconfig_ssh()
+        return 0, '', ''
+
+    @orchestrator._cli_write_command(
+        'ssh clear-key',
+        desc='Clear cluster SSH key')
+    def _clear_key(self):
+        self.set_store('ssh_identity_key', None)
+        self.set_store('ssh_identity_pub', None)
+        self._reconfig_ssh()
+        return 0, '', ''
+
     @orchestrator._cli_read_command(
         'ssh get-pub-key',
         desc='Show SSH public key for connecting to cluster hosts')