3737from pymongo .mongo_client import MongoClient
3838from pymongo .server_api import ServerApi
3939from pymongo .errors import ConnectionFailure , ServerSelectionTimeoutError , OperationFailure
40+ import certifi # Added import for certifi
4041
4142# Configure logging
4243logging .basicConfig (level = logging .INFO , format = '%(asctime)s - %(levelname)s - %(message)s' )
@@ -120,11 +121,15 @@ def __init__(self, uri: str = None, database: str = None):
120121 uri (str, optional): MongoDB connection URI. Defaults to environment variable.
121122 database (str, optional): MongoDB database name. Defaults to environment variable or "guardian_ai".
122123 """
123- # Get connection info - first try parameters, then environment vars/secrets
124- connection_info = get_mongodb_connection_info ()
125-
126- self .uri = uri or connection_info ['uri' ]
127- self .database_name = database or connection_info ['database' ]
124+ if uri and database :
125+ self .uri = uri
126+ self .database_name = database
127+ logger .info (f"GuardianModelDistribution initialized with provided URI and database: { database } " )
128+ else :
129+ # Only fetch from env/secrets if not provided explicitly
130+ connection_info = get_mongodb_connection_info ()
131+ self .uri = uri or connection_info ['uri' ]
132+ self .database_name = database or connection_info ['database' ]
128133
129134 if not self .uri :
130135 logger .warning ("⚠️ MongoDB URI not provided. Set MONGODB_URI environment variable." )
@@ -150,15 +155,18 @@ def connect(self) -> bool:
150155 logger .info ("🔗 Connecting to MongoDB for model distribution..." )
151156
152157 # Create client with more robust connection parameters
158+ # Using the correct parameters for newer pymongo versions
153159 self .client = MongoClient (
154160 self .uri ,
155161 server_api = ServerApi ('1' ),
156162 serverSelectionTimeoutMS = 5000 , # 5 second timeout
157163 connectTimeoutMS = 10000 , # 10 second connection timeout
158164 socketTimeoutMS = 45000 , # 45 second socket timeout
159- ssl = True ,
160- ssl_cert_reqs = ssl .CERT_NONE , # Skip certificate validation for CI/CD
161- tlsAllowInvalidCertificates = True # Allow invalid certificates
165+ # TLS/SSL settings for newer pymongo versions
166+ tls = True , # Enable TLS/SSL
167+ tlsCAFile = certifi .where (), # Use certifi's CA bundle
168+ tlsAllowInvalidCertificates = True , # Allow self-signed certificates (if needed for some envs)
169+ tlsAllowInvalidHostnames = True # Allow hostname mismatch (if needed for some envs)
162170 )
163171
164172 # Test connection with timeout
0 commit comments