embed-client 2.0.0.0__py3-none-any.whl → 3.1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- embed_client/async_client.py +376 -16
- embed_client/auth.py +491 -0
- embed_client/auth_examples.py +248 -0
- embed_client/client_factory.py +396 -0
- embed_client/client_factory_examples.py +353 -0
- embed_client/config.py +592 -0
- embed_client/config_examples.py +197 -0
- embed_client/example_async_usage.py +578 -90
- embed_client/example_async_usage_ru.py +536 -102
- embed_client/ssl_examples.py +329 -0
- embed_client/ssl_manager.py +475 -0
- embed_client-3.1.0.1.dist-info/METADATA +256 -0
- embed_client-3.1.0.1.dist-info/RECORD +17 -0
- embed_client-3.1.0.1.dist-info/licenses/LICENSE +21 -0
- embed_client-2.0.0.0.dist-info/METADATA +0 -9
- embed_client-2.0.0.0.dist-info/RECORD +0 -8
- {embed_client-2.0.0.0.dist-info → embed_client-3.1.0.1.dist-info}/WHEEL +0 -0
- {embed_client-2.0.0.0.dist-info → embed_client-3.1.0.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,329 @@
|
|
1
|
+
"""
|
2
|
+
SSL/TLS examples for embed-client.
|
3
|
+
|
4
|
+
This module provides examples of how to use the SSL/TLS manager
|
5
|
+
with different security modes and configurations.
|
6
|
+
|
7
|
+
Author: Vasiliy Zdanovskiy
|
8
|
+
email: vasilyvz@gmail.com
|
9
|
+
"""
|
10
|
+
|
11
|
+
from typing import Dict, Any
|
12
|
+
from embed_client.ssl_manager import ClientSSLManager, create_ssl_manager, create_ssl_context, create_connector
|
13
|
+
|
14
|
+
|
15
|
+
def get_http_config() -> Dict[str, Any]:
|
16
|
+
"""
|
17
|
+
Get example configuration for HTTP (no SSL).
|
18
|
+
|
19
|
+
Returns:
|
20
|
+
Configuration dictionary for HTTP
|
21
|
+
"""
|
22
|
+
return {
|
23
|
+
"ssl": {
|
24
|
+
"enabled": False
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
|
29
|
+
def get_https_config() -> Dict[str, Any]:
|
30
|
+
"""
|
31
|
+
Get example configuration for HTTPS.
|
32
|
+
|
33
|
+
Returns:
|
34
|
+
Configuration dictionary for HTTPS
|
35
|
+
"""
|
36
|
+
return {
|
37
|
+
"ssl": {
|
38
|
+
"enabled": True,
|
39
|
+
"verify_mode": "CERT_REQUIRED",
|
40
|
+
"check_hostname": True,
|
41
|
+
"check_expiry": True
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
def get_https_with_ca_config() -> Dict[str, Any]:
|
47
|
+
"""
|
48
|
+
Get example configuration for HTTPS with custom CA.
|
49
|
+
|
50
|
+
Returns:
|
51
|
+
Configuration dictionary for HTTPS with CA
|
52
|
+
"""
|
53
|
+
return {
|
54
|
+
"ssl": {
|
55
|
+
"enabled": True,
|
56
|
+
"verify_mode": "CERT_REQUIRED",
|
57
|
+
"check_hostname": True,
|
58
|
+
"check_expiry": True,
|
59
|
+
"ca_cert_file": "certs/ca.crt"
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
def get_mtls_config() -> Dict[str, Any]:
|
65
|
+
"""
|
66
|
+
Get example configuration for mTLS (mutual TLS).
|
67
|
+
|
68
|
+
Returns:
|
69
|
+
Configuration dictionary for mTLS
|
70
|
+
"""
|
71
|
+
return {
|
72
|
+
"ssl": {
|
73
|
+
"enabled": True,
|
74
|
+
"verify_mode": "CERT_REQUIRED",
|
75
|
+
"check_hostname": True,
|
76
|
+
"check_expiry": True,
|
77
|
+
"cert_file": "certs/client.crt",
|
78
|
+
"key_file": "keys/client.key",
|
79
|
+
"ca_cert_file": "certs/ca.crt"
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
def get_mtls_no_verify_config() -> Dict[str, Any]:
|
85
|
+
"""
|
86
|
+
Get example configuration for mTLS without verification.
|
87
|
+
|
88
|
+
Returns:
|
89
|
+
Configuration dictionary for mTLS without verification
|
90
|
+
"""
|
91
|
+
return {
|
92
|
+
"ssl": {
|
93
|
+
"enabled": True,
|
94
|
+
"verify_mode": "CERT_NONE",
|
95
|
+
"check_hostname": False,
|
96
|
+
"check_expiry": False,
|
97
|
+
"cert_file": "certs/client.crt",
|
98
|
+
"key_file": "keys/client.key"
|
99
|
+
}
|
100
|
+
}
|
101
|
+
|
102
|
+
|
103
|
+
def demo_http_ssl():
|
104
|
+
"""Demonstrate HTTP SSL configuration (disabled)."""
|
105
|
+
print("=== HTTP SSL Configuration Demo ===")
|
106
|
+
|
107
|
+
config = get_http_config()
|
108
|
+
ssl_manager = create_ssl_manager(config)
|
109
|
+
|
110
|
+
print(f"SSL enabled: {ssl_manager.is_ssl_enabled()}")
|
111
|
+
print(f"mTLS enabled: {ssl_manager.is_mtls_enabled()}")
|
112
|
+
|
113
|
+
# Create SSL context
|
114
|
+
context = ssl_manager.create_client_ssl_context()
|
115
|
+
print(f"SSL context: {context}")
|
116
|
+
|
117
|
+
# Create connector
|
118
|
+
connector = ssl_manager.create_connector()
|
119
|
+
print(f"Connector: {connector}")
|
120
|
+
|
121
|
+
# Validate configuration
|
122
|
+
errors = ssl_manager.validate_ssl_config()
|
123
|
+
print(f"Validation errors: {errors}")
|
124
|
+
|
125
|
+
|
126
|
+
def demo_https_ssl():
|
127
|
+
"""Demonstrate HTTPS SSL configuration."""
|
128
|
+
print("\n=== HTTPS SSL Configuration Demo ===")
|
129
|
+
|
130
|
+
config = get_https_config()
|
131
|
+
ssl_manager = create_ssl_manager(config)
|
132
|
+
|
133
|
+
print(f"SSL enabled: {ssl_manager.is_ssl_enabled()}")
|
134
|
+
print(f"mTLS enabled: {ssl_manager.is_mtls_enabled()}")
|
135
|
+
|
136
|
+
# Create SSL context
|
137
|
+
context = ssl_manager.create_client_ssl_context()
|
138
|
+
print(f"SSL context: {context}")
|
139
|
+
if context:
|
140
|
+
print(f" Verify mode: {context.verify_mode}")
|
141
|
+
print(f" Check hostname: {context.check_hostname}")
|
142
|
+
|
143
|
+
# Create connector
|
144
|
+
connector = ssl_manager.create_connector()
|
145
|
+
print(f"Connector: {connector}")
|
146
|
+
|
147
|
+
# Get supported protocols
|
148
|
+
protocols = ssl_manager.get_supported_protocols()
|
149
|
+
print(f"Supported protocols: {protocols}")
|
150
|
+
|
151
|
+
# Validate configuration
|
152
|
+
errors = ssl_manager.validate_ssl_config()
|
153
|
+
print(f"Validation errors: {errors}")
|
154
|
+
|
155
|
+
|
156
|
+
def demo_https_with_ca_ssl():
|
157
|
+
"""Demonstrate HTTPS SSL configuration with custom CA."""
|
158
|
+
print("\n=== HTTPS with CA SSL Configuration Demo ===")
|
159
|
+
|
160
|
+
config = get_https_with_ca_config()
|
161
|
+
ssl_manager = create_ssl_manager(config)
|
162
|
+
|
163
|
+
print(f"SSL enabled: {ssl_manager.is_ssl_enabled()}")
|
164
|
+
print(f"mTLS enabled: {ssl_manager.is_mtls_enabled()}")
|
165
|
+
|
166
|
+
# Create SSL context
|
167
|
+
context = ssl_manager.create_client_ssl_context()
|
168
|
+
print(f"SSL context: {context}")
|
169
|
+
if context:
|
170
|
+
print(f" Verify mode: {context.verify_mode}")
|
171
|
+
print(f" Check hostname: {context.check_hostname}")
|
172
|
+
|
173
|
+
# Validate configuration
|
174
|
+
errors = ssl_manager.validate_ssl_config()
|
175
|
+
print(f"Validation errors: {errors}")
|
176
|
+
|
177
|
+
|
178
|
+
def demo_mtls_ssl():
|
179
|
+
"""Demonstrate mTLS SSL configuration."""
|
180
|
+
print("\n=== mTLS SSL Configuration Demo ===")
|
181
|
+
|
182
|
+
config = get_mtls_config()
|
183
|
+
ssl_manager = create_ssl_manager(config)
|
184
|
+
|
185
|
+
print(f"SSL enabled: {ssl_manager.is_ssl_enabled()}")
|
186
|
+
print(f"mTLS enabled: {ssl_manager.is_mtls_enabled()}")
|
187
|
+
|
188
|
+
# Create SSL context
|
189
|
+
context = ssl_manager.create_client_ssl_context()
|
190
|
+
print(f"SSL context: {context}")
|
191
|
+
if context:
|
192
|
+
print(f" Verify mode: {context.verify_mode}")
|
193
|
+
print(f" Check hostname: {context.check_hostname}")
|
194
|
+
|
195
|
+
# Validate configuration
|
196
|
+
errors = ssl_manager.validate_ssl_config()
|
197
|
+
print(f"Validation errors: {errors}")
|
198
|
+
|
199
|
+
|
200
|
+
def demo_mtls_no_verify_ssl():
|
201
|
+
"""Demonstrate mTLS SSL configuration without verification."""
|
202
|
+
print("\n=== mTLS without Verification SSL Configuration Demo ===")
|
203
|
+
|
204
|
+
config = get_mtls_no_verify_config()
|
205
|
+
ssl_manager = create_ssl_manager(config)
|
206
|
+
|
207
|
+
print(f"SSL enabled: {ssl_manager.is_ssl_enabled()}")
|
208
|
+
print(f"mTLS enabled: {ssl_manager.is_mtls_enabled()}")
|
209
|
+
|
210
|
+
# Create SSL context
|
211
|
+
context = ssl_manager.create_client_ssl_context()
|
212
|
+
print(f"SSL context: {context}")
|
213
|
+
if context:
|
214
|
+
print(f" Verify mode: {context.verify_mode}")
|
215
|
+
print(f" Check hostname: {context.check_hostname}")
|
216
|
+
|
217
|
+
# Validate configuration
|
218
|
+
errors = ssl_manager.validate_ssl_config()
|
219
|
+
print(f"Validation errors: {errors}")
|
220
|
+
|
221
|
+
|
222
|
+
def demo_certificate_validation():
|
223
|
+
"""Demonstrate certificate validation."""
|
224
|
+
print("\n=== Certificate Validation Demo ===")
|
225
|
+
|
226
|
+
config = {"ssl": {"enabled": True}}
|
227
|
+
ssl_manager = create_ssl_manager(config)
|
228
|
+
|
229
|
+
# Test with non-existent certificate
|
230
|
+
result = ssl_manager.validate_certificate("nonexistent.crt")
|
231
|
+
print(f"Non-existent certificate validation: {result}")
|
232
|
+
|
233
|
+
# Test with invalid certificate
|
234
|
+
result = ssl_manager.validate_certificate("invalid.crt")
|
235
|
+
print(f"Invalid certificate validation: {result}")
|
236
|
+
|
237
|
+
|
238
|
+
def demo_ssl_context_creation():
|
239
|
+
"""Demonstrate SSL context creation using factory functions."""
|
240
|
+
print("\n=== SSL Context Creation Demo ===")
|
241
|
+
|
242
|
+
# HTTP (no SSL)
|
243
|
+
config = get_http_config()
|
244
|
+
context = create_ssl_context(config)
|
245
|
+
print(f"HTTP SSL context: {context}")
|
246
|
+
|
247
|
+
# HTTPS
|
248
|
+
config = get_https_config()
|
249
|
+
context = create_ssl_context(config)
|
250
|
+
print(f"HTTPS SSL context: {context}")
|
251
|
+
|
252
|
+
# mTLS
|
253
|
+
config = get_mtls_config()
|
254
|
+
context = create_ssl_context(config)
|
255
|
+
print(f"mTLS SSL context: {context}")
|
256
|
+
|
257
|
+
|
258
|
+
def demo_connector_creation():
|
259
|
+
"""Demonstrate connector creation using factory functions."""
|
260
|
+
print("\n=== Connector Creation Demo ===")
|
261
|
+
|
262
|
+
# HTTP (no SSL)
|
263
|
+
config = get_http_config()
|
264
|
+
connector = create_connector(config)
|
265
|
+
print(f"HTTP connector: {connector}")
|
266
|
+
|
267
|
+
# HTTPS
|
268
|
+
config = get_https_config()
|
269
|
+
connector = create_connector(config)
|
270
|
+
print(f"HTTPS connector: {connector}")
|
271
|
+
|
272
|
+
# mTLS
|
273
|
+
config = get_mtls_config()
|
274
|
+
connector = create_connector(config)
|
275
|
+
print(f"mTLS connector: {connector}")
|
276
|
+
|
277
|
+
|
278
|
+
def demo_ssl_configuration_validation():
|
279
|
+
"""Demonstrate SSL configuration validation."""
|
280
|
+
print("\n=== SSL Configuration Validation Demo ===")
|
281
|
+
|
282
|
+
# Valid configuration
|
283
|
+
config = get_http_config()
|
284
|
+
ssl_manager = create_ssl_manager(config)
|
285
|
+
errors = ssl_manager.validate_ssl_config()
|
286
|
+
print(f"Valid config errors: {errors}")
|
287
|
+
|
288
|
+
# Invalid configuration (missing files)
|
289
|
+
config = get_mtls_config()
|
290
|
+
ssl_manager = create_ssl_manager(config)
|
291
|
+
errors = ssl_manager.validate_ssl_config()
|
292
|
+
print(f"Invalid config errors: {errors}")
|
293
|
+
|
294
|
+
|
295
|
+
def demo_ssl_capabilities():
|
296
|
+
"""Demonstrate SSL capabilities detection."""
|
297
|
+
print("\n=== SSL Capabilities Demo ===")
|
298
|
+
|
299
|
+
config = {"ssl": {"enabled": True}}
|
300
|
+
ssl_manager = create_ssl_manager(config)
|
301
|
+
|
302
|
+
protocols = ssl_manager.get_supported_protocols()
|
303
|
+
print(f"Supported protocols: {protocols}")
|
304
|
+
|
305
|
+
ssl_config = ssl_manager.get_ssl_config()
|
306
|
+
print(f"SSL configuration: {ssl_config}")
|
307
|
+
|
308
|
+
|
309
|
+
def run_all_demos():
|
310
|
+
"""Run all SSL/TLS demos."""
|
311
|
+
print("🔒 SSL/TLS Manager Examples")
|
312
|
+
print("=" * 50)
|
313
|
+
|
314
|
+
demo_http_ssl()
|
315
|
+
demo_https_ssl()
|
316
|
+
demo_https_with_ca_ssl()
|
317
|
+
demo_mtls_ssl()
|
318
|
+
demo_mtls_no_verify_ssl()
|
319
|
+
demo_certificate_validation()
|
320
|
+
demo_ssl_context_creation()
|
321
|
+
demo_connector_creation()
|
322
|
+
demo_ssl_configuration_validation()
|
323
|
+
demo_ssl_capabilities()
|
324
|
+
|
325
|
+
print("\n✅ All SSL/TLS demos completed!")
|
326
|
+
|
327
|
+
|
328
|
+
if __name__ == "__main__":
|
329
|
+
run_all_demos()
|