summaryrefslogtreecommitdiff
path: root/tests/integration_tests/bugs/test_gh586.py
blob: 44b643f16ce2f31fab433fa98d49ac56f68e303e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Integration test for pull #586

If a non-default AuthorizedKeysFile is specified in /etc/ssh/sshd_config,
ensure we can still ssh as expected.
"""
import paramiko
import pytest
from io import StringIO
from tests.integration_tests.assets import get_test_rsa_keypair


public_rsa_key, private_rsa_key = get_test_rsa_keypair()
USER_DATA = """\
#cloud-config
bootcmd:
  - sed -i 's/#AuthorizedKeysFile.*/AuthorizedKeysFile\\ .ssh\\/authorized_keys2/' /etc/ssh/sshd_config
ssh_authorized_keys:
 - {public_key}
""".format(public_key=public_rsa_key)  # noqa: E501


@pytest.mark.sru_2020_11
@pytest.mark.user_data(USER_DATA)
def test_non_default_authorized_keys(client):
    sshd = client.read_from_file('/etc/ssh/sshd_config')
    assert 'AuthorizedKeysFile .ssh/authorized_keys2' in sshd
    assert sshd.count('AuthorizedKeysFile') == 1

    ssh_dir = client.execute('ls /home/ubuntu/.ssh').stdout
    assert 'authorized_keys2' in ssh_dir

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    paramiko_key = paramiko.RSAKey.from_private_key(StringIO(private_rsa_key))

    # Will fail with paramiko.ssh_exception.AuthenticationException
    # if this bug isn't fixed
    ssh.connect(
        client.instance.ip,
        username=client.instance.username,
        pkey=paramiko_key,
    )