Created
November 10, 2020 10:56
-
-
Save borislitvak/6ccea503abf1b2f9c89e87309d6dab88 to your computer and use it in GitHub Desktop.
ApacheMQ TLS connection in Python (stomp.py, AmazonMQ, SSL, TLS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Key point: Stomp.py must get the key files, does not work without them. Even stomp.py test_ssl unit test does not work without them. | |
# First: Generate the keys in cmdline. | |
# openssl req -newkey rsa:2048 -nodes -keyout privateKey.key -x509 -days 365 -out certificate.pem | |
# I am not a Python expert, so don't bash me on this :) | |
# Based on https://forums.aws.amazon.com/thread.jspa?messageID=885374 | |
import ssl | |
import stomp | |
# import other things, this is an excerpt from my scaffolding code | |
def send_request_message(req: ds.RequestToProcess, settings: SettingsService) -> None: | |
""" Used for testing """ | |
data = req.to_json() | |
logger.info(f'Sending {data}') | |
with __create_connection(settings) as conn: | |
conn.connect(settings.queue_username, settings.queue_password, wait=True) | |
conn.send(body=data, destination=settings.source_queue_name) | |
def get_default_stomp_host_port_list(settings: SettingsService) -> Tuple[str, int]: | |
return [(settings.queue_hostname, settings.queue_stomp_port)] | |
def __create_connection(settings: SettingsService): | |
conn = stomp.Connection(get_default_stomp_host_port_list(settings)) | |
if settings.use_queue_ssl: | |
conn.set_ssl(for_hosts=[(settings.queue_hostname, settings.queue_stomp_port)], | |
cert_file='certificate.pem', | |
key_file='privateKey.key', | |
ssl_version=ssl.PROTOCOL_TLSv1_2) | |
return conn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment