Skip to content

Commit 3139671

Browse files
bretambrosegraebm
andauthored
Update samples and builder with improved proxy support (#196)
* Update samples and builder with improved proxy support * Deprecate and don't use old parameter in samples when using websockets * Doc update Co-authored-by: Michael Graeb <graebm@amazon.com>
1 parent a59e276 commit 3139671

File tree

8 files changed

+60
-48
lines changed

8 files changed

+60
-48
lines changed

awsiot/mqtt_connection_builder.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
9191
**enable_metrics_collection** (`bool`): Whether to send the SDK version number in the CONNECT packet.
9292
Default is True.
93+
94+
**http_proxy_options** (:class: 'awscrt.http.HttpProxyOptions'): HTTP proxy options to use
9395
"""
9496

9597
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
@@ -143,7 +145,6 @@ def _builder(
143145
tls_ctx_options,
144146
use_websockets=False,
145147
websocket_handshake_transform=None,
146-
websocket_proxy_options=None,
147148
**kwargs):
148149

149150
ca_bytes = _get(kwargs, 'ca_bytes')
@@ -188,6 +189,7 @@ def _builder(
188189
tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
189190
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)
190191

192+
proxy_options = kwargs.get('http_proxy_options', kwargs.get('websocket_proxy_options', None))
191193
return awscrt.mqtt.Connection(
192194
client=mqtt_client,
193195
on_connection_interrupted=_get(kwargs, 'on_connection_interrupted'),
@@ -207,7 +209,7 @@ def _builder(
207209
socket_options=socket_options,
208210
use_websockets=use_websockets,
209211
websocket_handshake_transform=websocket_handshake_transform,
210-
websocket_proxy_options=websocket_proxy_options,
212+
proxy_options=proxy_options,
211213
)
212214

213215

@@ -264,7 +266,10 @@ def websockets_with_default_aws_signing(
264266
265267
credentials_provider (awscrt.auth.AwsCredentialsProvider): Source of AWS credentials to use when signing.
266268
267-
websocket_proxy_options (awscrt.http.HttpProxyOptions): If specified, a proxy is used when connecting.
269+
websocket_proxy_options (awscrt.http.HttpProxyOptions): Deprecated,
270+
for proxy settings use `http_proxy_options` (described in
271+
:mod:`common arguments<awsiot.mqtt_connection_builder>`)
272+
268273
"""
269274
_check_required_kwargs(**kwargs)
270275

@@ -312,7 +317,9 @@ def websockets_with_custom_handshake(
312317
313318
* `**kwargs` (dict): Forward-compatibility kwargs.
314319
315-
websocket_proxy_options (awscrt.http.HttpProxyOptions): If specified, a proxy is used when connecting.
320+
websocket_proxy_options (awscrt.http.HttpProxyOptions): Deprecated,
321+
for proxy settings use `http_proxy_options` (described in
322+
:mod:`common arguments<awsiot.mqtt_connection_builder>`)
316323
"""
317324
_check_required_kwargs(**kwargs)
318325
tls_ctx_options = awscrt.io.TlsContextOptions()

docs/awsiot/mqtt_connection_builder.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ <h3>Navigation</h3>
133133
<dt><strong>enable_metrics_collection</strong> (<cite>bool</cite>): Whether to send the SDK version number in the CONNECT packet.</dt><dd><p>Default is True.</p>
134134
</dd>
135135
</dl>
136+
<p><strong>http_proxy_options</strong> (:class: ‘awscrt.http.HttpProxyOptions’): HTTP proxy options to use</p>
136137
</div></blockquote>
137138
<dl class="py function">
138139
<dt class="sig sig-object py" id="awsiot.mqtt_connection_builder.mtls_from_path">
@@ -186,7 +187,9 @@ <h3>Navigation</h3>
186187
<dd class="field-odd"><ul class="simple">
187188
<li><p><strong>region</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.9)"><em>str</em></a>) – AWS region to use when signing.</p></li>
188189
<li><p><strong>credentials_provider</strong> (<a class="reference external" href="https://awslabs.github.io/aws-crt-python/api/auth.html#awscrt.auth.AwsCredentialsProvider" title="(in awscrt)"><em>awscrt.auth.AwsCredentialsProvider</em></a>) – Source of AWS credentials to use when signing.</p></li>
189-
<li><p><strong>websocket_proxy_options</strong> (<a class="reference external" href="https://awslabs.github.io/aws-crt-python/api/http.html#awscrt.http.HttpProxyOptions" title="(in awscrt)"><em>awscrt.http.HttpProxyOptions</em></a>) – If specified, a proxy is used when connecting.</p></li>
190+
<li><p><strong>websocket_proxy_options</strong> (<a class="reference external" href="https://awslabs.github.io/aws-crt-python/api/http.html#awscrt.http.HttpProxyOptions" title="(in awscrt)"><em>awscrt.http.HttpProxyOptions</em></a>) – Deprecated,
191+
for proxy settings use <cite>http_proxy_options</cite> (described in
192+
<a class="reference internal" href="#module-awsiot.mqtt_connection_builder" title="awsiot.mqtt_connection_builder"><code class="xref py py-mod docutils literal notranslate"><span class="pre">common</span> <span class="pre">arguments</span></code></a>)</p></li>
190193
</ul>
191194
</dd>
192195
<dt class="field-even">Return type</dt>
@@ -219,7 +222,9 @@ <h3>Navigation</h3>
219222
</ul>
220223
</div></blockquote>
221224
</p></li>
222-
<li><p><strong>websocket_proxy_options</strong> (<a class="reference external" href="https://awslabs.github.io/aws-crt-python/api/http.html#awscrt.http.HttpProxyOptions" title="(in awscrt)"><em>awscrt.http.HttpProxyOptions</em></a>) – If specified, a proxy is used when connecting.</p></li>
225+
<li><p><strong>websocket_proxy_options</strong> (<a class="reference external" href="https://awslabs.github.io/aws-crt-python/api/http.html#awscrt.http.HttpProxyOptions" title="(in awscrt)"><em>awscrt.http.HttpProxyOptions</em></a>) – Deprecated,
226+
for proxy settings use <cite>http_proxy_options</cite> (described in
227+
<a class="reference internal" href="#module-awsiot.mqtt_connection_builder" title="awsiot.mqtt_connection_builder"><code class="xref py py-mod docutils literal notranslate"><span class="pre">common</span> <span class="pre">arguments</span></code></a>)</p></li>
223228
</ul>
224229
</dd>
225230
<dt class="field-even">Return type</dt>

docs/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/fleetprovisioning.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@
3737
parser.add_argument('--client-id', default="test-" + str(uuid4()), help="Client ID for MQTT connection.")
3838
parser.add_argument('--use-websocket', default=False, action='store_true',
3939
help="To use a websocket instead of raw mqtt. If you " +
40-
"specify this option you must specify a region for signing, you can also enable proxy mode.")
40+
"specify this option you must specify a region for signing.")
4141
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
4242
"is the region that will be used for computing the Sigv4 signature")
43-
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
44-
"you will likely need to set --root-ca to the ca for your proxy.")
45-
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
43+
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
44+
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
4645
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
4746
help='Logging level')
4847
parser.add_argument("--csr", help="File path to your client CSR in PEM format")
@@ -233,18 +232,18 @@ def waitForRegisterThingResponse():
233232
host_resolver = io.DefaultHostResolver(event_loop_group)
234233
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
235234

236-
if args.use_websocket == True:
237-
proxy_options = None
238-
if (args.proxy_host):
239-
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
235+
proxy_options = None
236+
if (args.proxy_host):
237+
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
240238

239+
if args.use_websocket == True:
241240
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
242241
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
243242
endpoint=args.endpoint,
244243
client_bootstrap=client_bootstrap,
245244
region=args.signing_region,
246245
credentials_provider=credentials_provider,
247-
websocket_proxy_options=proxy_options,
246+
http_proxy_options=proxy_options,
248247
on_connection_interrupted=on_connection_interrupted,
249248
on_connection_resumed=on_connection_resumed,
250249
ca_filepath=args.root_ca,
@@ -263,7 +262,8 @@ def waitForRegisterThingResponse():
263262
on_connection_interrupted=on_connection_interrupted,
264263
on_connection_resumed=on_connection_resumed,
265264
clean_session=False,
266-
keep_alive_secs=6)
265+
keep_alive_secs=6,
266+
http_proxy_options=proxy_options)
267267

268268
print("Connecting to {} with client ID '{}'...".format(
269269
args.endpoint, args.client_id))

samples/jobs.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@
4646
parser.add_argument('--job-time', default=5, type=float, help="Emulate working on job by sleeping this many seconds.")
4747
parser.add_argument('--use-websocket', default=False, action='store_true',
4848
help="To use a websocket instead of raw mqtt. If you " +
49-
"specify this option you must specify a region for signing, you can also enable proxy mode.")
49+
"specify this option you must specify a region for signing.")
5050
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
5151
"is the region that will be used for computing the Sigv4 signature")
52-
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
53-
"you will likely need to set --root-ca to the ca for your proxy.")
54-
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
52+
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
53+
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
5554
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
5655
help='Logging level')
5756

@@ -230,18 +229,18 @@ def on_update_job_execution_rejected(rejected):
230229
host_resolver = io.DefaultHostResolver(event_loop_group)
231230
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
232231

233-
if args.use_websocket == True:
234-
proxy_options = None
235-
if (args.proxy_host):
236-
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
232+
proxy_options = None
233+
if (args.proxy_host):
234+
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
237235

236+
if args.use_websocket == True:
238237
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
239238
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
240239
endpoint=args.endpoint,
241240
client_bootstrap=client_bootstrap,
242241
region=args.signing_region,
243242
credentials_provider=credentials_provider,
244-
websocket_proxy_options=proxy_options,
243+
http_proxy_options=proxy_options,
245244
ca_filepath=args.root_ca,
246245
client_id=args.client_id,
247246
clean_session=False,
@@ -256,7 +255,8 @@ def on_update_job_execution_rejected(rejected):
256255
ca_filepath=args.root_ca,
257256
client_id=args.client_id,
258257
clean_session=False,
259-
keep_alive_secs=6)
258+
keep_alive_secs=6,
259+
http_proxy_options=proxy_options)
260260

261261
print("Connecting to {} with client ID '{}'...".format(
262262
args.endpoint, args.client_id))

samples/pubsub.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
"Specify 0 to run forever.")
3333
parser.add_argument('--use-websocket', default=False, action='store_true',
3434
help="To use a websocket instead of raw mqtt. If you " +
35-
"specify this option you must specify a region for signing, you can also enable proxy mode.")
35+
"specify this option you must specify a region for signing.")
3636
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
3737
"is the region that will be used for computing the Sigv4 signature")
38-
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
39-
"you will likely need to set --root-ca to the ca for your proxy.")
40-
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
38+
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
39+
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
4140
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
4241
help='Logging level')
4342

@@ -90,18 +89,18 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
9089
host_resolver = io.DefaultHostResolver(event_loop_group)
9190
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
9291

93-
if args.use_websocket == True:
94-
proxy_options = None
95-
if (args.proxy_host):
96-
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
92+
proxy_options = None
93+
if (args.proxy_host):
94+
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
9795

96+
if args.use_websocket == True:
9897
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
9998
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
10099
endpoint=args.endpoint,
101100
client_bootstrap=client_bootstrap,
102101
region=args.signing_region,
103102
credentials_provider=credentials_provider,
104-
websocket_proxy_options=proxy_options,
103+
http_proxy_options=proxy_options,
105104
ca_filepath=args.root_ca,
106105
on_connection_interrupted=on_connection_interrupted,
107106
on_connection_resumed=on_connection_resumed,
@@ -121,7 +120,8 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
121120
on_connection_resumed=on_connection_resumed,
122121
client_id=args.client_id,
123122
clean_session=False,
124-
keep_alive_secs=6)
123+
keep_alive_secs=6,
124+
http_proxy_options=proxy_options)
125125

126126
print("Connecting to {} with client ID '{}'...".format(
127127
args.endpoint, args.client_id))

samples/shadow.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@
4343
parser.add_argument('--shadow-property', default="color", help="Name of property in shadow to keep in sync")
4444
parser.add_argument('--use-websocket', default=False, action='store_true',
4545
help="To use a websocket instead of raw mqtt. If you " +
46-
"specify this option you must specify a region for signing, you can also enable proxy mode.")
46+
"specify this option you must specify a region for signing.")
4747
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
4848
"is the region that will be used for computing the Sigv4 signature")
49-
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
50-
"you will likely need to set --root-ca to the ca for your proxy.")
51-
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
49+
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
50+
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
5251
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
5352
help='Logging level')
5453

@@ -232,18 +231,18 @@ def user_input_thread_fn():
232231
host_resolver = io.DefaultHostResolver(event_loop_group)
233232
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
234233

235-
if args.use_websocket == True:
236-
proxy_options = None
237-
if (args.proxy_host):
238-
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
234+
proxy_options = None
235+
if (args.proxy_host):
236+
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
239237

238+
if args.use_websocket == True:
240239
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
241240
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
242241
endpoint=args.endpoint,
243242
client_bootstrap=client_bootstrap,
244243
region=args.signing_region,
245244
credentials_provider=credentials_provider,
246-
websocket_proxy_options=proxy_options,
245+
http_proxy_options=proxy_options,
247246
ca_filepath=args.root_ca,
248247
client_id=args.client_id,
249248
clean_session=False,
@@ -258,7 +257,8 @@ def user_input_thread_fn():
258257
ca_filepath=args.root_ca,
259258
client_id=args.client_id,
260259
clean_session=False,
261-
keep_alive_secs=6)
260+
keep_alive_secs=6,
261+
http_proxy_options=proxy_options)
262262

263263
print("Connecting to {} with client ID '{}'...".format(
264264
args.endpoint, args.client_id))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _load_version():
4040
"Operating System :: OS Independent",
4141
],
4242
install_requires=[
43-
'awscrt==0.11.19',
43+
'awscrt==0.11.20',
4444
],
4545
python_requires='>=3.6',
4646
)

0 commit comments

Comments
 (0)