diff --git a/docs/CLI.md b/docs/CLI.md index 68a0745f..559c325f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -91,6 +91,17 @@ token_command = "pass show ci.example.org/nixbot" # or: token_command = "rbw get nixbot-ci" ``` +Because the command runs only on demand, reads go out without the token, and +private repositories look missing to them. If you read from private +repositories, use `token_file` instead: a file holding the token, such as a +secret that sops-nix or agenix decrypts. `nbo` reads it up front and sends it +with every request: + +```toml +["https://ci.example.org"] +token_file = "/run/secrets/nixbot-api-token" +``` + `nbo auth status` shows which server and token are in use and whether the server is reachable. diff --git a/nixbot/nixbot/tests/test_cli.py b/nixbot/nixbot/tests/test_cli.py index 5c3c78b3..931f7439 100644 --- a/nixbot/nixbot/tests/test_cli.py +++ b/nixbot/nixbot/tests/test_cli.py @@ -428,6 +428,30 @@ def test_settings_url_trailing_slash( assert Settings.load() == Settings("https://ci.example.org", "bnix_abc") +def test_settings_token_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """A token_file is read up front, so reads carry the token too: a server + shows private repositories to an anonymous read as missing, not as a 401 + that would make nbo fetch a lazy token.""" + monkeypatch.delenv("NIXBOT_TOKEN", raising=False) + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path)) + hosts = tmp_path / "nixbot" / "hosts.toml" + hosts.parent.mkdir(parents=True) + secret = tmp_path / "nixbot-api-token" + secret.write_text("bnix_from_file\n") + hosts.write_text(f'["https://ci.example.org"]\ntoken_file = "{secret}"\n') + assert Settings.load().token == "bnix_from_file" + + monkeypatch.setenv("NIXBOT_TOKEN", "bnix_env") + assert Settings.load().token == "bnix_env" + monkeypatch.delenv("NIXBOT_TOKEN") + + # A missing secret is an error, not an anonymous client that sees no + # private repositories. + secret.unlink() + with pytest.raises(ValueError, match="token_file"): + Settings.load() + + def test_settings_token_command( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: diff --git a/nixbot_cli/nixbot_cli/config.py b/nixbot_cli/nixbot_cli/config.py index 7e21dd3c..51a2b77a 100644 --- a/nixbot_cli/nixbot_cli/config.py +++ b/nixbot_cli/nixbot_cli/config.py @@ -31,7 +31,9 @@ def load(cls, repo: str | None = None) -> Settings: """Server: NIXBOT_URL, the hosts.toml entry whose `remotes` cover `repo` (the -R argument), `git config nixbot.url`, the only entry, or the entry whose `remotes` match the origin URL. - NIXBOT_TOKEN overrides the token from hosts.toml.""" + NIXBOT_TOKEN overrides the token from hosts.toml. A token or + token_file is sent with every request; a token_command runs only + when a request needs it.""" url = os.environ.get("NIXBOT_URL") token = os.environ.get("NIXBOT_TOKEN") hosts: dict[str, dict] = {} @@ -54,7 +56,8 @@ def load(cls, repo: str | None = None) -> Settings: (e for u, e in hosts.items() if u.rstrip("/") == url), {}, ) - return cls(url, token or entry.get("token"), entry.get("token_command")) + token = token or entry.get("token") or _read_token_file(entry.get("token_file")) + return cls(url, token, entry.get("token_command")) def resolve_token(self) -> str | None: return self.token or _run_token_command(self.token_command) @@ -113,6 +116,21 @@ def _select_host(hosts: dict[str, dict]) -> str | None: return None +def _read_token_file(path: str | None) -> str | None: + """The first line of the host's token_file, such as a secret that sops + or agenix decrypts. Reading it cannot prompt, so unlike token_command it + is read up front: a server answers a private repository's anonymous + reads as missing rather than with a 401 that would fetch a lazy token.""" + if not path: + return None + try: + text = Path(path).expanduser().read_text() + except OSError as err: + msg = f"token_file {path!r} unreadable: {err}" + raise ValueError(msg) from err + return text.splitlines()[0].strip() if text.strip() else None + + def _run_token_command(command: str | None) -> str | None: """Fetch the token from a secret manager (pass, rbw, ...) via the host's token_command. The first line of its stdout is the token."""