Files @ 17cf34f73ca6
Branch filter:

Location: majic-ansible-roles/roles/backup_server/tests/test_default.py - annotation

branko
MAR-28: Implemented additional tests for mail_server role:

- Deploy a number of tools on clients in order to test SMTP, IMAP, and Sieve
services.
- Added one more user to LDAP directory for testing group restrictions.
- Deploy CA certificate on all testing machines for TLS validation purposes.
- Use different custom-configured cipher for mail server ciphers.
- Fixed invalid postmaster address for parameters-optional host.
- Deploy configuration files for use with Imap-CLI on client test machines.
- Updated testing of SMTP server to include checks for users that do not belong
to mail group.
- Extended some SMTP-related tests to cover both test servers.
- Some small fixes in SMTP-related tests for expected output from commands.
- Implemented tests covering Dovecot (IMAP + Sieve) functionality.
- Implemented tests for running/enabled services.
- Implemented tests for ClamAV.
- Implemented tests for firewall and connectivity.
- Implemented tests for Postfix TLS configuration.
- TODO: Tests for Sieve TLS configuration have not been written yet due to
limitation of available tools.
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
530a5b0abb6d
import socket

import paramiko

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    '.molecule/ansible_inventory').get_hosts('all')


def test_installed_software(Package):
    """
    Tests if the required packages have been installed.
    """

    assert Package('duplicity').is_installed
    assert Package('duply').is_installed


def test_backup_directory(File, Sudo):
    """
    Tests if the backup directory has been set-up correctly.
    """

    with Sudo():

        backup_directory = File('/srv/backups')

        assert backup_directory.is_directory
        assert backup_directory.user == 'root'
        assert backup_directory.group == 'root'
        assert backup_directory.mode == 0o751


def test_regular_ssh_server_configuration(File, Sudo):
    """
    Tests if the default SSH server has been configured correctly (to prevent
    access to it via backup users).
    """

    with Sudo():

        assert "DenyGroups backup" in File('/etc/ssh/sshd_config').content


def test_backup_ssh_server_configuration_directory(File, Sudo):
    """
    Tests if the backup SSH server configuration directory has been created
    correctly.
    """

    with Sudo():

        backup_ssh_server_directory = File('/etc/ssh-backup')

        assert backup_ssh_server_directory.is_directory
        assert backup_ssh_server_directory.user == 'root'
        assert backup_ssh_server_directory.group == 'root'
        assert backup_ssh_server_directory.mode == 0o700


def test_backup_ssh_server_service_configuration(File):
    """
    Tests if the backup SSH server service configuration file has been set-up
    correctly.
    """

    config_file = File('/etc/default/ssh-backup')

    assert config_file.is_file
    assert config_file.user == 'root'
    assert config_file.group == 'root'
    assert config_file.mode == 0o644
    assert 'SSHD_OPTS="-f /etc/ssh-backup/sshd_config"' in config_file.content


def test_backup_ssh_server_configuration(File, Sudo):
    """
    Tests if the backup SSH server configuration file has been set-up correctly.
    """

    with Sudo():

        config_file = File('/etc/ssh-backup/sshd_config')

        assert config_file.is_file
        assert config_file.user == 'root'
        assert config_file.group == 'root'
        assert config_file.mode == 0o600
        assert "AllowGroups backup" in config_file.content
        assert "ChrootDirectory %h" in config_file.content
        assert "ForceCommand internal-sftp" in config_file.content
        assert "Subsystem sftp internal-sftp" in config_file.content
        assert "PasswordAuthentication no" in config_file.content
        assert "PubkeyAuthentication yes" in config_file.content
        assert "PermitRootLogin no" in config_file.content
        assert "HostKey /etc/ssh-backup/ssh_host_rsa_key" in config_file.content
        assert "HostKey /etc/ssh-backup/ssh_host_dsa_key" in config_file.content
        assert "HostKey /etc/ssh-backup/ssh_host_ecdsa_key" in config_file.content
        assert "HostKey /etc/ssh-backup/ssh_host_ed25519_key" in config_file.content


def test_backup_ssh_server_keys(File, Sudo):
    """
    Tests if the backup SSH server private keys have been deployed correctly.
    """

    with Sudo():

        dsa = File('/etc/ssh-backup/ssh_host_dsa_key')
        assert dsa.is_file
        assert dsa.user == 'root'
        assert dsa.group == 'root'
        assert dsa.mode == 0o600
        assert dsa.content == open('tests/data/ssh/server_dsa', 'r').read()

        rsa = File('/etc/ssh-backup/ssh_host_rsa_key')
        assert rsa.is_file
        assert rsa.user == 'root'
        assert rsa.group == 'root'
        assert rsa.mode == 0o600
        assert rsa.content == open('tests/data/ssh/server_rsa', 'r').read()

        ed25519 = File('/etc/ssh-backup/ssh_host_ed25519_key')
        assert ed25519.is_file
        assert ed25519.user == 'root'
        assert ed25519.group == 'root'
        assert ed25519.mode == 0o600
        assert ed25519.content == open('tests/data/ssh/server_ed25519', 'r').read()

        ecdsa = File('/etc/ssh-backup/ssh_host_ecdsa_key')
        assert ecdsa.is_file
        assert ecdsa.user == 'root'
        assert ecdsa.group == 'root'
        assert ecdsa.mode == 0o600
        assert ecdsa.content == open('tests/data/ssh/server_ecdsa', 'r').read()


def test_backup_ssh_server_systemd_service(File):
    """
    Tests if the backup SSH server systemd service file has been deployed
    correctly.
    """

    service_file = File('/etc/systemd/system/ssh-backup.service')

    assert service_file.is_file
    assert service_file.user == 'root'
    assert service_file.group == 'root'
    assert service_file.mode == 0o644
    assert "EnvironmentFile=-/etc/default/ssh-backup" in service_file.content


def test_backup_ssh_server_service(Service, Socket, Sudo):
    """
    Tests if the backup SSH server service is running and listening on correct
    port.
    """

    with Sudo():

        service = Service('ssh-backup')
        assert service.is_running
        assert service.is_enabled
        assert Socket('tcp://0.0.0.0:2222').is_listening


def test_backup_ssh_server_login_mechanisms():
    """
    Tests available SSH login mechanisms (should be just public key).
    """

    sock = socket.socket()
    sock.connect(('10.31.127.11', 2222))

    transport = paramiko.transport.Transport(sock)
    transport.connect()

    try:
        transport.auth_none('')
    except paramiko.transport.BadAuthenticationType, err:
        assert err.allowed_types == ['publickey']