mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
Only offer an update when the remote tip is signed by the release key (#970)
check_for_update reported update_available whenever origin/<branch> was ahead, but upgrade.sh refuses an unsigned tip (exit 5). On master, merges land before they are re-signed, so users saw the update icon and then an upgrade error. The remote tip is now checked with verify_wrolpi_commit.sh (pinned fingerprint, key from the running checkout) before an update is offered. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3e23022418
commit
dc219e0775
2 changed files with 61 additions and 3 deletions
|
|
@ -15,9 +15,12 @@ from wrolpi.upgrade import (
|
|||
get_remote_commit,
|
||||
get_commits_behind,
|
||||
git_fetch,
|
||||
is_commit_signed,
|
||||
refresh_update_status,
|
||||
start_upgrade,
|
||||
)
|
||||
from wrolpi.test.test_common import gpg_test_key # noqa: F401 (fixture)
|
||||
from wrolpi.test.test_verify_wrolpi_commit import signed_repo # noqa: F401 (fixture)
|
||||
|
||||
|
||||
class TestGitFunctions:
|
||||
|
|
@ -200,13 +203,47 @@ class TestCheckForUpdate:
|
|||
patch('wrolpi.upgrade.git_fetch', return_value=True), \
|
||||
patch('wrolpi.upgrade.get_local_commit', return_value='abc1234'), \
|
||||
patch('wrolpi.upgrade.get_remote_commit', return_value='def5678'), \
|
||||
patch('wrolpi.upgrade.get_commits_behind', return_value=3):
|
||||
patch('wrolpi.upgrade.get_commits_behind', return_value=3), \
|
||||
patch('wrolpi.upgrade.is_commit_signed', return_value=True) as mock_signed:
|
||||
result = check_for_update(fetch=True)
|
||||
assert result['update_available'] is True
|
||||
assert result['branch'] == 'master'
|
||||
assert result['current_commit'] == 'abc1234'
|
||||
assert result['latest_commit'] == 'def5678'
|
||||
assert result['commits_behind'] == 3
|
||||
mock_signed.assert_called_once_with('origin/master')
|
||||
|
||||
def test_check_for_update_unsigned_remote(self):
|
||||
"""An update is not offered when the remote tip is not signed by the release key; upgrade.sh would refuse
|
||||
it."""
|
||||
mock_path = MagicMock()
|
||||
mock_path.is_dir.return_value = True
|
||||
with patch('wrolpi.upgrade.DOCKERIZED', False), \
|
||||
patch('wrolpi.upgrade.PROJECT_DIR', mock_path), \
|
||||
patch('wrolpi.upgrade.get_current_branch', return_value='master'), \
|
||||
patch('wrolpi.upgrade.git_fetch', return_value=True), \
|
||||
patch('wrolpi.upgrade.get_local_commit', return_value='abc1234'), \
|
||||
patch('wrolpi.upgrade.get_remote_commit', return_value='def5678'), \
|
||||
patch('wrolpi.upgrade.get_commits_behind', return_value=3), \
|
||||
patch('wrolpi.upgrade.is_commit_signed', return_value=False) as mock_signed:
|
||||
result = check_for_update(fetch=True)
|
||||
mock_signed.assert_called_once_with('origin/master')
|
||||
assert result['update_available'] is False
|
||||
assert result['latest_commit'] == 'def5678'
|
||||
assert result['commits_behind'] == 3
|
||||
|
||||
|
||||
def test_is_commit_signed(signed_repo, monkeypatch):
|
||||
"""is_commit_signed accepts only a commit signed by the pinned key."""
|
||||
repo, key, fingerprint = signed_repo
|
||||
monkeypatch.setenv('WROLPI_SIGNING_FINGERPRINT', fingerprint)
|
||||
assert is_commit_signed('signed', repo=repo, key=key) is True
|
||||
assert is_commit_signed('unsigned', repo=repo, key=key) is False
|
||||
assert is_commit_signed('does-not-exist', repo=repo, key=key) is False
|
||||
|
||||
# The real WROLPi key did not sign the test commit.
|
||||
monkeypatch.delenv('WROLPI_SIGNING_FINGERPRINT')
|
||||
assert is_commit_signed('signed', repo=repo) is False
|
||||
|
||||
def test_check_for_update_skip_fetch(self):
|
||||
"""check_for_update can skip fetch."""
|
||||
|
|
|
|||
|
|
@ -18,6 +18,9 @@ logger = logger.getChild(__name__)
|
|||
# Path to the upgrade script.
|
||||
UPGRADE_SCRIPT = PROJECT_DIR / 'upgrade.sh'
|
||||
|
||||
# Exits non-zero unless a commit is signed by the WROLPi release key (the same check install paths use).
|
||||
VERIFY_COMMIT_SCRIPT = pathlib.Path(__file__).parent / 'scripts/verify_wrolpi_commit.sh'
|
||||
|
||||
# The env file systemd reads for wrolpi-upgrade.service.
|
||||
UPGRADE_ENV_FILE = '/tmp/wrolpi-upgrade.env'
|
||||
|
||||
|
|
@ -175,6 +178,23 @@ def get_commits_behind(branch: str) -> int:
|
|||
return 0
|
||||
|
||||
|
||||
def is_commit_signed(rev: str, repo: pathlib.Path = None, key: pathlib.Path = None) -> bool:
|
||||
"""Return True only if `rev` in `repo` (default: the WROLPi checkout) is signed by the WROLPi release key.
|
||||
|
||||
The key comes from the running (already verified) checkout, never from the fetched commit."""
|
||||
cmd = ['bash', str(VERIFY_COMMIT_SCRIPT), str(repo or PROJECT_DIR), rev]
|
||||
if key:
|
||||
cmd.append(str(key))
|
||||
try:
|
||||
result = subprocess.run(cmd, capture_output=True, timeout=30)
|
||||
if result.returncode != 0:
|
||||
logger.info(f'{rev} is not signed by the WROLPi release key: {result.stderr.decode().strip()}')
|
||||
return result.returncode == 0
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to verify the signature of {rev}', exc_info=e)
|
||||
return False
|
||||
|
||||
|
||||
@wrol_mode_check
|
||||
def check_for_update(fetch: bool = True) -> dict:
|
||||
"""
|
||||
|
|
@ -230,11 +250,12 @@ def check_for_update(fetch: bool = True) -> dict:
|
|||
if not local_commit or not remote_commit:
|
||||
return result
|
||||
|
||||
# Check if update is available
|
||||
# Check if update is available. upgrade.sh refuses a remote tip that is not signed by the release key, so
|
||||
# only a signed tip is offered.
|
||||
if local_commit != remote_commit:
|
||||
commits_behind = get_commits_behind(branch)
|
||||
result['commits_behind'] = commits_behind
|
||||
result['update_available'] = commits_behind > 0
|
||||
result['update_available'] = commits_behind > 0 and is_commit_signed(f'origin/{branch}')
|
||||
|
||||
return result
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue