|
1 | 1 | import json
|
| 2 | +import os |
| 3 | +from urllib.parse import urlparse |
2 | 4 |
|
3 | 5 | from flask import current_app
|
4 | 6 | from requests import HTTPError, RequestException, request
|
@@ -58,26 +60,33 @@ def send_complaint_to_service(self, complaint_data):
|
58 | 60 | def _send_data_to_service_callback_api(self, data, service_callback_url, token, function_name):
|
59 | 61 | notification_id = data["notification_id"] if "notification_id" in data else data["id"]
|
60 | 62 | try:
|
61 |
| - ssl_crt = current_app.config["SSL_CLIENT_OVERRIDE_CERT"] |
62 |
| - |
63 |
| - if ssl_crt: |
64 |
| - response = request( |
65 |
| - method="POST", |
66 |
| - url=service_callback_url, |
67 |
| - data=json.dumps(data), |
68 |
| - headers={"Content-Type": "application/json", "Authorization": f"Bearer {token}"}, |
69 |
| - cert=ssl_crt, |
70 |
| - timeout=5, |
| 63 | + request_kwargs = { |
| 64 | + "method": "POST", |
| 65 | + "url": service_callback_url, |
| 66 | + "data": json.dumps(data), |
| 67 | + "headers": {"Content-Type": "application/json", "Authorization": f"Bearer {token}"}, |
| 68 | + "timeout": 5, |
| 69 | + } |
| 70 | + |
| 71 | + converted_url = urlparse(service_callback_url).hostname.replace(".", "-") |
| 72 | + certificate_name = f"{converted_url}.pem" |
| 73 | + |
| 74 | + certificate_path = f"{current_app.config['SSL_CERT_DIR']}/{certificate_name}" |
| 75 | + |
| 76 | + if os.path.exists(certificate_path): |
| 77 | + current_app.logger.info( |
| 78 | + "Certificate [%s] found for [%s] , using as client certificate.", certificate_name, service_callback_url |
71 | 79 | )
|
| 80 | + request_kwargs["cert"] = certificate_path |
72 | 81 | else:
|
73 |
| - response = request( |
74 |
| - method="POST", |
75 |
| - url=service_callback_url, |
76 |
| - data=json.dumps(data), |
77 |
| - headers={"Content-Type": "application/json", "Authorization": f"Bearer {token}"}, |
78 |
| - timeout=5, |
| 82 | + current_app.logger.warning( |
| 83 | + "Certificate [%s] not found for [%s], no client certificate used.", |
| 84 | + certificate_name, |
| 85 | + service_callback_url, |
79 | 86 | )
|
80 | 87 |
|
| 88 | + response = request(**request_kwargs) |
| 89 | + |
81 | 90 | current_app.logger.info(
|
82 | 91 | "%s sending %s to %s, response %s",
|
83 | 92 | function_name,
|
|
0 commit comments