44import argparse
55from typing import List , Dict , Any , Optional
66from functools import partial
7+ import os
8+ import ssl
79
810import asyncmy
911import anyio
1214# Import configuration settings
1315from config import (
1416 DB_HOST , DB_PORT , DB_USER , DB_PASSWORD , DB_NAME ,
17+ DB_SSL , DB_SSL_CA , DB_SSL_CERT , DB_SSL_KEY , DB_SSL_VERIFY_CERT , DB_SSL_VERIFY_IDENTITY ,
1518 MCP_READ_ONLY , MCP_MAX_POOL_SIZE , EMBEDDING_PROVIDER ,
1619 logger
1720)
@@ -72,6 +75,39 @@ async def initialize_pool(self):
7275
7376 try :
7477 logger .info (f"Creating connection pool for { DB_USER } @{ DB_HOST } :{ DB_PORT } /{ DB_NAME } (max size: { MCP_MAX_POOL_SIZE } )" )
78+
79+ if DB_SSL :
80+ ssl_context = ssl .create_default_context ()
81+ if DB_SSL_CA :
82+ if os .path .exists (DB_SSL_CA ):
83+ ssl_context .load_verify_locations (cafile = DB_SSL_CA )
84+ logger .info (f"Loaded SSL CA certificate: { DB_SSL_CA } " )
85+ else :
86+ logger .warning (f"SSL CA certificate file not found: { DB_SSL_CA } " )
87+
88+ if DB_SSL_CERT and DB_SSL_KEY :
89+ if os .path .exists (DB_SSL_CERT ) and os .path .exists (DB_SSL_KEY ):
90+ ssl_context .load_cert_chain (DB_SSL_CERT , DB_SSL_KEY )
91+ logger .info (f"Loaded SSL client certificate: { DB_SSL_CERT } " )
92+ else :
93+ logger .warning (f"SSL client certificate files not found: cert={ DB_SSL_CERT } , key={ DB_SSL_KEY } " )
94+
95+ if not DB_SSL_VERIFY_CERT :
96+ ssl_context .check_hostname = False
97+ ssl_context .verify_mode = ssl .CERT_NONE
98+ logger .info ("SSL certificate verification disabled" )
99+ elif not DB_SSL_VERIFY_IDENTITY :
100+ ssl_context .check_hostname = False
101+ ssl_context .verify_mode = ssl .CERT_REQUIRED
102+ logger .info ("SSL hostname verification disabled, certificate verification enabled" )
103+ else :
104+ logger .info ("Full SSL verification enabled" )
105+
106+ logger .info ("SSL enabled for database connection" )
107+ else :
108+ ssl_context = None
109+ logger .info ("SSL disabled for database connection" )
110+
75111 self .pool = await asyncmy .create_pool (
76112 host = DB_HOST ,
77113 port = DB_PORT ,
@@ -81,7 +117,8 @@ async def initialize_pool(self):
81117 minsize = 1 ,
82118 maxsize = MCP_MAX_POOL_SIZE ,
83119 autocommit = self .autocommit ,
84- pool_recycle = 3600
120+ pool_recycle = 3600 ,
121+ ssl = ssl_context
85122 )
86123 logger .info ("Connection pool initialized successfully." )
87124 except AsyncMyError as e :
0 commit comments