Skip to content

Commit

Permalink
Merge pull request #60 from pop-os/gpg-info
Browse files Browse the repository at this point in the history
Adds API and CLI for retrieving/printing signing key information
  • Loading branch information
isantop committed Nov 18, 2022
2 parents f99074d + 0143849 commit f9187a0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
1 change: 1 addition & 0 deletions data/zsh-completion/_apt-manage
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ case "$state" in
'--ascii'
'--fingerprint'
'--keyserver'
'--info'
'--remove'
# List subcommand
'--legacy'
Expand Down
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
repolib (2.2.0) jammy; urgency=medium

* Adds API for getting signing key details
* Adds CLI option to print signing key information

-- Ian Santopietro <ian@system76.com> Wed, 26 Oct 2022 15:56:23 -0600

repolib (2.1.1) jammy; urgency=medium

* Improved error CLI error handling
Expand Down
2 changes: 1 addition & 1 deletion repolib/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
You should have received a copy of the GNU Lesser General Public License
along with RepoLib. If not, see <https://www.gnu.org/licenses/>.
"""
__version__ = "2.1.1"
__version__ = "2.2.0"
56 changes: 53 additions & 3 deletions repolib/command/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def init_options(cls, subparsers) -> None:
help='Which repository to manage keys for.'
)

sub_group = sub.add_mutually_exclusive_group(
required=True
)
sub_group = sub.add_mutually_exclusive_group()

sub_group.add_argument(
'-n',
Expand Down Expand Up @@ -102,6 +100,13 @@ def init_options(cls, subparsers) -> None:
)
)

sub_group.add_argument(
'-i',
'--info',
action='store_true',
help='Print key information'
)

sub_group.add_argument(
'-r',
'--remove',
Expand Down Expand Up @@ -135,6 +140,7 @@ def finalize_options(self, args):
'url',
'ascii',
'fingerprint',
'info',
'remove',
]:
self.actions[act] = getattr(args, act)
Expand All @@ -161,6 +167,17 @@ def run(self):
return False

self.log.debug('Actions to take:\n%s', self.actions)
# Run info, unless a different action is specified
self.actions['info'] = True
for key in self.actions:
if key == 'info':
self.log.debug('Skipping info key')
continue
if self.actions[key]:
self.log.info('Got an action, skipping info')
self.actions['info'] = False
break

self.log.debug('Source before:\n%s', self.source)

rets = []
Expand All @@ -169,11 +186,17 @@ def run(self):
self.log.debug('Running action: %s - (%s)', action, self.actions[action])
ret = getattr(self, action)(self.actions[action])
rets.append(ret)
break

self.log.debug('Results: %s', rets)
self.log.debug('Source after: \n%s', self.source)

if self.actions['info']:
self.log.info('Running Info, skipping saving %s', self.source.ident)
return True

if True in rets:
self.log.info('Saving source %s', self.source.ident)
self.source.file.save()
return True
else:
Expand Down Expand Up @@ -283,6 +306,33 @@ def fingerprint(self, value:str) -> bool:
self.source.load_key()
return True

def info(self, value:str) -> bool:
"""Prints key information"""
from datetime import date
if not self.source.key:
self.log.error(
'The source %s does not have a key configured.',
self.repo
)
return True

else:
key:dict = self.source.get_key_info()
output: str = f'Key information for source {self.source.ident}:\n'
output += f'Key ID: {key["uids"][0]}\n'
output += f'Fingerprint: {key["keyid"]}\n'
if key['type'] == 'pub':
output += 'Key Type: Public\n'
else:
output += 'Key Type: Private\n'
keydate = date.fromtimestamp(int(key['date']))
output += f'Issue Date: {keydate.ctime()}\n'
output += f'Length: {key["length"]} Bytes\n'
output += f'Keyring Path: {str(self.source.key.path)}\n'
print(output)

return True

def remove(self, value:str) -> bool:
"""Removes the key from the source"""

Expand Down
25 changes: 25 additions & 0 deletions repolib/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ def get_description(self) -> str:
"""
return self.name

def get_key_info(self, halt_errors: bool = False) -> dict:
""" Get a dictionary containing information for the signing key for
this source.
Arguments:
halt_errors (bool): if there are errors or other unexpected data,
raise a SourceError exception. Otherwise, simply log a warning.
Returns: dict
The dictionary from gnupg with key info.
"""
if self.key:
keys:list = self.key.gpg.list_keys()
if len(keys) > 1:
error_msg = (
f'The keyring for {self.ident} contains {len(keys)} keys'
'. Check the keyring object for details about the keys.'
)
if halt_errors:
raise SourceError(error_msg)
self.log.warning(error_msg)
return keys[0]

return {}

def reset_values(self) -> None:
"""Reset the default values for all attributes"""
self.log.info('Resetting source info')
Expand Down

0 comments on commit f9187a0

Please sign in to comment.