From 626fce5f2c06103a60f1b1ddec6504c4fba23a5a Mon Sep 17 00:00:00 2001 From: "Piotr F. Mieszkowski" Date: Sat, 25 Nov 2023 16:08:54 +0100 Subject: [PATCH] lacre.admin: Implement 'identities' sub-command --- doc/admin.md | 34 ++++++++++++++++++++++++++++++++++ lacre/_keyringcommon.py | 6 ++++++ lacre/admin.py | 32 +++++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 doc/admin.md diff --git a/doc/admin.md b/doc/admin.md new file mode 100644 index 0000000..bb8cbd1 --- /dev/null +++ b/doc/admin.md @@ -0,0 +1,34 @@ +# Lacre administration + +## Command-line tool + +There's a little tool for administrators. As long as Lacre Python packages +are available via `PYTHONPATH`, you can use it like this: + +```sh +python -m lacre.admin -h +``` + +Of course `-h` displays some help. + +## Inspecting key confirmation queue + +To find out how many keys are waiting to be confirmed, run: + +```sh +python -m lacre.admin queue +``` + +## Inspecting identities registered + +To list all identities, run: + +```sh +python -m lacre.admin identities -a +``` + +To preview a particular identity, run: + +```sh +python -m lacre.admin identities -e alice@example.com +``` diff --git a/lacre/_keyringcommon.py b/lacre/_keyringcommon.py index b8f0ce1..f662d4d 100644 --- a/lacre/_keyringcommon.py +++ b/lacre/_keyringcommon.py @@ -36,6 +36,12 @@ class KeyCache: details = ' '.join(self._keys.keys()) return '' % (details) + def __iter__(self): + return iter(self._keys.keys()) + + def emails(self): + return { email: fingerprint for (fingerprint, email) in self._keys.items() } + class KeyRing: """Contract to be implemented by a key-store (a.k.a. keyring).""" diff --git a/lacre/admin.py b/lacre/admin.py index ce147a7..24e085a 100644 --- a/lacre/admin.py +++ b/lacre/admin.py @@ -32,6 +32,24 @@ def sub_queue(args): print(f'Keys in the queue: {cnt}') +def sub_identities(args): + """Sub-command to inspect identity database.""" + LOG.debug('Inspecting identities...') + + conn = repo.connect(conf.get_item('database', 'url')) + identities = repo.IdentityRepository(conn) + + all_identities = identities.freeze_identities() + if args.all: + for id_ in all_identities: + print('-', all_identities[id_], id_) + elif args.email: + all_rev = all_identities.emails() + print('-', args.email, all_rev[args.email]) + else: + exit(lacre.EX_UNAVAILABLE) + + def main(): conf.validate_config(additional=conf.SCRIPT_REQUIRED) @@ -54,13 +72,13 @@ def main(): ) cmd_queue.set_defaults(operation=sub_queue) - # cmd_identities = sub_commands.add_parser('identities', - # help='Inspect identity database', - # aliases=['id'] - # ) - # cmd_identities.add_argument('-e', '--email', help='Check single email') - # cmd_identities.add_argument('-a', '--all', help='List all emails', action='store_true') - # cmd_identities.set_defaults(operation=sub_identities) + cmd_identities = sub_commands.add_parser('identities', + help='Inspect identity database', + aliases=['id'] + ) + cmd_identities.add_argument('-e', '--email', help='Check single email') + cmd_identities.add_argument('-a', '--all', help='List all emails', action='store_true') + cmd_identities.set_defaults(operation=sub_identities) user_request = parser.parse_args() user_request.operation(user_request)