diff options
author | Michał Górny <mgorny@gentoo.org> | 2018-02-10 11:40:29 +0100 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2018-02-10 11:40:29 +0100 |
commit | 1d3b7e7c297193c21fc46bc134ec0aadd83a76d4 (patch) | |
tree | be6cf6efd869c19e435d132c4012173ed0411f29 | |
parent | b8366a4fa38db65a2726ca305dd428e185cde897 (diff) | |
download | gemato-1d3b7e7c297193c21fc46bc134ec0aadd83a76d4.tar.gz |
cli: Add helper openpgp-verify command
-rw-r--r-- | gemato/cli.py | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/gemato/cli.py b/gemato/cli.py index 42bde39..b975d1f 100644 --- a/gemato/cli.py +++ b/gemato/cli.py @@ -441,6 +441,58 @@ class HashCommand(GematoCommand): print(' '.join(e.to_list('DATA' if p != '-' else 'STDIN'))) +class OpenPGPVerifyCommand(VerifyingOpenPGPCommand): + name = 'openpgp-verify' + help = 'Verify OpenPGP signatures embedded in specified file(s) and/or stdin' + + __slots__ = ['paths'] + + def add_options(self, subp): + super(OpenPGPVerifyCommand, self).add_options(subp) + + subp.add_argument('paths', nargs='*', default=['-'], + help='Paths to hash (defaults to "-" (stdin) if not specified)') + + def parse_args(self, args, argp): + super(OpenPGPVerifyCommand, self).parse_args(args, argp) + + self.paths = args.paths + + def __call__(self): + ret = True + + for p in self.paths: + if p == '-': + if sys.hexversion >= 0x03000000: + f = sys.stdin + else: + f = io.open(sys.stdin.fileno(), 'r') + else: + f = io.open(p, 'r') + + try: + try: + sig = self.openpgp_env.verify_file(f) + except gemato.exceptions.GematoException as e: + logging.error('OpenPGP verification failed for {}:\n{}' + .format(p, str(e))) + ret = False + else: + logging.info('Valid OpenPGP signature found in {}:' + .format(p)) + logging.info('- primary key: {}'.format( + sig.primary_key_fingerprint)) + logging.info('- subkey: {}'.format( + sig.fingerprint)) + logging.info('- timestamp: {} UTC'.format( + sig.timestamp)) + finally: + if p != '-': + f.close() + + return 0 if ret else 1 + + def main(argv): argp = argparse.ArgumentParser( prog=argv[0], @@ -448,7 +500,7 @@ def main(argv): subp = argp.add_subparsers() commands = [VerifyCommand, UpdateCommand, CreateCommand, - HashCommand] + HashCommand, OpenPGPVerifyCommand] for cmdclass in commands: cmd = cmdclass() cmdp = subp.add_parser(cmd.name, help=cmd.help) |