Skip to content

Commit e68da34

Browse files
committed
feat: implement internal API authentication mechanism
1 parent 9b9f957 commit e68da34

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

application/.env_sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
API_KEY=your_api_key
22
EMBEDDINGS_KEY=your_api_key
33
API_URL=http://localhost:7091
4+
INTERNAL_KEY=your_internal_key
45
FLASK_APP=application/app.py
56
FLASK_DEBUG=true
67

application/api/internal/routes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import datetime
33
import json
4-
from flask import Blueprint, request, send_from_directory
4+
from flask import Blueprint, request, send_from_directory, jsonify
55
from werkzeug.utils import secure_filename
66
from bson.objectid import ObjectId
77
import logging
@@ -24,6 +24,16 @@
2424
internal = Blueprint("internal", __name__)
2525

2626

27+
@internal.before_request
28+
def verify_internal_key():
29+
"""Verify INTERNAL_KEY for all internal endpoint requests."""
30+
if settings.INTERNAL_KEY:
31+
internal_key = request.headers.get("X-Internal-Key")
32+
if not internal_key or internal_key != settings.INTERNAL_KEY:
33+
logger.warning(f"Unauthorized internal API access attempt from {request.remote_addr}")
34+
return jsonify({"error": "Unauthorized", "message": "Invalid or missing internal key"}), 401
35+
36+
2737
@internal.route("/api/download", methods=["get"])
2838
def download_file():
2939
user = secure_filename(request.args.get("user"))

application/core/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Settings(BaseSettings):
6262
CACHE_REDIS_URL: str = "redis://localhost:6379/2"
6363

6464
API_URL: str = "http://localhost:7091" # backend url for celery worker
65+
INTERNAL_KEY: Optional[str] = None # internal api key for worker-to-backend auth
6566

6667
API_KEY: Optional[str] = None # LLM api key (used by LLM_PROVIDER)
6768

application/worker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def download_file(url, params, dest_path):
109109
def upload_index(full_path, file_data):
110110
files = None
111111
try:
112+
headers = {}
113+
if settings.INTERNAL_KEY:
114+
headers["X-Internal-Key"] = settings.INTERNAL_KEY
115+
112116
if settings.VECTOR_STORE == "faiss":
113117
faiss_path = full_path + "/index.faiss"
114118
pkl_path = full_path + "/index.pkl"
@@ -129,10 +133,13 @@ def upload_index(full_path, file_data):
129133
urljoin(settings.API_URL, "/api/upload_index"),
130134
files=files,
131135
data=file_data,
136+
headers=headers,
132137
)
133138
else:
134139
response = requests.post(
135-
urljoin(settings.API_URL, "/api/upload_index"), data=file_data
140+
urljoin(settings.API_URL, "/api/upload_index"),
141+
data=file_data,
142+
headers=headers,
136143
)
137144
response.raise_for_status()
138145
except (requests.RequestException, FileNotFoundError) as e:

deployment/docker-compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ services:
2626
- MONGO_URI=mongodb://mongo:27017/docsgpt
2727
- CACHE_REDIS_URL=redis://redis:6379/2
2828
- OPENAI_BASE_URL=$OPENAI_BASE_URL
29+
- INTERNAL_KEY=$INTERNAL_KEY
2930
ports:
3031
- "7091:7091"
3132
volumes:
@@ -50,6 +51,7 @@ services:
5051
- MONGO_URI=mongodb://mongo:27017/docsgpt
5152
- API_URL=http://backend:7091
5253
- CACHE_REDIS_URL=redis://redis:6379/2
54+
- INTERNAL_KEY=$INTERNAL_KEY
5355
volumes:
5456
- ../application/indexes:/app/indexes
5557
- ../application/inputs:/app/inputs

0 commit comments

Comments
 (0)