Skip to content

Commit

Permalink
Merge branch 'hotfix/7.5' into hotfix/7.5-gpg-multi-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiusens authored Jun 30, 2023
2 parents 40e8049 + aa3d5d6 commit b8f75f5
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 35 deletions.
2 changes: 1 addition & 1 deletion requirements-devel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ git+https://github.com/canonical/craft-archives.git@hotfix/1.1.1#egg=craft_archi
craft-cli==1.2.0
craft-grammar==1.1.1
craft-parts==1.19.6
craft-providers==1.10.0
craft-providers==1.10.1
craft-store==2.4.0
cryptography==40.0.2
Deprecated==1.2.13
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ git+https://github.com/canonical/craft-archives.git@hotfix/1.1.1#egg=craft_archi
craft-cli==1.2.0
craft-grammar==1.1.1
craft-parts==1.19.6
craft-providers==1.10.0
craft-providers==1.10.1
craft-store==2.4.0
cryptography==40.0.2
Deprecated==1.2.13
Expand Down
3 changes: 2 additions & 1 deletion snapcraft/commands/names.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ class StoreNamesCommand(BaseCommand):
def run(self, parsed_args):
store_client = store.StoreClientCLI()
snaps = store_client.get_names()
snaps.sort(key=operator.itemgetter(0))

if not snaps:
emit.message("No registered snaps")
else:
tabulated_snaps = tabulate(
sorted(snaps, key=operator.itemgetter(0)),
snaps,
headers=["Name", "Since", "Visibility", "Notes"],
tablefmt="plain",
)
Expand Down
4 changes: 2 additions & 2 deletions snapcraft/commands/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ def run(self, parsed_args):
]
for track in snap_channel_map.snap.tracks
]
track_table.sort(key=operator.itemgetter(2)) # Sort by "creation-date".

emit.message(
tabulate(
# Sort by "creation-date".
sorted(track_table, key=operator.itemgetter(2)),
track_table,
headers=["Name", "Status", "Creation-Date", "Version-Pattern"],
tablefmt="plain",
)
Expand Down
32 changes: 21 additions & 11 deletions snapcraft_legacy/_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2016-2022 Canonical Ltd
# Copyright 2016-2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -435,11 +435,11 @@ def list_keys():
"""Lists keys available to sign assertions."""
keys = list(_get_usable_keys())
account_info = StoreClientCLI().get_account_information()
enabled_keys = {
enabled_keys = [
account_key["public-key-sha3-384"]
for account_key in account_info["account_keys"]
}
if keys and enabled_keys:
]
if keys:
tabulated_keys = tabulate(
[
(
Expand All @@ -453,19 +453,29 @@ def list_keys():
headers=["", "Name", "SHA3-384 fingerprint", ""],
tablefmt="plain",
)
print(
"The following keys are available on this system:"
)
print(tabulated_keys)
elif not keys and enabled_keys:
registered_keys = "\n".join([f"- {key}" for key in enabled_keys])
else:
print(
"No keys have been created on this system. "
" See 'snapcraft create-key --help' to create a key.\n"
"The following SHA3-384 key fingerprints have been registered "
f"but are not available on this system:\n{registered_keys}"
"See 'snapcraft create-key --help' to create a key."
)
if enabled_keys:
local_hashes = {key["sha3-384"] for key in keys}
registered_keys = "\n".join(
(f"- {key}" for key in enabled_keys if key not in local_hashes)
)
if registered_keys:
print(
"The following SHA3-384 key fingerprints have been registered "
f"but are not available on this system:\n{registered_keys}"
)
else:
print(
"No keys have been registered."
" See 'snapcraft register-key --help' to register a key."
"No keys have been registered with this account. "
"See 'snapcraft register-key --help' to register a key."
)


Expand Down
10 changes: 6 additions & 4 deletions spread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,12 @@ suites:
summary: tests of snapcraft's Go plugin
systems:
- ubuntu-18.04*
tests/spread/plugins/v1/godeps/:
summary: tests of snapcraft's Godeps plugin
systems:
- ubuntu-18.04*
# godeps can no longer build due to:
# https://github.com/golang/go/issues/57051
# tests/spread/plugins/v1/godeps/:
# summary: tests of snapcraft's Godeps plugin
# systems:
# - ubuntu-18.04*
tests/spread/plugins/v1/gradle/:
summary: tests of snapcraft's Gradle plugin
systems:
Expand Down
35 changes: 20 additions & 15 deletions tests/legacy/unit/commands/test_list_keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2016-2019 Canonical Ltd
# Copyright (C) 2016-2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -59,16 +59,14 @@ def test_list_keys_successfully(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
Equals(
dedent(
"""\
Name SHA3-384 fingerprint
* default {default_sha3_384}
- another {another_sha3_384} (not registered)
"""
).format(
default_sha3_384=get_sample_key("default")["sha3-384"],
another_sha3_384=get_sample_key("another")["sha3-384"],
f"""\
The following keys are available on this system:
Name SHA3-384 fingerprint
* default {get_sample_key("default")["sha3-384"]}
- another {get_sample_key("another")["sha3-384"]} (not registered)
"""
)
),
)
Expand All @@ -95,10 +93,10 @@ def test_list_keys_no_keys_on_system(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
Equals(
dedent(
"""\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- vdEeQvRxmZ26npJCFaGnl-VfGz0lU2jZZkWp_s7E-RxVCNtH2_mtjcxq2NkDKkIp
"""
Expand Down Expand Up @@ -132,8 +130,15 @@ def test_list_keys_without_registered(self):
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains(
"No keys have been registered. "
"See 'snapcraft register-key --help' to register a key."
Equals(
dedent(
f"""\
The following keys are available on this system:
Name SHA3-384 fingerprint
- default {get_sample_key("default")["sha3-384"]} (not registered)
- another {get_sample_key("another")["sha3-384"]} (not registered)
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
)
),
)
78 changes: 78 additions & 0 deletions tests/legacy/unit/store/test_keys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2023 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from unittest import mock

import pytest

from snapcraft_legacy import _store

NO_KEYS_OUTPUT = """\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
ONLY_CREATED_KEYS_OUTPUT = """\
The following keys are available on this system:
Name SHA3-384 fingerprint
- ctrl ModelM (not registered)
- alt Dvorak (not registered)
No keys have been registered with this account. See 'snapcraft register-key --help' to register a key.
"""
UNAVAILABLE_KEYS_OUTPUT = """\
No keys have been created on this system. See 'snapcraft create-key --help' to create a key.
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- Bach
- ModelM
"""
SOME_KEYS_OVERLAP_OUTPUT = """\
The following keys are available on this system:
Name SHA3-384 fingerprint
* ctrl ModelM
- alt Dvorak (not registered)
The following SHA3-384 key fingerprints have been registered but are not available on this system:
- Bach
"""
LOCAL_KEYS = [
{"name": "ctrl", "sha3-384": "ModelM"},
{"name": "alt", "sha3-384": "Dvorak"},
]
REMOTE_KEYS = [
{"public-key-sha3-384": "Bach"},
{"public-key-sha3-384": "ModelM"},
]
NO_KEYS = []


@pytest.mark.parametrize(
("usable_keys", "account_keys", "stdout"),
[
(NO_KEYS, NO_KEYS, NO_KEYS_OUTPUT),
(LOCAL_KEYS, NO_KEYS, ONLY_CREATED_KEYS_OUTPUT),
(NO_KEYS, REMOTE_KEYS, UNAVAILABLE_KEYS_OUTPUT),
(LOCAL_KEYS, REMOTE_KEYS, SOME_KEYS_OVERLAP_OUTPUT),
],
)
def test_list_keys(monkeypatch, capsys, usable_keys, account_keys, stdout):
monkeypatch.setattr(_store, "_get_usable_keys", lambda: usable_keys)
mock_client = mock.Mock(spec_set=_store.StoreClientCLI)
mock_client.get_account_information.return_value = {"account_keys": account_keys}
monkeypatch.setattr(_store, "StoreClientCLI", lambda: mock_client)

_store.list_keys()

out, err = capsys.readouterr()

assert out == stdout
assert err == ""

0 comments on commit b8f75f5

Please sign in to comment.