-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_mqtt_credentials.py
339 lines (299 loc) · 14.5 KB
/
test_mqtt_credentials.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
from awscrt.io import ClientBootstrap, ClientTlsContext, DefaultHostResolver, EventLoopGroup, Pkcs11Lib, TlsContextOptions
from awscrt import auth
from awscrt.mqtt import Client, Connection
from test import NativeResourceTest
import os
import unittest
import uuid
TIMEOUT = 100.0
def _get_env_variable(env_name):
env_data = os.environ.get(env_name)
if not env_data:
raise unittest.SkipTest(f"test requires env var: {env_name}")
return env_data
def create_client_id():
return 'aws-crt-python-unit-test-{0}'.format(uuid.uuid4())
class MqttConnectionTest(NativeResourceTest):
def test_mqtt311_cred_pkcs12(self):
input_key = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_PKCS12_KEY")
input_key_password = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_PKCS12_KEY_PASSWORD")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
tls_ctx_options = TlsContextOptions.create_client_with_mtls_pkcs12(
input_key,
input_key_password
)
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(tls_ctx_options))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=8883)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_cred_windows_cert(self):
input_windows = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_WINDOWS_CERT_STORE")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
tls_ctx_options = TlsContextOptions.create_client_with_mtls_windows_cert_store_path(
input_windows
)
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(tls_ctx_options))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=8883)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_cred_pkcs11(self):
input_pkcs11_lib = _get_env_variable("AWS_TEST_PKCS11_LIB")
input_pkcs11_pin = _get_env_variable("AWS_TEST_PKCS11_PIN")
input_pkcs11_token_label = _get_env_variable("AWS_TEST_PKCS11_TOKEN_LABEL")
input_pkcs11_private_key = _get_env_variable("AWS_TEST_PKCS11_PKEY_LABEL")
input_pkcs11_cert = _get_env_variable("AWS_TEST_PKCS11_CERT_FILE")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
test_pkcs11_lib = Pkcs11Lib(
file=input_pkcs11_lib,
behavior=Pkcs11Lib.InitializeFinalizeBehavior.STRICT)
tls_ctx_options = TlsContextOptions.create_client_with_mtls_pkcs11(
pkcs11_lib=test_pkcs11_lib,
user_pin=input_pkcs11_pin,
token_label=input_pkcs11_token_label,
private_key_label=input_pkcs11_private_key,
cert_file_path=input_pkcs11_cert
)
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(tls_ctx_options))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=8883)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_ws_cred_static(self):
input_role_access_key = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_ACCESS_KEY")
input_role_secret_key = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SECRET_ACCESS_KEY")
input_role_session_token = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SESSION_TOKEN")
input_role_region = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_REGION")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
credentials = auth.AwsCredentialsProvider.new_static(
input_role_access_key,
input_role_secret_key,
input_role_session_token
)
signing_config = auth.AwsSigningConfig(
algorithm=auth.AwsSigningAlgorithm.V4,
signature_type=auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS,
credentials_provider=credentials,
region=input_role_region,
service="iotdevicegateway",
omit_session_token=True
)
def sign_function(transform_args, **kwargs):
signing_future = auth.aws_sign_request(
http_request=transform_args.http_request,
signing_config=signing_config)
signing_future.add_done_callback(lambda x: transform_args.set_done(x.exception()))
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(TlsContextOptions()))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=int(443),
use_websockets=True,
websocket_handshake_transform=sign_function)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_ws_cred_cognito(self):
input_cognito_endpoint = _get_env_variable("AWS_TEST_MQTT311_COGNITO_ENDPOINT")
input_cognito_identity = _get_env_variable("AWS_TEST_MQTT311_COGNITO_IDENTITY")
input_region = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_REGION")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
credentials = auth.AwsCredentialsProvider.new_cognito(
endpoint=input_cognito_endpoint,
identity=input_cognito_identity,
tls_ctx=ClientTlsContext(TlsContextOptions()),
client_bootstrap=bootstrap
)
def sign_function(transform_args, **kwargs):
signing_config = auth.AwsSigningConfig(
algorithm=auth.AwsSigningAlgorithm.V4,
signature_type=auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS,
credentials_provider=credentials,
region=input_region,
service="iotdevicegateway",
omit_session_token=True
)
signing_future = auth.aws_sign_request(
http_request=transform_args.http_request,
signing_config=signing_config)
signing_future.add_done_callback(lambda x: transform_args.set_done(x.exception()))
client = Client(bootstrap, ClientTlsContext(TlsContextOptions()))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=int(443),
use_websockets=True,
websocket_handshake_transform=sign_function)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_ws_cred_x509(self):
input_x509_cert = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_X509_CERT")
input_x509_key = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_X509_KEY")
input_x509_endpoint = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_X509_ENDPOINT")
input_x509_role = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_X509_ROLE_ALIAS")
input_x509_thing = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_X509_THING_NAME")
input_region = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_REGION")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
x509_tls = TlsContextOptions.create_client_with_mtls_from_path(
input_x509_cert,
input_x509_key
)
credentials = auth.AwsCredentialsProvider.new_x509(
endpoint=input_x509_endpoint,
role_alias=input_x509_role,
thing_name=input_x509_thing,
tls_ctx=ClientTlsContext(x509_tls)
)
signing_config = auth.AwsSigningConfig(
algorithm=auth.AwsSigningAlgorithm.V4,
signature_type=auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS,
credentials_provider=credentials,
region=input_region,
service="iotdevicegateway",
omit_session_token=True
)
def sign_function(transform_args, **kwargs):
signing_future = auth.aws_sign_request(
http_request=transform_args.http_request,
signing_config=signing_config)
signing_future.add_done_callback(lambda x: transform_args.set_done(x.exception()))
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(TlsContextOptions()))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=int(443),
use_websockets=True,
websocket_handshake_transform=sign_function)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_ws_cred_profile(self):
input_profile_config = _get_env_variable("AWS_TEST_MQTT311_IOT_PROFILE_CONFIG")
input_profile_cred = _get_env_variable("AWS_TEST_MQTT311_IOT_PROFILE_CREDENTIALS")
input_region = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_REGION")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
credentials = auth.AwsCredentialsProvider.new_profile(
config_filepath=input_profile_config,
credentials_filepath=input_profile_cred
)
signing_config = auth.AwsSigningConfig(
algorithm=auth.AwsSigningAlgorithm.V4,
signature_type=auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS,
credentials_provider=credentials,
region=input_region,
service="iotdevicegateway",
omit_session_token=True
)
def sign_function(transform_args, **kwargs):
signing_future = auth.aws_sign_request(
http_request=transform_args.http_request,
signing_config=signing_config)
signing_future.add_done_callback(lambda x: transform_args.set_done(x.exception()))
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(TlsContextOptions()))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=int(443),
use_websockets=True,
websocket_handshake_transform=sign_function)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
def test_mqtt311_ws_cred_environment(self):
self._test_mqtt311_ws_cred_environment(use_default_chain=False)
def test_mqtt311_ws_cred_default(self):
self._test_mqtt311_ws_cred_environment(use_default_chain=True)
def _test_mqtt311_ws_cred_environment(self, use_default_chain):
input_access_key = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_ACCESS_KEY")
input_secret_access_key = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SECRET_ACCESS_KEY")
input_session_token = _get_env_variable("AWS_TEST_MQTT311_ROLE_CREDENTIAL_SESSION_TOKEN")
input_region = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_REGION")
input_host_name = _get_env_variable("AWS_TEST_MQTT311_IOT_CORE_HOST")
# Cache the current credentials
cache_access_key = os.environ.get("AWS_ACCESS_KEY_ID")
cache_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY")
cache_token = os.environ.get("AWS_SESSION_TOKEN")
# Set the environment variables from the static credentials
os.environ["AWS_ACCESS_KEY_ID"] = input_access_key
os.environ["AWS_SECRET_ACCESS_KEY"] = input_secret_access_key
os.environ["AWS_SESSION_TOKEN"] = input_session_token
if use_default_chain:
credentials = auth.AwsCredentialsProvider.new_default_chain()
else:
# This should load the environment variables we just set
credentials = auth.AwsCredentialsProvider.new_environment()
signing_config = auth.AwsSigningConfig(
algorithm=auth.AwsSigningAlgorithm.V4,
signature_type=auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS,
credentials_provider=credentials,
region=input_region,
service="iotdevicegateway",
omit_session_token=True
)
def sign_function(transform_args, **kwargs):
signing_future = auth.aws_sign_request(
http_request=transform_args.http_request,
signing_config=signing_config)
signing_future.add_done_callback(lambda x: transform_args.set_done(x.exception()))
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
client = Client(bootstrap, ClientTlsContext(TlsContextOptions()))
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=input_host_name,
port=int(443),
use_websockets=True,
websocket_handshake_transform=sign_function)
connection.connect().result(TIMEOUT)
connection.disconnect().result(TIMEOUT)
# Set it back to the cached result
if (cache_access_key is not None):
os.environ["AWS_ACCESS_KEY_ID"] = cache_access_key
else:
del os.environ["AWS_ACCESS_KEY_ID"]
if (cache_secret_access_key is not None):
os.environ["AWS_SECRET_ACCESS_KEY"] = cache_secret_access_key
else:
del os.environ["AWS_SECRET_ACCESS_KEY"]
if (cache_token is not None):
os.environ["AWS_SESSION_TOKEN"] = cache_token
else:
del os.environ["AWS_SESSION_TOKEN"]
if __name__ == 'main':
unittest.main()