| 1 |
# (c) 2015, Toshio Kuratomi <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
from io import StringIO |
| 21 |
|
| 22 |
import unittest |
| 23 |
from ansible.playbook.play_context import PlayContext |
| 24 |
from ansible.plugins.connection import ConnectionBase |
| 25 |
from ansible.plugins.loader import become_loader |
| 26 |
|
| 27 |
|
| 28 |
class NoOpConnection(ConnectionBase): |
| 29 |
|
| 30 |
@property |
| 31 |
def transport(self): |
| 32 |
"""This method is never called by unit tests.""" |
| 33 |
|
| 34 |
def _connect(self): |
| 35 |
"""This method is never called by unit tests.""" |
| 36 |
|
| 37 |
def exec_command(self): |
| 38 |
"""This method is never called by unit tests.""" |
| 39 |
|
| 40 |
def put_file(self): |
| 41 |
"""This method is never called by unit tests.""" |
| 42 |
|
| 43 |
def fetch_file(self): |
| 44 |
"""This method is never called by unit tests.""" |
| 45 |
|
| 46 |
def close(self): |
| 47 |
"""This method is never called by unit tests.""" |
| 48 |
|
| 49 |
|
| 50 |
class TestConnectionBaseClass(unittest.TestCase): |
| 51 |
|
| 52 |
def setUp(self): |
| 53 |
self.play_context = PlayContext() |
| 54 |
self.play_context.prompt = ( |
| 55 |
'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' |
| 56 |
) |
| 57 |
self.in_stream = StringIO() |
| 58 |
|
| 59 |
def tearDown(self): |
| 60 |
pass |
| 61 |
|
| 62 |
def test_subclass_error(self): |
| 63 |
class ConnectionModule1(ConnectionBase): |
| 64 |
pass |
| 65 |
with self.assertRaises(TypeError): |
| 66 |
ConnectionModule1() # pylint: disable=abstract-class-instantiated |
| 67 |
|
| 68 |
def test_subclass_success(self): |
| 69 |
self.assertIsInstance(NoOpConnection(self.play_context, self.in_stream), NoOpConnection) |
| 70 |
|
| 71 |
def test_check_password_prompt(self): |
| 72 |
local = ( |
| 73 |
b'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: \n' |
| 74 |
b'BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq\n' |
| 75 |
) |
| 76 |
|
| 77 |
ssh_pipelining_vvvv = b""" |
| 78 |
debug3: mux_master_read_cb: channel 1 packet type 0x10000002 len 251 |
| 79 |
debug2: process_mux_new_session: channel 1: request tty 0, X 1, agent 1, subsys 0, term "xterm-256color", cmd "/bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq; /bin/true'"'"' && sleep 0'", env 0 |
| 80 |
debug3: process_mux_new_session: got fds stdin 9, stdout 10, stderr 11 |
| 81 |
debug2: client_session2_setup: id 2 |
| 82 |
debug1: Sending command: /bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq; /bin/true'"'"' && sleep 0' |
| 83 |
debug2: channel 2: request exec confirm 1 |
| 84 |
debug2: channel 2: rcvd ext data 67 |
| 85 |
[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: debug2: channel 2: written 67 to efd 11 |
| 86 |
BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq |
| 87 |
debug3: receive packet: type 98 |
| 88 |
""" # noqa |
| 89 |
|
| 90 |
ssh_nopipelining_vvvv = b""" |
| 91 |
debug3: mux_master_read_cb: channel 1 packet type 0x10000002 len 251 |
| 92 |
debug2: process_mux_new_session: channel 1: request tty 1, X 1, agent 1, subsys 0, term "xterm-256color", cmd "/bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq; /bin/true'"'"' && sleep 0'", env 0 |
| 93 |
debug3: mux_client_request_session: session request sent |
| 94 |
debug3: send packet: type 98 |
| 95 |
debug1: Sending command: /bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: " -u root /bin/sh -c '"'"'echo BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq; /bin/true'"'"' && sleep 0' |
| 96 |
debug2: channel 2: request exec confirm 1 |
| 97 |
debug2: exec request accepted on channel 2 |
| 98 |
[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: debug3: receive packet: type 2 |
| 99 |
debug3: Received SSH2_MSG_IGNORE |
| 100 |
debug3: Received SSH2_MSG_IGNORE |
| 101 |
|
| 102 |
BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq |
| 103 |
debug3: receive packet: type 98 |
| 104 |
""" # noqa |
| 105 |
|
| 106 |
ssh_novvvv = ( |
| 107 |
b'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: \n' |
| 108 |
b'BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq\n' |
| 109 |
) |
| 110 |
|
| 111 |
dns_issue = ( |
| 112 |
b'timeout waiting for privilege escalation password prompt:\n' |
| 113 |
b'sudo: sudo: unable to resolve host tcloud014\n' |
| 114 |
b'[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: \n' |
| 115 |
b'BECOME-SUCCESS-ouzmdnewuhucvuaabtjmweasarviygqq\n' |
| 116 |
) |
| 117 |
|
| 118 |
nothing = b'' |
| 119 |
|
| 120 |
in_front = b""" |
| 121 |
debug1: Sending command: /bin/sh -c 'sudo -H -S -p "[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: " -u root /bin/sh -c '"'"'echo |
| 122 |
""" |
| 123 |
|
| 124 |
c = NoOpConnection(self.play_context, self.in_stream) |
| 125 |
c.set_become_plugin(become_loader.get('sudo')) |
| 126 |
c.become.prompt = '[sudo via ansible, key=ouzmdnewuhucvuaabtjmweasarviygqq] password: ' |
| 127 |
|
| 128 |
self.assertTrue(c.become.check_password_prompt(local)) |
| 129 |
self.assertTrue(c.become.check_password_prompt(ssh_pipelining_vvvv)) |
| 130 |
self.assertTrue(c.become.check_password_prompt(ssh_nopipelining_vvvv)) |
| 131 |
self.assertTrue(c.become.check_password_prompt(ssh_novvvv)) |
| 132 |
self.assertTrue(c.become.check_password_prompt(dns_issue)) |
| 133 |
self.assertFalse(c.become.check_password_prompt(nothing)) |
| 134 |
self.assertFalse(c.become.check_password_prompt(in_front)) |