Skip to content

Commit c59db4d

Browse files
committed
Add SSL support for database connections
1 parent b10c905 commit c59db4d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@
5252
DB_PASSWORD = os.getenv("DB_PASSWORD")
5353
DB_NAME = os.getenv("DB_NAME")
5454

55+
# --- SSL Configuration ---
56+
DB_SSL = os.getenv("DB_SSL", "false").lower() == "true"
57+
DB_SSL_CA = os.getenv("DB_SSL_CA") # Path to CA certificate
58+
DB_SSL_CERT = os.getenv("DB_SSL_CERT") # Path to client certificate
59+
DB_SSL_KEY = os.getenv("DB_SSL_KEY") # Path to client private key
60+
DB_SSL_VERIFY_CERT = os.getenv("DB_SSL_VERIFY_CERT", "true").lower() == "true"
61+
DB_SSL_VERIFY_IDENTITY = os.getenv("DB_SSL_VERIFY_IDENTITY", "false").lower() == "true"
62+
5563
# --- MCP Server Configuration ---
5664
# Read-only mode
5765
MCP_READ_ONLY = os.getenv("MCP_READ_ONLY", "true").lower() == "true"

src/server.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import argparse
55
from typing import List, Dict, Any, Optional
66
from functools import partial
7+
import os
8+
import ssl
79

810
import asyncmy
911
import anyio
@@ -12,6 +14,7 @@
1214
# Import configuration settings
1315
from 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

Comments
 (0)