SJS-HUB Claude commited on
Commit
60c83cd
·
1 Parent(s): 68204b3

Comprehensive SSL/TLS fix: Use Python certifi for MongoDB certificate verification

Browse files

Changes:
1. Dockerfile: Switch from python:3.11-slim to python:3.11-bullseye for fuller SSL support
2. Dockerfile: Run update-ca-certificates --fresh to ensure cert store is initialized
3. Dockerfile: Set SSL_CERT_FILE, SSL_CERT_DIR, REQUESTS_CA_BUNDLE env vars
4. database_mongo.py: Import certifi and ssl modules
5. database_mongo.py: Use certifi.where() to get proper CA cert bundle
6. database_mongo.py: Pass tlsCAFile parameter to MongoClient for explicit cert verification
7. requirements.txt: Add certifi>=2023.0.0 dependency

This approach uses Python's certifi package (standard for SSL) instead of relying
on system certs alone, which provides better cross-platform compatibility.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (3) hide show
  1. Dockerfile +11 -3
  2. database_mongo.py +9 -1
  3. requirements.txt +1 -0
Dockerfile CHANGED
@@ -1,19 +1,27 @@
1
  # Dockerfile for Hugging Face Spaces - DhammaAI
2
- FROM python:3.11-slim
 
3
 
4
  # Set working directory
5
  WORKDIR /app
6
 
7
- # Install system dependencies including SSL certificates for MongoDB
8
  RUN apt-get update && apt-get install -y \
9
  build-essential \
10
  curl \
11
  ca-certificates \
12
  libssl-dev \
13
  openssl \
14
- && update-ca-certificates \
 
 
15
  && rm -rf /var/lib/apt/lists/*
16
 
 
 
 
 
 
17
  # Copy requirements first for better caching
18
  COPY requirements.txt .
19
 
 
1
  # Dockerfile for Hugging Face Spaces - DhammaAI
2
+ # Use full Debian base instead of slim for better SSL support
3
+ FROM python:3.11-bullseye
4
 
5
  # Set working directory
6
  WORKDIR /app
7
 
8
+ # Install comprehensive system dependencies for SSL/TLS and MongoDB
9
  RUN apt-get update && apt-get install -y \
10
  build-essential \
11
  curl \
12
  ca-certificates \
13
  libssl-dev \
14
  openssl \
15
+ wget \
16
+ git \
17
+ && update-ca-certificates --fresh \
18
  && rm -rf /var/lib/apt/lists/*
19
 
20
+ # Explicitly set SSL certificate environment variables
21
+ ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
22
+ ENV SSL_CERT_DIR=/etc/ssl/certs
23
+ ENV REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
24
+
25
  # Copy requirements first for better caching
26
  COPY requirements.txt .
27
 
database_mongo.py CHANGED
@@ -11,6 +11,8 @@ from datetime import datetime
11
  from typing import List, Dict, Optional
12
  from bson.objectid import ObjectId
13
  from urllib.parse import quote_plus
 
 
14
 
15
  load_dotenv()
16
 
@@ -49,12 +51,18 @@ class MongoDBManager:
49
 
50
  # Connect to MongoDB Atlas with proper error handling
51
  print(f"[INFO] Connecting to MongoDB Atlas (timeout: 10s)...")
 
 
 
 
 
52
  self.client = MongoClient(
53
  mongo_uri_to_use,
54
  serverSelectionTimeoutMS=10000,
55
  connectTimeoutMS=10000,
56
  socketTimeoutMS=10000,
57
- retryWrites=True
 
58
  )
59
 
60
  # Test connection with proper error messaging
 
11
  from typing import List, Dict, Optional
12
  from bson.objectid import ObjectId
13
  from urllib.parse import quote_plus
14
+ import ssl
15
+ import certifi
16
 
17
  load_dotenv()
18
 
 
51
 
52
  # Connect to MongoDB Atlas with proper error handling
53
  print(f"[INFO] Connecting to MongoDB Atlas (timeout: 10s)...")
54
+
55
+ # Use certifi's certificate bundle for SSL verification
56
+ ca_certs = certifi.where()
57
+ print(f"[INFO] Using CA certificate bundle from: {ca_certs}")
58
+
59
  self.client = MongoClient(
60
  mongo_uri_to_use,
61
  serverSelectionTimeoutMS=10000,
62
  connectTimeoutMS=10000,
63
  socketTimeoutMS=10000,
64
+ retryWrites=True,
65
+ tlsCAFile=ca_certs
66
  )
67
 
68
  # Test connection with proper error messaging
requirements.txt CHANGED
@@ -27,6 +27,7 @@ rank-bm25>=0.2.2
27
  # MongoDB database (FREE - Atlas)
28
  pymongo>=4.0.0
29
  dnspython>=2.0.0
 
30
 
31
  # Data export to Excel (FREE)
32
  pandas>=2.0.0
 
27
  # MongoDB database (FREE - Atlas)
28
  pymongo>=4.0.0
29
  dnspython>=2.0.0
30
+ certifi>=2023.0.0 # SSL certificate verification for MongoDB Atlas
31
 
32
  # Data export to Excel (FREE)
33
  pandas>=2.0.0