|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
|
|
|
|
import mitogen.ssh
|
|
|
|
import mitogen.utils
|
|
|
|
|
|
|
|
import testlib
|
|
|
|
import plain_old_module
|
|
|
|
|
|
|
|
|
|
|
|
class StubSshMixin(testlib.RouterMixin):
|
|
|
|
"""
|
|
|
|
Mix-in that provides :meth:`stub_ssh` executing the stub 'ssh.py'.
|
|
|
|
"""
|
|
|
|
def stub_ssh(self, STUBSSH_MODE=None, **kwargs):
|
|
|
|
os.environ['STUBSSH_MODE'] = str(STUBSSH_MODE)
|
|
|
|
try:
|
|
|
|
return self.router.ssh(
|
|
|
|
hostname='hostname',
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
ssh_path=testlib.data_path('stubs/stub-ssh.py'),
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
del os.environ['STUBSSH_MODE']
|
|
|
|
|
|
|
|
|
|
|
|
class ConstructorTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
def test_okay(self):
|
|
|
|
context = self.router.ssh(
|
|
|
|
hostname='hostname',
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
ssh_path=testlib.data_path('stubs/stub-ssh.py'),
|
|
|
|
)
|
|
|
|
#context.call(mitogen.utils.log_to_file, '/tmp/log')
|
|
|
|
#context.call(mitogen.utils.disable_site_packages)
|
|
|
|
self.assertEqual(3, context.call(plain_old_module.add, 1, 2))
|
|
|
|
|
|
|
|
|
|
|
|
class SshTest(testlib.DockerMixin, testlib.TestCase):
|
|
|
|
def test_debug_decoding(self):
|
|
|
|
# ensure filter_debug_logs() decodes the logged string.
|
|
|
|
capture = testlib.LogCapturer()
|
|
|
|
capture.start()
|
|
|
|
try:
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
ssh_debug_level=3,
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
s = capture.stop()
|
|
|
|
|
|
|
|
expect = "%s: debug1: Reading configuration data" % (context.name,)
|
|
|
|
self.assertIn(expect, s)
|
|
|
|
|
|
|
|
def test_bash_permission_denied(self):
|
|
|
|
# issue #271: only match Permission Denied at start of line.
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__permdenied',
|
|
|
|
password='permdenied_password',
|
|
|
|
ssh_debug_level=3,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_stream_name(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
)
|
|
|
|
name = 'ssh.%s:%s' % (
|
|
|
|
self.dockerized_ssh.get_host(),
|
|
|
|
self.dockerized_ssh.port,
|
|
|
|
)
|
|
|
|
self.assertEqual(name, context.name)
|
|
|
|
|
|
|
|
def test_via_stream_name(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo_nopw',
|
|
|
|
password='has_sudo_nopw_password',
|
|
|
|
)
|
|
|
|
sudo = self.router.sudo(via=context)
|
|
|
|
|
|
|
|
name = 'ssh.%s:%s.sudo.root' % (
|
|
|
|
self.dockerized_ssh.host,
|
|
|
|
self.dockerized_ssh.port,
|
|
|
|
)
|
|
|
|
self.assertEqual(name, sudo.name)
|
|
|
|
|
|
|
|
def test_password_required(self):
|
|
|
|
e = self.assertRaises(mitogen.ssh.PasswordError,
|
|
|
|
lambda: self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
)
|
|
|
|
)
|
Refactor Stream, introduce quasi-asynchronous connect, much more
Split Stream into many, many classes
* mitogen.parent.Connection: Handles connection setup logic only.
* Maintain references to stdout and stderr streams.
* Manages TimerList timer to cancel connection attempt after
deadline
* Blocking setup code replaced by async equivalents running on the
broker
* mitogen.parent.Options: Tracks connection-specific options. This
keeps the connection class small, but more importantly, it is
generic to the future desire to build and execute command lines
without starting a full connection.
* mitogen.core.Protocol: Handles program behaviour relating to events
on a stream. Protocol performs no IO of its own, instead deferring
it to Stream and Side. This makes testing much easier, and means
libssh can reimplement Stream and Side to reuse MitogenProtocol
* mitogen.core.MitogenProtocol: Guts of the old Mitogen stream
implementtion
* mitogen.core.BufferedWriter: Guts of the old Mitogen buffered
transmit implementation, made generic
* mitogen.core.DelineatedProtocol: Guts of the old IoLogger, knows how
to split up input and pass it on to a
on_line_received()/on_partial_line_received() callback.
* mitogen.parent.BootstrapProtocol: Asynchronous equivalent of the old
blocking connect code. Waits for various prompts (MITO001 etc) and
writes the bootstrap using a BufferedWriter. On success, switches
the stream to MitogenProtocol.
* mitogen.core.Message: move encoding parts of MitogenProtocol out to
Message (where it belongs) and write a bunch of new tests for
pickling.
* The bizarre Stream.construct() is gone now, Option.__init__ is its
own constructor. Should fix many LGTM errors.
* Update all connection methods: Every connection method is updated to
use async logic, defining protocols as required to handle interactive
prompts like in SSH or su. Add new real integration tests for at least
doas and su.
* Eliminate manual fd management: File descriptors are trapped in file
objects at their point of origin, and Side is updated to use file
objects rather than raw descriptors. This eliminates a whole class of
bugs where unrelated FDs could be closed by the wrong component. Now
an FD's open/closed status is fused to it everywhere in the library.
* Halve file descriptor usage: now FD open/close state is tracked by
its file object, we don't need to duplicate FDs everywhere so that
receive/transmit side can be closed independently. Instead both sides
back on to the same file object. Closes #26, Closes #470.
* Remove most uses of dup/dup2: Closes #256. File descriptors are
trapped in a common file object and shared among classes. The
remaining few uses for dup/dup2 are as close to minimal as possible.
* Introduce mitogen.parent.Process: uniform interface for subprocesses
created either via mitogen.fork or the subprocess module. Remove all
the crap where we steal a pid from subprocess guts. Now we use
subprocess to manage its processes as it should be. Closes #169 by
using the new Timers facility to poll for a slow-to-exit subprocess.
* Fix su password race: Closes #363. DelineatedProtocol naturally
retries partially received lines, preventing the cause of the original
race.
* Delete old blocking IO utility functions
iter_read()/write_all()/discard_until().
Closes #26
Closes #147
Closes #169
Closes #256
Closes #363
Closes #419
Closes #470
6 years ago
|
|
|
self.assertEqual(e.args[0], mitogen.ssh.password_required_msg)
|
|
|
|
|
|
|
|
def test_password_incorrect(self):
|
|
|
|
e = self.assertRaises(mitogen.ssh.PasswordError,
|
|
|
|
lambda: self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='badpw',
|
|
|
|
)
|
|
|
|
)
|
Refactor Stream, introduce quasi-asynchronous connect, much more
Split Stream into many, many classes
* mitogen.parent.Connection: Handles connection setup logic only.
* Maintain references to stdout and stderr streams.
* Manages TimerList timer to cancel connection attempt after
deadline
* Blocking setup code replaced by async equivalents running on the
broker
* mitogen.parent.Options: Tracks connection-specific options. This
keeps the connection class small, but more importantly, it is
generic to the future desire to build and execute command lines
without starting a full connection.
* mitogen.core.Protocol: Handles program behaviour relating to events
on a stream. Protocol performs no IO of its own, instead deferring
it to Stream and Side. This makes testing much easier, and means
libssh can reimplement Stream and Side to reuse MitogenProtocol
* mitogen.core.MitogenProtocol: Guts of the old Mitogen stream
implementtion
* mitogen.core.BufferedWriter: Guts of the old Mitogen buffered
transmit implementation, made generic
* mitogen.core.DelineatedProtocol: Guts of the old IoLogger, knows how
to split up input and pass it on to a
on_line_received()/on_partial_line_received() callback.
* mitogen.parent.BootstrapProtocol: Asynchronous equivalent of the old
blocking connect code. Waits for various prompts (MITO001 etc) and
writes the bootstrap using a BufferedWriter. On success, switches
the stream to MitogenProtocol.
* mitogen.core.Message: move encoding parts of MitogenProtocol out to
Message (where it belongs) and write a bunch of new tests for
pickling.
* The bizarre Stream.construct() is gone now, Option.__init__ is its
own constructor. Should fix many LGTM errors.
* Update all connection methods: Every connection method is updated to
use async logic, defining protocols as required to handle interactive
prompts like in SSH or su. Add new real integration tests for at least
doas and su.
* Eliminate manual fd management: File descriptors are trapped in file
objects at their point of origin, and Side is updated to use file
objects rather than raw descriptors. This eliminates a whole class of
bugs where unrelated FDs could be closed by the wrong component. Now
an FD's open/closed status is fused to it everywhere in the library.
* Halve file descriptor usage: now FD open/close state is tracked by
its file object, we don't need to duplicate FDs everywhere so that
receive/transmit side can be closed independently. Instead both sides
back on to the same file object. Closes #26, Closes #470.
* Remove most uses of dup/dup2: Closes #256. File descriptors are
trapped in a common file object and shared among classes. The
remaining few uses for dup/dup2 are as close to minimal as possible.
* Introduce mitogen.parent.Process: uniform interface for subprocesses
created either via mitogen.fork or the subprocess module. Remove all
the crap where we steal a pid from subprocess guts. Now we use
subprocess to manage its processes as it should be. Closes #169 by
using the new Timers facility to poll for a slow-to-exit subprocess.
* Fix su password race: Closes #363. DelineatedProtocol naturally
retries partially received lines, preventing the cause of the original
race.
* Delete old blocking IO utility functions
iter_read()/write_all()/discard_until().
Closes #26
Closes #147
Closes #169
Closes #256
Closes #363
Closes #419
Closes #470
6 years ago
|
|
|
self.assertEqual(e.args[0], mitogen.ssh.password_incorrect_msg)
|
|
|
|
|
|
|
|
def test_password_specified(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
'i-am-mitogen-test-docker-image\n',
|
|
|
|
context.call(plain_old_module.get_sentinel_value),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_pubkey_required(self):
|
|
|
|
e = self.assertRaises(mitogen.ssh.PasswordError,
|
|
|
|
lambda: self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo_pubkey',
|
|
|
|
)
|
|
|
|
)
|
Refactor Stream, introduce quasi-asynchronous connect, much more
Split Stream into many, many classes
* mitogen.parent.Connection: Handles connection setup logic only.
* Maintain references to stdout and stderr streams.
* Manages TimerList timer to cancel connection attempt after
deadline
* Blocking setup code replaced by async equivalents running on the
broker
* mitogen.parent.Options: Tracks connection-specific options. This
keeps the connection class small, but more importantly, it is
generic to the future desire to build and execute command lines
without starting a full connection.
* mitogen.core.Protocol: Handles program behaviour relating to events
on a stream. Protocol performs no IO of its own, instead deferring
it to Stream and Side. This makes testing much easier, and means
libssh can reimplement Stream and Side to reuse MitogenProtocol
* mitogen.core.MitogenProtocol: Guts of the old Mitogen stream
implementtion
* mitogen.core.BufferedWriter: Guts of the old Mitogen buffered
transmit implementation, made generic
* mitogen.core.DelineatedProtocol: Guts of the old IoLogger, knows how
to split up input and pass it on to a
on_line_received()/on_partial_line_received() callback.
* mitogen.parent.BootstrapProtocol: Asynchronous equivalent of the old
blocking connect code. Waits for various prompts (MITO001 etc) and
writes the bootstrap using a BufferedWriter. On success, switches
the stream to MitogenProtocol.
* mitogen.core.Message: move encoding parts of MitogenProtocol out to
Message (where it belongs) and write a bunch of new tests for
pickling.
* The bizarre Stream.construct() is gone now, Option.__init__ is its
own constructor. Should fix many LGTM errors.
* Update all connection methods: Every connection method is updated to
use async logic, defining protocols as required to handle interactive
prompts like in SSH or su. Add new real integration tests for at least
doas and su.
* Eliminate manual fd management: File descriptors are trapped in file
objects at their point of origin, and Side is updated to use file
objects rather than raw descriptors. This eliminates a whole class of
bugs where unrelated FDs could be closed by the wrong component. Now
an FD's open/closed status is fused to it everywhere in the library.
* Halve file descriptor usage: now FD open/close state is tracked by
its file object, we don't need to duplicate FDs everywhere so that
receive/transmit side can be closed independently. Instead both sides
back on to the same file object. Closes #26, Closes #470.
* Remove most uses of dup/dup2: Closes #256. File descriptors are
trapped in a common file object and shared among classes. The
remaining few uses for dup/dup2 are as close to minimal as possible.
* Introduce mitogen.parent.Process: uniform interface for subprocesses
created either via mitogen.fork or the subprocess module. Remove all
the crap where we steal a pid from subprocess guts. Now we use
subprocess to manage its processes as it should be. Closes #169 by
using the new Timers facility to poll for a slow-to-exit subprocess.
* Fix su password race: Closes #363. DelineatedProtocol naturally
retries partially received lines, preventing the cause of the original
race.
* Delete old blocking IO utility functions
iter_read()/write_all()/discard_until().
Closes #26
Closes #147
Closes #169
Closes #256
Closes #363
Closes #419
Closes #470
6 years ago
|
|
|
self.assertEqual(e.args[0], mitogen.ssh.password_required_msg)
|
|
|
|
|
|
|
|
def test_pubkey_specified(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo_pubkey',
|
|
|
|
identity_file=testlib.data_path('docker/mitogen__has_sudo_pubkey.key'),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'i-am-mitogen-test-docker-image\n',
|
|
|
|
context.call(plain_old_module.get_sentinel_value),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_enforce_unknown_host_key(self):
|
|
|
|
fp = tempfile.NamedTemporaryFile()
|
|
|
|
try:
|
|
|
|
e = self.assertRaises(mitogen.ssh.HostKeyError,
|
|
|
|
lambda: self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo_pubkey',
|
|
|
|
password='has_sudo_password',
|
|
|
|
ssh_args=['-o', 'UserKnownHostsFile ' + fp.name],
|
|
|
|
check_host_keys='enforce',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.assertEqual(e.args[0], mitogen.ssh.hostkey_failed_msg)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
def test_accept_enforce_host_keys(self):
|
|
|
|
fp = tempfile.NamedTemporaryFile()
|
|
|
|
try:
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
ssh_args=['-o', 'UserKnownHostsFile ' + fp.name],
|
|
|
|
check_host_keys='accept',
|
|
|
|
)
|
|
|
|
context.shutdown(wait=True)
|
|
|
|
|
|
|
|
fp.seek(0)
|
|
|
|
# Lame test, but we're about to use enforce mode anyway, which
|
|
|
|
# verifies the file contents.
|
|
|
|
self.assertGreater(len(fp.read()), 0)
|
|
|
|
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
ssh_args=['-o', 'UserKnownHostsFile ' + fp.name],
|
|
|
|
check_host_keys='enforce',
|
|
|
|
)
|
|
|
|
context.shutdown(wait=True)
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
|
|
|
|
class BannerTest(testlib.DockerMixin, testlib.TestCase):
|
|
|
|
# Verify the ability to disambiguate random spam appearing in the SSHd's
|
|
|
|
# login banner from a legitimate password prompt.
|
|
|
|
def test_verbose_enabled(self):
|
|
|
|
context = self.docker_ssh(
|
|
|
|
username='mitogen__has_sudo',
|
|
|
|
password='has_sudo_password',
|
|
|
|
ssh_debug_level=3,
|
|
|
|
)
|
|
|
|
name = 'ssh.%s:%s' % (
|
|
|
|
self.dockerized_ssh.get_host(),
|
|
|
|
self.dockerized_ssh.port,
|
|
|
|
)
|
|
|
|
self.assertEqual(name, context.name)
|
|
|
|
|
|
|
|
|
|
|
|
class StubPermissionDeniedTest(StubSshMixin, testlib.TestCase):
|
|
|
|
def test_classic_prompt(self):
|
|
|
|
self.assertRaises(mitogen.ssh.PasswordError,
|
|
|
|
lambda: self.stub_ssh(STUBSSH_MODE='permdenied_classic'))
|
|
|
|
|
|
|
|
def test_openssh_75_prompt(self):
|
|
|
|
self.assertRaises(mitogen.ssh.PasswordError,
|
|
|
|
lambda: self.stub_ssh(STUBSSH_MODE='permdenied_75'))
|
|
|
|
|
|
|
|
|
|
|
|
class StubCheckHostKeysTest(StubSshMixin, testlib.TestCase):
|
|
|
|
def test_check_host_keys_accept(self):
|
|
|
|
# required=true, host_key_checking=accept
|
|
|
|
context = self.stub_ssh(STUBSSH_MODE='ask', check_host_keys='accept')
|
|
|
|
self.assertEqual('1', context.call(os.getenv, 'STDERR_WAS_TTY'))
|
|
|
|
|
|
|
|
def test_check_host_keys_enforce(self):
|
|
|
|
# required=false, host_key_checking=enforce
|
|
|
|
context = self.stub_ssh(check_host_keys='enforce')
|
|
|
|
self.assertEqual(None, context.call(os.getenv, 'STDERR_WAS_TTY'))
|
|
|
|
|
|
|
|
def test_check_host_keys_ignore(self):
|
|
|
|
# required=false, host_key_checking=ignore
|
|
|
|
context = self.stub_ssh(check_host_keys='ignore')
|
|
|
|
self.assertEqual(None, context.call(os.getenv, 'STDERR_WAS_TTY'))
|
|
|
|
|
|
|
|
def test_password_present(self):
|
|
|
|
# required=true, password is not None
|
|
|
|
context = self.stub_ssh(check_host_keys='ignore', password='willick')
|
|
|
|
self.assertEqual('1', context.call(os.getenv, 'STDERR_WAS_TTY'))
|