Skip to content

A full-featured download manager.

fix(snap): initialize the store client with persistent auth (#2484)

Dr_rOotd9431ab9eefa
diff --git a/scripts/upload_snap_revision.py b/scripts/upload_snap_revision.py
index a1259992..439298d2 100644
--- a/scripts/upload_snap_revision.py
+++ b/scripts/upload_snap_revision.py
@@ -92,7 +92,7 @@ def store_getter():
# Fail closed if the installed Snapcraft API changes. Never implement or log
# macaroon handling ourselves, and never send credentials to pagination hosts.
from snapcraft.store.client import get_client
- client = get_client()
+ client = get_client(ephemeral=False)
def get(url):
try:
@@ -127,9 +127,13 @@ def verify_before_release(get):
def main():
if sys.argv[1:] == ["check-client"]:
- from snapcraft.store.client import get_client
- require(callable(get_client), "Snapcraft client adapter is unavailable")
- print("Snapcraft authenticated Store adapter import passed")
+ # Exercise the real factory without credentials, network, or a host
+ # keyring (headless runners may have no desktop secret service).
+ import keyring
+ from craft_store.auth import MemoryKeyring
+ keyring.set_keyring(MemoryKeyring())
+ store_getter()
+ print("Snapcraft authenticated Store adapter initialization passed")
return
get = store_getter()
if len(sys.argv) == 2 and sys.argv[1] == "verify":
diff --git a/tests/scripts/test_snap_publication.py b/tests/scripts/test_snap_publication.py
index b0eee7e7..5a8d5663 100644
--- a/tests/scripts/test_snap_publication.py
+++ b/tests/scripts/test_snap_publication.py
@@ -6,6 +6,7 @@ from pathlib import Path
import subprocess
import sys
import unittest
+from types import SimpleNamespace
from unittest.mock import Mock, patch
import zipfile
@@ -102,6 +103,47 @@ class UploadRecoveryTests(unittest.TestCase):
upload.list_revisions(Mock(return_value=data))
+class StoreClientAdapterTests(unittest.TestCase):
+ def factory_module(self, client):
+ # Snapcraft 9 requires this argument; preserve its real signature.
+ def get_client(ephemeral):
+ self.assertIs(ephemeral, False)
+ return client
+ return {"snapcraft": SimpleNamespace(), "snapcraft.store": SimpleNamespace(),
+ "snapcraft.store.client": SimpleNamespace(get_client=get_client)}
+
+ def test_constructs_real_signature_without_network_and_binds_safe_request_options(self):
+ client = Mock()
+ client.request.return_value = SimpleNamespace(status_code=200, json=lambda: {"revisions": []})
+ with patch.dict(sys.modules, self.factory_module(client)):
+ get = upload.store_getter()
+ client.request.assert_not_called()
+ self.assertEqual(get(upload.ENDPOINT), {"revisions": []})
+ client.request.assert_called_once_with("GET", upload.ENDPOINT, headers={"Accept": "application/json"},
+ timeout=60, allow_redirects=False)
+
+ def test_compatibility_command_constructs_client_without_authentication_or_request(self):
+ client = Mock()
+ keyring = Mock()
+ modules = {**self.factory_module(client), "keyring": keyring,
+ "craft_store": SimpleNamespace(),
+ "craft_store.auth": SimpleNamespace(MemoryKeyring=lambda: "memory-only")}
+ with patch.dict(sys.modules, modules), \
+ patch.object(sys, "argv", ["upload_snap_revision.py", "check-client"]):
+ upload.main()
+ keyring.set_keyring.assert_called_once_with("memory-only")
+ client.request.assert_not_called()
+
+ def test_does_not_expose_request_exception_payloads(self):
+ client = Mock()
+ client.request.side_effect = RuntimeError("sensitive authentication response")
+ with patch.dict(sys.modules, self.factory_module(client)):
+ with self.assertRaises(publication.PublicationError) as error:
+ upload.store_getter()(upload.ENDPOINT)
+ self.assertNotIn("sensitive", str(error.exception))
+ self.assertIn("Store read failed", str(error.exception))
+
+
class SourceTests(unittest.TestCase):
def setUp(self):
self.run = {"id": 42, "event": "push", "head_branch": "v2.2.0", "head_sha": "a" * 40}