Skip to content

Add gpg.export_pubkeys convenience function #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions securesystemslib/gpg/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,29 @@ def export_pubkey(keyid, homedir=None):
key_bundle = securesystemslib.gpg.common.get_pubkey_bundle(key_packet, keyid)

return key_bundle


def export_pubkeys(keyids, homedir=None):
"""Export multiple public keys from a GnuPG keyring.

Arguments:
keyids: A list of OpenPGP keyids in KEYID_SCHEMA format.
homedir (optional): A path to the GnuPG home directory. If not set the
default GnuPG home directory is used.

Raises:
TypeError: Keyids is not iterable.
See 'export_pubkey' for other exceptions.

Returns:
A dict with the OpenPGP keyids passed as the keyids argument for dict keys
and keys in GPG_PUBKEY_SCHEMA format for values.

"""
public_key_dict = {}
for gpg_keyid in keyids:
public_key = export_pubkey(gpg_keyid, homedir=homedir)
keyid = public_key["keyid"]
public_key_dict[keyid] = public_key

return public_key_dict
6 changes: 5 additions & 1 deletion tests/check_public_interfaces_gpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from securesystemslib.gpg.constants import HAVE_GPG, NO_GPG_MSG
from securesystemslib.gpg.util import get_version
from securesystemslib.gpg.functions import (
create_signature, export_pubkey, verify_signature)
create_signature, export_pubkey, export_pubkeys, verify_signature)

from securesystemslib.exceptions import UnsupportedLibraryError

Expand All @@ -48,6 +48,10 @@ def test_gpg_functions(self):
export_pubkey('f00')
self.assertEqual(NO_GPG_MSG, str(ctx.exception))

with self.assertRaises(UnsupportedLibraryError) as ctx:
export_pubkeys(['f00'])
self.assertEqual(NO_GPG_MSG, str(ctx.exception))

with self.assertRaises(UnsupportedLibraryError) as ctx:
get_version()
self.assertEqual(NO_GPG_MSG, str(ctx.exception))
Expand Down
18 changes: 16 additions & 2 deletions tests/test_gpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

from securesystemslib import process
from securesystemslib.gpg.functions import (create_signature, export_pubkey,
verify_signature)
verify_signature, export_pubkeys)
from securesystemslib.gpg.util import (get_version, is_version_fully_supported,
get_hashing_class, parse_packet_header, parse_subpacket_header)
from securesystemslib.gpg.rsa import create_pubkey as rsa_create_pubkey
Expand All @@ -55,7 +55,8 @@
from securesystemslib.gpg.exceptions import (PacketParsingError,
PacketVersionNotSupportedError, SignatureAlgorithmNotSupportedError,
KeyNotFoundError, CommandError, KeyExpirationError)
from securesystemslib.formats import GPG_PUBKEY_SCHEMA
from securesystemslib.formats import (GPG_PUBKEY_SCHEMA,
ANY_PUBKEY_DICT_SCHEMA)


@unittest.skipIf(not HAVE_GPG, "gpg not found")
Expand Down Expand Up @@ -481,6 +482,8 @@ class TestGPGRSA(unittest.TestCase):
unsupported_subkey_keyid = "611A9B648E16F54E8A7FAD5DA51E8CDF3B06524F"
expired_key_keyid = "E8AC80C924116DABB51D4B987CB07D6D2C199C7C"

keyid_768C43 = "7B3ABB26B97B655AB9296BD15B0BD02E1C768C43"

@classmethod
def setUpClass(self):
# Create directory to run the tests without having everything blow up
Expand Down Expand Up @@ -547,6 +550,17 @@ def test_export_pubkey(self):
self.assertDictEqual(key_data, key_data2)


def test_export_pubkeys(self):
"""Test export multiple pubkeys at once. """
key_dict = export_pubkeys([self.default_keyid, self.keyid_768C43],
homedir=self.gnupg_home)

ANY_PUBKEY_DICT_SCHEMA.check_match(key_dict)
self.assertListEqual(
sorted([self.default_keyid.lower(), self.keyid_768C43.lower()]),
sorted(key_dict.keys()))


def test_gpg_sign_and_verify_object_with_default_key(self):
"""Create a signature using the default key on the keyring """

Expand Down