diff options
Diffstat (limited to 'scripts/lib/mic/3rdparty/pykickstart/urlgrabber/sslfactory.py')
| -rw-r--r-- | scripts/lib/mic/3rdparty/pykickstart/urlgrabber/sslfactory.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/scripts/lib/mic/3rdparty/pykickstart/urlgrabber/sslfactory.py b/scripts/lib/mic/3rdparty/pykickstart/urlgrabber/sslfactory.py new file mode 100644 index 0000000000..07848dac7c --- /dev/null +++ b/scripts/lib/mic/3rdparty/pykickstart/urlgrabber/sslfactory.py | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | # This library is free software; you can redistribute it and/or | ||
| 2 | # modify it under the terms of the GNU Lesser General Public | ||
| 3 | # License as published by the Free Software Foundation; either | ||
| 4 | # version 2.1 of the License, or (at your option) any later version. | ||
| 5 | # | ||
| 6 | # This library is distributed in the hope that it will be useful, | ||
| 7 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 8 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 9 | # Lesser General Public License for more details. | ||
| 10 | # | ||
| 11 | # You should have received a copy of the GNU Lesser General Public | ||
| 12 | # License along with this library; if not, write to the | ||
| 13 | # Free Software Foundation, Inc., | ||
| 14 | # 59 Temple Place, Suite 330, | ||
| 15 | # Boston, MA 02111-1307 USA | ||
| 16 | |||
| 17 | # This file is part of urlgrabber, a high-level cross-protocol url-grabber | ||
| 18 | |||
| 19 | import httplib | ||
| 20 | import urllib2 | ||
| 21 | |||
| 22 | try: | ||
| 23 | from M2Crypto import SSL | ||
| 24 | from M2Crypto import httpslib | ||
| 25 | from M2Crypto import m2urllib2 | ||
| 26 | |||
| 27 | SSL.Connection.clientPostConnectionCheck = None | ||
| 28 | have_m2crypto = True | ||
| 29 | except ImportError: | ||
| 30 | have_m2crypto = False | ||
| 31 | |||
| 32 | DEBUG = None | ||
| 33 | |||
| 34 | if have_m2crypto: | ||
| 35 | |||
| 36 | class M2SSLFactory: | ||
| 37 | |||
| 38 | def __init__(self, ssl_ca_cert, ssl_context): | ||
| 39 | self.ssl_context = self._get_ssl_context(ssl_ca_cert, ssl_context) | ||
| 40 | |||
| 41 | def _get_ssl_context(self, ssl_ca_cert, ssl_context): | ||
| 42 | """ | ||
| 43 | Create an ssl context using the CA cert file or ssl context. | ||
| 44 | |||
| 45 | The CA cert is used first if it was passed as an option. If not, | ||
| 46 | then the supplied ssl context is used. If no ssl context was supplied, | ||
| 47 | None is returned. | ||
| 48 | """ | ||
| 49 | if ssl_ca_cert: | ||
| 50 | context = SSL.Context() | ||
| 51 | context.load_verify_locations(ssl_ca_cert) | ||
| 52 | context.set_verify(SSL.verify_none, -1) | ||
| 53 | return context | ||
| 54 | else: | ||
| 55 | return ssl_context | ||
| 56 | |||
| 57 | def create_https_connection(self, host, response_class = None): | ||
| 58 | connection = httplib.HTTPSConnection(host, self.ssl_context) | ||
| 59 | if response_class: | ||
| 60 | connection.response_class = response_class | ||
| 61 | return connection | ||
| 62 | |||
| 63 | def create_opener(self, *handlers): | ||
| 64 | return m2urllib2.build_opener(self.ssl_context, *handlers) | ||
| 65 | |||
| 66 | |||
| 67 | class SSLFactory: | ||
| 68 | |||
| 69 | def create_https_connection(self, host, response_class = None): | ||
| 70 | connection = httplib.HTTPSConnection(host) | ||
| 71 | if response_class: | ||
| 72 | connection.response_class = response_class | ||
| 73 | return connection | ||
| 74 | |||
| 75 | def create_opener(self, *handlers): | ||
| 76 | return urllib2.build_opener(*handlers) | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | def get_factory(ssl_ca_cert = None, ssl_context = None): | ||
| 81 | """ Return an SSLFactory, based on if M2Crypto is available. """ | ||
| 82 | if have_m2crypto: | ||
| 83 | return M2SSLFactory(ssl_ca_cert, ssl_context) | ||
| 84 | else: | ||
| 85 | # Log here if someone provides the args but we don't use them. | ||
| 86 | if ssl_ca_cert or ssl_context: | ||
| 87 | if DEBUG: | ||
| 88 | DEBUG.warning("SSL arguments supplied, but M2Crypto is not available. " | ||
| 89 | "Using Python SSL.") | ||
| 90 | return SSLFactory() | ||
