Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _new_client_connection(self, secure, proxy_options=None):
if secure:
tls_ctx_opt = TlsContextOptions()
tls_ctx_opt.override_default_trust_store_from_path(None, 'test/resources/unittests.crt')
tls_ctx = ClientTlsContext(tls_ctx_opt)
tls_conn_opt = tls_ctx.new_connection_options()
tls_conn_opt.set_server_name(self.hostname)
else:
tls_conn_opt = None
event_loop_group = EventLoopGroup()
host_resolver = DefaultHostResolver(event_loop_group)
bootstrap = ClientBootstrap(event_loop_group, host_resolver)
connection_future = HttpClientConnection.new(host_name=self.hostname,
port=self.port,
bootstrap=bootstrap,
tls_connection_options=tls_conn_opt,
proxy_options=proxy_options)
return connection_future.result(self.timeout)
def _test_connection(self):
config = Config.get()
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
tls_opts = TlsContextOptions.create_client_with_mtls(config.cert, config.key)
tls = ClientTlsContext(tls_opts)
client = Client(bootstrap, tls)
connection = Connection(
client=client,
client_id=create_client_id(),
host_name=config.endpoint,
port=8883)
connection.connect().result(TIMEOUT)
return connection
def test_mtls_from_path(self):
config = Config.get()
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
# test "from path" builder by writing secrets to tempfiles
tmp_dirpath = tempfile.mkdtemp()
try:
cert_filepath = os.path.join(tmp_dirpath, 'cert')
with open(cert_filepath, 'wb') as cert_file:
cert_file.write(config.cert)
key_filepath = os.path.join(tmp_dirpath, 'key')
with open(key_filepath, 'wb') as key_file:
key_file.write(config.key)
connection = awsiot_mqtt_connection_builder.mtls_from_path(
cert_filepath=cert_filepath,
pri_key_filepath=key_filepath,
def test_mtls_from_bytes(self):
config = Config.get()
elg = EventLoopGroup()
resolver = DefaultHostResolver(elg)
bootstrap = ClientBootstrap(elg, resolver)
connection = awsiot_mqtt_connection_builder.mtls_from_bytes(
cert_bytes=config.cert,
pri_key_bytes=config.key,
endpoint=config.endpoint,
client_id=create_client_id(),
client_bootstrap=bootstrap)
self._test_connection(connection)
except Exception as e:
print("Exception on input thread.")
exit(e)
break
if __name__ == '__main__':
# Process input args
args = parser.parse_args()
thing_name = args.thing_name
shadow_property = args.shadow_property
io.init_logging(getattr(io.LogLevel, args.verbosity), 'stderr')
# Spin up resources
event_loop_group = io.EventLoopGroup(1)
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)
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,
ca_filepath=args.root_ca,
client_id=args.client_id,
time.sleep(1)
def waitForRegisterThingResponse():
# Wait for the response.
loopCount = 0
while loopCount < 20 and registerThingResponse is None:
if registerThingResponse is not None:
break
loopCount += 1
print('Waiting... RegisterThingResponse: ' + json.dumps(registerThingResponse))
time.sleep(1)
if __name__ == '__main__':
# Spin up resources
event_loop_group = io.EventLoopGroup(1)
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)
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,
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,