| 1 |
import os |
| 2 |
import stat |
| 3 |
from unittest.mock import patch |
| 4 |
|
| 5 |
import pytest |
| 6 |
import responses |
| 7 |
from pytest_mock import MockerFixture |
| 8 |
|
| 9 |
from censys.cli import main as cli_main |
| 10 |
from censys.common.config import ( |
| 11 |
CENSYS_PATH, |
| 12 |
CONFIG_PATH, |
| 13 |
DEFAULT, |
| 14 |
_restricted_opener, |
| 15 |
default_config, |
| 16 |
get_config, |
| 17 |
write_config, |
| 18 |
) |
| 19 |
from tests.search.v1.test_api import ACCOUNT_JSON |
| 20 |
from tests.utils import V1_URL, CensysTestCase |
| 21 |
|
| 22 |
TEST_CONFIG_PATH = CONFIG_PATH + ".test" |
| 23 |
|
| 24 |
|
| 25 |
def prompt_side_effect(arg, **kwargs): |
| 26 |
if arg == "Censys API ID": |
| 27 |
return CensysTestCase.api_id |
| 28 |
if arg == "Censys API Secret": |
| 29 |
return CensysTestCase.api_secret |
| 30 |
if arg == "Censys API ID [cyan](****************************aaaa)[/cyan]": |
| 31 |
return "" |
| 32 |
if arg == "Censys API Secret [cyan********************************bbbb)[/cyan]": |
| 33 |
return "" |
| 34 |
raise NotImplementedError(f"No prompt handler for {arg}") |
| 35 |
|
| 36 |
|
| 37 |
def confirm_side_effect(arg, **kwargs): |
| 38 |
return arg == "Do you want color output?" |
| 39 |
|
| 40 |
|
| 41 |
class CensysConfigCliTest(CensysTestCase): |
| 42 |
@pytest.fixture(autouse=True) |
| 43 |
def _config_setup(self, mocker: MockerFixture): |
| 44 |
mocker.patch("censys.common.config.CONFIG_PATH", TEST_CONFIG_PATH) |
| 45 |
self.mock_open = mocker.patch( |
| 46 |
"builtins.open", |
| 47 |
new_callable=mocker.mock_open, |
| 48 |
read_data="[DEFAULT]\napi_id =\napi_secret =\nasm_api_key =", |
| 49 |
) |
| 50 |
mocker.patch("rich.prompt.Prompt.ask", side_effect=prompt_side_effect) |
| 51 |
mocker.patch("rich.prompt.Confirm.ask", side_effect=confirm_side_effect) |
| 52 |
self.mock_chmod = mocker.patch("censys.common.config._try_chmod") |
| 53 |
|
| 54 |
def test_search_config(self): |
| 55 |
# Mock |
| 56 |
self.patch_args( |
| 57 |
[ |
| 58 |
"censys", |
| 59 |
"config", |
| 60 |
] |
| 61 |
) |
| 62 |
self.responses.add( |
| 63 |
responses.GET, |
| 64 |
V1_URL + "/account", |
| 65 |
status=200, |
| 66 |
json=ACCOUNT_JSON, |
| 67 |
) |
| 68 |
|
| 69 |
with pytest.raises(SystemExit, match="0"): |
| 70 |
cli_main() |
| 71 |
|
| 72 |
# Assert that the config file was read from the right place |
| 73 |
self.mock_open.assert_called_with(TEST_CONFIG_PATH, "w", opener=_restricted_opener) |
| 74 |
|
| 75 |
def test_search_config_failed(self): |
| 76 |
# Mock |
| 77 |
self.patch_args( |
| 78 |
[ |
| 79 |
"censys", |
| 80 |
"config", |
| 81 |
] |
| 82 |
) |
| 83 |
self.responses.add( |
| 84 |
responses.GET, |
| 85 |
V1_URL + "/account", |
| 86 |
status=401, |
| 87 |
json={"error": "Unauthorized"}, |
| 88 |
) |
| 89 |
|
| 90 |
# Actual call/error raising |
| 91 |
with pytest.raises(SystemExit, match="1"): |
| 92 |
cli_main() |
| 93 |
|
| 94 |
def test_search_config_makedirs(self): |
| 95 |
self.patch_args( |
| 96 |
[ |
| 97 |
"censys", |
| 98 |
"config", |
| 99 |
] |
| 100 |
) |
| 101 |
self.mocker.patch("censys.common.config.os.path.isdir", return_value=False) |
| 102 |
mock_makedirs = self.mocker.patch("censys.common.config.os.makedirs") |
| 103 |
|
| 104 |
self.responses.add( |
| 105 |
responses.GET, |
| 106 |
V1_URL + "/account", |
| 107 |
status=200, |
| 108 |
json=ACCOUNT_JSON, |
| 109 |
) |
| 110 |
|
| 111 |
with pytest.raises(SystemExit, match="0"): |
| 112 |
cli_main() |
| 113 |
|
| 114 |
mock_makedirs.assert_called_with(CENSYS_PATH, mode=0o700) |
| 115 |
|
| 116 |
def test_config_default(self): |
| 117 |
mock_isfile = self.mocker.patch("censys.common.config.os.path.isfile", return_value=True) |
| 118 |
config = get_config() |
| 119 |
mock_isfile.return_value = False |
| 120 |
mock_isfile.assert_called_with(TEST_CONFIG_PATH) |
| 121 |
self.mock_open.assert_called_once() |
| 122 |
for key, value in default_config.items(): |
| 123 |
assert value == config.get(DEFAULT, key) |
| 124 |
|
| 125 |
def test_search_config_custom_config(self): |
| 126 |
self.patch_args( |
| 127 |
[ |
| 128 |
"censys", |
| 129 |
"config", |
| 130 |
] |
| 131 |
) |
| 132 |
self.mocker.patch.dict("censys.common.config.os.environ", {"CENSYS_CONFIG_PATH": "censys.cfg"}) |
| 133 |
|
| 134 |
self.responses.add( |
| 135 |
responses.GET, |
| 136 |
V1_URL + "/account", |
| 137 |
status=200, |
| 138 |
json=ACCOUNT_JSON, |
| 139 |
) |
| 140 |
|
| 141 |
with pytest.raises(SystemExit, match="0"): |
| 142 |
cli_main() |
| 143 |
|
| 144 |
# Assert that the config file was read from the right place |
| 145 |
self.mock_open.assert_called_with("censys.cfg", "w", opener=_restricted_opener) |
| 146 |
|
| 147 |
def test_search_config_perm_error(self): |
| 148 |
self.patch_args( |
| 149 |
[ |
| 150 |
"censys", |
| 151 |
"config", |
| 152 |
] |
| 153 |
) |
| 154 |
self.mocker.patch("censys.common.config.os.access", return_value=False) |
| 155 |
self.responses.add( |
| 156 |
responses.GET, |
| 157 |
V1_URL + "/account", |
| 158 |
status=200, |
| 159 |
json=ACCOUNT_JSON, |
| 160 |
) |
| 161 |
|
| 162 |
with pytest.raises(SystemExit, match="1"): |
| 163 |
cli_main() |
| 164 |
|
| 165 |
|
| 166 |
@pytest.fixture |
| 167 |
def home_config(tmp_path, mocker, monkeypatch): |
| 168 |
"""Points the default config location at a throwaway home directory.""" |
| 169 |
monkeypatch.delenv("CENSYS_CONFIG_PATH", raising=False) |
| 170 |
censys_path = tmp_path / ".config" / "censys" |
| 171 |
config_path = censys_path / "censys.cfg" |
| 172 |
mocker.patch("censys.common.config.HOME_PATH", str(tmp_path)) |
| 173 |
mocker.patch("censys.common.config.CENSYS_PATH", str(censys_path)) |
| 174 |
mocker.patch("censys.common.config.CONFIG_PATH", str(config_path)) |
| 175 |
return censys_path, config_path |
| 176 |
|
| 177 |
|
| 178 |
def test_write_config_creates_and_rewrites(home_config): |
| 179 |
censys_path, config_path = home_config |
| 180 |
|
| 181 |
# First write creates the directory and the file |
| 182 |
write_config(get_config()) |
| 183 |
assert censys_path.is_dir() |
| 184 |
assert config_path.is_file() |
| 185 |
|
| 186 |
# Second write takes the existing-directory and existing-file branches |
| 187 |
write_config(get_config()) |
| 188 |
assert get_config().get(DEFAULT, "color") == "auto" |
| 189 |
|
| 190 |
|
| 191 |
def test_write_config_survives_unchmodable_path(home_config): |
| 192 |
# A path we may not chmod must not stop the config from being written: |
| 193 |
# root-owned config dirs in containers, a non-owned CENSYS_CONFIG_PATH, and |
| 194 |
# mounts that reject chmod outright (NFS, CIFS, WSL DrvFs without metadata). |
| 195 |
_, config_path = home_config |
| 196 |
|
| 197 |
write_config(get_config()) |
| 198 |
|
| 199 |
with patch( |
| 200 |
"censys.common.config.os.chmod", |
| 201 |
side_effect=PermissionError(1, "Operation not permitted"), |
| 202 |
): |
| 203 |
write_config(get_config()) |
| 204 |
|
| 205 |
assert config_path.is_file() |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions only") |
| 209 |
def test_write_config_restricts_permissions(home_config): |
| 210 |
censys_path, config_path = home_config |
| 211 |
old_umask = os.umask(0o022) |
| 212 |
try: |
| 213 |
write_config(get_config()) |
| 214 |
|
| 215 |
assert stat.S_IMODE(os.stat(censys_path).st_mode) & 0o077 == 0 |
| 216 |
assert stat.S_IMODE(os.stat(config_path).st_mode) & 0o077 == 0 |
| 217 |
|
| 218 |
# Pre-existing loose permissions are tightened on rewrite |
| 219 |
os.chmod(censys_path, 0o755) |
| 220 |
os.chmod(config_path, 0o644) |
| 221 |
write_config(get_config()) |
| 222 |
|
| 223 |
assert stat.S_IMODE(os.stat(censys_path).st_mode) & 0o077 == 0 |
| 224 |
assert stat.S_IMODE(os.stat(config_path).st_mode) & 0o077 == 0 |
| 225 |
finally: |
| 226 |
os.umask(old_umask) |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions only") |
| 230 |
def test_write_config_keeps_permissions_when_chmod_fails(home_config): |
| 231 |
_, config_path = home_config |
| 232 |
|
| 233 |
write_config(get_config()) |
| 234 |
|
| 235 |
with patch( |
| 236 |
"censys.common.config.os.chmod", |
| 237 |
side_effect=PermissionError(1, "Operation not permitted"), |
| 238 |
): |
| 239 |
write_config(get_config()) |
| 240 |
|
| 241 |
assert stat.S_IMODE(os.stat(config_path).st_mode) & 0o077 == 0 |