def test_openssh_public_key_compare_load_file( shared_datadir, format, encryption, comment, ): filename = f"test_key_{format}.pub" contents = (shared_datadir / filename).read_text() pubkey = Ssh2Key.parse(contents)[0] fpubkey = Ssh2Key.parse_file(shared_datadir / filename)[0] assert pubkey == fpubkey
def test_convert_rfc4716_to_openssh(shared_datadir, format, encryption): openssh_filename = f"test_key_{format}.pub" openssh_contents = (shared_datadir / openssh_filename).read_text() openssh_pubkey = Ssh2Key.parse(openssh_contents)[0] rfc4716_filename = f"test_key_{format}_rfc4716.pub" rfc4716_contents = (shared_datadir / rfc4716_filename).read_text() rfc4716_pubkey = Ssh2Key.parse(rfc4716_contents)[0] # force the comments to be the same openssh_pubkey.headers["Comment"] = rfc4716_pubkey.comment() # # check the generated openssh version matches the loaded one assert openssh_pubkey.secsh() == rfc4716_contents
def test_load_inline(): key_contents = ( "---- BEGIN SSH2 PUBLIC KEY ----\r\n" "Comment: [email protected]\r\n" "AAAAC3NzaC1lZDI1NTE5AAAAIC8LzfyUV6Z7hZu7dPZ6aDpE/dMRjIaIlaFgQB/vf1FH\r\n" "---- END SSH2 PUBLIC KEY ----\r\n" "---- BEGIN SSH2 PUBLIC KEY ----\r\n" "Comment: [email protected]\r\n" "AAAAC3NzaC1lZDI1NTE5AAAAIC8LzfyUV6Z7hZu7dPZ6aDpE/dMRjIaIlaFgQB/vf1FH\r\n" "---- END SSH2 PUBLIC KEY ----\r\n" "---- BEGIN SSH2 PUBLIC KEY ----\r\n" "Comment: nigel@weatherwax-2016\r\n" "AAAAB3NzaC1yc2EAAAADAQABAAABgQDUY4IlGS2XSu+HpopBTE2FFOUzg6zY+B9h0I3kG5\r\n" "EmUNdV/ybduOH/zTuynNL4nfF1dPUxFmd+YfwAmmBxYXhLA5vHFdcASK5Lg4GazJVVy+nO\r\n" "aw3KedsO4IpRgDDwUTG6mwUp5WNXOrjdQJDhSGucaLvXYOZcxP2juvKgkOQHQp2oEmftC1\r\n" "WcYCR0BKx7NlctFHw+3hMI3ZHqRulnMKZ7MCOjDUEBX3XW5LTlTtT8XrUm6aZ+AxLoyhQI\r\n" "K7Ny7yj7lyuVD56hvzU6JhAKeW+TZ3+Sewo92/ljVy3fT6KHXP7keUJVkDwPFir8afsWEv\r\n" "hVi9fjbWNKic2YUz18f3t5qsb0y9Qw88UIM7argp7ncg0oSrmh+0R92+wLz3VAmR7iD4Wp\r\n" "Rk45zKU/bnRjdYw8omJJQ4qQuFFDF83qpbbSncDBtn/hMzqP6Ynbis6HqsYpbLKYByNvYZ\r\n" "tMVsP4DAn5zhsBbBUJm7F6nwY4VcTZoUd+KYD3+DOHigaI6vcbxas=\r\n" "---- END SSH2 PUBLIC KEY ----\r\n" "---- BEGIN SSH2 PUBLIC KEY ----\r\n" "Comment: [email protected]\r\n" "AAAAC3NzaC1lZDI1NTE5AAAAIC8LzfyUV6Z7hZu7dPZ6aDpE/dMRjIaIlaFgQB/vf1FH\r\n" "---- END SSH2 PUBLIC KEY ----\r\n" ) keys = Ssh2Key.parse("".join(key_contents)) assert len(keys) == 4
def test_openssh_public_key_file(shared_datadir, format, encryption, comment): filename = f"test_key_{format}.pub" pubkeys = Ssh2Key.parse_file(shared_datadir / filename) pubkey = pubkeys[0] assert len(pubkeys) == 1 assert pubkey.encryption == encryption assert pubkey.type == "public" assert pubkey.comment() == comment assert len(pubkey.key) > 65
def test_load_multiple_rfc4716(shared_datadir): contents = [] for (format, encryption) in convert_pubkey_tests: rfc4716_filename = f"test_key_{format}_rfc4716.pub" rfc4716_contents = (shared_datadir / rfc4716_filename).read_text() contents.append(rfc4716_contents.replace("\n", "\r\n")) keys = Ssh2Key.parse("".join(contents)) # # check we have the right number assert len(keys) == len(contents)
def test_load_multiple_rfc4716_pubkey_key(shared_datadir): key_contents = [] for format in multiple_key_tests: rfc4716_filename = f"test_key_{format}.pub" rfc4716_contents = (shared_datadir / rfc4716_filename).read_text() key_contents.append(rfc4716_contents) all_keys = "\n".join(key_contents) print(all_keys) keys = Ssh2Key.parse(all_keys) assert len(keys) == len(multiple_key_tests) for key in keys: assert isinstance(key, Ssh2Key)
def test_openssh_public_key_load(shared_datadir, format, encryption, comment): filename = f"test_key_{format}.pub" contents = (shared_datadir / filename).read_text() pubkeys = Ssh2Key.parse(contents) pubkey = pubkeys[0] assert len(pubkeys) == 1 assert pubkey.encryption == encryption assert pubkey.type == "public" assert pubkey.comment() == comment assert len(pubkey.key) > 65 # # check the key round trips OK (will break if any additional crap in file) assert contents == pubkey.openssh()
def test_load_inline(): """This exposed a bug in the parser previously""" key_contents = ( "---- BEGIN SSH2 PUBLIC KEY ----\n" "Comment: [email protected]\n" "AAAAC3NzaC1lZDI1NTE5AAAAIHV8K5j1UNZSmAl4Xq7Yp8Qe1nN74B5NA7UyZDvz0H4M\n" "---- END SSH2 PUBLIC KEY ----\n" "---- BEGIN SSH2 PUBLIC KEY ----\n" "Comment: [email protected]\n" "AAAAC3NzaC1lZDI1NTE5AAAAIHV8K5j1UNZSmAl4Xq7Yp8Qe1nN74B5NA7UyZDvz0H4M\n" "---- END SSH2 PUBLIC KEY ----\n" ) keys = Ssh2Key.parse(key_contents) assert len(keys) == 2
def test_load_missing_key_file(shared_datadir, format, encryption): openssh_filename = f"test_key_duff_{format}" with pytest.raises(OSError): keys = Ssh2Key.parse_file(shared_datadir / openssh_filename) # noqa: F841
def test_load_private_key(shared_datadir, format, encryption): openssh_filename = f"test_key_{format}" openssh_contents = (shared_datadir / openssh_filename).read_text() with pytest.raises(ValueError): keys = Ssh2Key.parse(openssh_contents) # noqa: F841
def test_load_no_valid_key_key(): for content in invalid_key_tests: with pytest.raises(ValueError): keys = Ssh2Key.parse(content) # noqa: F841