diff --git a/AGENTS.md b/AGENTS.md index ef5b62af..730bd165 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -186,7 +186,7 @@ Key principles: | `src/vip/bundle/` | `vip bundle` internals: `summary.py` (allowlisted config summary and the secret list, with a selftest that fails on an unclassified config field), `archive.py` (allowlisted report files, encoded-form secret scan, atomic zip write), and `stamp.py` (the `_output/.vip-render.json` stamp that `vip report` writes, so that a bundle holds only rendered files made from its own `results.json`) | | `src/vip/config.py` | TOML config loader and dataclasses (includes `[proxy]` → `ProxyConfig`) | | `src/vip/proxy.py` | Single source of truth for outbound-proxy resolution. `ProxyConfig` + `build_proxy_map` (mirrors httpx's `get_environment_proxies`, incl. NO_PROXY formatting), `build_mounts` (per-scheme `HTTPTransport` mounts that keep `verify`), `proxy_for_url` (httpx-identical most-specific-pattern selection, used by non-httpx probes), `playwright_proxy` (renders a Playwright `launch(proxy=)` dict). Every HTTP egress path routes through this so VIP never diverges from httpx's own env-proxy behavior — see "Outbound proxy support" below | -| `src/vip/auth/` | Interactive and headless browser authentication for OIDC providers, split into `browser.py` (Chromium launch, `InteractiveAuthSession`), `cache.py`, `flows.py` (`start_interactive_auth`, `start_headless_auth`), `sso.py`, `workbench.py`, `scheme.py` (`resolve_url_scheme`) and `apikey.py` (Connect API-key minting); `__init__.py` re-exports the names callers import from `vip.auth`, but a selftest must patch a helper on the submodule that calls it; `authenticated_page` opens a headless page from a cached auth session for `vip cleanup --workbench-url`; `auth_cache_path()` is the single source of truth for the `.vip-auth-cache.json` location (both `plugin/auth.py` and `cli/cleanup.py` must use it), and `_load_cached_auth` probes Workbench before trusting a cached session; `refresh_auth_cache_from_storage_state` writes a live context's cookies back over a cache whose session has been invalidated (atomic, 0600, existing caches only); both cache writers drop Entra's long-lived `ESTSAUTHPERSISTENT` cookie (`_CACHE_DROPPED_COOKIES`) because Entra sets it even when "Stay signed in?" is answered No | +| `src/vip/auth/` | Interactive and headless browser authentication for OIDC providers, split into `browser.py` (Chromium launch, `InteractiveAuthSession`), `cache.py`, `flows.py` (`start_interactive_auth`, `start_headless_auth`), `sso.py`, `workbench.py`, `scheme.py` (`resolve_url_scheme`) and `apikey.py` (Connect API-key minting); `__init__.py` re-exports the names callers import from `vip.auth`, but a selftest must patch a helper on the submodule that calls it; `authenticated_page` opens a headless page from a cached auth session for `vip cleanup --workbench-url`; `auth_cache_path()` is the single source of truth for the `.vip-auth-cache.json` location, under the user cache dir (`%LOCALAPPDATA%` on Windows, else `$XDG_CACHE_HOME` or `~/.cache`) in `vip/auth//`, never in cwd; it only computes the path (`None` when there is no home directory or cwd). `prepare_auth_cache()` runs on every `vip verify` (in `plugin/auth.py`, pytest controller only) and every `vip cleanup` (`cli/cleanup.py`), with or without a browser login, and both login paths must take the cache path from it: it makes `vip/` and `vip/auth/` owner-only, moves a legacy cwd cache there as a state+meta pair (deleting it if the move fails), and prunes other directories' caches past `AUTH_CACHE_PRUNE_AFTER_SECONDS` (a day, so a run that reused a cache just inside the 4-hour `AUTH_CACHE_TTL_SECONDS` keeps its storage state). `_load_cached_auth` treats a state file with no `.meta.json` as a miss and probes Workbench before trusting a cached session; `refresh_auth_cache_from_storage_state` writes a live context's cookies back over a cache whose session has been invalidated (atomic, 0600, existing caches only); both cache writers drop Entra's long-lived `ESTSAUTHPERSISTENT` cookie (`_CACHE_DROPPED_COOKIES`) because Entra sets it even when "Stay signed in?" is answered No | | `src/vip/idp.py` | IdP login form strategies for headless auth (Keycloak, Okta, Snowflake, Entra ID) | | `src/vip/attest.py` | The two skip helpers (`not_applicable`, `unproven`) that record whether a skipped check was out of scope or simply never verified | | `src/vip/errors.py` | The `VipError` exception hierarchy for user-facing CLI failures (`ConfigError`, `AuthError`, `ProductUnreachableError`, `InstallError`, `ReportError`, `BundleError`, `AuthConfigError`, `AuthTimeoutError`), each carrying an `exit_code`; raise one instead of calling `sys.exit` from command code — see "Error handling" below | diff --git a/README.md b/README.md index 2ed80877..8b399c3b 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,35 @@ Fast confidence check — skip the detailed/long-running checks: vip verify --config vip.toml --basic ``` +### Credentials on disk + +`--interactive-auth` and `--headless-auth` cache a live session for 4 hours: +the identity provider (IdP), Connect, and Workbench cookies, plus a Connect +API key that VIP creates. VIP keeps one cache for each directory you run it +from, in a folder named after a hash of that directory: + +| Platform | Location | +|---|---| +| Linux, macOS | `$XDG_CACHE_HOME/vip/auth//`, or `~/.cache/vip/auth//` when `XDG_CACHE_HOME` is unset | +| Windows | `%LOCALAPPDATA%\vip\auth\\` | + +Only your user can read these folders. Each `vip verify` or `vip cleanup` +deletes caches that have not been written for a day. VIP never reuses a cache +older than 4 hours, and the next login deletes the API keys of expired caches +from Connect. To sign out now, +delete the `vip/auth` folder. With no home directory and no `XDG_CACHE_HOME`, +VIP skips the cache. + +Earlier releases wrote `.vip-auth-cache.json` and `.vip-auth-cache.meta.json` +into the working directory. The first `vip verify` or `vip cleanup` of this +release moves them to the cache folder, or deletes them if it cannot. Never +share those files. + +`vip.toml` can hold `api_key` and `password` values. Remove or redact them +before you share a VIP directory. Prefer the `VIP_CONNECT_API_KEY`, +`VIP_WORKBENCH_API_KEY`, and `VIP_TEST_PASSWORD` environment variables over +inline secrets. + ## Uninstalling To reverse what `vip install` (or `just setup`) did: diff --git a/selftests/conftest.py b/selftests/conftest.py index f9647207..38769a04 100644 --- a/selftests/conftest.py +++ b/selftests/conftest.py @@ -52,6 +52,37 @@ def _restore_auth_entrypoints(): setattr(_auth, name, func) +_auth_cache_setup_patch_key = pytest.StashKey[pytest.MonkeyPatch]() + + +@pytest.hookimpl(tryfirst=True) +def pytest_configure(config): + """Keep outer-session configuration away from the user's credential files.""" + patch = pytest.MonkeyPatch() + config.stash[_auth_cache_setup_patch_key] = patch + config.add_cleanup(patch.undo) + patch.setattr("vip.plugin.auth.prepare_auth_cache", lambda **_: None) + + +@pytest.hookimpl(tryfirst=True) +def pytest_sessionstart(session): + """Restore cache preparation before selftests and nested pytest runs.""" + session.config.stash[_auth_cache_setup_patch_key].undo() + + +@pytest.fixture(autouse=True) +def _isolated_auth_cache_root(monkeypatch, tmp_path_factory): + """Point the auth cache root at a temp directory for every selftest. + + ``vip verify`` and ``vip cleanup`` tidy the real cache (migrate, prune, chmod) on + every run, so a selftest must never resolve the user's own ``~/.cache``. On Windows + ``LOCALAPPDATA`` comes before ``XDG_CACHE_HOME``, so it is cleared too. Tests about + the cache root set their own values with ``monkeypatch``. + """ + monkeypatch.delenv("LOCALAPPDATA", raising=False) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path_factory.mktemp("xdg-cache"))) + + @pytest.fixture(autouse=True) def _no_ambient_proxy(monkeypatch): """Clear proxy environment variables for every selftest. diff --git a/selftests/test_auth_cache.py b/selftests/test_auth_cache.py index 26b621cd..f121e09c 100644 --- a/selftests/test_auth_cache.py +++ b/selftests/test_auth_cache.py @@ -2,7 +2,11 @@ from __future__ import annotations +import errno +import os +import pathlib import sys +import time from typing import ClassVar import pytest @@ -30,6 +34,42 @@ def _make_session(self, tmp_path, *, connect_url: str, api_key: str | None): _connect_url=connect_url, ) + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") + def test_creates_the_cache_directory_owner_only(self, tmp_path): + from vip.auth import _save_auth_cache + + session = self._make_session(tmp_path, connect_url="", api_key=None) + cache = tmp_path / "a" / "b" / ".vip-auth-cache.json" + + _save_auth_cache(session, cache) + + assert cache.exists() + assert cache.parent.stat().st_mode & 0o777 == 0o700 + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") + @pytest.mark.skipif( + hasattr(os, "geteuid") and os.geteuid() == 0, reason="root ignores directory permissions" + ) + def test_unwritable_cache_root_skips_caching_without_raising( + self, tmp_path, monkeypatch, capsys + ): + from vip.auth import _save_auth_cache + + session = self._make_session(tmp_path, connect_url="", api_key=None) + locked = tmp_path / "locked" + locked.mkdir() + locked.chmod(0o500) + work = tmp_path / "work" + work.mkdir() + monkeypatch.chdir(work) + try: + _save_auth_cache(session, locked / "vip" / ".vip-auth-cache.json") + finally: + locked.chmod(0o700) + + assert "Skipping auth cache" in capsys.readouterr().out + assert not list(work.glob(".vip-auth-cache*")) + def test_skips_cache_when_connect_configured_but_mint_failed(self, tmp_path): from vip.auth import _save_auth_cache @@ -95,6 +135,124 @@ def test_writes_both_resolved_and_requested_connect_urls(self, tmp_path): assert meta["connect_url"] == "https://c.example.com" assert meta["requested_connect_url"] == "https://c.example.com/dashboard" + def _seed_old_pair(self, cache): + cache.write_text('{"cookies": [{"name": "old"}]}', encoding="utf-8") + cache.with_suffix(".meta.json").write_text('{"api_key": "OLD"}', encoding="utf-8") + + def test_failed_meta_write_leaves_no_cache(self, tmp_path, monkeypatch): + from vip.auth import _save_auth_cache + from vip.auth import cache as cache_module + + session = self._make_session(tmp_path, connect_url="https://c.example.com", api_key="NEW") + cache = tmp_path / ".vip-auth-cache.json" + self._seed_old_pair(cache) + real_write = cache_module._write_private_atomic + + def fail_meta(path, payload): + if path.name.endswith(".meta.json"): + raise OSError(errno.ENOSPC, "No space left on device") + return real_write(path, payload) + + monkeypatch.setattr(cache_module, "_write_private_atomic", fail_meta) + + _save_auth_cache(session, cache) + + assert not cache.exists() + assert not cache.with_suffix(".meta.json").exists() + + def test_unserialisable_meta_leaves_no_cache(self, tmp_path, capsys): + from vip.auth import _save_auth_cache + + session = self._make_session(tmp_path, connect_url="https://c.example.com", api_key="NEW") + session.key_name = object() + cache = tmp_path / ".vip-auth-cache.json" + self._seed_old_pair(cache) + + _save_auth_cache(session, cache) + + assert not cache.exists() + assert not cache.with_suffix(".meta.json").exists() + assert "Skipping auth cache" in capsys.readouterr().out + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") + def test_tightens_an_existing_cache_directory(self, tmp_path): + from vip.auth import _save_auth_cache + + session = self._make_session(tmp_path, connect_url="", api_key=None) + cache = tmp_path / "loose" / ".vip-auth-cache.json" + cache.parent.mkdir(mode=0o755) + cache.parent.chmod(0o755) + + _save_auth_cache(session, cache) + + assert cache.parent.stat().st_mode & 0o777 == 0o700 + + def test_failed_state_write_leaves_no_cache(self, tmp_path, monkeypatch): + from vip.auth import _save_auth_cache + from vip.auth import cache as cache_module + + session = self._make_session(tmp_path, connect_url="https://c.example.com", api_key="NEW") + cache = tmp_path / ".vip-auth-cache.json" + self._seed_old_pair(cache) + + def fail(_state): + raise OSError(errno.ENOSPC, "No space left on device") + + monkeypatch.setattr(cache_module, "_cacheable_storage_state", fail) + + _save_auth_cache(session, cache) + + assert not cache.exists() + assert not cache.with_suffix(".meta.json").exists() + + +_NO_CHMOD = pytest.mark.skipif( + sys.platform == "win32" or (hasattr(os, "geteuid") and os.geteuid() == 0), + reason="needs POSIX permission bits that root does not honour", +) + + +@pytest.fixture +def inaccessible_cache(tmp_path): + """A cache path inside a chmod-000 directory, restored on teardown.""" + locked = tmp_path / "locked" + locked.mkdir() + locked.chmod(0) + try: + yield locked / ".vip-auth-cache.json" + finally: + locked.chmod(0o700) + + +@_NO_CHMOD +class TestInaccessibleCacheDir: + """An unreadable cache directory disables caching instead of raising.""" + + def test_load_returns_none(self, inaccessible_cache): + from vip.auth import _load_cached_auth + + assert _load_cached_auth(inaccessible_cache) is None + + def test_refresh_returns_false(self, inaccessible_cache): + from vip.auth import refresh_auth_cache_from_storage_state + + assert refresh_auth_cache_from_storage_state({}, inaccessible_cache) is False + + def test_cache_is_not_referenced(self, tmp_path, inaccessible_cache): + from vip.auth import InteractiveAuthSession + + state = tmp_path / "state.json" + state.write_text('{"cookies": []}', encoding="utf-8") + session = InteractiveAuthSession( + storage_state_path=state, + api_key="K", + key_name="_vip_interactive_1", + _connect_url="https://c.example.com", + _cache_path=inaccessible_cache, + ) + + assert session._cache_references_this_key() is False + class TestLoadCachedAuth: """_load_cached_auth must refuse to reuse a cache that was minted @@ -139,6 +297,14 @@ def test_reuses_cache_when_urls_match(self, tmp_path): assert session is not None assert session.api_key == "CACHED" + def test_state_without_meta_is_a_miss(self, tmp_path): + from vip.auth import _load_cached_auth + + cache = self._write_cache(tmp_path, connect_url="https://c.example.com") + cache.with_suffix(".meta.json").unlink() + + assert _load_cached_auth(cache, "https://c.example.com") is None + def test_rejects_cache_when_connect_url_differs(self, tmp_path, capsys): from vip.auth import _load_cached_auth @@ -561,11 +727,166 @@ class TestAuthCachePath: silently disagreed and ``vip cleanup`` looked in the wrong place. """ - def test_resolves_relative_to_the_invocation_directory(self, tmp_path, monkeypatch): + def test_lives_under_the_user_cache_root_not_cwd(self, tmp_path, monkeypatch): from vip.auth import auth_cache_path + work, cache_home = tmp_path / "work", tmp_path / "xdg" + work.mkdir() + monkeypatch.chdir(work) + monkeypatch.setenv("XDG_CACHE_HOME", str(cache_home)) + + p = auth_cache_path() + + assert p.name == ".vip-auth-cache.json" + assert p.parent.parent == cache_home / "vip" / "auth" + assert len(p.parent.name) == 16 + assert work.resolve() not in p.parents + + def test_is_keyed_on_the_invocation_directory(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + (tmp_path / "a").mkdir() + (tmp_path / "b").mkdir() + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + + monkeypatch.chdir(tmp_path / "a") + first = auth_cache_path() + monkeypatch.chdir(tmp_path / "b") + second = auth_cache_path() + + assert first.parent != second.parent + assert first.parent.parent == second.parent.parent + + @pytest.mark.skipif(sys.platform == "win32", reason="symlinks need privileges on Windows") + def test_symlinked_cwd_resolves_to_the_same_cache(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + real = tmp_path / "real" + real.mkdir() + link = tmp_path / "link" + link.symlink_to(real, target_is_directory=True) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + + monkeypatch.chdir(real) + via_real = auth_cache_path() + monkeypatch.chdir(link) + via_link = auth_cache_path() + + assert via_real == via_link + + @pytest.mark.skipif(sys.platform == "win32", reason="Windows uses LOCALAPPDATA first") + def test_relative_xdg_cache_home_is_ignored(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + home = tmp_path / "home" + home.mkdir() monkeypatch.chdir(tmp_path) - assert auth_cache_path() == tmp_path / ".vip-auth-cache.json" + monkeypatch.setenv("XDG_CACHE_HOME", "rel") + monkeypatch.setenv("HOME", str(home)) + + assert auth_cache_path().parent.parent == home / ".cache" / "vip" / "auth" + + def test_windows_uses_localappdata(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + monkeypatch.chdir(tmp_path) + monkeypatch.setattr(sys, "platform", "win32") + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + monkeypatch.setenv("LOCALAPPDATA", str(tmp_path / "lad")) + + assert auth_cache_path().parent.parent == tmp_path / "lad" / "vip" / "auth" + + def test_windows_prefers_localappdata_over_xdg_cache_home(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + monkeypatch.chdir(tmp_path) + monkeypatch.setattr(sys, "platform", "win32") + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + monkeypatch.setenv("LOCALAPPDATA", str(tmp_path / "lad")) + + assert auth_cache_path().parent.parent == tmp_path / "lad" / "vip" / "auth" + + def test_macos_key_ignores_the_case_of_the_path(self, tmp_path, monkeypatch): + from vip.auth.cache import _cwd_key + + monkeypatch.setattr(sys, "platform", "darwin") + + assert _cwd_key(tmp_path / "Proj") == _cwd_key(tmp_path / "proj") + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX paths can hold non-UTF-8 bytes") + def test_non_utf8_directory_name_still_gets_a_key(self, tmp_path): + from vip.auth.cache import _cwd_key + + key = _cwd_key(tmp_path / os.fsdecode(b"caf\xe9")) + + assert len(key) == 16 + + def test_deleted_cwd_returns_none(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + + def gone(): + raise FileNotFoundError(errno.ENOENT, "No such file or directory") + + monkeypatch.setattr(pathlib.Path, "cwd", gone) + + assert auth_cache_path() is None + + def test_resolving_moves_nothing_and_prints_nothing(self, tmp_path, monkeypatch, capsys): + from vip.auth import auth_cache_path + + TestLegacyAuthCacheMigration._legacy(tmp_path) + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + + auth_cache_path() + + assert len(list(tmp_path.glob(".vip-auth-cache*"))) == 2 + assert capsys.readouterr().out == "" + + def test_resolving_creates_nothing_without_a_legacy_cache(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path + + cache_home = tmp_path / "xdg" + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("XDG_CACHE_HOME", str(cache_home)) + + auth_cache_path() + + assert not cache_home.exists() + + def _no_home(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("XDG_CACHE_HOME", raising=False) + monkeypatch.setattr(sys, "platform", "linux") + + def _raise(): + raise RuntimeError("Could not determine home directory.") + + monkeypatch.setattr(pathlib.Path, "home", _raise) + + def test_no_home_directory_skips_the_cache(self, tmp_path, monkeypatch, capsys): + from vip.auth import prepare_auth_cache + + self._no_home(tmp_path, monkeypatch) + before = set(tmp_path.iterdir()) + + assert prepare_auth_cache() is None + + assert "Skipping auth cache" in capsys.readouterr().out + assert set(tmp_path.iterdir()) == before + + def test_no_home_directory_leaves_legacy_cache_alone(self, tmp_path, monkeypatch): + from vip.auth import prepare_auth_cache + + self._no_home(tmp_path, monkeypatch) + legacy = tmp_path / ".vip-auth-cache.json" + legacy.write_text('{"cookies": []}', encoding="utf-8") + + assert prepare_auth_cache() is None + + assert legacy.read_text(encoding="utf-8") == '{"cookies": []}' def test_call_sites_do_not_build_the_path_inline(self): """Invariant: the filename literal lives in one place. A second inline @@ -587,6 +908,364 @@ def test_call_sites_do_not_build_the_path_inline(self): assert ".vip-auth-cache.json" in _Path(vip.auth.cache.__file__).read_text(encoding="utf-8") +class TestLegacyAuthCacheMigration: + """Caches an older VIP wrote into the invocation directory move to the user + cache dir exactly once, so a live session and API key stop sitting in cwd (#620). + """ + + @staticmethod + def _legacy(work, state="STATE", meta="META"): + work.mkdir(exist_ok=True) + (work / ".vip-auth-cache.json").write_text(state, encoding="utf-8") + (work / ".vip-auth-cache.meta.json").write_text(meta, encoding="utf-8") + + @staticmethod + def _dest(tmp_path): + return tmp_path / "xdg" / "vip" / "auth" / "abc" / ".vip-auth-cache.json" + + def test_moves_both_legacy_files_out_of_cwd(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + _migrate_legacy_auth_cache(work, dest) + + assert list(work.glob(".vip-auth-cache*")) == [] + assert dest.read_text(encoding="utf-8") == "STATE" + assert dest.with_suffix(".meta.json").read_text(encoding="utf-8") == "META" + + def test_keeps_mtime_so_the_ttl_still_applies(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + old = time.time() - 5 * 3600 + os.utime(work / ".vip-auth-cache.json", (old, old)) + + _migrate_legacy_auth_cache(work, dest) + + assert abs(dest.stat().st_mtime - old) < 1 + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX file modes do not apply on Windows") + def test_moved_files_are_owner_only(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + (work / ".vip-auth-cache.json").chmod(0o644) + + _migrate_legacy_auth_cache(work, dest) + + assert dest.stat().st_mode & 0o777 == 0o600 + assert dest.with_suffix(".meta.json").stat().st_mode & 0o777 == 0o600 + assert dest.parent.stat().st_mode & 0o777 == 0o700 + + def test_cross_device_move_still_lands(self, tmp_path, monkeypatch): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + def fail(*args, **kwargs): + raise OSError(errno.EXDEV, "x") + + monkeypatch.setattr(os, "rename", fail) + old = time.time() - 5 * 3600 + os.utime(work / ".vip-auth-cache.json", (old, old)) + + _migrate_legacy_auth_cache(work, dest) + + assert abs(dest.stat().st_mtime - old) < 1 + + assert list(work.glob(".vip-auth-cache*")) == [] + assert dest.read_text(encoding="utf-8") == "STATE" + assert dest.with_suffix(".meta.json").read_text(encoding="utf-8") == "META" + + def test_existing_dest_wins_and_legacy_is_deleted(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + dest.parent.mkdir(parents=True) + dest.write_text("NEW", encoding="utf-8") + dest.with_suffix(".meta.json").write_text("NEWMETA", encoding="utf-8") + + _migrate_legacy_auth_cache(work, dest) + + assert dest.read_text(encoding="utf-8") == "NEW" + assert dest.with_suffix(".meta.json").read_text(encoding="utf-8") == "NEWMETA" + assert list(work.glob(".vip-auth-cache*")) == [] + + def test_failed_move_deletes_the_legacy_pair_and_warns(self, tmp_path, monkeypatch, caplog): + import shutil + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + def fail(*args, **kwargs): + raise OSError(errno.EACCES, "denied") + + monkeypatch.setattr(shutil, "move", fail) + + _migrate_legacy_auth_cache(work, dest) + + assert list(work.glob(".vip-auth-cache*")) == [] + assert not dest.exists() + warned = [r.getMessage() for r in caplog.records if r.levelname == "WARNING"] + assert len(warned) == 1 + assert str(work) in warned[0] + + def test_failed_meta_move_leaves_no_half_pair(self, tmp_path, monkeypatch): + import shutil + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + real_move = shutil.move + + def fail_meta(src, dst): + if str(src).endswith(".meta.json"): + raise OSError(errno.ENOSPC, "No space left on device") + return real_move(src, dst) + + monkeypatch.setattr(shutil, "move", fail_meta) + + _migrate_legacy_auth_cache(work, dest) + + assert not dest.exists() + assert not dest.with_suffix(".meta.json").exists() + assert list(work.glob(".vip-auth-cache*")) == [] + + def test_dest_state_without_meta_does_not_take_the_legacy_meta(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + dest.parent.mkdir(parents=True) + dest.write_text("NEW", encoding="utf-8") + + _migrate_legacy_auth_cache(work, dest) + + assert dest.read_text(encoding="utf-8") == "NEW" + assert not dest.with_suffix(".meta.json").exists() + assert list(work.glob(".vip-auth-cache*")) == [] + + def test_stray_dest_meta_is_replaced_by_the_legacy_pair(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + dest.parent.mkdir(parents=True) + dest.with_suffix(".meta.json").write_text("STRAY", encoding="utf-8") + + _migrate_legacy_auth_cache(work, dest) + + assert dest.read_text(encoding="utf-8") == "STATE" + assert dest.with_suffix(".meta.json").read_text(encoding="utf-8") == "META" + + def test_legacy_meta_without_state_is_deleted_not_moved(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + work.mkdir() + (work / ".vip-auth-cache.meta.json").write_text("META", encoding="utf-8") + + _migrate_legacy_auth_cache(work, dest) + + assert list(work.glob(".vip-auth-cache*")) == [] + assert not dest.with_suffix(".meta.json").exists() + + def test_unreachable_dest_still_deletes_the_legacy_pair(self, tmp_path, monkeypatch, caplog): + from pathlib import Path + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + real_exists = Path.exists + + def denied(self, *args, **kwargs): + if self == dest: + raise PermissionError(errno.EACCES, "denied") + return real_exists(self, *args, **kwargs) + + monkeypatch.setattr(Path, "exists", denied) + + _migrate_legacy_auth_cache(work, dest) + + assert list(work.glob(".vip-auth-cache*")) == [] + warned = [r.getMessage() for r in caplog.records if r.levelname == "WARNING"] + assert len(warned) == 1 + assert str(work) in warned[0] + + def test_unreadable_legacy_dir_never_raises(self, tmp_path, monkeypatch): + from pathlib import Path + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + def denied(self): + raise PermissionError(errno.EACCES, "denied") + + monkeypatch.setattr(Path, "is_file", denied) + + _migrate_legacy_auth_cache(work, dest) + + assert not dest.exists() + + def test_failed_move_and_failed_delete_warns_with_the_path(self, tmp_path, monkeypatch, caplog): + import shutil + from pathlib import Path + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + def fail(*args, **kwargs): + raise OSError(errno.EACCES, "denied") + + monkeypatch.setattr(shutil, "move", fail) + monkeypatch.setattr(Path, "unlink", fail) + + _migrate_legacy_auth_cache(work, dest) + + warned = [r.getMessage() for r in caplog.records if r.levelname == "WARNING"] + assert any(str(work / ".vip-auth-cache.json") in m for m in warned) + assert any(str(work / ".vip-auth-cache.meta.json") in m for m in warned) + + def test_legacy_file_vanishing_mid_migration_is_not_an_error( + self, tmp_path, monkeypatch, caplog + ): + import shutil + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + self._legacy(work) + + def lose_race(src, dst): + pathlib.Path(src).unlink() + raise FileNotFoundError(errno.ENOENT, "gone") + + monkeypatch.setattr(shutil, "move", lose_race) + + _migrate_legacy_auth_cache(work, dest) + + assert list(work.glob(".vip-auth-cache*")) == [] + assert caplog.records == [] + + def test_legacy_vanishing_before_delete_when_dest_wins_is_not_an_error( + self, tmp_path, monkeypatch, caplog + ): + from pathlib import Path + + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + work.mkdir() + dest.parent.mkdir(parents=True) + dest.write_text("NEW", encoding="utf-8") + dest.with_suffix(".meta.json").write_text("NEWMETA", encoding="utf-8") + monkeypatch.setattr(Path, "is_file", lambda self: True) + + _migrate_legacy_auth_cache(work, dest) + + assert dest.read_text(encoding="utf-8") == "NEW" + assert caplog.records == [] + + def test_is_a_no_op_when_there_is_nothing_to_move(self, tmp_path): + from vip.auth.cache import _migrate_legacy_auth_cache + + work, dest = tmp_path / "work", self._dest(tmp_path) + work.mkdir() + + _migrate_legacy_auth_cache(work, dest) + + assert not dest.parent.exists() + + def test_prepare_auth_cache_migrates(self, tmp_path, monkeypatch): + from vip.auth import prepare_auth_cache + + work = tmp_path / "work" + self._legacy(work) + monkeypatch.chdir(work) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + + path = prepare_auth_cache() + + assert list(work.glob(".vip-auth-cache*")) == [] + assert path.read_text(encoding="utf-8") == "STATE" + + +class TestPrepareAuthCache: + """prepare_auth_cache runs once per login: it tightens the cache tree and removes + expired caches other directories left behind. + """ + + @staticmethod + def _cache_dir(base, name, age): + directory = base / name + directory.mkdir(parents=True) + for f in (".vip-auth-cache.json", ".vip-auth-cache.meta.json"): + (directory / f).write_text("{}", encoding="utf-8") + old = time.time() - age + os.utime(directory / f, (old, old)) + return directory + + def test_prunes_expired_caches_from_other_directories(self, tmp_path, monkeypatch): + from vip.auth import prepare_auth_cache + + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + base = tmp_path / "xdg" / "vip" / "auth" + expired = self._cache_dir(base, "0123456789abcdef", 25 * 3600) + fresh = self._cache_dir(base, "fedcba9876543210", 60) + not_ours = self._cache_dir(base, "notes", 25 * 3600) + # Past the reuse TTL but maybe still read by a long run in that directory. + in_use = self._cache_dir(base, "00112233445566ff", 5 * 3600) + + path = prepare_auth_cache() + + assert not expired.exists() + assert fresh.exists() + assert in_use.exists() + assert not_ours.exists() + assert path.parent.parent == base + + def test_keeps_the_current_directory_cache_even_when_expired(self, tmp_path, monkeypatch): + from vip.auth import auth_cache_path, prepare_auth_cache + + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + own = auth_cache_path().parent + self._cache_dir(own.parent, own.name, 5 * 3600) + + prepare_auth_cache() + + assert own.exists() + + @pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits") + def test_vip_and_auth_directories_are_owner_only(self, tmp_path, monkeypatch): + from vip.auth import prepare_auth_cache + + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "xdg")) + (tmp_path / "xdg" / "vip").mkdir(parents=True, mode=0o755) + (tmp_path / "xdg" / "vip").chmod(0o755) + + prepare_auth_cache() + + assert (tmp_path / "xdg" / "vip").stat().st_mode & 0o777 == 0o700 + assert (tmp_path / "xdg" / "vip" / "auth").stat().st_mode & 0o777 == 0o700 + + class TestStaleCacheTriggersReauth: """End-to-end wiring: a dead cached session must fall through to the real auth flow, not be handed to the tests. The helper-level tests above prove diff --git a/selftests/test_cli_cleanup.py b/selftests/test_cli_cleanup.py index 4a72581e..acf12c11 100644 --- a/selftests/test_cli_cleanup.py +++ b/selftests/test_cli_cleanup.py @@ -134,6 +134,31 @@ def _fail(*a, **k): assert "Deleted 3 VIP test content item(s)" in out assert "Cleanup completed successfully" in out + def test_connect_only_still_moves_a_legacy_auth_cache(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + monkeypatch.delenv("VIP_CONFIG", raising=False) + (tmp_path / ".vip-auth-cache.json").write_text("{}", encoding="utf-8") + (tmp_path / ".vip-auth-cache.meta.json").write_text("{}", encoding="utf-8") + + class _FakeConnectClient: + def __init__(self, *a, **k): + pass + + def __enter__(self): + return self + + def __exit__(self, *a): + return False + + def cleanup_vip_content(self): + return 0 + + monkeypatch.setattr("vip.cli.cleanup.ConnectClient", _FakeConnectClient) + + vip.cli.run_cleanup(_make_args(connect_url="https://c.example.com")) + + assert list(tmp_path.glob(".vip-auth-cache*")) == [] + def test_workbench_only_does_not_touch_connect(self, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) monkeypatch.delenv("VIP_CONFIG", raising=False) @@ -284,6 +309,28 @@ def _fail_interactive(**kwargs): assert calls["headless"]["workbench_url"] == "https://wb.example.com" assert calls["headless"]["username"] == "admin" + def test_no_cache_path_is_passed_through_to_auth(self, tmp_path, monkeypatch): + from vip.config import VIPConfig + + self._patch_client_and_ui(monkeypatch) + monkeypatch.setattr(vip.cli.cleanup, "prepare_auth_cache", lambda **_: None) + + calls = {} + + def _fake_headless(**kwargs): + calls["headless"] = kwargs + return _fake_session(tmp_path) + + monkeypatch.setattr(vip.cli.cleanup, "start_headless_auth", _fake_headless) + + config = VIPConfig() + config.auth.username = "admin" + config.auth.password = "secret" + + vip.cli._cleanup_workbench_sessions("https://wb.example.com", _make_args(), config) + + assert calls["headless"]["cache_path"] is None + def test_uses_interactive_auth_when_no_credentials(self, tmp_path, monkeypatch): from vip.config import VIPConfig diff --git a/selftests/test_plugin.py b/selftests/test_plugin.py index 6ecc5257..d76d1366 100644 --- a/selftests/test_plugin.py +++ b/selftests/test_plugin.py @@ -541,6 +541,26 @@ def test_headless_auth_option_registered(self, pytester): class TestHeadlessAuthFixture: + def test_run_without_browser_auth_moves_a_legacy_auth_cache(self, pytester): + """Every controller run tidies the auth cache, so a legacy cwd cache moves even + when no browser login happens. + """ + pytester.makefile(".toml", vip='[general]\ndeployment_name = "Selftest"') + (pytester.path / ".vip-auth-cache.json").write_text("{}", encoding="utf-8") + (pytester.path / ".vip-auth-cache.meta.json").write_text("{}", encoding="utf-8") + pytester.makepyfile( + """ + def test_placeholder(): + assert True + """ + ) + + result = pytester.runpytest_subprocess("--vip-config=vip.toml") + + result.assert_outcomes(passed=1) + assert list(pytester.path.glob(".vip-auth-cache*")) == [] + assert "Skipping auth cache" not in result.stdout.str() + def test_headless_auth_skipped_when_no_auth_products(self, pytester): """--headless-auth skips the browser flow when no auth-requiring products are enabled. diff --git a/src/vip/auth/__init__.py b/src/vip/auth/__init__.py index d62e841b..2d3aee5b 100644 --- a/src/vip/auth/__init__.py +++ b/src/vip/auth/__init__.py @@ -19,6 +19,7 @@ _ProbeResult, _save_auth_cache, auth_cache_path, + prepare_auth_cache, refresh_auth_cache_from_storage_state, ) from vip.auth.flows import start_headless_auth, start_interactive_auth @@ -48,6 +49,7 @@ "_scheme_resolution_cache", "_wait_for_product_redirect", "auth_cache_path", + "prepare_auth_cache", "authenticated_page", "refresh_auth_cache_from_storage_state", "resolve_url_scheme", diff --git a/src/vip/auth/browser.py b/src/vip/auth/browser.py index e63d4090..4ac5041d 100644 --- a/src/vip/auth/browser.py +++ b/src/vip/auth/browser.py @@ -181,12 +181,10 @@ def _cache_references_this_key(self) -> bool: """ if not self._cache_path or not self.api_key: return False - if not self._cache_path.exists(): - return False meta_path = self._cache_path.with_suffix(".meta.json") - if not meta_path.exists(): - return False try: + if not self._cache_path.exists() or not meta_path.exists(): + return False # Validate the cache state file is parseable JSON. A corrupt # state file would make Playwright's ``storage_state=`` load # fail on the next run; treating it as a live reference would diff --git a/src/vip/auth/cache.py b/src/vip/auth/cache.py index 05dfc008..9319a2b9 100644 --- a/src/vip/auth/cache.py +++ b/src/vip/auth/cache.py @@ -3,9 +3,13 @@ from __future__ import annotations import contextlib +import hashlib import json import logging import os +import re +import shutil +import sys import tempfile import time from pathlib import Path @@ -29,18 +33,55 @@ AUTH_CACHE_FILENAME = ".vip-auth-cache.json" +# A cached session older than this is never reused. +AUTH_CACHE_TTL_SECONDS = 4 * 3600 + +# Another directory's cache is pruned only after this long. A run that reused a cache +# just inside the TTL keeps reading its storage state for as long as its tests run. +AUTH_CACHE_PRUNE_AFTER_SECONDS = 24 * 3600 + +# Per-directory cache folders are the first 16 hex digits of a SHA-256. +_CACHE_DIR_NAME = re.compile(r"[0-9a-f]{16}") + # Cookies dropped before a storage state is written to the on-disk cache. # Entra sets ESTSAUTHPERSISTENT even when "Stay signed in?" is answered No; # the session ESTSAUTH cookie covers silent SSO for the cache's four-hour TTL. _CACHE_DROPPED_COOKIES = frozenset({"ESTSAUTHPERSISTENT"}) -def auth_cache_path() -> Path: +def _user_cache_root() -> Path: + """Per-user cache root: ``%LOCALAPPDATA%`` on Windows, else ``$XDG_CACHE_HOME`` or ``~/.cache``. + + A relative ``XDG_CACHE_HOME`` is ignored, as the XDG spec requires. Creates nothing. + """ + if sys.platform == "win32": + local_app_data = os.environ.get("LOCALAPPDATA") + if local_app_data: + return Path(local_app_data) + xdg = os.environ.get("XDG_CACHE_HOME") + if xdg and Path(xdg).is_absolute(): + return Path(xdg) + return Path.home() / ".cache" + + +def _cwd_key(cwd: Path) -> str: + """Name of the cache directory for *cwd*. + + The resolved path is case-folded on Windows and macOS, whose default filesystems + ignore case, so ``~/Proj`` and ``~/proj`` share one cache. + """ + text = os.path.normcase(str(cwd.resolve())) + if sys.platform == "darwin": + text = text.casefold() + return hashlib.sha256(os.fsencode(text)).hexdigest()[:16] + + +def auth_cache_path() -> Path | None: """Path to the auth-session cache for the current invocation directory. - Single source of truth for both call sites: ``plugin/auth.py`` (``vip verify``) - and ``cli/cleanup.py`` (``vip cleanup --workbench-url``). They must agree, or - ``cleanup`` cannot find the session ``verify`` just cached. + Single source of truth for the cache location. ``vip verify`` and ``vip cleanup`` + reach it through :func:`prepare_auth_cache`; they must agree, or ``cleanup`` cannot + find the session ``verify`` just cached. Keyed on the *invocation* directory rather than pytest's ``config.rootpath``. For a repo checkout the two are the same, but for an installed VIP (``uv tool @@ -48,8 +89,156 @@ def auth_cache_path() -> Path: invocation directory and the ``site-packages`` test paths — which lands in ``$HOME``. That put the cache nowhere near the user's ``vip.toml`` and made the two call sites read different files. + + The cache lives under the user cache directory, not in the invocation directory: + cwd is what users zip and share, and the cache holds a live session and API key + (#620). This only computes the path; it touches no files and prints nothing. + + Returns ``None`` when there is no home directory (and ``XDG_CACHE_HOME`` is unset) + or the invocation directory no longer exists. """ - return Path.cwd() / AUTH_CACHE_FILENAME + try: + return _user_cache_root() / "vip" / "auth" / _cwd_key(Path.cwd()) / AUTH_CACHE_FILENAME + except (RuntimeError, OSError): + return None + + +def prepare_auth_cache(*, announce_skip: bool = True) -> Path | None: + """Resolve the auth cache and tidy it, once per command. + + Besides :func:`auth_cache_path`, this tightens ``vip/`` and ``vip/auth/`` to + owner-only, moves a cache an older VIP left in cwd, and deletes expired caches + that other directories left behind. ``vip verify`` (the pytest controller only, + not each xdist worker) and ``vip cleanup`` call it on every run, logging in or not, + so an upgrade never leaves a legacy cache in cwd. *announce_skip* prints why there + is no cache; pass False on runs that would not use one. Never raises. + """ + path = auth_cache_path() + if path is None: + if not announce_skip: + return None + print( + ">>> Skipping auth cache: cannot determine a home directory or the working " + "directory; set XDG_CACHE_HOME to enable it." + ) + return None + base = path.parent.parent + with contextlib.suppress(OSError): + for directory in (base.parent, base): + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + directory.chmod(0o700) + with contextlib.suppress(OSError): + _migrate_legacy_auth_cache(Path.cwd(), path) + _prune_expired_auth_caches(base, keep=path.parent) + return path + + +def _make_private_dir(directory: Path) -> None: + """Create *directory* owner-only, tightening it if it already exists.""" + directory.mkdir(mode=0o700, parents=True, exist_ok=True) + directory.chmod(0o700) + + +def _prune_expired_auth_caches(base: Path, keep: Path) -> None: + """Delete cache directories under *base* not written for a day, never raising. + + Such a cache is past the TTL, so it is never reused and only keeps cookies and an API + key on disk. The key itself is removed by the stale-key sweep the next login runs on + Connect. The margin over the TTL keeps a long run in another directory working. + """ + now = time.time() + try: + entries = list(base.iterdir()) + except OSError: + return + for directory in entries: + if directory == keep or not _CACHE_DIR_NAME.fullmatch(directory.name): + continue + try: + newest = max((f.stat().st_mtime for f in directory.iterdir()), default=0.0) + if now - newest > AUTH_CACHE_PRUNE_AFTER_SECONDS: + shutil.rmtree(directory) + except OSError as exc: + logger.debug("Could not prune expired auth cache %s: %s", directory, exc) + + +def _migrate_legacy_auth_cache(legacy_dir: Path, dest: Path) -> None: + """Move the cache pair an older VIP wrote into ``legacy_dir`` to ``dest``, never raising. + + The state and meta files move together or not at all: a meta next to another + session's state would hand out the wrong API key. When ``dest`` already holds a + cache, or the move fails, the legacy files are deleted instead, because a live session + must not stay in a directory users zip and share (#620). The orphaned Connect key is + removed by the next login's stale-key sweep. + """ + meta_dest = dest.with_suffix(".meta.json") + legacy_state = legacy_dir / dest.name + legacy_meta = legacy_dir / meta_dest.name + try: + if legacy_state == dest or not (legacy_state.is_file() or legacy_meta.is_file()): + return + has_state = legacy_state.is_file() + except OSError: + return + try: + move = has_state and not dest.exists() + except OSError as exc: + move = False + logger.warning( + "Cannot reach the auth cache at %s (%s); deleting the auth cache in %s instead. " + "The next run signs in again.", + dest.parent, + exc, + legacy_dir, + ) + if move: + try: + _make_private_dir(dest.parent) + # A meta with no state belongs to no session; it must not pair with the moved one. + meta_dest.unlink(missing_ok=True) + shutil.move(str(legacy_state), str(dest)) + dest.chmod(0o600) + if legacy_meta.is_file(): + shutil.move(str(legacy_meta), str(meta_dest)) + meta_dest.chmod(0o600) + return + except FileNotFoundError: + # Another VIP run in this directory moved the state first; its copy stands. + pass + except OSError as exc: + for moved in (dest, meta_dest): + with contextlib.suppress(OSError): + moved.unlink(missing_ok=True) + logger.warning( + "Could not move the auth cache out of %s (%s); deleting it instead. " + "The next run signs in again.", + legacy_dir, + exc, + ) + for legacy in (legacy_state, legacy_meta): + try: + legacy.unlink(missing_ok=True) + except OSError: + logger.warning("Could not remove legacy auth cache %s; delete it by hand.", legacy) + + +def _write_private_atomic(path: Path, payload: str) -> None: + """Write *payload* to *path* owner-only, via a temp file and rename. + + The temp file sits in the same directory so the rename is atomic, and a concurrent + reader never sees a half-written file. + """ + fd, tmp_name = tempfile.mkstemp(dir=str(path.parent), prefix=".vip-auth-cache-") + tmp = Path(tmp_name) + try: + with os.fdopen(fd, "w", encoding="utf-8") as handle: + handle.write(payload) + tmp.chmod(0o600) + tmp.replace(path) + except BaseException: + with contextlib.suppress(OSError): + tmp.unlink(missing_ok=True) + raise def _cookies_from_storage_state(storage_state_path: Path) -> httpx.Cookies: @@ -185,37 +374,33 @@ def _load_cached_auth( ``None`` and every Workbench test skipped with a message that named no cause. So when Workbench is requested we probe it before trusting the cache. """ - if not cache_path.exists(): + meta_path = cache_path.with_suffix(".meta.json") + try: + if not cache_path.exists(): + return None + # Check if the cache is less than 4 hours old. + age = time.time() - cache_path.stat().st_mtime + meta_exists = meta_path.exists() + except OSError: + # An inaccessible cache directory disables caching rather than aborting auth. return None - - # Check if the cache is less than 4 hours old. - age = time.time() - cache_path.stat().st_mtime - if age > 4 * 3600: + if age > AUTH_CACHE_TTL_SECONDS: + return None + # A state with no meta is a save in progress or a half-finished one; its key is unknown. + if not meta_exists: return None - # Read the companion metadata file if it exists. - meta_path = cache_path.with_suffix(".meta.json") - api_key = None - key_name = "" - resolved_connect_url = "" - cached_request_connect_url = "" - cached_request_workbench_url = "" - if meta_path.exists(): - try: - meta = json.loads(meta_path.read_text(encoding="utf-8")) - api_key = meta.get("api_key") - key_name = meta.get("key_name", "") - resolved_connect_url = meta.get("connect_url", "") - # Older caches (pre-fix) only stored the resolved URL. Fall - # back to it so a stale cache still matches when the - # resolved and requested forms are identical (no sub-path - # rewrite happened). - cached_request_connect_url = ( - meta.get("requested_connect_url", "") or resolved_connect_url - ) - cached_request_workbench_url = meta.get("workbench_url", "") - except (OSError, ValueError, AttributeError): - pass + try: + meta = json.loads(meta_path.read_text(encoding="utf-8")) + api_key = meta.get("api_key") + key_name = meta.get("key_name", "") + resolved_connect_url = meta.get("connect_url", "") + # Older caches only stored the resolved URL. Fall back to it so a stale cache + # still matches when the resolved and requested forms are identical. + cached_request_connect_url = meta.get("requested_connect_url", "") or resolved_connect_url + cached_request_workbench_url = meta.get("workbench_url", "") + except (OSError, ValueError, AttributeError): + return None # Match against the *requested* Connect URL so that # ``_resolve_connect_api_base`` rewriting the dashboard URL to a @@ -351,26 +536,19 @@ def refresh_auth_cache_from_storage_state( cleanup path, where failing loudly would mask the test's own result. """ path = cache_path if cache_path is not None else auth_cache_path() - if not path.exists(): + if path is None: + return False + try: + if not path.exists(): + return False + except OSError: return False - tmp: Path | None = None try: - payload = json.dumps(_cacheable_storage_state(storage_state)) - # Same directory so the rename stays on one filesystem (and therefore - # atomic); mkstemp creates it 0600 already. - fd, tmp_name = tempfile.mkstemp(dir=str(path.parent), prefix=".vip-auth-cache-") - tmp = Path(tmp_name) - with os.fdopen(fd, "w", encoding="utf-8") as handle: - handle.write(payload) - tmp.chmod(0o600) - tmp.replace(path) + _write_private_atomic(path, json.dumps(_cacheable_storage_state(storage_state))) return True except Exception as exc: # noqa: BLE001 -- cleanup path; never raises, see docstring logger.debug("Could not refresh the auth cache at %s: %s", path, exc) - if tmp is not None and tmp.exists(): - with contextlib.suppress(OSError): - tmp.unlink() return False @@ -392,11 +570,6 @@ def _save_auth_cache(session: InteractiveAuthSession, cache_path: Path) -> None: # Write the storage state to the cache location, minus long-lived IdP cookies. state = json.loads(session.storage_state_path.read_text(encoding="utf-8")) - fd = os.open(cache_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) - with os.fdopen(fd, "w", encoding="utf-8") as handle: - json.dump(_cacheable_storage_state(state), handle) - cache_path.chmod(0o600) - # Write companion metadata. ``connect_url`` keeps the resolved # form (used for API key cleanup); ``requested_connect_url`` keeps # the pre-resolve form for cache-key matching. Older releases only @@ -411,5 +584,19 @@ def _save_auth_cache(session: InteractiveAuthSession, cache_path: Path) -> None: "requested_connect_url": session._requested_connect_url or session._connect_url, "workbench_url": session._workbench_url, } - meta_path.write_text(json.dumps(meta), encoding="utf-8") - meta_path.chmod(0o600) + try: + _make_private_dir(cache_path.parent) + # Drop the old meta first so it never pairs with the new state. Until the new + # meta lands, a concurrent _load_cached_auth sees no meta and treats it as a miss. + meta_path.unlink(missing_ok=True) + _write_private_atomic(cache_path, json.dumps(_cacheable_storage_state(state))) + _write_private_atomic(meta_path, json.dumps(meta)) + except (OSError, TypeError, ValueError) as exc: + # A failed save must leave no cache: a partial pair could be reused. + for leftover in (cache_path, meta_path): + with contextlib.suppress(OSError): + leftover.unlink(missing_ok=True) + print( + f">>> Skipping auth cache: cannot write {cache_path.parent} ({exc}); " + "next run will re-authenticate." + ) diff --git a/src/vip/cli/cleanup.py b/src/vip/cli/cleanup.py index 99f86f58..f0415dd2 100644 --- a/src/vip/cli/cleanup.py +++ b/src/vip/cli/cleanup.py @@ -10,8 +10,8 @@ from vip.auth import ( AuthConfigError, - auth_cache_path, authenticated_page, + prepare_auth_cache, resolve_url_scheme, start_headless_auth, start_interactive_auth, @@ -95,7 +95,7 @@ def _cleanup_workbench_sessions( proxy = config.proxy # Same helper plugin/auth.py uses, so this finds the session a prior `vip verify` # from this directory cached. - cache_path = auth_cache_path() + cache_path = prepare_auth_cache() username = config.auth.username password = config.auth.password @@ -244,6 +244,9 @@ def run_cleanup(args: argparse.Namespace) -> None: # explicit `vip cleanup --connect-url ...` with no vip.toml must not emit a # "Config file not found" warning (env-based credentials still apply). config = _load_cleanup_config(args) + # Tidy the auth cache on every run, so an upgrade moves a legacy cache out of cwd + # even when no browser login follows (Connect only, or a Workbench API token). + prepare_auth_cache(announce_skip=False) # A CLI flag wins over vip.toml. Wrapping the CLI arg in ProductConfig # routes it through the same _normalize_url a bare hostname gets from diff --git a/src/vip/plugin/auth.py b/src/vip/plugin/auth.py index 50a4aec7..30b978d4 100644 --- a/src/vip/plugin/auth.py +++ b/src/vip/plugin/auth.py @@ -10,7 +10,7 @@ from vip.auth import ( AuthConfigError, InteractiveAuthSession, - auth_cache_path, + prepare_auth_cache, start_headless_auth, start_interactive_auth, ) @@ -39,7 +39,14 @@ def _configure_auth(config: pytest.Config, vip_cfg: VIPConfig) -> None: # still mirror the controller's auth mode so the auth_mode fixture # matches a non-xdist run. config.stash[_auth_mode_key] = config.workerinput["vip_auth_mode"] - elif config.getoption("--interactive-auth"): + return + + # Tidy the auth cache on every controller run, not only browser logins, so an + # upgrade moves a legacy cache out of cwd even under --api-auth. + browser_auth = config.getoption("--interactive-auth") or config.getoption("--headless-auth") + cache_path = prepare_auth_cache(announce_skip=bool(browser_auth)) + + if config.getoption("--interactive-auth"): config.stash[_auth_mode_key] = "interactive" connect_url = vip_cfg.connect.url if vip_cfg.connect.is_configured else None wb_url = vip_cfg.workbench.url if vip_cfg.workbench.is_configured else None @@ -53,7 +60,6 @@ def _configure_auth(config: pytest.Config, vip_cfg: VIPConfig) -> None: stacklevel=1, ) else: - cache_path = auth_cache_path() try: session = start_interactive_auth( connect_url=connect_url, @@ -104,7 +110,6 @@ def _configure_auth(config: pytest.Config, vip_cfg: VIPConfig) -> None: stacklevel=1, ) else: - cache_path = auth_cache_path() try: session = start_headless_auth( connect_url=connect_url, diff --git a/vip.toml.example b/vip.toml.example index be00ba68..76444707 100644 --- a/vip.toml.example +++ b/vip.toml.example @@ -30,6 +30,7 @@ url = "https://connect.example.com" # API key for a Connect admin user. # Prefer setting the VIP_CONNECT_API_KEY environment variable. +# Never share this file with a real key in it. # api_key = "..." # Timeout in seconds for content deployments (default: 1200) diff --git a/website/src/pages/guide.astro b/website/src/pages/guide.astro index 80153567..3bcaa67d 100644 --- a/website/src/pages/guide.astro +++ b/website/src/pages/guide.astro @@ -102,6 +102,30 @@ import "../styles/docs.css"; VIP_TEST_PASSWORD.

+

Where the saved session lives

+

+ Interactive and headless auth save the browser session and the + minted Connect API key for 4 hours, so the next run from the same + directory skips the login. VIP keeps one cache for each directory + you run it from, outside that directory, so a zipped or shared VIP + folder never contains it: +

+
    +
  • Linux and macOS: $XDG_CACHE_HOME/vip/auth/, or ~/.cache/vip/auth/ when XDG_CACHE_HOME is unset
  • +
  • Windows: %LOCALAPPDATA%\vip\auth\
  • +
+

+ Only your user can read these folders. Each vip verify + or vip cleanup deletes caches that have not been + written for a day, and the next login removes their API keys from + Connect. To sign out + sooner, delete the vip/auth folder. Older releases + saved .vip-auth-cache.json and + .vip-auth-cache.meta.json in the working directory; + the first run of a newer release moves them, or deletes them if it + cannot. +

+

API-key-only auth

If you only have an API key and no browser credentials, use