Esempio n. 1
0
    def start(self):
        event_loop_group = io.EventLoopGroup(1)
        host_resolver = io.DefaultHostResolver(event_loop_group)
        client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

        tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(self.certificate_path,
                                                                             self.private_key_path)
        if self.root_ca_path:
            tls_options.override_default_trust_store_from_path(None, self.root_ca_path)
        tls_context = io.ClientTlsContext(tls_options)

        socket_options = io.SocketOptions()
        socket_options.connect_timeout_ms = 3000

        print('Performing greengrass discovery...')
        discovery_client = DiscoveryClient(client_bootstrap, socket_options, tls_context, self.region)
        resp_future = discovery_client.discover(self.thing_name)
        discover_response = resp_future.result()
        mqtt_connection = self.try_iot_endpoints(discover_response, client_bootstrap)
        self.conn = mqtt_connection
        if self.mode == 'both' or self.mode == 'subscribe':
            def on_publish(topic, payload, **kwargs):
                print('Publish received on topic {}'.format(topic))
                f = open(self.response_file_path, "w")
                f.write(payload)
                f.close()
                print(payload)

            subscribe_future, _ = mqtt_connection.subscribe(self.topic_response_validateTicket, QoS.AT_MOST_ONCE,
                                                            on_publish)
            subscribe_result = subscribe_future.result()
            print(subscribe_result)
Esempio n. 2
0
def discover_gg_host():

    event_loop_group = io.EventLoopGroup(1)
    host_resolver = io.DefaultHostResolver(event_loop_group)
    client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

    tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(
        certificate_path, private_key_path)
    #tls_options.override_default_trust_store_from_path(None, root_ca_path)
    tls_context = io.ClientTlsContext(tls_options)

    socket_options = io.SocketOptions()
    socket_options.connect_timeout_ms = 3000

    logger.info('Performing greengrass discovery...')
    discovery_client = DiscoveryClient(
        client_bootstrap, socket_options, tls_context, region)
    resp_future = discovery_client.discover(device_name)
    discover_response = resp_future.result()

    logger.debug(discover_response)

    for gg_group in discover_response.gg_groups:
        for gg_core in gg_group.cores:
            for connectivity_info in gg_core.connectivity:
                try:
                    print(
                        'Trying core {} at host {} port {}'.format(
                            gg_core.thing_arn,
                            connectivity_info.host_address,
                            connectivity_info.port))
                    connection = mqtt_connection_builder.mtls_from_path(
                        endpoint=connectivity_info.host_address,
                        port=connectivity_info.port,
                        cert_filepath=certificate_path,
                        pri_key_filepath=private_key_path,
                        client_bootstrap=client_bootstrap,
                        ca_bytes=gg_group.certificate_authorities[0].encode(
                            'utf-8'),
                        on_connection_interrupted=on_connection_interupted,
                        on_connection_resumed=on_connection_resumed,
                        client_id=device_name,
                        clean_session=False,
                        keep_alive_secs=6)

                    connect_future = connection.connect()
                    connect_future.result()
                    print('Connected!')

                    return connection

                except Exception as e:
                    print('Connection failed with exception {}'.format(e))
                    continue

    sys.exit('All connection attempts failed')
Esempio n. 3
0
    if args.root_ca:
        with open(args.root_ca, mode='rb') as ca:
            rootca = ca.read()
        tls_options.override_default_trust_store(rootca)

if args.port:
    port = args.port
elif io.is_alpn_available():
    port = 443
    if tls_options:
        tls_options.alpn_list = ['x-amzn-mqtt-ca']
else:
    port = 8883

tls_context = io.ClientTlsContext(tls_options) if tls_options else None
mqtt_client = mqtt.Client(client_bootstrap, tls_context)

# Connect
print("Connecting to {}:{} with client-id:{}".format(args.endpoint, port, CLIENT_ID))
mqtt_connection = mqtt.Connection(
    client=mqtt_client,
    host_name=args.endpoint,
    port=port,
    client_id=CLIENT_ID,
    on_connection_interrupted=on_connection_interrupted,
    on_connection_resumed=on_connection_resumed)

connect_results = mqtt_connection.connect().result(TIMEOUT)
assert(connect_results['session_present'] == False)
Esempio n. 4
0
root_ca_path = '/app/certs/root.ca.pem'
certificate_path = '/app/certs/709da90f0c.cert.pem'
private_key_path = '/app/certs/709da90f0c.private.key'
thing_name = 'docker_local'

io.init_logging(1, 'stderr')

event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)

tls_options = io.TlsContextOptions.create_client_with_mtls_from_path(certificate_path, private_key_path)
if root_ca_path:
    tls_options.override_default_trust_store_from_path(None, root_ca_path)
tls_context = io.ClientTlsContext(tls_options)

socket_options = io.SocketOptions()
socket_options.connect_timeout_ms = 3000

print('Performing greengrass discovery...')
discovery_client = DiscoveryClient(client_bootstrap, socket_options, tls_context, 'ap-southeast-2')
resp_future = discovery_client.discover(thing_name)
discover_response = resp_future.result()

print(discover_response)


def on_connection_interupted(connection, error, **kwargs):
    print('connection interrupted with error {}'.format(error))
Esempio n. 5
0
        received_all_event.set()


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)

    tls_options = io.TlsContextOptions()
    tls_options.alpn_list = ['mqtt']

    if args.root_ca:
        tls_options.override_default_trust_store_from_path(ca_dirpath=None,
            ca_filepath=args.root_ca)
    tls_ctx = io.ClientTlsContext(options=tls_options)
    client = mqtt.Client(client_bootstrap, tls_ctx)

    username = args.username
    if args.authorizer_name:
        username += f'?x-amz-customauthorizer-name={args.authorizer_name}'
    if args.token:
        username += f'&{args.token_name}={args.token}'
    mqtt_connection = mqtt.Connection(client=client,
        host_name=args.endpoint,
        port=443,
        on_connection_interrupted=on_connection_interrupted,
        on_connection_resumed=on_connection_resumed,
        client_id=args.client_id,
        clean_session=True,
        keep_alive_secs=6,
Esempio n. 6
0
 def __init__(self, eventloop):
     # TODO: accept an AWSEventLoop object or similar
     self._client_bootstrap = eventloop
     self._tls_ctx = io.ClientTlsContext(io.TlsContextOptions())
     self._socket_options = io.SocketOptions()
     self._connections = {}
Esempio n. 7
0
if scheme == 'https':
    if args.cert is not None and args.key is not None:
        tls_ctx_options = io.TlsContextOptions.create_client_with_mtls_from_path(
            args.cert, args.key)
    else:
        tls_ctx_options = io.TlsContextOptions()

    if args.cacert is not None or args.capath is not None:
        tls_ctx_options.override_default_trust_store_from_path(
            args.capath, args.cacert)

    if args.insecure:
        tls_ctx_options.verify_peer = False

    tls_ctx = io.ClientTlsContext(tls_ctx_options)

    tls_connection_options = tls_ctx.new_connection_options()
    tls_connection_options.set_server_name(url.hostname)

    if args.alpn:
        tls_connection_options.set_alpn_list(args.alpn)

# invoked up on the connection closing


def on_connection_shutdown(shutdown_future):
    print('connection close with error: {}'.format(
        shutdown_future.exception()))