Skip to content

Commit 2008ae1

Browse files
author
Brett Hazen
committed
Merge pull request #397 from alefend/master
Only use pyOpenSSL when using Python < 2.7.9
2 parents 2810cea + 8023f22 commit 2008ae1

File tree

6 files changed

+144
-134
lines changed

6 files changed

+144
-134
lines changed

riak/security.py

+48-38
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,25 @@
1616
under the License.
1717
"""
1818

19+
import ssl
1920
import warnings
20-
from six import PY2
2121
from riak import RiakError
2222
from riak.util import str_to_long
2323

24-
OPENSSL_VERSION_101G = 268439679
25-
if PY2:
24+
if hasattr(ssl, 'SSLContext'):
25+
# For Python >= 2.7.9 and Python 3.x
26+
USE_STDLIB_SSL = True
27+
else:
28+
# For Python 2.6 and <= 2.7.8
29+
USE_STDLIB_SSL = False
30+
31+
if not USE_STDLIB_SSL:
2632
import OpenSSL.SSL
2733
from OpenSSL import crypto
28-
sslver = OpenSSL.SSL.OPENSSL_VERSION_NUMBER
29-
# Be sure to use at least OpenSSL 1.0.1g
30-
if (sslver < OPENSSL_VERSION_101G) or \
31-
not hasattr(OpenSSL.SSL, 'TLSv1_2_METHOD'):
32-
verstring = OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_VERSION)
33-
msg = "Found {0} version, but expected at least OpenSSL 1.0.1g. " \
34-
"Security may not support TLS 1.2.".format(verstring)
35-
warnings.warn(msg, UserWarning)
36-
if hasattr(OpenSSL.SSL, 'TLSv1_2_METHOD'):
37-
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_2_METHOD
38-
elif hasattr(OpenSSL.SSL, 'TLSv1_1_METHOD'):
39-
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_1_METHOD
40-
elif hasattr(OpenSSL.SSL, 'TLSv1_METHOD'):
41-
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_METHOD
42-
else:
43-
DEFAULT_TLS_VERSION = OpenSSL.SSL.SSLv23_METHOD
44-
else:
45-
import ssl
4634

35+
OPENSSL_VERSION_101G = 268439679
36+
if hasattr(ssl, 'OPENSSL_VERSION_NUMBER'):
37+
# For Python 2.7 and Python 3.x
4738
sslver = ssl.OPENSSL_VERSION_NUMBER
4839
# Be sure to use at least OpenSSL 1.0.1g
4940
if sslver < OPENSSL_VERSION_101G or \
@@ -61,6 +52,25 @@
6152
else:
6253
DEFAULT_TLS_VERSION = ssl.PROTOCOL_SSLv23
6354

55+
else:
56+
# For Python 2.6
57+
sslver = OpenSSL.SSL.OPENSSL_VERSION_NUMBER
58+
# Be sure to use at least OpenSSL 1.0.1g
59+
if (sslver < OPENSSL_VERSION_101G) or \
60+
not hasattr(OpenSSL.SSL, 'TLSv1_2_METHOD'):
61+
verstring = OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_VERSION)
62+
msg = "Found {0} version, but expected at least OpenSSL 1.0.1g. " \
63+
"Security may not support TLS 1.2.".format(verstring)
64+
warnings.warn(msg, UserWarning)
65+
if hasattr(OpenSSL.SSL, 'TLSv1_2_METHOD'):
66+
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_2_METHOD
67+
elif hasattr(OpenSSL.SSL, 'TLSv1_1_METHOD'):
68+
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_1_METHOD
69+
elif hasattr(OpenSSL.SSL, 'TLSv1_METHOD'):
70+
DEFAULT_TLS_VERSION = OpenSSL.SSL.TLSv1_METHOD
71+
else:
72+
DEFAULT_TLS_VERSION = OpenSSL.SSL.SSLv23_METHOD
73+
6474

6575
class SecurityError(RiakError):
6676
"""
@@ -197,7 +207,7 @@ def ssl_version(self):
197207
"""
198208
return self._ssl_version
199209

200-
if PY2:
210+
if not USE_STDLIB_SSL:
201211
@property
202212
def pkey(self):
203213
"""
@@ -266,20 +276,20 @@ def _has_credential(self, key):
266276
return (getattr(self, internal_key) is not None) or \
267277
(getattr(self, internal_key + "_file") is not None)
268278

269-
def _check_revoked_cert(self, ssl_socket):
270-
"""
271-
Checks whether the server certificate on the passed socket has been
272-
revoked by checking the CRL.
279+
def _check_revoked_cert(self, ssl_socket):
280+
"""
281+
Checks whether the server certificate on the passed socket has been
282+
revoked by checking the CRL.
273283
274-
:param ssl_socket: the SSL/TLS socket
275-
:rtype: bool
276-
:raises SecurityError: when the certificate has been revoked
277-
"""
278-
if not self._has_credential('crl'):
279-
return True
280-
281-
servcert = ssl_socket.get_peer_certificate()
282-
servserial = servcert.get_serial_number()
283-
for rev in self.crl.get_revoked():
284-
if servserial == str_to_long(rev.get_serial(), 16):
285-
raise SecurityError("Server certificate has been revoked")
284+
:param ssl_socket: the SSL/TLS socket
285+
:rtype: bool
286+
:raises SecurityError: when the certificate has been revoked
287+
"""
288+
if not self._has_credential('crl'):
289+
return True
290+
291+
servcert = ssl_socket.get_peer_certificate()
292+
servserial = servcert.get_serial_number()
293+
for rev in self.crl.get_revoked():
294+
if servserial == str_to_long(rev.get_serial(), 16):
295+
raise SecurityError("Server certificate has been revoked")

riak/tests/test_security.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
under the License.
1818
"""
1919

20-
import platform
21-
if platform.python_version() < '2.7':
20+
import sys
21+
if sys.version_info < (2, 7):
2222
unittest = __import__('unittest2')
2323
else:
2424
import unittest
2525
from riak.tests import RUN_SECURITY, SECURITY_USER, SECURITY_PASSWD, \
2626
SECURITY_CACERT, SECURITY_KEY, SECURITY_CERT, SECURITY_REVOKED, \
2727
SECURITY_CERT_USER, SECURITY_CERT_PASSWD, SECURITY_BAD_CERT
2828
from riak.security import SecurityCreds
29-
from six import PY3
3029

3130

3231
class SecurityTests(object):
@@ -110,9 +109,9 @@ def test_security_revoked_cert(self):
110109
creds = SecurityCreds(username=SECURITY_USER, password=SECURITY_PASSWD,
111110
cacert_file=SECURITY_CACERT,
112111
crl_file=SECURITY_REVOKED)
113-
# Curenly Python 3.x native CRL doesn't seem to work
114-
# as advertised
115-
if PY3:
112+
# Currently Python >= 2.7.9 and Python 3.x native CRL doesn't seem to
113+
# work as advertised
114+
if sys.version_info >= (2, 7, 9):
116115
return
117116
client = self.create_client(credentials=creds)
118117
with self.assertRaises(Exception):

riak/transports/http/__init__.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@
1919
import socket
2020
import select
2121
from six import PY2
22-
if PY2:
22+
from riak.security import SecurityError, USE_STDLIB_SSL
23+
if USE_STDLIB_SSL:
24+
import ssl
25+
from riak.transports.security import configure_ssl_context
26+
else:
2327
import OpenSSL.SSL
28+
from riak.transports.security import RiakWrappedSocket,\
29+
configure_pyopenssl_context
30+
if PY2:
2431
from httplib import HTTPConnection, \
2532
NotConnected, \
2633
IncompleteRead, \
2734
ImproperConnectionState, \
2835
BadStatusLine, \
2936
HTTPSConnection
30-
from riak.transports.security import RiakWrappedSocket,\
31-
configure_pyopenssl_context
3237
else:
3338
from http.client import HTTPConnection, \
3439
HTTPSConnection, \
3540
NotConnected, \
3641
IncompleteRead, \
3742
ImproperConnectionState, \
3843
BadStatusLine
39-
import ssl
40-
from riak.transports.security import configure_ssl_context
4144

42-
from riak.security import SecurityError
4345
from riak.transports.pool import Pool
4446
from riak.transports.http.transport import RiakHttpTransport
4547

@@ -106,7 +108,7 @@ def connect(self):
106108
Connect to a host on a given (SSL) port using PyOpenSSL.
107109
"""
108110
sock = socket.create_connection((self.host, self.port), self.timeout)
109-
if PY2:
111+
if not USE_STDLIB_SSL:
110112
ssl_ctx = configure_pyopenssl_context(self.credentials)
111113

112114
# attempt to upgrade the socket to TLS

riak/transports/pbc/connection.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import socket
2020
import struct
2121
import riak_pb
22-
from riak.security import SecurityError
22+
from riak.security import SecurityError, USE_STDLIB_SSL
2323
from riak import RiakError
2424
from riak_pb.messages import (
2525
MESSAGE_CLASSES,
@@ -30,7 +30,7 @@
3030
)
3131
from riak.util import bytes_to_str, str_to_bytes
3232
from six import PY2
33-
if PY2:
33+
if not USE_STDLIB_SSL:
3434
from OpenSSL.SSL import Connection
3535
from riak.transports.security import configure_pyopenssl_context
3636
else:
@@ -113,7 +113,7 @@ def _auth(self):
113113
else:
114114
return False
115115

116-
if PY2:
116+
if not USE_STDLIB_SSL:
117117
def _ssl_handshake(self):
118118
"""
119119
Perform an SSL handshake w/ the server.

0 commit comments

Comments
 (0)