15
15
import copy
16
16
import json
17
17
import logging
18
+ import os
18
19
import ssl
19
20
from json import JSONDecodeError
20
21
from typing import Any , AsyncGenerator
@@ -70,10 +71,34 @@ def __init__(self, config: HttpConfiguration):
70
71
self .headers = {"Content-Type" : "application/json" }
71
72
i = self .config .timeout
72
73
self ._timeout = int (i ) if i is not None else None
74
+ # Help ssl.create_default_context() find mounted certificates
75
+ self ._setup_ssl_context ()
73
76
74
77
def task_gen_timeout (self , task_count = 1 ):
75
78
return self ._timeout * task_count if self ._timeout else None
76
79
80
+ def _setup_ssl_context (self ):
81
+ """Let ssl.create_default_context() discover certs.
82
+ Following container best practices - use environment variables to help
83
+ Python's default SSL context find mounted certificates automatically.
84
+ This avoids explicit certificate path management in application code.
85
+ """
86
+ if self .config .verify_ssl :
87
+ # Check for mounted service-ca certificate (container/K8s pattern)
88
+ service_ca = settings .SERVICE_CA_PATH
89
+ if os .path .exists (service_ca ):
90
+ os .environ .setdefault ("REQUESTS_CA_BUNDLE" , service_ca )
91
+ os .environ .setdefault ("SSL_CERT_FILE" , service_ca )
92
+ logger .info ("Configured SSL context to use mounted service-ca certificate" )
93
+
94
+ def get_ssl_verification (self ):
95
+ """Just return verify_ssl boolean.
96
+ ssl.create_default_context() will automatically discover certificates
97
+ via environment variables set in _setup_ssl_context().
98
+ No explicit certificate path management needed.
99
+ """
100
+ return self .config .verify_ssl
101
+
77
102
78
103
@Register (api_type = "http" )
79
104
class HttpCompletionsPipeline (HttpMetaData , ModelPipelineCompletions [HttpConfiguration ]):
@@ -97,9 +122,7 @@ def invoke(self, params: CompletionsParameters) -> CompletionsResponse:
97
122
headers = self .headers ,
98
123
json = model_input ,
99
124
timeout = self .task_gen_timeout (task_count ),
100
- verify = (
101
- self .config .ca_cert_file if self .config .ca_cert_file else self .config .verify_ssl
102
- ),
125
+ verify = self .get_ssl_verification (),
103
126
)
104
127
result .raise_for_status ()
105
128
response = json .loads (result .text )
@@ -119,9 +142,7 @@ def self_test(self) -> HealthCheckSummary:
119
142
try :
120
143
res = requests .get (
121
144
url ,
122
- verify = (
123
- self .config .ca_cert_file if self .config .ca_cert_file else self .config .verify_ssl
124
- ),
145
+ verify = self .get_ssl_verification (),
125
146
timeout = 1 ,
126
147
)
127
148
res .raise_for_status ()
@@ -155,9 +176,7 @@ def self_test(self) -> HealthCheckSummary:
155
176
self .config .inference_url + "/readiness" ,
156
177
headers = headers ,
157
178
timeout = 1 ,
158
- verify = (
159
- self .config .ca_cert_file if self .config .ca_cert_file else self .config .verify_ssl
160
- ),
179
+ verify = self .get_ssl_verification (),
161
180
)
162
181
r .raise_for_status ()
163
182
@@ -214,7 +233,7 @@ def invoke(self, params: ChatBotParameters) -> ChatBotResponse:
214
233
headers = self .headers ,
215
234
json = data ,
216
235
timeout = self .task_gen_timeout (1 ),
217
- verify = self .config . ca_cert_file if self . config . ca_cert_file else self . config . verify_ssl ,
236
+ verify = self .get_ssl_verification () ,
218
237
)
219
238
220
239
if response .status_code == 200 :
@@ -277,9 +296,8 @@ def send_schema1_event(self, ev):
277
296
278
297
async def async_invoke (self , params : StreamingChatBotParameters ) -> AsyncGenerator :
279
298
280
- # Configure SSL context based on verify_ssl setting
281
- if self .config .ca_cert_file :
282
- ssl_context = ssl .create_default_context (cafile = self .config .ca_cert_file )
299
+ if self .config .verify_ssl :
300
+ ssl_context = ssl .create_default_context ()
283
301
connector = aiohttp .TCPConnector (ssl = ssl_context )
284
302
else :
285
303
connector = aiohttp .TCPConnector (ssl = self .config .verify_ssl )
0 commit comments