Passed
Push — master ( ce647f...f4ead8 )
by Dean
02:57
created

OpenSSL.on_success()   A

Complexity

Conditions 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 6
cts 9
cp 0.6667
rs 9.4285
cc 2
crap 2.1481
1 1
from plugin.core.libraries.tests.core.base import BaseTest
2
3 1
import logging
4
5 1
log = logging.getLogger(__name__)
6
7
8 1
class OpenSSL(BaseTest):
9 1
    name = 'openssl'
10 1
    optional = True
11
12 1
    @staticmethod
13
    def test_import():
14
        import OpenSSL.SSL
15
16
        # Try construct SSL context
17
        ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
18
19
        # Ensure library has SNI support
20
        cnx = OpenSSL.SSL.Connection(ctx)
21
22
        if not hasattr(cnx, 'set_tlsext_host_name'):
23
            raise Exception('Missing SNI extension')
24
25
        # Ensure binding can be imported
26
        from cryptography.hazmat.bindings.openssl.binding import Binding
27
        assert Binding
28
29
        return {
30
            'versions': {
31
                'pyopenssl': getattr(OpenSSL, '__version__', None)
32
            }
33
        }
34
35 1
    @classmethod
36
    def on_success(cls, metadata):
37
        # Inject pyOpenSSL into requests
38 1
        try:
39 1
            from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3
40 1
            inject_into_urllib3()
41
        except Exception, ex:
42
            log.warn('Unable to inject pyOpenSSL into urllib3 - %s', ex, exc_info=True)
43
            return
44
45
        # Enable secure error reporting
46 1
        from plugin.core.logger.handlers.error_reporter import RAVEN
47
        RAVEN.set_protocol('threaded+requests+https')
48