Connect to the Coordinator with TLS

Problem

You want to connect to a Coordinator requiring a secured connection.

Solution

If the Coordinator and Agent(s) are using secured communications, any client code submitting jobs must also enable the option to use SSL. To do this, set the ClusterJobScheduler’s use_ssl property to True. If using self-signed certificates, set the allow_self_signed_certificates property to True.

If the Coordinator and Agent(s) are using mutual SSL, any client code must provide a client certificate. To do this, set the ClusterJobScheduler’s provide_client_certificate property to True. Then, to specify a certificate, set the client_certificate_certfile property to the path of a certificate file.

To verify the Coordinator’s certificate, set the coordinator_thumbprint property to the Coordinator’s certificate thumbprint (found on the Coordinator’s connection settings page).

All of these properties need to be set in the ClusterJobScheduler’s initialization.

 1from agiparallel.client import ClusterJobScheduler
 2
 3
 4class HelloTask():
 5    def execute(self):
 6        self.result = "Hello from task"
 7
 8
 9def secure_connect():
10    with ClusterJobScheduler(hostname="localhost",
11                         use_ssl=True,
12                         allow_self_signed_certificates=False,
13                         provide_client_certificate=True,   # only needed if using mutual SSL
14                         coordinator_thumbprint="CoordinatorThumbprint",  # only needed if the coordinator certificate should be verified
15                         ) as scheduler:
16        scheduler.connect()
17        job = scheduler.create_job()
18        job.add_task(HelloTask())
19        job.submit()
20        job.wait_until_done()
21        print("Result: ", job.tasks[0].result)
22
23
24if __name__ == "__main__":
25    secure_connect()