Skip to content

Commit ffa317e

Browse files
committed
Added TLS feature at CRT layer
1 parent 04f0cf7 commit ffa317e

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

awscrt/_aws_iot_metrics.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,24 @@ def get_encoded_feature_list(client_options):
259259

260260
# I: certificate_source - Would need to be tracked from TLS context setup. This is set at a IoT SDK level
261261

262-
# LOOK into it
263-
# J: tls_cipher_preference
262+
# J: tls_cipher_preference - security policy
263+
if client_options.tls_ctx is not None:
264+
val = _tls_cipher_preference_metrics_value(client_options.tls_ctx.cipher_pref)
265+
if val:
266+
features.append(f"{MetricsFeatureId.TLS_CIPHER_PREFERENCE}/{val}")
264267

265-
# K: minimum_tls_version - The minimum TLS version is set on
266-
# TLSContextOptions but not stored/accessible from TLSContext.
268+
# K: minimum_tls_version - The minimum TLS version set on TLSContextOptions
269+
if client_options.tls_ctx is not None:
270+
val = _minimum_tls_version_metrics_value(client_options.tls_ctx.min_tls_ver)
271+
if val:
272+
features.append(f"{MetricsFeatureId.MINIMUM_TLS_VERSION}/{val}")
267273

268274
return ",".join(features)
269275

270276
# MQTT3 encoding list
271277

272278

273-
def get_encoded_feature_list_mqtt3(proxy_options):
279+
def get_encoded_feature_list_mqtt3(proxy_options, tls_ctx=None):
274280
"""
275281
Generates encoded feature list for MQTT3 connections
276282
Args:
@@ -282,11 +288,24 @@ def get_encoded_feature_list_mqtt3(proxy_options):
282288
f"{MetricsFeatureId.PROTOCOL_VERSION}/{MetricsProtocolVersionValue.MQTT311}",
283289
f"{MetricsFeatureId.SOCKET_IMPLEMENTATION}/{_detect_socket_implementation()}"
284290
]
291+
# H: http_proxy_type - Determine based on whether proxy uses TLS
285292
if proxy_options is not None:
286293
proxy_type = MetricsHttpProxyTypeValue.HTTPS if getattr(
287294
proxy_options, 'tls_connection_options', None) is not None else MetricsHttpProxyTypeValue.HTTP
288295
features.append(f"{MetricsFeatureId.HTTP_PROXY_TYPE}/{proxy_type}")
289296

297+
# J: tls_cipher_preference - security policy
298+
if tls_ctx is not None:
299+
val = _tls_cipher_preference_metrics_value(tls_ctx.cipher_pref)
300+
if val:
301+
features.append(f"{MetricsFeatureId.TLS_CIPHER_PREFERENCE}/{val}")
302+
303+
# K: minimum_tls_version - the minimum TLS version set on TLSContextOptions
304+
if tls_ctx is not None:
305+
val = _minimum_tls_version_metrics_value(tls_ctx.min_tls_ver)
306+
if val:
307+
features.append(f"{MetricsFeatureId.MINIMUM_TLS_VERSION}/{val}")
308+
290309
return ",".join(features)
291310

292311

@@ -380,7 +399,7 @@ def create_metrics_mqtt5(client_options):
380399
return create_metrics(client_options.metrics, crt_feature_list)
381400

382401

383-
def create_metrics_mqtt3(user_metrics=None, proxy_options=None):
402+
def create_metrics_mqtt3(user_metrics=None, proxy_options=None, tls_ctx = None):
384403
"""
385404
Creates final metrics for MQTT3 connection.
386405
Args:
@@ -389,5 +408,5 @@ def create_metrics_mqtt3(user_metrics=None, proxy_options=None):
389408
Returns:
390409
AWSIoTMetrics: The final metrics object
391410
"""
392-
crt_feature_list = get_encoded_feature_list_mqtt3(proxy_options)
411+
crt_feature_list = get_encoded_feature_list_mqtt3(proxy_options, tls_ctx)
393412
return create_metrics(user_metrics, crt_feature_list)

awscrt/io.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,16 @@ class ClientTlsContext(NativeResource):
606606
Args:
607607
options (TlsContextOptions): Configuration options.
608608
"""
609-
__slots__ = ()
609+
__slots__ = ('min_tls_ver', 'cipher_pref')
610610

611611
def __init__(self, options):
612612
assert isinstance(options, TlsContextOptions)
613613

614614
super().__init__()
615+
616+
self.min_tls_ver = options.min_tls_ver
617+
self.cipher_pref = options.cipher_pref
618+
615619
self._binding = _awscrt.client_tls_ctx_new(
616620
options.min_tls_ver.value,
617621
options.cipher_pref.value,

awscrt/mqtt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ class Connection(NativeResource):
334334
335335
enable_metrics (bool): Enable IoT SDK metrics in MQTT CONNECT packet username field, including SDK name, version, and platform. Default to True.
336336
337-
metrics (Optional[:class: `AWSIoTMetrics`]) : Optional metrics configuration for IoT SDK metrics reporting. If provided, the CRT will use the given metrics. If None, a default AWSIoTMetrics will be created.
337+
metrics (Optional[:class:`AWSIoTMetrics`]) : Optional metrics configuration for IoT SDK metrics reporting. If provided, the CRT will use the given metrics. If None, a default AWSIoTMetrics will be created.
338338
"""
339339

340340
def __init__(self,
@@ -416,7 +416,7 @@ def __init__(self,
416416
self.socket_options = socket_options if socket_options else SocketOptions()
417417
self.proxy_options = proxy_options if proxy_options else websocket_proxy_options
418418
if enable_metrics:
419-
self._metrics = create_metrics_mqtt3(metrics, self.proxy_options)
419+
self._metrics = create_metrics_mqtt3(metrics, self.proxy_options, self.client.tls_ctx)
420420
else:
421421
self._metrics = None
422422

awscrt/mqtt5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ class ClientOptions:
13721372
on_lifecycle_event_connection_failure_fn (Callable[[LifecycleConnectFailureData],]): Callback for Lifecycle Event Connection Failure.
13731373
on_lifecycle_event_disconnection_fn (Callable[[LifecycleDisconnectData],]): Callback for Lifecycle Event Disconnection.
13741374
enable_metrics (bool): Enable IoT SDK metrics in MQTT CONNECT packet username field, including SDK name, version, and platform. Default to True.
1375-
metrics (Optional[:class: `AWSIoTMetrics`]) : Optional metrics configuration for IoT SDK metrics reporting. If provided, the CRT will use the given metrics. If None, a default AWSIoTMetrics will be created.
1375+
metrics (Optional[:class:`AWSIoTMetrics`]) : Optional metrics configuration for IoT SDK metrics reporting. If provided, the CRT will use the given metrics. If None, a default AWSIoTMetrics will be created.
13761376
13771377
"""
13781378
host_name: str

0 commit comments

Comments
 (0)