Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit a6eed46

Browse files
committed
dynamic client ssl support
1 parent 0b890a1 commit a6eed46

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

app/celery/service_callback_tasks.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
import os
3+
from urllib.parse import urlparse
24

35
from flask import current_app
46
from requests import HTTPError, RequestException, request
@@ -58,26 +60,33 @@ def send_complaint_to_service(self, complaint_data):
5860
def _send_data_to_service_callback_api(self, data, service_callback_url, token, function_name):
5961
notification_id = data["notification_id"] if "notification_id" in data else data["id"]
6062
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
7179
)
80+
request_kwargs["cert"] = certificate_path
7281
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,
7986
)
8087

88+
response = request(**request_kwargs)
89+
8190
current_app.logger.info(
8291
"%s sending %s to %s, response %s",
8392
function_name,

app/config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,7 @@ class Development(Config):
499499

500500
CBC_PROXY_ENABLED = False
501501

502-
SSL_CLIENT_OVERRIDE_CERT = os.getenv("SSL_CLIENT_OVERRIDE_CERT")
503-
SSL_CLIENT_OVERRIDE_KEY = os.getenv("SSL_CLIENT_OVERRIDE_KEY")
504-
SSL_VERIFY_OVERRIDE = os.getenv("SSL_CERT_FILE")
502+
SSL_CERT_DIR = os.getenv("SSL_CERT_DIR")
505503

506504

507505
class Test(Development):

0 commit comments

Comments
 (0)