Skip to content

Commit 0c7f845

Browse files
committed
Update tls_example.py
1 parent 140e949 commit 0c7f845

File tree

1 file changed

+75
-11
lines changed

1 file changed

+75
-11
lines changed

examples/tls/tls_example.py

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
# type: ignore
2-
2+
import sys
3+
from traceback import print_exception
34

45
from rabbitmq_amqp_python_client import (
56
AddressHelper,
67
AMQPMessagingHandler,
78
Connection,
9+
CurrentUserStore,
810
Environment,
911
Event,
1012
ExchangeSpecification,
1113
ExchangeToQueueBindingSpecification,
14+
LocalMachineStore,
1215
Message,
16+
PKCS12Store,
1317
PosixClientCert,
1418
PosixSslConfigurationContext,
1519
QuorumQueueSpecification,
20+
WinClientCert,
21+
WinSslConfigurationContext,
22+
)
23+
from rabbitmq_amqp_python_client.ssl_configuration import (
24+
FriendlyName,
1625
)
1726

1827
messages_to_publish = 100
@@ -74,20 +83,75 @@ def main() -> None:
7483
exchange_name = "test-exchange"
7584
queue_name = "example-queue"
7685
routing_key = "routing-key"
86+
ca_p12_store = ".ci/certs/ca.p12"
7787
ca_cert_file = ".ci/certs/ca_certificate.pem"
7888
client_cert = ".ci/certs/client_certificate.pem"
7989
client_key = ".ci/certs/client_key.pem"
90+
client_p12_store = ".ci/certs/client.p12"
91+
uri = "amqps://guest:guest@localhost:5671/"
92+
93+
if sys.platform == "win32":
94+
ca_stores = [
95+
# names for the current user and local machine are not
96+
# case-sensitive
97+
CurrentUserStore(name="Root"),
98+
LocalMachineStore(name="Root"),
99+
PKCS12Store(path=ca_p12_store),
100+
]
101+
client_stores = [
102+
# `personal` is treated as an alias for `my` by qpid proton
103+
# Recommended read:
104+
# https://github.com/apache/qpid-proton/blob/2847000fbb3732e80537e3c3ff5e097bb95bfae0/c/src/ssl/PLATFORM_NOTES.md
105+
CurrentUserStore(name="Personal"),
106+
LocalMachineStore(name="my"),
107+
PKCS12Store(path=client_p12_store),
108+
]
109+
110+
for ca_store, client_store in zip(ca_stores, client_stores):
111+
ssl_context = WinSslConfigurationContext(
112+
ca_store=ca_store,
113+
client_cert=WinClientCert(
114+
store=client_store,
115+
# qpid proton uses Windows constant CERT_NAME_FRIENDLY_DISPLAY_TYPE
116+
# to retrieve the value which is compare to the one we provide
117+
# If certificates have no friendly name Windows falls back to
118+
# CERT_NAME_SIMPLE_DISPLAY_TYPE which has further fallbacks
119+
# https://learn.microsoft.com/en-us/windows/win32/api/wincrypt/nf-wincrypt-certgetnamestringa
120+
disambiguation_method=FriendlyName("1"),
121+
password=None,
122+
),
123+
)
124+
environment = Environment(
125+
uri,
126+
ssl_context=ssl_context,
127+
)
128+
129+
try:
130+
print("connection to amqp server")
131+
connection = create_connection(environment)
132+
break
133+
except Exception as e:
134+
print_exception(e)
135+
continue
136+
else:
137+
raise RuntimeError(
138+
"connection failed. working directory should be project root"
139+
)
140+
else:
141+
environment = Environment(
142+
uri,
143+
ssl_context=PosixSslConfigurationContext(
144+
ca_cert=ca_cert_file,
145+
client_cert=PosixClientCert(
146+
client_cert=client_cert,
147+
client_key=client_key,
148+
password=None,
149+
),
150+
),
151+
)
80152

81-
environment = Environment(
82-
"amqps://guest:guest@localhost:5671/",
83-
ssl_context=PosixSslConfigurationContext(
84-
ca_cert=ca_cert_file,
85-
client_cert=PosixClientCert(client_cert=client_cert, client_key=client_key),
86-
),
87-
)
88-
89-
print("connection to amqp server")
90-
connection = create_connection(environment)
153+
print("connection to amqp server")
154+
connection = create_connection(environment)
91155

92156
management = connection.management()
93157

0 commit comments

Comments
 (0)