From fbd23f0c853c1cdbd8b7a6be9b04e2e61a5e68a8 Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Fri, 30 Oct 2026 17:00:41 +0100 Subject: [PATCH] Fall back to FREQUENZ_API_{KEY,SECRET} in the CLI Lets the CLI reuse the generic Frequenz credentials that other tools already read. The fallback applies only when neither key nor secret is given and then takes both, so a key and secret from different sources are never combined. Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- README.md | 4 + RELEASE_NOTES.md | 1 + src/frequenz/client/reporting/cli/__main__.py | 16 +++- tests/test_cli.py | 76 +++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/test_cli.py diff --git a/README.md b/README.md index fbe4b44..fda7f75 100644 --- a/README.md +++ b/README.md @@ -247,4 +247,8 @@ reporting-cli \ --states \ --bounds ``` + +If neither `--auth_key` nor `--sign_secret` is given, both are read from +`FREQUENZ_API_KEY` and `FREQUENZ_API_SECRET` as a pair. + In addition to the default CSV format, individual samples can also be output using the `--format iter` option. diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 6cff5c3..e723f15 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -11,6 +11,7 @@ ## New Features - The CLI now rejects `--end` before `--start`. +- `reporting-cli` falls back to `FREQUENZ_API_KEY` and `FREQUENZ_API_SECRET` when neither `--auth_key` nor `--sign_secret` is given. ## Bug Fixes diff --git a/src/frequenz/client/reporting/cli/__main__.py b/src/frequenz/client/reporting/cli/__main__.py index 5b21910..cd21a12 100644 --- a/src/frequenz/client/reporting/cli/__main__.py +++ b/src/frequenz/client/reporting/cli/__main__.py @@ -6,6 +6,7 @@ import argparse import asyncio import csv +import os import sys from datetime import datetime, timedelta from typing import AsyncIterator @@ -97,7 +98,10 @@ def main() -> None: parser.add_argument( "--auth_key", type=str, - help="API key", + help=( + "API key. If neither key nor secret is given, both are read from " + "FREQUENZ_API_KEY and FREQUENZ_API_SECRET." + ), default=None, ) parser.add_argument( @@ -109,6 +113,16 @@ def main() -> None: args = parser.parse_args() if args.start and args.end and args.end < args.start: parser.error("--end must not be before --start") + + # Take the fallback as a pair so a key and secret from different sources + # are never combined. + if not args.auth_key and not args.sign_secret: + args.auth_key = os.environ.get("FREQUENZ_API_KEY") + args.sign_secret = os.environ.get("FREQUENZ_API_SECRET") + + if args.sign_secret and not args.auth_key: + parser.error("no auth key: --sign_secret was given without --auth_key") + asyncio.run( run( microgrid_id=args.mid, diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..12a7b0b --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,76 @@ +# License: MIT +# Copyright © 2026 Frequenz Energy-as-a-Service GmbH + +"""Tests for the reporting CLI.""" + +from unittest.mock import AsyncMock + +import pytest + +from frequenz.client.reporting.cli import __main__ as cli + +_ARGV = ["reporting-cli", "--mid", "1", "--cid", "1"] + + +def _run_main( + monkeypatch: pytest.MonkeyPatch, args: list[str], env: dict[str, str] +) -> AsyncMock: + """Run `main` with `args` and `env`, returning the mocked `run`.""" + run = AsyncMock() + monkeypatch.setattr(cli, "run", run) + monkeypatch.setattr("sys.argv", _ARGV + args) + for name in ("FREQUENZ_API_KEY", "FREQUENZ_API_SECRET"): + monkeypatch.delenv(name, raising=False) + for name, value in env.items(): + monkeypatch.setenv(name, value) + cli.main() + return run + + +_FALLBACK_ENV = { + "FREQUENZ_API_KEY": "fallback-key", + "FREQUENZ_API_SECRET": "fallback-secret", +} + + +def test_credentials_fall_back_to_frequenz_env_vars( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """`FREQUENZ_API_*` are used when no credentials are given.""" + run = _run_main(monkeypatch, [], _FALLBACK_ENV) + + assert run.call_args.kwargs["auth_key"] == "fallback-key" + assert run.call_args.kwargs["sign_secret"] == "fallback-secret" + + +def test_flags_take_precedence(monkeypatch: pytest.MonkeyPatch) -> None: + """`--auth_key` and `--sign_secret` win over `FREQUENZ_API_*`.""" + run = _run_main( + monkeypatch, + ["--auth_key", "own-key", "--sign_secret", "own-secret"], + _FALLBACK_ENV, + ) + + assert run.call_args.kwargs["auth_key"] == "own-key" + assert run.call_args.kwargs["sign_secret"] == "own-secret" + + +def test_credentials_are_not_mixed_across_sources( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """A `--auth_key` is never paired with a `FREQUENZ_API_*` secret.""" + run = _run_main(monkeypatch, ["--auth_key", "own-key"], _FALLBACK_ENV) + + assert run.call_args.kwargs["auth_key"] == "own-key" + assert run.call_args.kwargs["sign_secret"] is None + + +def test_secret_without_key_does_not_fall_back( + monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] +) -> None: + """A `--sign_secret` alone is an error, not a mixed pair.""" + with pytest.raises(SystemExit) as exc_info: + _run_main(monkeypatch, ["--sign_secret", "own-secret"], _FALLBACK_ENV) + + assert exc_info.value.code != 0 + assert "auth key" in capsys.readouterr().err