|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import yaml |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +from zxcvbn import zxcvbn |
| 8 | + |
| 9 | +pytestmark = pytest.mark.deb |
| 10 | + |
| 11 | + |
| 12 | +# TODO: use fixtures to install/purge and register/unregister bouncers |
| 13 | + |
| 14 | + |
| 15 | +def test_deb_install_purge(deb_package_path, bouncer_under_test, must_be_root): |
| 16 | + # test the full install-purge cycle, doing that in separate tests would |
| 17 | + # be a bit too much |
| 18 | + |
| 19 | + # TODO: remove and reinstall |
| 20 | + |
| 21 | + # use the package built as non-root by test_deb_build() |
| 22 | + assert deb_package_path.exists(), f'This test requires {deb_package_path}' |
| 23 | + |
| 24 | + p = subprocess.check_output( |
| 25 | + ['dpkg-deb', '-f', deb_package_path.as_posix(), 'Package'], |
| 26 | + encoding='utf-8' |
| 27 | + ) |
| 28 | + package_name = p.strip() |
| 29 | + |
| 30 | + subprocess.check_call(['dpkg', '--purge', package_name]) |
| 31 | + |
| 32 | + bouncer_exe = f"/usr/bin/{bouncer_under_test}" |
| 33 | + assert not os.path.exists(bouncer_exe) |
| 34 | + |
| 35 | + config = f"/etc/crowdsec/bouncers/{bouncer_under_test}.yaml" |
| 36 | + assert not os.path.exists(config) |
| 37 | + |
| 38 | + # install the package |
| 39 | + p = subprocess.run( |
| 40 | + ['dpkg', '--install', deb_package_path.as_posix()], |
| 41 | + stdout=subprocess.PIPE, |
| 42 | + stderr=subprocess.PIPE, |
| 43 | + encoding='utf-8' |
| 44 | + ) |
| 45 | + assert p.returncode == 0, f'Failed to install {deb_package_path}' |
| 46 | + |
| 47 | + assert os.path.exists(bouncer_exe) |
| 48 | + assert os.stat(bouncer_exe).st_mode & 0o777 == 0o755 |
| 49 | + |
| 50 | + assert os.path.exists(config) |
| 51 | + assert os.stat(config).st_mode & 0o777 == 0o600 |
| 52 | + |
| 53 | + with open(config) as f: |
| 54 | + cfg = yaml.safe_load(f) |
| 55 | + api_key = cfg['api_key'] |
| 56 | + # the api key has been set to a random value |
| 57 | + assert zxcvbn(api_key)['score'] == 4, f"weak api_key: '{api_key}'" |
| 58 | + |
| 59 | + with open(config+'.id') as f: |
| 60 | + bouncer_name = f.read().strip() |
| 61 | + |
| 62 | + p = subprocess.check_output(['cscli', 'bouncers', 'list', '-o', 'json']) |
| 63 | + bouncers = yaml.safe_load(p) |
| 64 | + assert len([b for b in bouncers if b['name'] == bouncer_name]) == 1 |
| 65 | + |
| 66 | + p = subprocess.run( |
| 67 | + ['dpkg', '--purge', package_name], |
| 68 | + stdout=subprocess.PIPE, |
| 69 | + stderr=subprocess.PIPE, |
| 70 | + encoding='utf-8' |
| 71 | + ) |
| 72 | + assert p.returncode == 0, f'Failed to purge {package_name}' |
| 73 | + |
| 74 | + assert not os.path.exists(bouncer_exe) |
| 75 | + assert not os.path.exists(config) |
| 76 | + |
| 77 | + |
| 78 | +def test_deb_install_purge_yaml_local(deb_package_path, bouncer_under_test, must_be_root): |
| 79 | + """ |
| 80 | + Check .deb package installation with: |
| 81 | +
|
| 82 | + - a pre-existing .yaml.local file with an api key |
| 83 | + - a pre-registered bouncer |
| 84 | +
|
| 85 | + => the configuration files are not touched (no new api key) |
| 86 | + """ |
| 87 | + |
| 88 | + assert deb_package_path.exists(), f'This test requires {deb_package_path}' |
| 89 | + |
| 90 | + p = subprocess.check_output( |
| 91 | + ['dpkg-deb', '-f', deb_package_path.as_posix(), 'Package'], |
| 92 | + encoding='utf-8' |
| 93 | + ) |
| 94 | + package_name = p.strip() |
| 95 | + |
| 96 | + subprocess.check_call(['dpkg', '--purge', package_name]) |
| 97 | + subprocess.run(['cscli', 'bouncers', 'delete', 'testbouncer']) |
| 98 | + |
| 99 | + bouncer_exe = f"/usr/bin/{bouncer_under_test}" |
| 100 | + config = Path(f"/etc/crowdsec/bouncers/{bouncer_under_test}.yaml") |
| 101 | + config.parent.mkdir(parents=True, exist_ok=True) |
| 102 | + |
| 103 | + subprocess.check_call(['cscli', 'bouncers', 'add', 'testbouncer', '-k', '123456']) |
| 104 | + |
| 105 | + with open(config.with_suffix('.yaml.local'), 'w') as f: |
| 106 | + f.write('api_key: "123456"') |
| 107 | + |
| 108 | + p = subprocess.run( |
| 109 | + ['dpkg', '--install', deb_package_path.as_posix()], |
| 110 | + stdout=subprocess.PIPE, |
| 111 | + stderr=subprocess.PIPE, |
| 112 | + encoding='utf-8' |
| 113 | + ) |
| 114 | + assert p.returncode == 0, f'Failed to install {deb_package_path}' |
| 115 | + |
| 116 | + assert os.path.exists(bouncer_exe) |
| 117 | + assert os.path.exists(config) |
| 118 | + |
| 119 | + with open(config) as f: |
| 120 | + cfg = yaml.safe_load(f) |
| 121 | + api_key = cfg['api_key'] |
| 122 | + # the api key has not been set |
| 123 | + assert api_key == '${API_KEY}' |
| 124 | + |
| 125 | + p = subprocess.check_output([bouncer_exe, '-c', config, '-T']) |
| 126 | + merged_config = yaml.safe_load(p) |
| 127 | + assert merged_config['api_key'] == '123456' |
| 128 | + |
| 129 | + os.unlink(config.with_suffix('.yaml.local')) |
| 130 | + |
| 131 | + p = subprocess.run( |
| 132 | + ['dpkg', '--purge', package_name], |
| 133 | + stdout=subprocess.PIPE, |
| 134 | + stderr=subprocess.PIPE, |
| 135 | + encoding='utf-8' |
| 136 | + ) |
| 137 | + assert p.returncode == 0, f'Failed to purge {package_name}' |
| 138 | + |
| 139 | + assert not os.path.exists(bouncer_exe) |
| 140 | + assert not os.path.exists(config) |
0 commit comments