|
11 | 11 | from pytest_mock import MockerFixture
|
12 | 12 | from pytest_test_utils.waiters import wait_until
|
13 | 13 |
|
| 14 | +from scmrepo.exceptions import AuthError |
14 | 15 | from scmrepo.git.backend.dulwich.asyncssh_vendor import AsyncSSHVendor
|
15 | 16 |
|
16 |
| -from .vendor.test_paramiko_vendor import ( |
17 |
| - CLIENT_KEY, |
18 |
| - PASSWORD, |
19 |
| - USER, |
20 |
| - Server, |
21 |
| -) |
22 |
| - |
23 | 17 | # pylint: disable=redefined-outer-name
|
24 | 18 |
|
25 | 19 |
|
| 20 | +USER = "testuser" |
| 21 | +PASSWORD = "test" |
| 22 | +CLIENT_KEY = """-----BEGIN RSA PRIVATE KEY----- |
| 23 | +MIIEpAIBAAKCAQEAxvREKSElPOm/0z/nPO+j5rk2tjdgGcGc7We1QZ6TRXYLu7nN |
| 24 | +GeEFIL4p8N1i6dmB+Eydt7xqCU79MWD6Yy4prFe1+/K1wCDUxIbFMxqQcX5zjJzd |
| 25 | +i8j8PbcaUlVhP/OkjtkSxrXaGDO1BzfdV4iEBtTV/2l3zmLKJlt3jnOHLczP24CB |
| 26 | +DTQKp3rKshbRefzot9Y+wnaK692RsYgsyo9YEP0GyWKG9topCHk13r46J6vGLeuj |
| 27 | +ryUKqmbLJkzbJbIcEqwTDo5iHaCVqaMr5Hrb8BdMucSseqZQJsXSd+9tdRcIblUQ |
| 28 | +38kZjmFMm4SFbruJcpZCNM2wNSZPIRX+3eiwNwIDAQABAoIBAHSacOBSJsr+jIi5 |
| 29 | +KUOTh9IPtzswVUiDKwARCjB9Sf8p4lKR4N1L/n9kNJyQhApeikgGT2GCMftmqgoo |
| 30 | +tlculQoHFgemBlOmak0MV8NNzF5YKEy/GzF0CDH7gJfEpoyetVFrdA+2QS5yD6U9 |
| 31 | +XqKQxiBi2VEqdScmyyeT8AwzNYTnPeH/DOEcnbdRjqiy/CD79F49CQ1lX1Fuqm0K |
| 32 | +I7BivBH1xo/rVnUP4F+IzocDqoga+Pjdj0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TXilEA3ALJp0kNolLnEttnhENwJpZHlqtes0ZA4w== |
| 48 | +-----END RSA PRIVATE KEY-----""" |
| 49 | + |
| 50 | + |
| 51 | +class Server(paramiko.ServerInterface): |
| 52 | + """http://docs.paramiko.org/en/2.4/api/server.html.""" |
| 53 | + |
| 54 | + def __init__(self, commands, *args, **kwargs) -> None: |
| 55 | + super().__init__(*args, **kwargs) |
| 56 | + self.commands = commands |
| 57 | + |
| 58 | + def check_channel_exec_request(self, channel, command): |
| 59 | + self.commands.append(command) |
| 60 | + return True |
| 61 | + |
| 62 | + def check_auth_password(self, username, password): |
| 63 | + if username == USER and password == PASSWORD: |
| 64 | + return paramiko.AUTH_SUCCESSFUL |
| 65 | + return paramiko.AUTH_FAILED |
| 66 | + |
| 67 | + def check_auth_publickey(self, username, key): |
| 68 | + pubkey = paramiko.RSAKey.from_private_key(StringIO(CLIENT_KEY)) |
| 69 | + if username == USER and key == pubkey: |
| 70 | + return paramiko.AUTH_SUCCESSFUL |
| 71 | + return paramiko.AUTH_FAILED |
| 72 | + |
| 73 | + def check_channel_request(self, kind, chanid): |
| 74 | + if kind == "session": |
| 75 | + return paramiko.OPEN_SUCCEEDED |
| 76 | + return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED |
| 77 | + |
| 78 | + def get_allowed_auths(self, username): |
| 79 | + return "password,publickey" |
| 80 | + |
| 81 | + |
26 | 82 | @pytest.fixture
|
27 | 83 | def ssh_conn(request: pytest.FixtureRequest) -> dict[str, Any]:
|
28 | 84 | server = Server([])
|
@@ -77,6 +133,18 @@ def test_run_command_password(server: Server, ssh_port: int):
|
77 | 133 | assert b"test_run_command_password" in server.commands
|
78 | 134 |
|
79 | 135 |
|
| 136 | +def test_run_command_no_password(server: Server, ssh_port: int): |
| 137 | + vendor = AsyncSSHVendor() |
| 138 | + with pytest.raises(AuthError): |
| 139 | + vendor.run_command( |
| 140 | + "127.0.0.1", |
| 141 | + "test_run_command_password", |
| 142 | + username=USER, |
| 143 | + port=ssh_port, |
| 144 | + password=None, |
| 145 | + ) |
| 146 | + |
| 147 | + |
80 | 148 | def test_run_command_with_privkey(server: Server, ssh_port: int):
|
81 | 149 | key = asyncssh.import_private_key(CLIENT_KEY)
|
82 | 150 |
|
|
0 commit comments