Changeset 3764e3b in trunk


Ignore:
Timestamp:
2021-01-07T18:59:57Z (5 years ago)
Author:
Itamar Turner-Trauring <itamar@…>
Branches:
master
Children:
7a15f7e
Parents:
b887991
Message:

A (so far failing) test for SSH public key authentication.

Location:
integration
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified integration/conftest.py

    rb887991 r3764e3b  
    99from functools import partial
    1010from json import loads
    11 from subprocess import check_call
    1211
    1312from foolscap.furl import (
     
    4241    cli,
    4342    _run_node,
     43    generate_ssh_key
    4444)
    4545
     
    344344
    345345
    346 def generate_ssh_key(path):
    347     """Create a new SSH private/public key pair."""
    348     check_call(["ckeygen", "--type", "rsa", "--no-passphrase", "--bits", "512",
    349                 "--file", path])
    350 
    351 
    352346@pytest.fixture(scope='session')
    353347@log_call(action_type=u"integration:alice", include_args=[], include_result=False)
     
    367361
    368362    # 2. Enable SFTP on the node:
    369     ssh_key_path = join(process.node_dir, "private", "ssh_host_rsa_key")
     363    host_ssh_key_path = join(process.node_dir, "private", "ssh_host_rsa_key")
    370364    accounts_path = join(process.node_dir, "private", "accounts")
    371365    with open(join(process.node_dir, "tahoe.cfg"), "a") as f:
     
    377371host_privkey_file = {ssh_key_path}
    378372accounts.file = {accounts_path}
    379 """.format(ssh_key_path=ssh_key_path, accounts_path=accounts_path))
    380     generate_ssh_key(ssh_key_path)
    381 
    382     # 3. Add a SFTP access file with username, password, and rwcap.
     373""".format(ssh_key_path=host_ssh_key_path, accounts_path=accounts_path))
     374    generate_ssh_key(host_ssh_key_path)
     375
     376    # 3. Add a SFTP access file with username/password and SSH key auth.
     377
     378    # The client SSH key path is typically going to be somewhere else (~/.ssh,
     379    # typically), but for convenience sake for testing we'll put it inside node.
     380    client_ssh_key_path = join(process.node_dir, "private", "ssh_client_rsa_key")
     381    generate_ssh_key(client_ssh_key_path)
     382    # Pub key format is "ssh-rsa <thekey> <username>". We want the key.
     383    ssh_public_key = open(client_ssh_key_path + ".pub").read().strip().split()[1]
    383384    with open(accounts_path, "w") as f:
    384385        f.write("""\
    385 alice password {}
    386 """.format(rwcap))
    387     # TODO add sftp access with public key
     386alice password {rwcap}
     387
     388alice2 ssh-rsa {ssh_public_key} {rwcap}
     389""".format(rwcap=rwcap, ssh_public_key=ssh_public_key))
    388390
    389391    # 4. Restart the node with new SFTP config.
  • TabularUnified integration/test_sftp.py

    rb887991 r3764e3b  
    2424
    2525
    26 def connect_sftp(alice, username="alice", password="password"):
     26def connect_sftp(connect_args={"username": "alice", "password": "password"}):
    2727    """Create an SFTP client."""
    2828    client = SSHClient()
    2929    client.set_missing_host_key_policy(AutoAddPolicy)
    30     client.connect(
    31         "localhost", username=username, password=password, port=8022,
    32         look_for_keys=False
    33     )
     30    client.connect("localhost", port=8022, look_for_keys=False, **connect_args)
    3431    sftp = SFTPClient.from_transport(client.get_transport())
    3532
     
    5047
    5148
    52 def test_bad_account_password(alice):
    53     """Can't login with unknown username or wrong password."""
     49def test_bad_account_password_ssh_key(alice):
     50    """
     51    Can't login with unknown username, wrong password, or wrong SSH pub key.
     52    """
    5453    for u, p in [("alice", "wrong"), ("someuser", "password")]:
    5554        with pytest.raises(AuthenticationException):
    56             connect_sftp(alice, u, p)
     55            connect_sftp(connect_args={
     56                "username": u, "password": p,
     57            })
     58    # TODO bad pubkey
     59
     60
     61def test_ssh_key_auth(alice):
     62    """It's possible to login authenticating with SSH public key."""
     63    key_filename = join(alice.node_dir, "private", "ssh_client_rsa_key")
     64    sftp = connect_sftp(connect_args={
     65        "username": "alice2", "key_filename": key_filename
     66    })
     67    assert sftp.listdir() == []
    5768
    5869
    5970def test_read_write_files(alice):
    6071    """It's possible to upload and download files."""
    61     sftp = connect_sftp(alice)
     72    sftp = connect_sftp()
    6273    f = sftp.file("myfile", "wb")
    6374    f.write(b"abc")
     
    7687    them.
    7788    """
    78     sftp = connect_sftp(alice)
     89    sftp = connect_sftp()
    7990    assert sftp.listdir() == []
    8091
     
    102113    sftp.rmdir("childdir")
    103114    assert sftp.listdir() == []
     115
     116
     117def test_rename(alice):
     118    """Directories and files can be renamed."""
     119    sftp = connect_sftp()
     120    sftp.mkdir("dir")
     121
     122    filepath = join("dir", "file")
     123    with sftp.file(filepath, "wb") as f:
     124        f.write(b"abc")
     125
     126    sftp.rename(filepath, join("dir", "file2"))
     127    sftp.rename("dir", "dir2")
     128
     129    with sftp.file(join("dir2", "file2"), "rb") as f:
     130        assert f.read() == b"abc"
  • TabularUnified integration/util.py

    rb887991 r3764e3b  
    66from six.moves import StringIO
    77from functools import partial
    8 from subprocess import check_output
     8from subprocess import check_output, check_call
    99
    1010from twisted.python.filepath import (
     
    507507        )
    508508    )
     509
     510
     511def generate_ssh_key(path):
     512    """Create a new SSH private/public key pair."""
     513    check_call(["ckeygen", "--type", "rsa", "--no-passphrase", "--bits", "512",
     514                "--file", path])
Note: See TracChangeset for help on using the changeset viewer.