From 69497b151ee35aa626be6ddccb40742528241d45 Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Tue, 11 Apr 2023 09:13:59 -0400 Subject: [PATCH] Make paramiko import optional Since paramiko does not support FIPS, some deployments may run without paramiko installed. Handle this in ssh_utils. (This does not handle the paramiko requirement for drivers that import it directly.) Change-Id: Id87876543df825f9d84938c615c5976abdebd8f4 --- cinder/exception.py | 4 ++++ cinder/ssh_utils.py | 9 ++++++++- cinder/tests/unit/test_ssh_utils.py | 8 ++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cinder/exception.py b/cinder/exception.py index 906ed3923ae..dddcbc68a88 100644 --- a/cinder/exception.py +++ b/cinder/exception.py @@ -1092,3 +1092,7 @@ class DriverInitiatorDataExists(Duplicate): "Driver initiator data for initiator '%(initiator)s' and backend " "'%(namespace)s' with key '%(key)s' already exists." ) + + +class RequirementMissing(CinderException): + message = _('Requirement %(req)s is not installed.') diff --git a/cinder/ssh_utils.py b/cinder/ssh_utils.py index 6bd3fb7319d..bef393e28a5 100644 --- a/cinder/ssh_utils.py +++ b/cinder/ssh_utils.py @@ -24,7 +24,11 @@ from eventlet import pools from oslo_config import cfg from oslo_log import log as logging from oslo_utils import excutils -import paramiko + +try: + import paramiko +except ImportError: + paramiko = None from cinder import exception from cinder.i18n import _ @@ -65,6 +69,9 @@ class SSHPool(pools.Pool): self.hosts_key_file = None self.current_size = 0 + if paramiko is None: + raise exception.RequirementMissing(req='paramiko') + # Validate good config setting here. # Paramiko handles the case where the file is inaccessible. if not CONF.ssh_hosts_key_file: diff --git a/cinder/tests/unit/test_ssh_utils.py b/cinder/tests/unit/test_ssh_utils.py index 3d4dd622547..67091179f96 100644 --- a/cinder/tests/unit/test_ssh_utils.py +++ b/cinder/tests/unit/test_ssh_utils.py @@ -405,3 +405,11 @@ class SSHPoolTestCase(test.TestCase): sshpool = None self.assertEqual(fake_close.mock_calls, close_expect_calls + close_expect_calls) + + @mock.patch('cinder.ssh_utils.paramiko', new=None) + def test_missing_paramiko(self): + self.assertRaises(exception.RequirementMissing, + ssh_utils.SSHPool, + '192.0.2.1', 22, 10, + 'test', + password='hello')