|
1 | 1 | # type: ignore
|
2 |
| - |
| 2 | +import sys |
| 3 | +from traceback import print_exception |
3 | 4 |
|
4 | 5 | from rabbitmq_amqp_python_client import (
|
5 | 6 | AddressHelper,
|
6 | 7 | AMQPMessagingHandler,
|
7 | 8 | Connection,
|
| 9 | + CurrentUserStore, |
8 | 10 | Environment,
|
9 | 11 | Event,
|
10 | 12 | ExchangeSpecification,
|
11 | 13 | ExchangeToQueueBindingSpecification,
|
| 14 | + LocalMachineStore, |
12 | 15 | Message,
|
| 16 | + PKCS12Store, |
13 | 17 | PosixClientCert,
|
14 | 18 | PosixSslConfigurationContext,
|
15 | 19 | QuorumQueueSpecification,
|
| 20 | + WinClientCert, |
| 21 | + WinSslConfigurationContext, |
| 22 | +) |
| 23 | +from rabbitmq_amqp_python_client.ssl_configuration import ( |
| 24 | + FriendlyName, |
16 | 25 | )
|
17 | 26 |
|
18 | 27 | messages_to_publish = 100
|
@@ -74,20 +83,75 @@ def main() -> None:
|
74 | 83 | exchange_name = "test-exchange"
|
75 | 84 | queue_name = "example-queue"
|
76 | 85 | routing_key = "routing-key"
|
| 86 | + ca_p12_store = ".ci/certs/ca.p12" |
77 | 87 | ca_cert_file = ".ci/certs/ca_certificate.pem"
|
78 | 88 | client_cert = ".ci/certs/client_certificate.pem"
|
79 | 89 | 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 | + ) |
80 | 152 |
|
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) |
91 | 155 |
|
92 | 156 | management = connection.management()
|
93 | 157 |
|
|
0 commit comments