From 453ffedabdd53673c4a4bfb7429ea6b79961feaf Mon Sep 17 00:00:00 2001 From: Samuel Stavinoha Date: Thu, 28 Aug 2014 10:24:48 -0500 Subject: [PATCH] fixes "su -" escalation root_password was not being passed through from the RemoteClient to the SSH client instantiation. Change-Id: I4cd61821dd9fbe099bcdc2097807446d4072f9c4 Closes-Bug:#1362666 --- satori/bash.py | 9 +++++++-- satori/tests/test_bash.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/satori/bash.py b/satori/bash.py index 0f0dd48..93a2fd7 100644 --- a/satori/bash.py +++ b/satori/bash.py @@ -170,7 +170,7 @@ class RemoteShell(ShellMixin): def __init__(self, address, password=None, username=None, private_key=None, key_filename=None, port=None, timeout=None, gateway=None, options=None, interactive=False, - protocol='ssh', **kwargs): + protocol='ssh', root_password=None, **kwargs): """An interface for executing shell commands on remote machines. :param str host: The ip address or host name of the server @@ -180,6 +180,10 @@ class RemoteShell(ShellMixin): :param username: The username to authenticate as :param private_key: Private SSH Key string to use (instead of using a filename) + :param root_password: root user password to be used if username is + not root. This will use username and password + to login and then 'su' to root using + root_password :param key_filename: a private key filename (path) :param port: tcp/ip port to use (defaults to 22) :param float timeout: an optional timeout (in seconds) for the @@ -213,7 +217,8 @@ class RemoteShell(ShellMixin): port=port, timeout=timeout, gateway=gateway, options=options, - interactive=interactive) + interactive=interactive, + root_password=root_password) self.host = self._client.host self.port = self._client.port diff --git a/satori/tests/test_bash.py b/satori/tests/test_bash.py index 1f41a40..7bde863 100644 --- a/satori/tests/test_bash.py +++ b/satori/tests/test_bash.py @@ -138,6 +138,37 @@ class TestRemoteShell(TestBashModule): self.assertEqual(self.resultdict, resultdict) +class TestRemoteShellInit(unittest.TestCase): + + def initpatch(self, ssh_instance, *args, **kwargs): + ssh_instance.host = self.host + ssh_instance.port = self.port + self._instance = ssh_instance + + def setUp(self): + self.host = "192.168.2.10" + self.port = 23 + + @mock.patch.object(bash.ssh.SSH, '__init__', return_value=None, + autospec=True) + def test_init_contains_kwargs(self, mock_init): + mock_init.side_effect = self.initpatch + allkwargs = { + 'password': 'pass', + 'username': 'user', + 'private_key': 'pkey', + 'key_filename': 'pkeyfile', + 'port': self.port, + 'timeout': 100, + 'gateway': 'g', + 'options': {'StrictHostKeyChecking': False}, + 'interactive': True, + 'root_password': 'sudopass', + } + self.remoteshell = bash.RemoteShell(self.host, **allkwargs) + mock_init.assert_called_once_with(self._instance, self.host, **allkwargs) + + class TestContextManager(utils.TestCase): def setUp(self):