RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
Adapters.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 
5 # Copyright 2015 RAPP
6 
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 
11  #http://www.apache.org/licenses/LICENSE-2.0
12 
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 # Authors: Konstantinos Panayiotou, Manos Tsardoulias
20 # contact: klpanagi@gmail.com, etsardou@iti.gr
21 
22 
23 ## @file RappCloud/ServiceController/Adapters.py
24 #
25 # @copyright Rapp Projecty EU 2016
26 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
27 #
28 
29 
30 
31 
32 import sys
33 import requests
34 from requests.adapters import HTTPAdapter
35 from urllib3.poolmanager import PoolManager
36 
37 
38 
39 class SSLDef:
40  """ SSL/TLS Definitions namespace
41 
42  SSL/TLS protocol versions definitions
43  """
44  TLSv1 = None
45  TLSv1_1 = None
46  TLSv1_2 = None
47  SSLv2 = None
48  SSLv3 = None
49  SSLv2_3 = None
50 
51 
52 if sys.version_info[:3] >= (2, 7, 9):
53  """
54  ssl module in Python 2.7.9 or later supports TLS versions 1.1 and 1.2
55  """
56  import ssl # Python 2.7 ssl library
57  print "\n--> Using python's ssl module with support to TLS v1_1 and 1_2"
58  SSLDef.TLSv1 = ssl.PROTOCOL_TLSv1
59  SSLDef.TLSv1_1 = ssl.PROTOCOL_TLSv1_1
60  SSLDef.TLSv1_2 = ssl.PROTOCOL_TLSv1_2
61  SSLDef.SSLv2 = ssl.PROTOCOL_SSLv2
62  SSLDef.SSLv3 = ssl.PROTOCOL_SSLv3
63  SSLDef.SSLv2_3 = ssl.PROTOCOL_SSLv23
64 else:
65  """ Else import pyopenssl and load tls1_1 and tls_1_2 if available.
66  Need to build pyOpenSSL on top of OpenSSL 1.0.1 to
67  get TLSv1.1 and 1.2 support
68  """
69  try:
70  from OpenSSL import SSL
71  """ Tell urllib3 to switch the ssl backend to PyOpenSSL """
72  import urllib3.contrib.pyopenssl
73  urllib3.contrib.pyopenssl.inject_into_urllib3()
74  """
75  Disable Insecure Request Warning caused due to
76  missing https cert verification
77  """
78  requests.packages.urllib3.disable_warnings()
79 
80  SSLDef.TLSv1 = SSL.TLSv1_METHOD
81  SSLDef.TLSv1_1 = SSL.TLSv1_1_METHOD
82  SSLDef.TLSv1_2 = SSL.TLSv1_2_METHOD
83  SSLDef.SSLv2 = SSL.SSLv2_METHOD
84  SSLDef.SSLv3 = SSL.SSLv3_METHOD
85  SSLDef.SSLv2_3 = SSL.SSLv23_METHOD
86 
87  print "\n--> Using pyopenssl module instead of python's ssl library" + \
88  " with support to tlsv1_2"
89 
90  except ImportError as e:
91  import ssl # Python 2.7 ssl library
92  print str(e)
93  print "\n--> Falling back to python's ssl library without tlsv1_2 support"
94  SSLDef.TLSv1 = ssl.PROTOCOL_TLSv1
95  SSLDef.TLSv1_1 = ssl.PROTOCOL_TLSv1
96  SSLDef.TLSv1_2 = ssl.PROTOCOL_TLSv1
97  SSLDef.SSLv2 = ssl.PROTOCOL_SSLv2
98  SSLDef.SSLv3 = ssl.PROTOCOL_SSLv3
99  SSLDef.SSLv2_3 = ssl.PROTOCOL_SSLv23
100 
101 
102  except AttributeError as e:
103  print "--> pyOpenSSL does not allow support for tls1_1 and tls1_2." + \
104  " PyOpenSSL needs to be build against openssl-1.0.1 to get " + \
105  " TLSv1.1 and 1.2 support"
106  print "--> Falling back to TLSv1"
107 
108  SSLDef.TLSv1 = SSL.TLSv1_METHOD
109  SSLDef.TLSv1_1 = SSL.TLSv1_METHOD
110  SSLDef.TLSv1_2 = SSL.TLSv1_METHOD
111  SSLDef.SSLv2 = SSL.SSLv2_METHOD
112  SSLDef.SSLv3 = SSL.SSLv3_METHOD
113  SSLDef.SSLv2_3 = SSL.SSLv23_METHOD
114 
115 
116 
117 class SSLAdapter(HTTPAdapter):
118  """ SSL Default transport Adapter """
119  def __init__(self, ssl_version=None, **kwargs):
120  self.ssl_version = ssl_version
121  super(SSLAdapter, self).__init__(**kwargs)
122 
123 
124  def init_poolmanager(self, connections, maxsize, block=False):
125  self.poolmanager = PoolManager(num_pools=connections,
126  maxsize=maxsize,
127  block=block,
128  ssl_version=self.ssl_version)
129 
130 
132  """ TLS Default transport Adapter """
133  def __init__(self, **kwargs):
134  super(TLSAdapter, self).__init__(ssl_version=None, **kwargs)
135 
136 
138  """ TLSv1 Default transport Adapter """
139  def __init__(self, **kwargs):
140  super(TLS1Adapter, self).__init__(ssl_version=SSLDef.TLSv1, **kwargs)
141 
142 
144  """ TLSv1.1 Default transport Adapter """
145  def __init__(self, **kwargs):
146  super(TLS11Adapter, self).__init__(ssl_version=SSLDef.TLSv1_1, **kwargs)
147 
148 
150  """ TLSv1.2 Default transport Adapter """
151  def __init__(self, **kwargs):
152  super(TLS12Adapter, self).__init__(ssl_version=SSLDef.TLSv1_2, **kwargs)
153 
154 
156  """ SSLv2 Default transport Adapter """
157  def __init__(self, **kwargs):
158  super(SSL2Adapter, self).__init__(ssl_version=SSLDef.SSLv2,
159  **kwargs)
160 
162  """ SSLv3 Default transport Adapter """
163  def __init__(self, **kwargs):
164  super(SSL3Adapter, self).__init__(ssl_version=SSLDef.SSLv3,
165  **kwargs)
166 
168  """ SSLv2_3 Default transport Adapter """
169  def __init__(self, **kwargs):
170  super(SSL23Adapter, self).__init__(ssl_version=SSLDef.SSLv2_3,
171  **kwargs)