Skip to content

Commit

Permalink
fix: use local keys for package-repos in core24
Browse files Browse the repository at this point in the history
Fixes #4743
  • Loading branch information
tigarmo authored and mr-cal committed Apr 24, 2024
1 parent 7b01ab0 commit a3834e7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions snapcraft/services/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from overrides import overrides

from snapcraft import __version__, errors, models, os_release, parts, utils
from snapcraft.parts.yaml_utils import get_snap_project


class Lifecycle(LifecycleService):
Expand Down Expand Up @@ -169,6 +170,15 @@ def generate_manifest(self) -> models.Manifest:
primed_stage_packages=sorted(primed_stage_packages),
)

@overrides
def _get_local_keys_path(self) -> Path | None:
snap_project = get_snap_project()
keys_dir = snap_project.assets_dir / "keys"
if keys_dir.is_dir():
return keys_dir

return None


def get_prime_dirs_from_project(project_info: ProjectInfo) -> dict[str | None, Path]:
"""Get a mapping of component names to prime directories from a ProjectInfo.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ package-repositories:
suites: [focal]
key-id: 78E1918602959B9C59103100F1831DDAFC42E99D
url: http://ppa.launchpad.net/snappy-dev/snapcraft-daily/ubuntu
# Set an invalid keyserver here to force the use of the local key
key-server: keyserver.invalid.com
35 changes: 35 additions & 0 deletions tests/unit/services/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import platform
import shutil
import sys
from pathlib import Path
from unittest import mock

import pytest
Expand Down Expand Up @@ -220,3 +221,37 @@ def test_lifecycle_installs_gpg_dirmngr(lifecycle_service, mocker):
mock_install.assert_called_once_with(
["gpg", "dirmngr"], refresh_package_cache=False
)


@pytest.mark.parametrize(
("project_location", "assets_dir", "expected_keys_path"),
[
# These locations come from yaml_utils._SNAP_PROJECT_FILES
# snapcraft.yaml locations where the keys are expected to be in snap/keys
("snapcraft.yaml", "snap", "snap/keys"),
("snap/snapcraft.yaml", "snap", "snap/keys"),
(".snapcraft.yaml", "snap", "snap/keys"),
# snapcraft.yaml location where the keys are expected to be somewhere else
("build-aux/snap/snapcraft.yaml", "build-aux/snap", "build-aux/snap/keys"),
],
)
def test_local_keys_path(
new_dir, lifecycle_service, project_location, assets_dir, expected_keys_path
):
"""Check that _get_local_keys_path() is correct given the location of snapcraft.yaml."""
snap_dir = new_dir / Path(project_location).parent
snap_dir.mkdir(exist_ok=True, parents=True)

# The project file itself doesn't really matter, but must exist
project_yaml = snap_dir / "snapcraft.yaml"
project_yaml.touch()

# app = application.create_app()
keys_dir = Path(assets_dir) / "keys"

# If the keys dir doesn't exist the method should return None
assert not keys_dir.is_dir()
assert lifecycle_service._get_local_keys_path() is None

keys_dir.mkdir(exist_ok=True, parents=True)
assert lifecycle_service._get_local_keys_path() == Path(expected_keys_path)

0 comments on commit a3834e7

Please sign in to comment.