test_paramiko_vendor.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # test_paramiko_vendor.py
  2. #
  3. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  4. # General Public License as public by the Free Software Foundation; version 2.0
  5. # or (at your option) any later version. You can redistribute it and/or
  6. # modify it under the terms of either of these two licenses.
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. #
  14. # You should have received a copy of the licenses; if not, see
  15. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  16. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  17. # License, Version 2.0.
  18. #
  19. """Tests for paramiko_vendor."""
  20. import socket
  21. import threading
  22. from io import StringIO
  23. from unittest import skipIf
  24. from .. import TestCase
  25. try:
  26. import paramiko
  27. except ImportError:
  28. has_paramiko = False
  29. else:
  30. has_paramiko = True
  31. from dulwich.contrib.paramiko_vendor import ParamikoSSHVendor
  32. class Server(paramiko.ServerInterface):
  33. """http://docs.paramiko.org/en/2.4/api/server.html."""
  34. def __init__(self, commands, *args, **kwargs) -> None:
  35. super().__init__(*args, **kwargs)
  36. self.commands = commands
  37. def check_channel_exec_request(self, channel, command):
  38. self.commands.append(command)
  39. return True
  40. def check_auth_password(self, username, password):
  41. if username == USER and password == PASSWORD:
  42. return paramiko.AUTH_SUCCESSFUL
  43. return paramiko.AUTH_FAILED
  44. def check_auth_publickey(self, username, key):
  45. pubkey = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY))
  46. if username == USER and key == pubkey:
  47. return paramiko.AUTH_SUCCESSFUL
  48. return paramiko.AUTH_FAILED
  49. def check_channel_request(self, kind, chanid):
  50. if kind == "session":
  51. return paramiko.OPEN_SUCCEEDED
  52. return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
  53. def get_allowed_auths(self, username):
  54. return "password,publickey"
  55. USER = "testuser"
  56. PASSWORD = "test"
  57. SERVER_KEY = """\
  58. -----BEGIN RSA PRIVATE KEY-----
  59. MIIEpAIBAAKCAQEAy/L1sSYAzxsMprtNXW4u/1jGXXkQmQ2xtmKVlR+RlIL3a1BH
  60. bzTpPlZyjltAAwzIP8XRh0iJFKz5y3zSQChhX47ZGN0NvQsVct8R+YwsUonwfAJ+
  61. JN0KBKKvC8fPHlzqBr3gX+ZxqsFH934tQ6wdQPH5eQWtdM8L826lMsH1737uyTGk
  62. +mCSDjL3c6EzY83g7qhkJU2R4qbi6ne01FaWADzG8sOzXnHT+xpxtk8TTT8yCVUY
  63. MmBNsSoA/ka3iWz70ghB+6Xb0WpFJZXWq1oYovviPAfZGZSrxBZMxsWMye70SdLl
  64. TqsBEt0+miIcm9s0fvjWvQuhaHX6mZs5VO4r5QIDAQABAoIBAGYqeYWaYgFdrYLA
  65. hUrubUCg+g3NHdFuGL4iuIgRXl4lFUh+2KoOuWDu8Uf60iA1AQNhV0sLvQ/Mbv3O
  66. s4xMLisuZfaclctDiCUZNenqnDFkxEF7BjH1QJV94W5nU4wEQ3/JEmM4D2zYkfKb
  67. FJW33JeyH6TOgUvohDYYEU1R+J9V8qA243p+ui1uVtNI6Pb0TXJnG5y9Ny4vkSWH
  68. Fi0QoMPR1r9xJ4SEearGzA/crb4SmmDTKhGSoMsT3d5ATieLmwcS66xWz8w4oFGJ
  69. yzDq24s4Fp9ccNjMf/xR8XRiekJv835gjEqwF9IXyvgOaq6XJ1iCqGPFDKa25nui
  70. JnEstOkCgYEA/ZXk7aIanvdeJlTqpX578sJfCnrXLydzE8emk1b7+5mrzGxQ4/pM
  71. PBQs2f8glT3t0O0mRX9NoRqnwrid88/b+cY4NCOICFZeasX336/gYQxyVeRLJS6Z
  72. hnGEQqry8qS7PdKAyeHMNmZFrUh4EiHiObymEfQS+mkRUObn0cGBTw8CgYEAzeQU
  73. D2baec1DawjppKaRynAvWjp+9ry1lZx9unryKVRwjRjkEpw+b3/+hdaF1IvsVSce
  74. cNj+6W2guZ2tyHuPhZ64/4SJVyE2hKDSKD4xTb2nVjsMeN0bLD2UWXC9mwbx8nWa
  75. 2tmtUZ7a/okQb2cSdosJinRewLNqXIsBXamT1csCgYEA0cXb2RCOQQ6U3dTFPx4A
  76. 3vMXuA2iUKmrsqMoEx6T2LBow/Sefdkik1iFOdipVYwjXP+w9zC2QR1Rxez/DR/X
  77. 8ymceNUjxPHdrSoTQQG29dFcC92MpDeGXQcuyA+uZjcLhbrLOzYEvsOfxBb87NMG
  78. 14hNQPDNekTMREafYo9WrtUCgYAREK54+FVzcwf7fymedA/xb4r9N4v+d3W1iNsC
  79. 8d3Qfyc1CrMct8aVB07ZWQaOr2pPRIbJY7L9NhD0UZVt4I/sy1MaGqonhqE2LP4+
  80. R6legDG2e/50ph7yc8gwAaA1kUXMiuLi8Nfkw/3yyvmJwklNegi4aRzRbA2Mzhi2
  81. 4q9WMQKBgQCb0JNyxHG4pvLWCF/j0Sm1FfvrpnqSv5678n1j4GX7Ka/TubOK1Y4K
  82. U+Oib7dKa/zQMWehVFNTayrsq6bKVZ6q7zG+IHiRLw4wjeAxREFH6WUjDrn9vl2l
  83. D48DKbBuBwuVOJWyq3qbfgJXojscgNQklrsPdXVhDwOF0dYxP89HnA==
  84. -----END RSA PRIVATE KEY-----"""
  85. CLIENT_KEY = """\
  86. -----BEGIN RSA PRIVATE KEY-----
  87. MIIEpAIBAAKCAQEAxvREKSElPOm/0z/nPO+j5rk2tjdgGcGc7We1QZ6TRXYLu7nN
  88. GeEFIL4p8N1i6dmB+Eydt7xqCU79MWD6Yy4prF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7gJDFDf6JPupfJ/xiwe6z0fjKK9S57EhqgUYMB55XYnE5iIw
  105. ZQ6BV9SAZ4V7VsRs4dJLdNC3tn/rDGHJBgCaym2PlbsX6rvFT+h1IC8dwv0V79Ui
  106. Ehq9WTzkMoE8yhvNokvkPZUCgYEAgBAFxv5xGdh79ftdtXLmhnDvZ6S8l6Fjcxqo
  107. Ay/jg66Tp43OU226iv/0mmZKM8Dd1xC8dnon4GBVc19jSYYiWBulrRPlx0Xo/o+K
  108. CzZBN1lrXH1i6dqufpc0jq8TMf/N+q1q/c1uMupsKCY1/xVYpc+ok71b7J7c49zQ
  109. nOeuUW8CgYA9Infooy65FTgbzca0c9kbCUBmcAPQ2ItH3JcPKWPQTDuV62HcT00o
  110. fZdIV47Nez1W5Clk191RMy8TXuqI54kocciUWpThc6j44hz49oUueb8U4bLcEHzA
  111. WxtWBWHwxfSmqgTXilEA3ALJp0kNolLnEttnhENwJpZHlqtes0ZA4w==
  112. -----END RSA PRIVATE KEY-----"""
  113. @skipIf(not has_paramiko, "paramiko is not installed")
  114. class ParamikoSSHVendorTests(TestCase):
  115. def setUp(self):
  116. import paramiko.transport
  117. # re-enable server functionality for tests
  118. if hasattr(paramiko.transport, "SERVER_DISABLED_BY_GENTOO"):
  119. paramiko.transport.SERVER_DISABLED_BY_GENTOO = False
  120. self.commands = []
  121. socket.setdefaulttimeout(10)
  122. self.addCleanup(socket.setdefaulttimeout, None)
  123. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  124. self.socket.bind(("127.0.0.1", 0))
  125. self.socket.listen(5)
  126. self.addCleanup(self.socket.close)
  127. self.port = self.socket.getsockname()[1]
  128. self.thread = threading.Thread(target=self._run)
  129. self.thread.start()
  130. def tearDown(self):
  131. self.thread.join()
  132. def _run(self):
  133. try:
  134. conn, addr = self.socket.accept()
  135. except OSError:
  136. return False
  137. self.transport = paramiko.Transport(conn)
  138. self.addCleanup(self.transport.close)
  139. host_key = paramiko.RSAKey.from_private_key(StringIO(SERVER_KEY))
  140. self.transport.add_server_key(host_key)
  141. server = Server(self.commands)
  142. self.transport.start_server(server=server)
  143. def test_run_command_password(self):
  144. vendor = ParamikoSSHVendor(
  145. allow_agent=False,
  146. look_for_keys=False,
  147. )
  148. vendor.run_command(
  149. "127.0.0.1",
  150. "test_run_command_password",
  151. username=USER,
  152. port=self.port,
  153. password=PASSWORD,
  154. )
  155. self.assertIn(b"test_run_command_password", self.commands)
  156. def test_run_command_with_privkey(self):
  157. key = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY))
  158. vendor = ParamikoSSHVendor(
  159. allow_agent=False,
  160. look_for_keys=False,
  161. )
  162. vendor.run_command(
  163. "127.0.0.1",
  164. "test_run_command_with_privkey",
  165. username=USER,
  166. port=self.port,
  167. pkey=key,
  168. )
  169. self.assertIn(b"test_run_command_with_privkey", self.commands)
  170. def test_run_command_data_transfer(self):
  171. vendor = ParamikoSSHVendor(
  172. allow_agent=False,
  173. look_for_keys=False,
  174. )
  175. con = vendor.run_command(
  176. "127.0.0.1",
  177. "test_run_command_data_transfer",
  178. username=USER,
  179. port=self.port,
  180. password=PASSWORD,
  181. )
  182. self.assertIn(b"test_run_command_data_transfer", self.commands)
  183. channel = self.transport.accept(5)
  184. channel.send(b"stdout\n")
  185. channel.send_stderr(b"stderr\n")
  186. channel.close()
  187. # Fixme: it's return false
  188. # self.assertTrue(con.can_read())
  189. self.assertEqual(b"stdout\n", con.read(4096))
  190. # Fixme: it's return empty string
  191. # self.assertEqual(b'stderr\n', con.read_stderr(4096))