|
1 | 1 | """Tests for launching the Coana CLI from ReachabilityAnalyzer. |
2 | 2 |
|
3 | | -A fake cli-wrapper.mjs is installed into a temporary cache with the real coana-cache.mjs, so |
4 | | -nothing is mocked. The fake records the args, cwd and environment it was run with. |
| 3 | +A fake cli-wrapper.mjs is installed into a temporary cache with the real coana-cache.mjs. The |
| 4 | +fake records the args, cwd and environment it was run with. The tests of downloads through the |
| 5 | +mirror that SOCKET_COANA_MIRROR_URL names mock the Socket API and the download. |
5 | 6 | """ |
| 7 | +import hashlib |
| 8 | +import io |
6 | 9 | import json |
7 | 10 | import os |
| 11 | +import pathlib |
8 | 12 | import subprocess |
| 13 | +import tarfile |
| 14 | +from types import SimpleNamespace |
| 15 | +from typing import List, Sequence |
9 | 16 |
|
10 | 17 | import pytest |
| 18 | +import requests |
| 19 | +import urllib3 |
11 | 20 |
|
12 | 21 | from socketsecurity import __version__ |
13 | 22 | from socketsecurity.core.cli_client import CliClient |
@@ -187,3 +196,171 @@ def test_env_strips_npm_package_vars(fake_run_path, monkeypatch): |
187 | 196 | def test_failing_run_raises(fake_run_path): |
188 | 197 | with pytest.raises(Exception, match="exit code 1"): |
189 | 198 | _run(fake_run_path, additional_params=["--fail"]) |
| 199 | + |
| 200 | + |
| 201 | +MIRROR_URL = "https://mirror.test/socket-coana-cli" |
| 202 | + |
| 203 | + |
| 204 | +def _signed_url(signature: str) -> str: |
| 205 | + return f"https://storage.googleapis.com/socket-coana-cli/objects/SHA.tar.gz?X-Goog-Signature={signature}" |
| 206 | + |
| 207 | + |
| 208 | +@pytest.fixture(scope="module") |
| 209 | +def tarball() -> bytes: |
| 210 | + archive = io.BytesIO() |
| 211 | + with tarfile.open(fileobj=archive, mode="w:gz") as tar: |
| 212 | + wrapper = FAKE_CLI_WRAPPER.encode() |
| 213 | + member = tarfile.TarInfo("cli-wrapper.mjs") |
| 214 | + member.size = len(wrapper) |
| 215 | + tar.addfile(member, io.BytesIO(wrapper)) |
| 216 | + return archive.getvalue() |
| 217 | + |
| 218 | + |
| 219 | +@pytest.fixture |
| 220 | +def downloads() -> List[str]: |
| 221 | + """The URLs that ``install_from_mirror`` downloaded from.""" |
| 222 | + return [] |
| 223 | + |
| 224 | + |
| 225 | +@pytest.fixture |
| 226 | +def install_from_mirror(tarball, downloads, monkeypatch, tmp_path): |
| 227 | + """Installs the Coana CLI from an empty cache, downloading through SOCKET_COANA_MIRROR_URL. |
| 228 | +
|
| 229 | + The Socket API answers with ``signed_urls`` in turn. Downloads answer with the statuses in |
| 230 | + ``answers`` in turn, then with the tarball. |
| 231 | + """ |
| 232 | + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache")) |
| 233 | + sha256 = hashlib.sha256(tarball).hexdigest() |
| 234 | + |
| 235 | + def install( |
| 236 | + signed_urls: Sequence[str], |
| 237 | + answers: Sequence[int] = (), |
| 238 | + mirror_url: str = MIRROR_URL, |
| 239 | + version_spec: str = VERSION, |
| 240 | + ) -> str: |
| 241 | + pending_urls, pending_answers = list(signed_urls), list(answers) |
| 242 | + |
| 243 | + def request(path, **kwargs): |
| 244 | + body = { |
| 245 | + "version": VERSION, |
| 246 | + "components": {"cli": {"sha256": sha256, "size": len(tarball), "url": pending_urls.pop(0)}}, |
| 247 | + } |
| 248 | + return SimpleNamespace(json=lambda: body) |
| 249 | + |
| 250 | + def get(url, **kwargs): |
| 251 | + downloads.append(url) |
| 252 | + status = pending_answers.pop(0) if pending_answers else 200 |
| 253 | + response = requests.Response() |
| 254 | + response.status_code = status |
| 255 | + response.raw = urllib3.HTTPResponse( |
| 256 | + body=io.BytesIO(tarball if status == 200 else b""), status=status, preload_content=False |
| 257 | + ) |
| 258 | + return response |
| 259 | + |
| 260 | + monkeypatch.setenv("SOCKET_COANA_MIRROR_URL", mirror_url) |
| 261 | + monkeypatch.setattr(reachability.requests, "get", get) |
| 262 | + analyzer = _analyzer() |
| 263 | + analyzer.client.request = request |
| 264 | + return analyzer._get_coana_cli_dir(version_spec, "my-org", dict(os.environ)) |
| 265 | + |
| 266 | + return install |
| 267 | + |
| 268 | + |
| 269 | +@pytest.mark.parametrize( |
| 270 | + "mirror_url, base_url", |
| 271 | + [ |
| 272 | + (MIRROR_URL, "https://mirror.test/socket-coana-cli/"), |
| 273 | + ("https://mirror.test/socket-coana-cli/", "https://mirror.test/socket-coana-cli/"), |
| 274 | + ("http://user:secret@mirror.test:8081/socket-coana-cli", "http://user:secret@mirror.test:8081/socket-coana-cli/"), |
| 275 | + ], |
| 276 | +) |
| 277 | +def test_mirror_replaces_the_bucket_prefix_and_keeps_the_signature( |
| 278 | + install_from_mirror, downloads, mirror_url, base_url |
| 279 | +): |
| 280 | + cli_dir = install_from_mirror([_signed_url("first")], mirror_url=mirror_url) |
| 281 | + |
| 282 | + assert (pathlib.Path(cli_dir) / "cli-wrapper.mjs").read_text() == FAKE_CLI_WRAPPER |
| 283 | + assert downloads == [f"{base_url}objects/SHA.tar.gz?X-Goog-Signature=first"] |
| 284 | + |
| 285 | + |
| 286 | +def test_mirror_rewrites_the_url_it_requests_anew_after_the_mirror_refuses_one(install_from_mirror, downloads): |
| 287 | + install_from_mirror([_signed_url("first"), _signed_url("second")], answers=[403]) |
| 288 | + |
| 289 | + assert downloads == [ |
| 290 | + "https://mirror.test/socket-coana-cli/objects/SHA.tar.gz?X-Goog-Signature=first", |
| 291 | + "https://mirror.test/socket-coana-cli/objects/SHA.tar.gz?X-Goog-Signature=second", |
| 292 | + ] |
| 293 | + |
| 294 | + |
| 295 | +@pytest.mark.parametrize( |
| 296 | + "answers, message", |
| 297 | + [ |
| 298 | + ([500], "mirror.test:8081 responded with status 500 to the Coana CLI download"), |
| 299 | + ([403, 403], "mirror.test:8081 refused the Coana CLI URL with status 403"), |
| 300 | + ], |
| 301 | +) |
| 302 | +def test_mirror_host_is_named_without_credentials_when_a_download_fails(install_from_mirror, answers, message): |
| 303 | + with pytest.raises(Exception) as error: |
| 304 | + install_from_mirror( |
| 305 | + [_signed_url("first"), _signed_url("second")], |
| 306 | + answers=answers, |
| 307 | + mirror_url="https://user:secret@mirror.test:8081/socket-coana-cli", |
| 308 | + ) |
| 309 | + |
| 310 | + assert str(error.value) == message |
| 311 | + |
| 312 | + |
| 313 | +def test_mirror_invalid_value_and_url_outside_the_bucket_are_ignored_when_the_version_is_cached( |
| 314 | + install_from_mirror, downloads, cache_home, monkeypatch |
| 315 | +): |
| 316 | + monkeypatch.setenv("XDG_CACHE_HOME", str(cache_home)) |
| 317 | + |
| 318 | + cli_dir = install_from_mirror( |
| 319 | + ["https://example.test/coana.tar.gz"], mirror_url="ftp://mirror.test/", version_spec="^1.2.0" |
| 320 | + ) |
| 321 | + |
| 322 | + assert (pathlib.Path(cli_dir) / "cli-wrapper.mjs").read_text() == FAKE_CLI_WRAPPER |
| 323 | + assert downloads == [] |
| 324 | + |
| 325 | + |
| 326 | +@pytest.mark.parametrize( |
| 327 | + "outside", |
| 328 | + [ |
| 329 | + "https://storage.googleapis.com/another-bucket/objects/SHA.tar.gz", |
| 330 | + "https://storage.googleapis.com/socket-coana-cli/../another-bucket/objects/SHA.tar.gz", |
| 331 | + "https://storage.googleapis.com/socket-coana-cli/%2e%2e/another-bucket/objects/SHA.tar.gz", |
| 332 | + "https://storage.googleapis.com/socket-coana-cli/objects/%2E/SHA.tar.gz", |
| 333 | + ], |
| 334 | +) |
| 335 | +def test_mirror_refuses_a_url_outside_the_bucket_without_showing_its_signature( |
| 336 | + install_from_mirror, downloads, outside |
| 337 | +): |
| 338 | + with pytest.raises(Exception) as error: |
| 339 | + install_from_mirror([f"{outside}?X-Goog-Signature=secret"]) |
| 340 | + |
| 341 | + assert str(error.value) == ( |
| 342 | + f"Socket provided the Coana CLI at {outside}, outside https://storage.googleapis.com/socket-coana-cli/, " |
| 343 | + "which SOCKET_COANA_MIRROR_URL mirrors." |
| 344 | + ) |
| 345 | + assert downloads == [] |
| 346 | + |
| 347 | + |
| 348 | +@pytest.mark.parametrize( |
| 349 | + "mirror_url", |
| 350 | + [ |
| 351 | + "not a URL", |
| 352 | + "ftp://mirror.test/", |
| 353 | + "https:mirror.test", |
| 354 | + "https://mirror.test:99999/", |
| 355 | + "https://mirror.test/?token=secret", |
| 356 | + "https://mirror.test/#secret", |
| 357 | + "https://mirror.test/?", |
| 358 | + "https://mirror.test/#", |
| 359 | + ], |
| 360 | +) |
| 361 | +def test_mirror_refuses_an_invalid_value_without_showing_it(install_from_mirror, downloads, mirror_url): |
| 362 | + with pytest.raises(Exception) as error: |
| 363 | + install_from_mirror([_signed_url("first")], mirror_url=mirror_url) |
| 364 | + |
| 365 | + assert str(error.value) == "SOCKET_COANA_MIRROR_URL must be an http or https URL without a query or fragment." |
| 366 | + assert downloads == [] |
0 commit comments