-
Notifications
You must be signed in to change notification settings - Fork 168
Add keyserver-options parser and expose ca-cert-file for recv-keys #166
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
e837451
62e5c4f
6dbbb28
2838ee0
a4e6e94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,12 @@ | |
| except ImportError: | ||
| from ordereddict import OrderedDict | ||
|
|
||
| try: | ||
| import urlparse | ||
| except ImportError: | ||
| from urllib import parse as urlparse | ||
|
|
||
| import os | ||
| import re | ||
|
|
||
| from . import _util | ||
|
|
@@ -74,6 +80,67 @@ def _check_keyserver(location): | |
| return keyserver | ||
| return None | ||
|
|
||
|
|
||
| def _check_keyserver_option(ks_option): | ||
| """Check that the provided keyserver option is valid and safe. | ||
|
|
||
| :param str ks_option: A valid argument to --keyserver-option. | ||
| :rtype: :obj:`str` or :obj:`None` | ||
| :returns: A string of the keyserver option or None. | ||
| """ | ||
| def _is_valid_file(option_value): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably want to use
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing. |
||
| """Verify option value is a file.""" | ||
| return os.path.isfile(option_value) | ||
|
|
||
| def _is_valid_integer(option_value): | ||
| """Verify option value is an integer.""" | ||
| return str.isdigit(option_value) | ||
|
|
||
| def _is_valid_http_proxy(option_value): | ||
| """Verify option value looks like a proxy URL.""" | ||
| if not (option_value.startswith('http://') or | ||
| option_value.startswith('https://')): | ||
| proxy = 'http://{0}'.format(option_value) | ||
| else: | ||
| proxy = option_value | ||
| parsed_url = urlparse.urlparse(proxy) | ||
| if parsed_url.scheme and parsed_url.hostname: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
| boolean_options = { | ||
| 'auto-key-retrieve', | ||
| 'check-cert', | ||
| 'honor-keyserver-url', | ||
| 'honor-pka-record', | ||
| 'keep-temp-files', | ||
| 'include-disabled', | ||
| 'include-revoked', | ||
| 'include-subkeys', | ||
| 'use-temp-files', | ||
| } | ||
| no_prefixed_options = set(['no{}'.format(opt) for opt in boolean_options]) | ||
| options_with_validators = { | ||
| 'ca-cert-file': _is_valid_file, | ||
| 'http-proxy': _is_valid_http_proxy, | ||
| 'max-cert-size': _is_valid_integer, | ||
| 'timeout': _is_valid_integer, | ||
| } | ||
| valid_simple_options = (set(['debug', 'verbose']) | | ||
| boolean_options | | ||
| no_prefixed_options) | ||
| if ks_option in valid_simple_options: | ||
| return ks_option | ||
| opt, opt_arg = ks_option.split('=', 1) | ||
| if opt in options_with_validators: | ||
| arg_ok = options_with_validators[opt](opt_arg) | ||
| if arg_ok: | ||
| return ks_option | ||
| log.debug('Dropping invalid keyserver option: {}'.format(ks_option)) | ||
| return None | ||
|
|
||
|
|
||
| def _check_preferences(prefs, pref_type=None): | ||
| """Check cipher, digest, and compression preference settings. | ||
|
|
||
|
|
@@ -328,6 +395,16 @@ def _check_option(arg, value): | |
| checked += (v + " ") | ||
| else: log.debug("Dropping keyserver: %s" % v) | ||
| continue | ||
| elif flag in ['--keyserver-options']: | ||
| print('found keyserver options: %s' % v) | ||
| keyserver_option = _check_keyserver_option(v) | ||
| if keyserver_option: | ||
| log.debug('Setting keyserver option: %s' % | ||
| keyserver_option) | ||
| checked += (keyserver_option + " ") | ||
| else: | ||
| log.debug('Dropping keyserver option: %s' % v) | ||
| continue | ||
|
|
||
| ## the rest are strings, filenames, etc, and should be | ||
| ## shell escaped: | ||
|
|
@@ -496,6 +573,7 @@ def _get_options_group(group=None): | |
| #: These have their own parsers and don't really fit into a group | ||
| other_options = frozenset(['--debug-level', | ||
| '--keyserver', | ||
| '--keyserver-options', | ||
|
|
||
| ]) | ||
| #: These should have a directory for an argument | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,13 +369,22 @@ def recv_keys(self, *keyids, **kwargs): | |
| """Import keys from a keyserver. | ||
|
|
||
| >>> gpg = gnupg.GPG(homedir="doctests") | ||
| >>> key = gpg.recv_keys('hkp://pgp.mit.edu', '3FF0DB166A7476EA') | ||
| >>> key = gpg.recv_keys('3FF0DB166A7476EA', | ||
| keyserver='hkp://pgp.mit.edu') | ||
| >>> assert key | ||
|
|
||
| >>> ssl_keyserver = 'hkps://hkps.pool.sks-keyservers.net' | ||
| >>> ca_cert = '/home/user/hkps.pool.sks-keyservers.netCA.pem' | ||
| >>> gpg.recv_keys('6F682D87', | ||
| keyserver=ssl_keyserver, | ||
| keyserver_certs=ca_cert) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll need
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding. |
||
|
|
||
| :param str keyids: Each ``keyids`` argument should be a string | ||
| containing a keyid to request. | ||
| :param str keyserver: The keyserver to request the ``keyids`` from; | ||
| defaults to `gnupg.GPG.keyserver`. | ||
| :param str keyserver_certs: A file passed as the CA cert file for the | ||
| keyserver. | ||
| """ | ||
| if keyids: | ||
| keys = ' '.join([key for key in keyids]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you renamed the parameter to
option_value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nope, I'm sorry. I appear to be used to C and Rust where the docstrings go above the function they document. Carry on!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, no worries! I've solidly moved into Go these days so who knows what I'm going to write in during this merge :)