Removed DB query because this is in an internal service
This commit is contained in:
1 parent
3a3d78c59b
commit
aa5853f0fa
3 files changed
+2
-45
No files matched your search
@@ -4,24 +4,17 @@ aiormq==7.2.2
|
|||||||
annotated-doc==0.0.5
|
annotated-doc==0.0.5
|
||||||
annotated-types==0.8.0
|
annotated-types==0.8.0
|
||||||
anyio==4.15.1
|
anyio==4.15.1
|
||||||
argon2-cffi==25.1.0
|
|
||||||
argon2-cffi-bindings==26.1.0
|
|
||||||
asyncio==4.0.0
|
|
||||||
cffi==2.1.1
|
|
||||||
click==8.5.0
|
click==8.5.0
|
||||||
fastapi==0.143.0
|
fastapi==0.143.0
|
||||||
h11==0.16.0
|
h11==0.16.0
|
||||||
idna==3.20
|
idna==3.20
|
||||||
lib-db-module==0.2.0
|
|
||||||
lib-secret-manager==0.2.0
|
lib-secret-manager==0.2.0
|
||||||
lib-uvicorn-config==0.2.0
|
lib-uvicorn-config==0.2.0
|
||||||
multidict==7.1.0
|
multidict==7.1.0
|
||||||
mysql-connector-python==9.4.0
|
|
||||||
opentelemetry-api==1.45.1
|
opentelemetry-api==1.45.1
|
||||||
pamqp==4.0.1
|
pamqp==4.0.1
|
||||||
prometheus_client==0.26.0
|
prometheus_client==0.26.0
|
||||||
propcache==0.5.4
|
propcache==0.5.4
|
||||||
pycparser==3.11
|
|
||||||
pydantic==2.14.0
|
pydantic==2.14.0
|
||||||
pydantic_core==2.50.0
|
pydantic_core==2.50.0
|
||||||
python-json-logger==4.2.0
|
python-json-logger==4.2.0
|
||||||
|
|||||||
+2
-27
@@ -4,22 +4,19 @@ from contextlib import asynccontextmanager
|
|||||||
|
|
||||||
import uvicorn
|
import uvicorn
|
||||||
from aio_pika.exceptions import AMQPException
|
from aio_pika.exceptions import AMQPException
|
||||||
from fastapi import Depends, FastAPI, HTTPException, Request
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from fastapi.security.api_key import APIKeyHeader
|
from fastapi.security.api_key import APIKeyHeader
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread
|
|
||||||
from metrics_server import REQUEST_COUNTER
|
from metrics_server import REQUEST_COUNTER
|
||||||
from rabbitmq_handler import RabbitMQConsumer
|
from rabbitmq_handler import RabbitMQConsumer
|
||||||
from secret_manager import cleanup_secret_files
|
from secret_manager import cleanup_secret_files
|
||||||
from simple_logger_handler import LOG_LEVEL, setup_logger
|
from simple_logger_handler import LOG_LEVEL, setup_logger
|
||||||
from uvicorn_logger_config import LOGGING_CONFIG
|
from uvicorn_logger_config import LOGGING_CONFIG
|
||||||
from validator import verify_api_key
|
|
||||||
|
|
||||||
logger = setup_logger(__name__)
|
logger = setup_logger(__name__)
|
||||||
api_key_header_internal = APIKeyHeader(name="X-API-Key-Internal")
|
|
||||||
MAX_MESSAGES = 100
|
MAX_MESSAGES = 100
|
||||||
|
|
||||||
class PullRequest(BaseModel):
|
class PullRequest(BaseModel):
|
||||||
@@ -33,12 +30,6 @@ class PullResponse(BaseModel):
|
|||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
logger.info("[FastAPI] Starting application...")
|
logger.info("[FastAPI] Starting application...")
|
||||||
|
|
||||||
logger.info("[FastAPI] Creating MySQL connection pool...")
|
|
||||||
create_connection_pool("ApiPullPool")
|
|
||||||
start_healthcheck_thread()
|
|
||||||
logger.info("[FastAPI] MySQL healthcheck thread started.")
|
|
||||||
|
|
||||||
consumer = RabbitMQConsumer()
|
consumer = RabbitMQConsumer()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -48,8 +39,6 @@ async def lifespan(app: FastAPI):
|
|||||||
logger.info("[FastAPI] RabbitMQ consumer initialized.")
|
logger.info("[FastAPI] RabbitMQ consumer initialized.")
|
||||||
|
|
||||||
SECRET_PATHS = frozenset({
|
SECRET_PATHS = frozenset({
|
||||||
"/etc/secrets/db_username",
|
|
||||||
"/etc/secrets/db_password",
|
|
||||||
"/etc/secrets/rmq_username",
|
"/etc/secrets/rmq_username",
|
||||||
"/etc/secrets/rmq_password"
|
"/etc/secrets/rmq_password"
|
||||||
})
|
})
|
||||||
@@ -60,9 +49,6 @@ async def lifespan(app: FastAPI):
|
|||||||
logger.info("[FastAPI] Closing RabbitMQ producer...")
|
logger.info("[FastAPI] Closing RabbitMQ producer...")
|
||||||
await consumer.close()
|
await consumer.close()
|
||||||
|
|
||||||
logger.info("[FastAPI] Closing MySQL connection pool...")
|
|
||||||
close_connection_pool()
|
|
||||||
|
|
||||||
api = FastAPI(
|
api = FastAPI(
|
||||||
title="Backend Pull Notifications",
|
title="Backend Pull Notifications",
|
||||||
description="API to retrieve pull notifications on invocation",
|
description="API to retrieve pull notifications on invocation",
|
||||||
@@ -94,16 +80,6 @@ async def prometheus_middleware(request: Request, call_next):
|
|||||||
logger.debug(f"[FastAPI] Prometheus counter incremented: {method} {path} {status}")
|
logger.debug(f"[FastAPI] Prometheus counter incremented: {method} {path} {status}")
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def verify_api_key_dependency_internal(db=Depends(get_db), api_key: str = Depends(api_key_header_internal)) -> str:
|
|
||||||
cursor = db.cursor()
|
|
||||||
cursor.execute("SELECT program_name, api_key FROM internal_api_keys WHERE status = 'active'")
|
|
||||||
for program_name, hashed_key in cursor.fetchall():
|
|
||||||
if verify_api_key(api_key=api_key, hashed=hashed_key):
|
|
||||||
logger.debug(f"[FastAPI] API key verified for program: {program_name}")
|
|
||||||
return program_name
|
|
||||||
logger.warning("Internal API key verification failed")
|
|
||||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
|
||||||
|
|
||||||
@api.exception_handler(StarletteHTTPException)
|
@api.exception_handler(StarletteHTTPException)
|
||||||
async def custom_http_exception_handler(request,exc):
|
async def custom_http_exception_handler(request,exc):
|
||||||
logger.debug(f"[FastAPI] Handling HTTP exception: {exc}")
|
logger.debug(f"[FastAPI] Handling HTTP exception: {exc}")
|
||||||
@@ -120,8 +96,7 @@ async def custom_http_exception_handler(request,exc):
|
|||||||
@api.post("/notifications/pull", response_model=PullResponse)
|
@api.post("/notifications/pull", response_model=PullResponse)
|
||||||
async def pull_notifications(
|
async def pull_notifications(
|
||||||
pull_request: PullRequest,
|
pull_request: PullRequest,
|
||||||
request: Request,
|
request: Request
|
||||||
program_name: str = Depends(verify_api_key_dependency_internal),
|
|
||||||
):
|
):
|
||||||
user_id = pull_request.user_id
|
user_id = pull_request.user_id
|
||||||
logger.debug(f"[FastAPI] User id {user_id} is requesting messages")
|
logger.debug(f"[FastAPI] User id {user_id} is requesting messages")
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
from argon2 import PasswordHasher
|
|
||||||
|
|
||||||
ph = PasswordHasher()
|
|
||||||
|
|
||||||
|
|
||||||
def verify_api_key(api_key: str, hashed: str) -> bool:
|
|
||||||
try:
|
|
||||||
return ph.verify(hashed, api_key)
|
|
||||||
except Exception:
|
|
||||||
return False
|
|
||||||
|
|
||||||
Reference in new issue
Block a user