Debian Codegraph @

test/test_proxy.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 from test import NativeResourceTest, TIMEOUT
5 from awscrt.http import HttpProxyOptions, HttpProxyAuthenticationType, HttpProxyConnectionType, HttpClientConnection, HttpRequest
6 from awscrt.io import ClientTlsContext, TlsContextOptions, ClientBootstrap, ClientTlsContext, DefaultHostResolver, EventLoopGroup
7 from awscrt.mqtt import Client, Connection
8 from awscrt.auth import AwsCredentialsProvider
9 import os
10 import unittest
11 from test.test_http_client import Response
12 from test.test_mqtt import create_client_id
13
14
15 """
16
17 # AWS_TEST_HTTP_PROXY_HOST - host address of the proxy to use for tests that make open connections to the proxy
18 # AWS_TEST_HTTP_PROXY_PORT - port to use for tests that make open connections to the proxy
19 # AWS_TEST_HTTPS_PROXY_HOST - host address of the proxy to use for tests that make tls-protected connections to the
20 proxy
21 # AWS_TEST_HTTPS_PROXY_PORT - port to use for tests that make tls-protected connections to the proxy
22 # AWS_TEST_HTTP_PROXY_BASIC_HOST - host address of the proxy to use for tests that make open connections to the proxy
23 with basic authentication
24 # AWS_TEST_HTTP_PROXY_BASIC_PORT - port to use for tests that make open connections to the proxy with basic
25 authentication
26
27 # AWS_TEST_BASIC_AUTH_USERNAME - username to use when using basic authentication to the proxy
28 # AWS_TEST_BASIC_AUTH_PASSWORD - password to use when using basic authentication to the proxy
29
30 # AWS_TEST_TLS_CERT_PATH - file path to certificate used to initialize the tls context of the mqtt connection
31 # AWS_TEST_TLS_KEY_PATH - file path to the key used to initialize the tls context of the mqtt connection
32 # AWS_TEST_TLS_ROOT_CERT_PATH - file path to the root CA used to initialize the tls context of the mqtt connection
33
34 # AWS_TEST_X509_ENDPOINT - AWS account-specific endpoint to source x509 credentials from
35 # AWS_TEST_X509_THING_NAME - associated name of the x509 thing
36 # AWS_TEST_X509_ROLE_ALIAS - associated role alias ...
37
38 # AWS_TEST_IOT_MQTT_ENDPOINT - AWS account-specific endpoint to connect to IoT core by
39
40 """
41
42
43 class ProxyTestType:
44 FORWARDING = 0
45 TUNNELING_HTTP = 1
46 TUNNELING_HTTPS = 2
47 TUNNELING_DOUBLE_TLS = 3
48 LEGACY_HTTP = 4
49 LEGACY_HTTPS = 5
50
51
52 class ProxyTestConfiguration():
53 HTTP_PROXY_HOST = os.environ.get('AWS_TEST_HTTP_PROXY_HOST')
54 HTTP_PROXY_PORT = int(os.environ.get('AWS_TEST_HTTP_PROXY_PORT', '0'))
55 HTTPS_PROXY_HOST = os.environ.get('AWS_TEST_HTTPS_PROXY_HOST')
56 HTTPS_PROXY_PORT = int(os.environ.get('AWS_TEST_HTTPS_PROXY_PORT', '0'))
57 HTTP_PROXY_BASIC_HOST = os.environ.get('AWS_TEST_HTTP_PROXY_BASIC_HOST')
58 HTTP_PROXY_BASIC_PORT = int(os.environ.get('AWS_TEST_HTTP_PROXY_BASIC_PORT', '0'))
59
60 HTTP_PROXY_BASIC_AUTH_USERNAME = os.environ.get('AWS_TEST_BASIC_AUTH_USERNAME')
61 HTTP_PROXY_BASIC_AUTH_PASSWORD = os.environ.get('AWS_TEST_BASIC_AUTH_PASSWORD')
62
63 HTTP_PROXY_TLS_CERT_PATH = os.environ.get('AWS_TEST_MQTT311_IOT_CORE_RSA_CERT')
64 HTTP_PROXY_TLS_KEY_PATH = os.environ.get('AWS_TEST_MQTT311_IOT_CORE_RSA_KEY')
65 HTTP_PROXY_TLS_ROOT_CA_PATH = os.environ.get('AWS_TEST_MQTT311_ROOT_CA')
66
67 X509_CREDENTIALS_ENDPOINT = os.environ.get("AWS_TEST_X509_ENDPOINT")
68 X509_CREDENTIALS_THING_NAME = os.environ.get("AWS_TEST_X509_THING_NAME")
69 X509_CREDENTIALS_ROLE_ALIAS = os.environ.get("AWS_TEST_X509_ROLE_ALIAS")
70
71 HTTP_PROXY_MQTT_ENDPOINT = os.environ.get('AWS_TEST_MQTT311_IOT_CORE_HOST')
72
73 @staticmethod
74 def is_proxy_environment_initialized():
75 return ProxyTestConfiguration.HTTP_PROXY_HOST is not None and \
76 ProxyTestConfiguration.HTTP_PROXY_PORT > 0 and \
77 ProxyTestConfiguration.HTTPS_PROXY_HOST is not None and \
78 ProxyTestConfiguration.HTTPS_PROXY_PORT > 0 and \
79 ProxyTestConfiguration.HTTP_PROXY_BASIC_HOST is not None and \
80 ProxyTestConfiguration.HTTP_PROXY_BASIC_PORT > 0 and \
81 ProxyTestConfiguration.HTTP_PROXY_BASIC_AUTH_USERNAME is not None and \
82 ProxyTestConfiguration.HTTP_PROXY_BASIC_AUTH_PASSWORD is not None
83
84 @staticmethod
85 def is_mqtt_env_initialized():
86 return ProxyTestConfiguration.is_proxy_environment_initialized() and \
87 ProxyTestConfiguration.HTTP_PROXY_MQTT_ENDPOINT is not None and \
88 ProxyTestConfiguration.HTTP_PROXY_TLS_CERT_PATH is not None and \
89 ProxyTestConfiguration.HTTP_PROXY_TLS_KEY_PATH is not None and \
90 ProxyTestConfiguration.HTTP_PROXY_TLS_ROOT_CA_PATH is not None
91
92 @staticmethod
93 def is_x509_env_initialized():
94 return ProxyTestConfiguration.is_proxy_environment_initialized() and \
95 ProxyTestConfiguration.is_mqtt_env_initialized() and \
96 ProxyTestConfiguration.X509_CREDENTIALS_ENDPOINT is not None and \
97 ProxyTestConfiguration.X509_CREDENTIALS_THING_NAME is not None and \
98 ProxyTestConfiguration.X509_CREDENTIALS_ROLE_ALIAS is not None
99
100 @staticmethod
101 def get_proxy_host_for_test(test_type, auth_type):
102 if auth_type == HttpProxyAuthenticationType.Basic:
103 return ProxyTestConfiguration.HTTP_PROXY_BASIC_HOST
104
105 if test_type == ProxyTestType.TUNNELING_DOUBLE_TLS:
106 return ProxyTestConfiguration.HTTPS_PROXY_HOST
107
108 return ProxyTestConfiguration.HTTP_PROXY_HOST
109
110 @staticmethod
111 def get_proxy_port_for_test(test_type, auth_type):
112 if auth_type == HttpProxyAuthenticationType.Basic:
113 return ProxyTestConfiguration.HTTP_PROXY_BASIC_PORT
114
115 if test_type == ProxyTestType.TUNNELING_DOUBLE_TLS:
116 return ProxyTestConfiguration.HTTPS_PROXY_PORT
117
118 return ProxyTestConfiguration.HTTP_PROXY_PORT
119
120 @staticmethod
121 def get_proxy_connection_type_for_test(test_type):
122 if test_type == ProxyTestType.FORWARDING:
123 return HttpProxyConnectionType.Forwarding
124 elif test_type == ProxyTestType.TUNNELING_DOUBLE_TLS or \
125 test_type == ProxyTestType.TUNNELING_HTTP or \
126 test_type == ProxyTestType.TUNNELING_HTTPS:
127 return HttpProxyConnectionType.Tunneling
128 else:
129 return HttpProxyConnectionType.Legacy
130
131 @staticmethod
132 def get_proxy_tls_connection_options_for_test(test_type):
133 if test_type == ProxyTestType.TUNNELING_DOUBLE_TLS:
134 tls_ctx_opt = TlsContextOptions()
135 tls_ctx_opt.verify_peer = False
136 tls_ctx = ClientTlsContext(tls_ctx_opt)
137 tls_conn_opt = tls_ctx.new_connection_options()
138 tls_conn_opt.set_server_name("localhost")
139 return tls_conn_opt
140 else:
141 return None
142
143 @staticmethod
144 def create_http_proxy_options_from_environment(test_type, auth_type):
145 return HttpProxyOptions(
146 ProxyTestConfiguration.get_proxy_host_for_test(
147 test_type,
148 auth_type),
149 ProxyTestConfiguration.get_proxy_port_for_test(
150 test_type,
151 auth_type),
152 tls_connection_options=ProxyTestConfiguration.get_proxy_tls_connection_options_for_test(test_type),
153 auth_type=auth_type,
154 auth_username=ProxyTestConfiguration.HTTP_PROXY_BASIC_AUTH_USERNAME,
155 auth_password=ProxyTestConfiguration.HTTP_PROXY_BASIC_AUTH_PASSWORD,
156 connection_type=ProxyTestConfiguration.get_proxy_connection_type_for_test(test_type))
157
158 @staticmethod
159 def get_tls_connection_options_for_test(test_type, host_name):
160 if test_type == ProxyTestType.FORWARDING or test_type == ProxyTestType.LEGACY_HTTP or test_type == ProxyTestType.TUNNELING_HTTP:
161 return None
162 else:
163 tls_ctx_opt = TlsContextOptions()
164 tls_ctx = ClientTlsContext(tls_ctx_opt)
165 tls_conn_opt = tls_ctx.new_connection_options()
166 tls_conn_opt.set_server_name(host_name)
167 return tls_conn_opt
168
169 @staticmethod
170 def get_uri_from_test_type(test_type):
171 if test_type == ProxyTestType.FORWARDING or test_type == ProxyTestType.LEGACY_HTTP or test_type == ProxyTestType.TUNNELING_HTTP:
172 return "www.example.com"
173 else:
174 return "www.amazon.com"
175
176 @staticmethod
177 def get_port_from_test_type(test_type):
178 if test_type == ProxyTestType.FORWARDING or test_type == ProxyTestType.LEGACY_HTTP or test_type == ProxyTestType.TUNNELING_HTTP:
179 return 80
180 else:
181 return 443
182
183
184 class ProxyHttpTest(NativeResourceTest):
185
186 def _establish_http_connection(self, test_type, uri, proxy_options):
187 event_loop_group = EventLoopGroup()
188 host_resolver = DefaultHostResolver(event_loop_group)
189 bootstrap = ClientBootstrap(event_loop_group, host_resolver)
190
191 connection_future = HttpClientConnection.new(
192 host_name=uri,
193 port=ProxyTestConfiguration.get_port_from_test_type(test_type),
194 bootstrap=bootstrap,
195 tls_connection_options=ProxyTestConfiguration.get_tls_connection_options_for_test(
196 test_type,
197 uri),
198 proxy_options=proxy_options)
199 return connection_future.result(TIMEOUT)
200
201 def _do_proxy_http_test(self, test_type, auth_type):
202 uri = ProxyTestConfiguration.get_uri_from_test_type(test_type)
203 proxy_options = ProxyTestConfiguration.create_http_proxy_options_from_environment(test_type, auth_type)
204 connection = self._establish_http_connection(test_type, uri, proxy_options)
205
206 request = HttpRequest('GET', '/')
207 request.headers.add('host', uri)
208 response = Response()
209 stream = connection.request(request, response.on_response, response.on_body)
210 stream.activate()
211
212 # wait for stream to complete
213 stream_completion_result = stream.completion_future.result(TIMEOUT)
214
215 self.assertEqual(200, response.status_code)
216 self.assertEqual(200, stream_completion_result)
217
218 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
219 def test_forwarding_proxy_no_auth(self):
220 self._do_proxy_http_test(ProxyTestType.FORWARDING, HttpProxyAuthenticationType.Nothing)
221
222 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
223 def test_forwarding_proxy_legacy_http_no_auth(self):
224 self._do_proxy_http_test(ProxyTestType.LEGACY_HTTP, HttpProxyAuthenticationType.Nothing)
225
226 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
227 def test_proxy_legacy_https_no_auth(self):
228 self._do_proxy_http_test(ProxyTestType.LEGACY_HTTPS, HttpProxyAuthenticationType.Nothing)
229
230 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
231 def test_tunneling_proxy_http_no_auth(self):
232 self._do_proxy_http_test(ProxyTestType.TUNNELING_HTTP, HttpProxyAuthenticationType.Nothing)
233
234 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
235 def test_tunneling_proxy_https_no_auth(self):
236 self._do_proxy_http_test(ProxyTestType.TUNNELING_HTTPS, HttpProxyAuthenticationType.Nothing)
237
238 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
239 def test_tunneling_proxy_double_tls_no_auth(self):
240 self._do_proxy_http_test(ProxyTestType.TUNNELING_DOUBLE_TLS, HttpProxyAuthenticationType.Nothing)
241
242 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
243 def test_forwarding_proxy_basic_auth(self):
244 self._do_proxy_http_test(ProxyTestType.FORWARDING, HttpProxyAuthenticationType.Basic)
245
246 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
247 def test_forwarding_proxy_legacy_http_basic_auth(self):
248 self._do_proxy_http_test(ProxyTestType.LEGACY_HTTP, HttpProxyAuthenticationType.Basic)
249
250 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
251 def test_proxy_legacy_https_basic_auth(self):
252 self._do_proxy_http_test(ProxyTestType.LEGACY_HTTPS, HttpProxyAuthenticationType.Basic)
253
254 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
255 def test_tunneling_proxy_http_basic_auth(self):
256 self._do_proxy_http_test(ProxyTestType.TUNNELING_HTTP, HttpProxyAuthenticationType.Basic)
257
258 @unittest.skipIf(not ProxyTestConfiguration.is_proxy_environment_initialized(), 'requires proxy test env vars')
259 def test_tunneling_proxy_https_basic_auth(self):
260 self._do_proxy_http_test(ProxyTestType.TUNNELING_HTTPS, HttpProxyAuthenticationType.Basic)
261
262 def _establish_mqtt_connection(self, proxy_options):
263 event_loop_group = EventLoopGroup()
264 host_resolver = DefaultHostResolver(event_loop_group)
265 bootstrap = ClientBootstrap(event_loop_group, host_resolver)
266
267 tls_opts = TlsContextOptions.create_client_with_mtls_from_path(
268 ProxyTestConfiguration.HTTP_PROXY_TLS_CERT_PATH,
269 ProxyTestConfiguration.HTTP_PROXY_TLS_KEY_PATH)
270 tls_opts.override_default_trust_store_from_path(ca_filepath=ProxyTestConfiguration.HTTP_PROXY_TLS_ROOT_CA_PATH)
271 tls = ClientTlsContext(tls_opts)
272
273 client = Client(bootstrap, tls)
274 connection = Connection(
275 client=client,
276 client_id=create_client_id(),
277 host_name=ProxyTestConfiguration.HTTP_PROXY_MQTT_ENDPOINT,
278 port=8883,
279 proxy_options=proxy_options)
280 connection.connect().result(TIMEOUT)
281 return connection
282
283 def _do_proxy_mqtt_test(self, test_type, auth_type):
284 proxy_options = ProxyTestConfiguration.create_http_proxy_options_from_environment(test_type, auth_type)
285 connection = self._establish_mqtt_connection(proxy_options)
286
287 @unittest.skipIf(not ProxyTestConfiguration.is_mqtt_env_initialized(), 'requires proxy and MQTT test env vars')
288 def test_tunneling_http_proxy_mqtt_no_auth(self):
289 self._do_proxy_mqtt_test(ProxyTestType.TUNNELING_HTTP, HttpProxyAuthenticationType.Nothing)
290
291 @unittest.skipIf(not ProxyTestConfiguration.is_mqtt_env_initialized(), 'requires proxy and MQTT test env vars')
292 def test_tunneling_http_proxy_mqtt_basic_auth(self):
293 self._do_proxy_mqtt_test(ProxyTestType.TUNNELING_HTTP, HttpProxyAuthenticationType.Basic)
294
295 @unittest.skipIf(not ProxyTestConfiguration.is_mqtt_env_initialized(), 'requires proxy and MQTT test env vars')
296 def test_tunneling_http_proxy_mqtt_double_tls(self):
297 self._do_proxy_mqtt_test(ProxyTestType.TUNNELING_DOUBLE_TLS, HttpProxyAuthenticationType.Nothing)
298
299 def _create_x509_client_tls_context(self):
300 tls_ctx_opt = TlsContextOptions.create_client_with_mtls_from_path(
301 ProxyTestConfiguration.HTTP_PROXY_TLS_CERT_PATH,
302 ProxyTestConfiguration.HTTP_PROXY_TLS_KEY_PATH)
303 return ClientTlsContext(tls_ctx_opt)
304
305 def _build_proxied_x509_credentials(self, test_type, auth_type):
306 event_loop_group = EventLoopGroup(num_threads=1)
307 resolver = DefaultHostResolver(event_loop_group=event_loop_group)
308 bootstrap = ClientBootstrap(event_loop_group=event_loop_group, host_resolver=resolver)
309 proxy_options = ProxyTestConfiguration.create_http_proxy_options_from_environment(test_type, auth_type)
310 client_tls_ctx = self._create_x509_client_tls_context()
311 credentials = AwsCredentialsProvider.new_x509(
312 endpoint=ProxyTestConfiguration.X509_CREDENTIALS_ENDPOINT,
313 thing_name=ProxyTestConfiguration.X509_CREDENTIALS_THING_NAME,
314 role_alias=ProxyTestConfiguration.X509_CREDENTIALS_ROLE_ALIAS,
315 tls_ctx=client_tls_ctx,
316 client_bootstrap=bootstrap,
317 http_proxy_options=proxy_options
318 )
319 return credentials
320
321 def _do_credentials_provider_test(self, provider: AwsCredentialsProvider):
322 try:
323 credentials = provider.get_credentials().result(300) # wait 5 minutes
324 self.assertNotEqual(credentials, None, "Credentials returned was none")
325 except Exception as e:
326 raise RuntimeError(e)
327
328 @unittest.skipIf(not ProxyTestConfiguration.is_x509_env_initialized(),
329 "Requires Proxy, MQTT, and X509 test env vars")
330 def test_x509_credentials_tunneling_proxy_no_auth(self):
331 provider = self._build_proxied_x509_credentials(
332 ProxyTestType.TUNNELING_HTTPS, HttpProxyAuthenticationType.Nothing)
333 self._do_credentials_provider_test(provider)
334
335 @unittest.skipIf(not ProxyTestConfiguration.is_x509_env_initialized(),
336 "Requires Proxy, MQTT, and X509 test env vars")
337 def test_x509_credentials_tunneling_proxy_double_tls_no_auth(self):
338 provider = self._build_proxied_x509_credentials(
339 ProxyTestType.TUNNELING_DOUBLE_TLS,
340 HttpProxyAuthenticationType.Nothing)
341 self._do_credentials_provider_test(provider)
342
343 @unittest.skipIf(not ProxyTestConfiguration.is_x509_env_initialized(),
344 "Requires Proxy, MQTT, and X509 test env vars")
345 def test_x509_credentials_tunneling_proxy_basic_auth(self):
346 provider = self._build_proxied_x509_credentials(
347 ProxyTestType.TUNNELING_HTTPS, HttpProxyAuthenticationType.Basic)
348 self._do_credentials_provider_test(provider)
349
350
351 if __name__ == '__main__':
352 unittest.main()