| 1 |
import logging |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
import threading |
| 5 |
from pathlib import Path |
| 6 |
|
| 7 |
import pytest |
| 8 |
from icecream import ic |
| 9 |
from icecream import install as install_ic |
| 10 |
from invoke.vendor.lexicon import Lexicon |
| 11 |
|
| 12 |
from paramiko import ( |
| 13 |
SFTP, |
| 14 |
ECDSAKey, |
| 15 |
Ed25519Key, |
| 16 |
PKey, |
| 17 |
RSAKey, |
| 18 |
SFTPServer, |
| 19 |
Transport, |
| 20 |
) |
| 21 |
|
| 22 |
from ._loop import LoopSocket |
| 23 |
from ._stub_sftp import StubServer, StubSFTPServer |
| 24 |
from ._util import _support |
| 25 |
|
| 26 |
# Better print() for debugging - use ic()! |
| 27 |
install_ic() |
| 28 |
ic.configureOutput(includeContext=True) |
| 29 |
|
| 30 |
|
| 31 |
# Perform logging by default; pytest will capture and thus hide it normally, |
| 32 |
# presenting it on error/failure. (But also allow turning it off when doing |
| 33 |
# very pinpoint debugging - e.g. using breakpoints, so you don't want output |
| 34 |
# hiding enabled, but also don't want all the logging to gum up the terminal.) |
| 35 |
if not os.environ.get("DISABLE_LOGGING", False): |
| 36 |
logging.basicConfig( |
| 37 |
level=logging.DEBUG, |
| 38 |
# Also make sure to set up timestamping for more sanity when debugging. |
| 39 |
format="[%(relativeCreated)s]\t%(levelname)s:%(name)s:%(message)s", |
| 40 |
datefmt="%H:%M:%S", |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def make_sftp_folder(): |
| 45 |
""" |
| 46 |
Ensure expected target temp folder exists on the remote end. |
| 47 |
|
| 48 |
Will clean it out if it already exists. |
| 49 |
""" |
| 50 |
# TODO: go back to using the sftp functionality itself for folder setup so |
| 51 |
# we can test against live SFTP servers again someday. (Not clear if anyone |
| 52 |
# is/was using the old capability for such, though...) |
| 53 |
# TODO: something that would play nicer with concurrent testing (but |
| 54 |
# probably e.g. using thread ID or UUIDs or something; not the "count up |
| 55 |
# until you find one not used!" crap from before...) |
| 56 |
# TODO: if we want to lock ourselves even harder into localhost-only |
| 57 |
# testing (probably not?) could use tempdir modules for this for improved |
| 58 |
# safety. Then again...why would someone have such a folder??? |
| 59 |
path = os.environ.get("TEST_FOLDER", "paramiko-test-target") |
| 60 |
# Forcibly nuke this directory locally, since at the moment, the below |
| 61 |
# fixtures only ever run with a locally scoped stub test server. |
| 62 |
shutil.rmtree(path, ignore_errors=True) |
| 63 |
# Then create it anew, again locally, for the same reason. |
| 64 |
os.mkdir(path) |
| 65 |
return path |
| 66 |
|
| 67 |
|
| 68 |
@pytest.fixture # (scope='session') |
| 69 |
def sftp_server(): |
| 70 |
""" |
| 71 |
Set up an in-memory SFTP server thread. Yields the client Transport/socket. |
| 72 |
|
| 73 |
The resulting client Transport (along with all the server components) will |
| 74 |
be the same object throughout the test session; the `sftp` fixture then |
| 75 |
creates new higher level client objects wrapped around the client |
| 76 |
Transport, as necessary. |
| 77 |
""" |
| 78 |
# Sockets & transports |
| 79 |
socks = LoopSocket() |
| 80 |
sockc = LoopSocket() |
| 81 |
sockc.link(socks) |
| 82 |
# TODO: reuse with new server fixture if possible |
| 83 |
tc = Transport(sockc) |
| 84 |
ts = Transport(socks) |
| 85 |
# Auth |
| 86 |
host_key = RSAKey.from_private_key_file(_support("rsa.key")) |
| 87 |
ts.add_server_key(host_key) |
| 88 |
# Server setup |
| 89 |
event = threading.Event() |
| 90 |
server = StubServer() |
| 91 |
ts.set_subsystem_handler("sftp", SFTPServer, StubSFTPServer) |
| 92 |
ts.start_server(event, server) |
| 93 |
# Wait (so client has time to connect? Not sure. Old.) |
| 94 |
event.wait(1.0) |
| 95 |
# Make & yield connection. |
| 96 |
tc.connect(username="slowdive", password="pygmalion") |
| 97 |
yield tc |
| 98 |
# TODO: any need for shutdown? Why didn't old suite do so? Or was that the |
| 99 |
# point of the "join all threads from threading module" crap in test.py? |
| 100 |
|
| 101 |
|
| 102 |
@pytest.fixture |
| 103 |
def sftp(sftp_server): |
| 104 |
""" |
| 105 |
Yield an SFTP client connected to the global in-session SFTP server thread. |
| 106 |
""" |
| 107 |
# Client setup |
| 108 |
client = SFTP.from_transport(sftp_server) |
| 109 |
# Work in 'remote' folder setup (as it wants to use the client) |
| 110 |
# TODO: how cleanest to make this available to tests? Doing it this way is |
| 111 |
# marginally less bad than the previous 'global'-using setup, but not by |
| 112 |
# much? |
| 113 |
client.FOLDER = make_sftp_folder() |
| 114 |
# Yield client to caller |
| 115 |
yield client |
| 116 |
# Clean up - as in make_sftp_folder, we assume local-only exec for now. |
| 117 |
shutil.rmtree(client.FOLDER, ignore_errors=True) |
| 118 |
|
| 119 |
|
| 120 |
# short type, full type, class, fingerprint, optional passphrase (will imply a |
| 121 |
# 2nd copy of key which is passphrased) |
| 122 |
key_data = [ |
| 123 |
[ |
| 124 |
"rsa", |
| 125 |
"ssh-rsa", |
| 126 |
RSAKey, |
| 127 |
"SHA256:OhNL391d/beeFnxxg18AwWVYTAHww+D4djEE7Co0Yng", |
| 128 |
None, |
| 129 |
], |
| 130 |
[ |
| 131 |
"rsa", |
| 132 |
"rsa-sha2-512", |
| 133 |
RSAKey, |
| 134 |
"SHA256:44W6xS7SyDLHqSar8WCiclOLpjn7OwuGz+vQ9OQ+Eaw", |
| 135 |
b"secret", |
| 136 |
], |
| 137 |
[ |
| 138 |
"ed25519", |
| 139 |
"ssh-ed25519", |
| 140 |
Ed25519Key, |
| 141 |
"SHA256:J6VESFdD3xSChn8y9PzWzeF+1tl892mOy2TqkMLO4ow", |
| 142 |
None, |
| 143 |
], |
| 144 |
[ |
| 145 |
"ecdsa-256", |
| 146 |
"ecdsa-sha2-nistp256", |
| 147 |
ECDSAKey, |
| 148 |
"SHA256:BrQG04oNKUETjKCeL4ifkARASg3yxS/pUHl3wWM26Yg", |
| 149 |
None, |
| 150 |
], |
| 151 |
# TODO: moar? |
| 152 |
] |
| 153 |
|
| 154 |
|
| 155 |
@pytest.fixture(scope="session", params=key_data, ids=lambda x: x[1]) |
| 156 |
def keys(request): |
| 157 |
""" |
| 158 |
Parameterize associated tests with an object for each known type of key, |
| 159 |
with attributes: |
| 160 |
|
| 161 |
- ``short_type``: short identifier, eg ``rsa`` or ``ecdsa-256`` |
| 162 |
- ``full_type``: the "message style" key identifier, eg ``ssh-rsa``, or |
| 163 |
``ecdsa-sha2-nistp256``. |
| 164 |
- ``path``: a pathlib Path object to the fixture key file |
| 165 |
- ``pkey``: PKey object |
| 166 |
- ``pkey_with_cert``: PKey object with cert loaded (if applicable) |
| 167 |
- ``expected_fp``: the expected fingerprint of said key |
| 168 |
- ``passphrase``: if not ``None``, the passphrase this key requires. |
| 169 |
""" |
| 170 |
short_type, full_type, key_class, fingerprint, passphrase = request.param |
| 171 |
bag = Lexicon() |
| 172 |
bag.short_type = short_type |
| 173 |
bag.full_type = full_type |
| 174 |
filename = short_type |
| 175 |
if passphrase is not None: |
| 176 |
filename += "-passphrased" |
| 177 |
bag.path = Path(_support(f"{filename}.key")) |
| 178 |
with bag.path.open() as fd: |
| 179 |
bag.pkey = key_class.from_private_key(fd, password=passphrase) |
| 180 |
# Second copy for things like equality-but-not-identity testing |
| 181 |
with bag.path.open() as fd: |
| 182 |
bag.pkey2 = key_class.from_private_key(fd, password=passphrase) |
| 183 |
bag.expected_fp = fingerprint |
| 184 |
# Also tack on the cert-bearing variant for some tests |
| 185 |
cert = bag.path.with_suffix(".key-cert.pub") |
| 186 |
bag.pkey_with_cert = ( |
| 187 |
PKey.from_path(cert, password=passphrase) if cert.exists() else None |
| 188 |
) |
| 189 |
bag.passphrase = passphrase |
| 190 |
# Safety checks |
| 191 |
assert bag.pkey.fingerprint == fingerprint |
| 192 |
yield bag |