66 port: The port where the mqtt server is listening
77 username: The username to connect to the broker
88 password: The password to connect to the broker
9- cert_path: The directory containing certificate files.
10- Can be in root of repo or under rulebooks.
11- ca_certs: The filename of optional certificate authority file containing
9+ ca_certs: Multi-line string containing
1210 certificate used to sign mqtt broker certificates
1311 validate_certs: Disable certificate validation - true/false
14- certfile: The optional client certificate file name containing
12+ certfile: The optional multi-line string containing
1513 the client certificate, as well as CA certificates needed
1614 to establish the certificate's authenticity
17- keyfile: The optional client key file name containing the client
15+ keyfile: Multi-line string containing the client
1816 private key
1917 keyfile_password: The optional password to be used when loading the
2018 certificate chain
2523import asyncio
2624import json
2725import logging
28- import os
2926from typing import Any , Dict
3027
3128import aiomqtt
3229
3330
31+ async def write_certfile (path , content , logger ):
32+ """
33+ Function to write certificate data to a temporary file.
34+
35+ Args:
36+ path (str): Path to temporary file
37+ content (str): Certificate data
38+ logger (object): Logger object
39+ """
40+ with open (path , "w+" , encoding = 'utf-8' ) as certfile :
41+ certfile .writelines (content )
42+ logger .info ("Cert data written to %s" , path )
43+
3444async def main (queue : asyncio .Queue , args : Dict [str , Any ]):
3545 logger = logging .getLogger ()
3646
@@ -41,41 +51,36 @@ async def main(queue: asyncio.Queue, args: Dict[str, Any]):
4151 username = args .get ("username" )
4252 password = args .get ("password" )
4353
44- cert_path = args .get ("cert_path" )
4554 ca_certs = args .get ("ca_certs" )
4655 validate_certs = bool (args .get ("validate_certs" ))
4756 certfile = args .get ("certfile" )
4857 keyfile = args .get ("keyfile" )
4958 keyfile_password = args .get ("keyfile_password" )
5059
5160 # Path management for certificate files
52- # This solves an issue when using EDA server and finding file paths
53- path_to_certs = None
61+ # EDA Server does not support file handling with decision environments
62+ # We will accept the cert data as strings and write out temporary files
63+ # to pass when configuring TLS.
64+ path_to_certs = "/tmp"
5465 ca_certs_path = None
5566 certfile_path = None
5667 keyfile_path = None
5768
58- if cert_path :
59- # Find the absolute path to the ca_certs filename
60- for root , dirs , _ in os .walk ('./' , topdown = True ):
61- for dirname in dirs :
62- if cert_path in dirname :
63- path_to_certs = os .path .join (root , dirname )
64- logger .info ("Cert path found at %s" , path_to_certs )
65- break
66-
67- # Build out cert file absolute paths
69+ # Build out cert file and absolute paths
6870 if ca_certs and path_to_certs :
69- ca_certs_path = f'{ path_to_certs } /{ ca_certs } '
70- logger .info ("ca_certs path found at %s" , ca_certs_path )
71+ # Write Certificate to file
72+ ca_certs_path = f'{ path_to_certs } /ca_certs.crt'
73+ await write_certfile (ca_certs_path , ca_certs , logger )
7174
7275 if certfile and path_to_certs :
73- certfile_path = f'{ path_to_certs } /{ certfile } '
74- logger .info ("certfile path found at %s" , certfile_path )
76+ # Write Certificate to file
77+ certfile_path = f'{ path_to_certs } /certfile.crt'
78+ await write_certfile (certfile_path , certfile , logger )
7579
7680 if keyfile and path_to_certs :
77- keyfile_path = f'{ path_to_certs } /{ keyfile } '
78- logger .info ("keyfile path found at %s" , keyfile_path )
81+ # Write Certificate to file
82+ keyfile_path = f'{ path_to_certs } /keyfile.crt'
83+ await write_certfile (keyfile_path , keyfile , logger )
7984
8085 if ca_certs_path or certfile_path or keyfile_path :
8186 logger .info ("Certificates provided, setting tls_params..." )
0 commit comments