Skip to content

Update samples and builder with improved proxy support #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions awsiot/mqtt_connection_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@

**enable_metrics_collection** (`bool`): Whether to send the SDK version number in the CONNECT packet.
Default is True.

**http_proxy_options** (:class: 'awscrt.http.HttpProxyOptions'): HTTP proxy options to use
"""

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Expand Down Expand Up @@ -143,7 +145,6 @@ def _builder(
tls_ctx_options,
use_websockets=False,
websocket_handshake_transform=None,
websocket_proxy_options=None,
**kwargs):

ca_bytes = _get(kwargs, 'ca_bytes')
Expand Down Expand Up @@ -188,6 +189,7 @@ def _builder(
tls_ctx = awscrt.io.ClientTlsContext(tls_ctx_options)
mqtt_client = awscrt.mqtt.Client(client_bootstrap, tls_ctx)

proxy_options = kwargs.get('http_proxy_options', kwargs.get('websocket_proxy_options', None))
return awscrt.mqtt.Connection(
client=mqtt_client,
on_connection_interrupted=_get(kwargs, 'on_connection_interrupted'),
Expand All @@ -207,7 +209,7 @@ def _builder(
socket_options=socket_options,
use_websockets=use_websockets,
websocket_handshake_transform=websocket_handshake_transform,
websocket_proxy_options=websocket_proxy_options,
proxy_options=proxy_options,
)


Expand Down Expand Up @@ -264,7 +266,10 @@ def websockets_with_default_aws_signing(

credentials_provider (awscrt.auth.AwsCredentialsProvider): Source of AWS credentials to use when signing.

websocket_proxy_options (awscrt.http.HttpProxyOptions): If specified, a proxy is used when connecting.
websocket_proxy_options (awscrt.http.HttpProxyOptions): Deprecated,
for proxy settings use `http_proxy_options` (described in
:mod:`common arguments<awsiot.mqtt_connection_builder>`)

"""
_check_required_kwargs(**kwargs)

Expand Down Expand Up @@ -312,7 +317,9 @@ def websockets_with_custom_handshake(

* `**kwargs` (dict): Forward-compatibility kwargs.

websocket_proxy_options (awscrt.http.HttpProxyOptions): If specified, a proxy is used when connecting.
websocket_proxy_options (awscrt.http.HttpProxyOptions): Deprecated,
for proxy settings use `http_proxy_options` (described in
:mod:`common arguments<awsiot.mqtt_connection_builder>`)
"""
_check_required_kwargs(**kwargs)
tls_ctx_options = awscrt.io.TlsContextOptions()
Expand Down
20 changes: 10 additions & 10 deletions samples/fleetprovisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
parser.add_argument('--client-id', default="test-" + str(uuid4()), help="Client ID for MQTT connection.")
parser.add_argument('--use-websocket', default=False, action='store_true',
help="To use a websocket instead of raw mqtt. If you " +
"specify this option you must specify a region for signing, you can also enable proxy mode.")
"specify this option you must specify a region for signing.")
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
"is the region that will be used for computing the Sigv4 signature")
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
"you will likely need to set --root-ca to the ca for your proxy.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
help='Logging level')
parser.add_argument("--csr", help="File path to your client CSR in PEM format")
Expand Down Expand Up @@ -233,18 +232,18 @@ def waitForRegisterThingResponse():
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

if args.use_websocket == True:
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)

if args.use_websocket == True:
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=args.endpoint,
client_bootstrap=client_bootstrap,
region=args.signing_region,
credentials_provider=credentials_provider,
websocket_proxy_options=proxy_options,
http_proxy_options=proxy_options,
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
ca_filepath=args.root_ca,
Expand All @@ -263,7 +262,8 @@ def waitForRegisterThingResponse():
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
clean_session=False,
keep_alive_secs=6)
keep_alive_secs=6,
http_proxy_options=proxy_options)

print("Connecting to {} with client ID '{}'...".format(
args.endpoint, args.client_id))
Expand Down
20 changes: 10 additions & 10 deletions samples/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@
parser.add_argument('--job-time', default=5, type=float, help="Emulate working on job by sleeping this many seconds.")
parser.add_argument('--use-websocket', default=False, action='store_true',
help="To use a websocket instead of raw mqtt. If you " +
"specify this option you must specify a region for signing, you can also enable proxy mode.")
"specify this option you must specify a region for signing.")
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
"is the region that will be used for computing the Sigv4 signature")
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
"you will likely need to set --root-ca to the ca for your proxy.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
help='Logging level')

Expand Down Expand Up @@ -230,18 +229,18 @@ def on_update_job_execution_rejected(rejected):
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

if args.use_websocket == True:
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
proxy_options = None
if (args.proxy_host):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the proxy_port is non-zero like we do in all the other languages?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nevermind, it's defaulted.

proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)

if args.use_websocket == True:
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=args.endpoint,
client_bootstrap=client_bootstrap,
region=args.signing_region,
credentials_provider=credentials_provider,
websocket_proxy_options=proxy_options,
http_proxy_options=proxy_options,
ca_filepath=args.root_ca,
client_id=args.client_id,
clean_session=False,
Expand All @@ -256,7 +255,8 @@ def on_update_job_execution_rejected(rejected):
ca_filepath=args.root_ca,
client_id=args.client_id,
clean_session=False,
keep_alive_secs=6)
keep_alive_secs=6,
http_proxy_options=proxy_options)

print("Connecting to {} with client ID '{}'...".format(
args.endpoint, args.client_id))
Expand Down
20 changes: 10 additions & 10 deletions samples/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
"Specify 0 to run forever.")
parser.add_argument('--use-websocket', default=False, action='store_true',
help="To use a websocket instead of raw mqtt. If you " +
"specify this option you must specify a region for signing, you can also enable proxy mode.")
"specify this option you must specify a region for signing.")
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
"is the region that will be used for computing the Sigv4 signature")
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
"you will likely need to set --root-ca to the ca for your proxy.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
help='Logging level')

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

if args.use_websocket == True:
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)

if args.use_websocket == True:
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=args.endpoint,
client_bootstrap=client_bootstrap,
region=args.signing_region,
credentials_provider=credentials_provider,
websocket_proxy_options=proxy_options,
http_proxy_options=proxy_options,
ca_filepath=args.root_ca,
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
Expand All @@ -121,7 +120,8 @@ def on_message_received(topic, payload, dup, qos, retain, **kwargs):
on_connection_resumed=on_connection_resumed,
client_id=args.client_id,
clean_session=False,
keep_alive_secs=6)
keep_alive_secs=6,
http_proxy_options=proxy_options)

print("Connecting to {} with client ID '{}'...".format(
args.endpoint, args.client_id))
Expand Down
20 changes: 10 additions & 10 deletions samples/shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@
parser.add_argument('--shadow-property', default="color", help="Name of property in shadow to keep in sync")
parser.add_argument('--use-websocket', default=False, action='store_true',
help="To use a websocket instead of raw mqtt. If you " +
"specify this option you must specify a region for signing, you can also enable proxy mode.")
"specify this option you must specify a region for signing.")
parser.add_argument('--signing-region', default='us-east-1', help="If you specify --use-web-socket, this " +
"is the region that will be used for computing the Sigv4 signature")
parser.add_argument('--proxy-host', help="Hostname for proxy to connect to. Note: if you use this feature, " +
"you will likely need to set --root-ca to the ca for your proxy.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port for proxy to connect to.")
parser.add_argument('--proxy-host', help="Hostname of proxy to connect to.")
parser.add_argument('--proxy-port', type=int, default=8080, help="Port of proxy to connect to.")
parser.add_argument('--verbosity', choices=[x.name for x in io.LogLevel], default=io.LogLevel.NoLogs.name,
help='Logging level')

Expand Down Expand Up @@ -232,18 +231,18 @@ def user_input_thread_fn():
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

if args.use_websocket == True:
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)
proxy_options = None
if (args.proxy_host):
proxy_options = http.HttpProxyOptions(host_name=args.proxy_host, port=args.proxy_port)

if args.use_websocket == True:
credentials_provider = auth.AwsCredentialsProvider.new_default_chain(client_bootstrap)
mqtt_connection = mqtt_connection_builder.websockets_with_default_aws_signing(
endpoint=args.endpoint,
client_bootstrap=client_bootstrap,
region=args.signing_region,
credentials_provider=credentials_provider,
websocket_proxy_options=proxy_options,
http_proxy_options=proxy_options,
ca_filepath=args.root_ca,
client_id=args.client_id,
clean_session=False,
Expand All @@ -258,7 +257,8 @@ def user_input_thread_fn():
ca_filepath=args.root_ca,
client_id=args.client_id,
clean_session=False,
keep_alive_secs=6)
keep_alive_secs=6,
http_proxy_options=proxy_options)

print("Connecting to {} with client ID '{}'...".format(
args.endpoint, args.client_id))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _load_version():
"Operating System :: OS Independent",
],
install_requires=[
'awscrt==0.11.19',
'awscrt==0.11.20',
],
python_requires='>=3.6',
)