review: add support for fetching patchwork token via git credentials
v2 cover review: add support for fetching patchwork token via git credentials
Currently the patchwork token needs to be stored plaintext in your git configuration. This series adds support for fetching the token via git credentials, which allows storing this externally in a password manager if desired.
---
Changes in v2:
- Changed to use git credentials to fetch the patchwork key.
- Link to v1: https://patch.msgid.link/[email protected]
---
Brian Masney (3):
review: centralize lookup of patchwork key in one place
review: add support for fetching patchwork key via git credentials
review: tests: add tests for the patchwork key
docs/config.rst | 29 ++++++++++ src/b4/__init__.py | 56 +++++++++++++++++- src/b4/review/_review.py | 4 +- src/b4/review_tui/_common.py | 2 +- src/b4/review_tui/_entry.py | 2 +- src/b4/review_tui/_tracking_app.py | 2 +- src/tests/test___init__.py | 114 +++++++++++++++++++++++++++++++++++++ 7 files changed, 202 insertions(+), 7 deletions(-)
---
base-commit: 6be7c6139d7734d84afddf989fa2874935b6d85b
change-id: 20261015-pw-key-command-12c3634b2e8f
Best regards,
Signature · 1 line
Brian Masney <[email protected]>
PATCH v2 1/3 review: centralize lookup of patchwork key in one place
Introduce get_pw_key() so that the patchwork key lookup is done in one place. This is in preparation for adding a pw-key-command configuration option.
--- src/b4/__init__.py | 14 ++++++++++++-- src/b4/review/_review.py | 4 ++-- src/b4/review_tui/_common.py | 2 +- src/b4/review_tui/_entry.py | 2 +- src/b4/review_tui/_tracking_app.py | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-)
src/b4/__init__.py +12 -2
@@ -2504,7 +2504,7 @@ class LoreMessage: @staticmethod def get_patchwork_data_by_msgid(msgid: str) -> Dict[str, str]: config = get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = get_pw_key(config) pwurl = str(config.get('pw-url', '')) pwproj = str(config.get('pw-project', '')) if not (pwkey and pwurl and pwproj):
@@ -6402,6 +6402,16 @@ def get_smtp( return SMTPConnector(_connect), fromaddr +def get_pw_key(config: Optional[ConfigDictT] = None) -> str: + """Return the Patchwork API key from b4.pw-key. + + Returns an empty string when no key is configured. + """ + if config is None: + config = get_main_config() + return str(config.get('pw-key', '') or '') + + def get_patchwork_session(pwkey: str, pwurl: str) -> Tuple[requests.Session, str]: session = requests.session() session.headers.update(
@@ -6418,7 +6428,7 @@ def get_patchwork_session(pwkey: str, pwurl: str) -> Tuple[requests.Session, str def patchwork_set_state(msgids: List[str], state: str) -> None: # Do we have a pw-key defined in config? config = get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = get_pw_key(config) pwurl = str(config.get('pw-url', '')) pwproj = str(config.get('pw-project', '')) if not (pwkey and pwurl and pwproj):
src/b4/review/_review.py +2 -2
@@ -4232,7 +4232,7 @@ def pw_update_series_state( Patchwork is not configured), False on failure. """ config = b4.get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = b4.get_pw_key(config) pwurl = str(config.get('pw-url', '')) if not (pwkey and pwurl): # Patchwork not configured, nothing to do
@@ -4270,7 +4270,7 @@ def pw_lookup_series_id(msgid: str) -> Optional[int]: configured or does not know the message. """ config = b4.get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = b4.get_pw_key(config) pwurl = str(config.get('pw-url', '')) pwproj = str(config.get('pw-project', '')) if not (pwkey and pwurl and pwproj):
src/b4/review_tui/_common.py +1 -1
@@ -361,7 +361,7 @@ class CheckRunnerMixin: # Patchwork config for _builtin_patchwork config = b4.get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = b4.get_pw_key(config) pwurl = str(config.get('pw-url', '')) # Dump tracking data to a temp file for external scripts. This is
src/b4/review_tui/_entry.py +1 -1
@@ -151,7 +151,7 @@ def run_tracking_tui( if branch_name == TrackingApp.PATCHWORK_SENTINEL: # User pressed [p]atchwork — run PwApp and loop back config = b4.get_main_config() - pwkey = str(config.get('pw-key', '')) + pwkey = b4.get_pw_key(config) pwurl = str(config.get('pw-url', '')) pwproj = str(config.get('pw-project', '')) if pwkey and pwurl and pwproj:
src/b4/review_tui/_tracking_app.py +1 -1
@@ -1159,7 +1159,7 @@ class TrackingApp(LoreNodeShutdownMixin, CheckRunnerMixin, App[Optional[str]]): self._db_mtime: float = 0.0 # Detect patchwork configuration config = b4.get_main_config() - self._pwkey = str(config.get('pw-key', '')) + self._pwkey = b4.get_pw_key(config) self._pwurl = str(config.get('pw-url', '')) self._pwproj = str(config.get('pw-project', '')) # Remember last snooze choices within the session
Signature · 1 line
2.56.0
PATCH v2 2/3 review: add support for fetching patchwork key via git credentials
In reply to Brian MasneyIntroduce a b4.pw-user and support for fetching the patchwork key via git credentials.
--- docs/config.rst | 29 +++++++++++++++++++++++++++++ src/b4/__init__.py | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 3 deletions(-)
docs/config.rst +29 -0
@@ -1022,6 +1022,35 @@ integrate your b4 workflow with patchwork. Default: ``None`` + :term:`b4.pw-user` + Your username on the patchwork server. When this is set and + :term:`b4.pw-key` is not, b4 asks your git credential helpers for the + password of this user on the :term:`b4.pw-url` host, and uses it as + the API key. This lets you keep the key in a password manager instead + of storing it in plain text in your git config. b4 asks at most once + per invocation. For example:: + + pw-url = https://patchwork.kernel.org/ + pw-user = jdoe + + To store the key with a credential helper that supports it:: + + printf 'protocol=https\nhost=patchwork.kernel.org\nusername=jdoe\npassword=%s\n' \ + "$PW_API_KEY" | git credential approve + + You can also inject the key via a password manager by placing the following + in your .gitconfig: + + [credential "https://[email protected]"] + helper = "!f() { echo password=$(pass korg-patchwork-token); }; f" + + If no helper returns a password, git may prompt for one on the + terminal. This setting is ignored in the in-tree ``.b4-config`` file. + + Default: ``None`` + + .. versionadded:: v0.17 + .. _contributor_settings: Contributor-oriented settings
src/b4/__init__.py +45 -3
76 lines not on this page · raw message
Signature · 1 line
2.56.0
PATCH v2 3/3 review: tests: add tests for the patchwork key
In reply to Brian MasneyAdd tests for the various ways that the patchwork key can be provided.
--- src/tests/test___init__.py | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+)
src/tests/test___init__.py +114 -0
@@ -2924,3 +2924,117 @@ class TestGetAmMessageFromLines: lines = payload.decode().splitlines() assert 'From the manual, bar() must be called here.' in lines assert '>From an old thread.' in lines + + +class TestGetPwKey: + """b4.pw-user fetches the Patchwork key from git credential helpers.""" + + @pytest.fixture(autouse=True) + def _clear_cache(self, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(b4, '_PW_KEY_CACHE', {}) + # Never fall back to prompting on the terminal + monkeypatch.setenv('GIT_TERMINAL_PROMPT', '0') + monkeypatch.delenv('GIT_ASKPASS', raising=False) + monkeypatch.delenv('SSH_ASKPASS', raising=False) + + @staticmethod + def _set_helper( + monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path, body: str + ) -> pathlib.Path: + """Replace all credential helpers with a script that runs body.""" + helper = tmp_path / 'helper.sh' + helper.write_text(f'#!/bin/sh\necho "$@" >> {tmp_path}/calls\n{body}\n') + helper.chmod(0o755) + count = int(os.environ.get('GIT_CONFIG_COUNT', '0')) + # An empty value resets any helpers from the user's own config + for i, val in enumerate(('', f'!{helper}')): + monkeypatch.setenv(f'GIT_CONFIG_KEY_{count + i}', 'credential.helper') + monkeypatch.setenv(f'GIT_CONFIG_VALUE_{count + i}', val) + monkeypatch.setenv('GIT_CONFIG_COUNT', str(count + 2)) + return tmp_path / 'calls' + + def test_plain_key(self) -> None: + assert b4.get_pw_key({'pw-key': 'secret'}) == 'secret' + + def test_unset(self) -> None: + assert b4.get_pw_key({}) == '' + + def test_no_user( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path + ) -> None: + """Without pw-user, credential helpers are never asked.""" + calls = self._set_helper(monkeypatch, tmp_path, 'echo password=nope') + assert b4.get_pw_key({'pw-url': 'https://pw.example.org/'}) == '' + assert not calls.exists() + + def test_key_wins( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path + ) -> None: + calls = self._set_helper(monkeypatch, tmp_path, 'echo password=nope') + config: b4.ConfigDictT = { + 'pw-key': 'secret', + 'pw-user': 'me', + 'pw-url': 'https://pw.example.org/', + } + assert b4.get_pw_key(config) == 'secret' + assert not calls.exists() + + def test_credential_helper( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path + ) -> None: + req = tmp_path / 'request' + self._set_helper( + monkeypatch, tmp_path, f'cat > {req}\necho password=fromhelper' + ) + config: b4.ConfigDictT = { + 'pw-user': 'me', + 'pw-url': 'https://pw.example.org:8443/', + } + assert b4.get_pw_key(config) == 'fromhelper' + request = req.read_text().splitlines() + assert 'protocol=https' in request + assert 'host=pw.example.org:8443' in request + assert 'username=me' in request + + def test_credential_helper_runs_once( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path + ) -> None: + calls = self._set_helper(monkeypatch, tmp_path, 'echo password=key') + config: b4.ConfigDictT = { + 'pw-user': 'me', + 'pw-url': 'https://pw.example.org/', + } + assert b4.get_pw_key(config) == 'key' + assert b4.get_pw_key(config) == 'key' + assert calls.read_text().count('get') == 1 + + def test_credential_helper_empty( + self, + monkeypatch: pytest.MonkeyPatch, + tmp_path: pathlib.Path, + caplog: pytest.LogCaptureFixture, + ) -> None: + self._set_helper(monkeypatch, tmp_path, 'true') + config: b4.ConfigDictT = { + 'pw-user': 'me', + 'pw-url': 'https://pw.example.org/', + } + with caplog.at_level(logging.CRITICAL, logger='b4'): + assert b4.get_pw_key(config) == '' + assert 'No patchwork key for [email protected]' in caplog.text + + def test_bad_url(self, caplog: pytest.LogCaptureFixture) -> None: + config: b4.ConfigDictT = {'pw-user': 'me', 'pw-url': 'pw.example.org'} + with caplog.at_level(logging.CRITICAL, logger='b4'): + assert b4.get_pw_key(config) == '' + assert 'Could not parse pw-url' in caplog.text + + def test_ignored_in_worktree_config(self, gitdir: str) -> None: + """A cloned tree's .b4-config must not get to ask for credentials.""" + wtcfg = pathlib.Path(gitdir) / '.b4-config' + wtcfg.write_text( + '[b4]\n\tpw-user = stolen\n\tpw-url = https://pw.example.org\n' + ) + b4._setup_main_config(topdir=gitdir) + assert b4.MAIN_CONFIG.get('pw-url') == 'https://pw.example.org' + assert b4.MAIN_CONFIG.get('pw-user') is None
Signature · 1 line
2.56.0
Konstantin Ryabitsev
AppliedQuoted 8 lines · Brian Masney
review: add support for fetching patchwork token via git credentials
Currently the patchwork token needs to be stored plaintext in your git configuration. This series adds support for fetching the token via git credentials, which allows storing this externally in a password manager if desired.
[...]
Applied, thanks!
[1/3] review: centralize lookup of patchwork key in one place
https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=a02d2c76e5071ffff33fd2dcfb39d7ff499262a4 [2/3] review: add support for fetching patchwork key via git credentials https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=06ea4123afebed14a2a617afe3509ffbaaebc374 [3/3] review: tests: add tests for the patchwork key https://git.kernel.org/pub/scm/utils/b4/b4.git/commit/?id=bf22d0198c34f68d068c150bdb9c2d90da134b56
Best regards,
Signature · 1 line
Konstantin Ryabitsev <[email protected]>