Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
523265e550 | ||
|
|
5d2db84d75 | ||
|
|
4826859152 | ||
|
|
92f8b68f0e | ||
|
|
87a4b6e2de | ||
|
|
58d30344f5 | ||
|
|
a6064035bd | ||
|
|
141fb4758f | ||
|
|
5450027bbc | ||
|
|
721a213bbf | ||
|
|
9ce81f5012 | ||
|
|
a28f3bb680 | ||
|
|
14e45235b4 | ||
|
|
6a69d678fd | ||
|
|
48ba7ffd96 | ||
|
|
faa1f552e6 |
No files matched your search
+8
-2
@@ -6,7 +6,7 @@ MYSQL_PASSWORD=mysql_ZTdhRB
|
||||
MYSQL_DB=xpack
|
||||
|
||||
# 服务端口配置
|
||||
API_PORT=8000
|
||||
API_PORT=8002
|
||||
ADMIN_PORT=8001
|
||||
|
||||
# 调试配置
|
||||
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
|
||||
RABBITMQ_PORT=5672
|
||||
RABBITMQ_USER=rabbitmq
|
||||
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
|
||||
RABBITMQ_VHOST=/
|
||||
RABBITMQ_VHOST=/
|
||||
|
||||
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
|
||||
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
|
||||
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
|
||||
# Optional: key ID used to support key rotation identification
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
@@ -162,7 +162,7 @@ export const Home: React.FC<HomeProps> = ({
|
||||
<CardBody className="p-4">
|
||||
{/* Header */}
|
||||
<div className="flex items-start justify-between mb-3">
|
||||
<div className="flex items-center gap-2 w-full">
|
||||
<div className="flex items-center gap-2 w-full overflow-hidden">
|
||||
<div className="w-8 h-8 bg-slate-600 rounded-sm flex items-center justify-center">
|
||||
<Star className="w-4 h-4 text-white" />
|
||||
</div>
|
||||
|
||||
@@ -168,7 +168,7 @@ export const Home: React.FC<HomeProps> = ({
|
||||
<CardBody className="p-4">
|
||||
{/* Header */}
|
||||
<div className="flex items-start justify-between mb-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="flex items-center gap-2 overflow-hidden">
|
||||
<div className="w-8 h-8 bg-blue-100 rounded-lg flex items-center justify-center">
|
||||
<Zap className="w-4 h-4 text-blue-600" />
|
||||
</div>
|
||||
|
||||
+2
-1
@@ -15,4 +15,5 @@ google-auth>=2.0.0
|
||||
google-auth-oauthlib>=1.0.0
|
||||
fastapi_mcp
|
||||
alipay-sdk-python>=3.0.0
|
||||
wechatpayv3[async]
|
||||
wechatpayv3[async]
|
||||
cryptography>=41.0.0
|
||||
@@ -6,12 +6,12 @@ MYSQL_PASSWORD=mysql_ZTdhRB
|
||||
MYSQL_DB=xpack
|
||||
|
||||
# 服务端口配置
|
||||
API_PORT=8000
|
||||
API_PORT=8002
|
||||
ADMIN_PORT=8001
|
||||
|
||||
# 调试配置
|
||||
DEBUG=false
|
||||
LOG_LEVEL=INFO
|
||||
LOG_LEVEL=DEBUG
|
||||
|
||||
# Redis 配置
|
||||
REDIS_HOST=xpack-redis
|
||||
@@ -24,4 +24,10 @@ RABBITMQ_HOST=xpack-rabbitmq
|
||||
RABBITMQ_PORT=5672
|
||||
RABBITMQ_USER=rabbitmq
|
||||
RABBITMQ_PASSWORD=rabbitmq_Gs123dA
|
||||
RABBITMQ_VHOST=/
|
||||
RABBITMQ_VHOST=/
|
||||
|
||||
# Payment config encryption key (Base64-encoded 32-byte random value for AES-GCM)
|
||||
# Quick generation: python -c "import os,base64; print(base64.b64encode(os.urandom(32)).decode())"
|
||||
PAYMENT_CONFIG_SECRET_KEY=uyyhfMJ+xpEE+IC5mPigpscekkR2ucz6Sj7sHYLhJQA=
|
||||
# Optional: key ID used to support key rotation identification
|
||||
PAYMENT_CONFIG_KEY_ID=default
|
||||
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
One-time migration: encrypt plaintext payment channel configs at rest.
|
||||
|
||||
Usage:
|
||||
python scripts/resource/migrate_encrypt_payment_configs.py [--dry-run] [--limit N] [--ids alipay,wechat,stripe]
|
||||
|
||||
Requirements:
|
||||
- Environment variable PAYMENT_CONFIG_SECRET_KEY must be set to a base64-encoded 32-byte key.
|
||||
- cryptography library installed.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from typing import List
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from services.common.database import SessionLocal
|
||||
from services.common.models.payment_channel import PaymentChannel
|
||||
from services.common.utils.secure_config import is_encrypted, decrypt_config, encrypt_config
|
||||
|
||||
|
||||
def migrate_configs(dry_run: bool = False, limit: int = 0, only_ids: List[str] | None = None) -> int:
|
||||
db = SessionLocal()
|
||||
migrated = 0
|
||||
try:
|
||||
query = db.query(PaymentChannel)
|
||||
if only_ids:
|
||||
query = query.filter(PaymentChannel.id.in_(only_ids))
|
||||
rows = query.all()
|
||||
if limit and len(rows) > limit:
|
||||
rows = rows[:limit]
|
||||
|
||||
for row in rows:
|
||||
raw = row.config
|
||||
if not raw:
|
||||
continue
|
||||
if is_encrypted(raw):
|
||||
# already encrypted
|
||||
continue
|
||||
cfg = decrypt_config(raw)
|
||||
if cfg is None:
|
||||
# cannot parse plaintext json; skip but report
|
||||
print(f"[WARN] Skip {row.id}: config not JSON or decrypt failed")
|
||||
continue
|
||||
try:
|
||||
enc = encrypt_config(cfg)
|
||||
except Exception as e:
|
||||
print(f"[ERROR] Encrypt {row.id} failed: {e}")
|
||||
continue
|
||||
if dry_run:
|
||||
print(f"[DRY-RUN] Would encrypt {row.id}")
|
||||
else:
|
||||
row.config = enc
|
||||
row.updated_at = datetime.now(timezone.utc)
|
||||
db.add(row)
|
||||
migrated += 1
|
||||
if not dry_run and migrated:
|
||||
db.commit()
|
||||
print(f"Done. Migrated {migrated} rows. Dry-run={dry_run}")
|
||||
return migrated
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Encrypt legacy payment channel configs")
|
||||
parser.add_argument("--dry-run", action="store_true", help="Do not write changes, only report")
|
||||
parser.add_argument("--limit", type=int, default=0, help="Limit number of rows to process")
|
||||
parser.add_argument("--ids", type=str, default="", help="Comma-separated channel ids to process")
|
||||
args = parser.parse_args()
|
||||
|
||||
only_ids = [x.strip() for x in args.ids.split(",") if x.strip()] if args.ids else None
|
||||
try:
|
||||
migrate_configs(dry_run=args.dry_run, limit=args.limit, only_ids=only_ids)
|
||||
except Exception as e:
|
||||
print(f"Migration failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -34,11 +34,11 @@ fi
|
||||
# 初始化数据库
|
||||
python ./init_db.py
|
||||
|
||||
nohup uvicorn services.admin_service.main:app --host 0.0.0.0 --port 8001 --reload > ${LOG_DIR}/admin_service.log 2>&1 &
|
||||
nohup uvicorn services.admin_service.main:app --host 0.0.0.0 --port 8001 --reload --timeout-graceful-shutdown 2 --timeout-keep-alive 1 > ${LOG_DIR}/admin_service.log 2>&1 &
|
||||
|
||||
sleep 5s
|
||||
|
||||
nohup uvicorn services.api_service.main:app --host 0.0.0.0 --port 8002 --reload > ${LOG_DIR}/api_service.log 2>&1 &
|
||||
nohup uvicorn services.api_service.main:app --host 0.0.0.0 --port 8002 --reload --timeout-graceful-shutdown 2 --timeout-keep-alive 1 > ${LOG_DIR}/api_service.log 2>&1 &
|
||||
HOSTNAME=""
|
||||
cd frontend/ && nohup node server.js> ${LOG_DIR}//frontend.log 2>&1 &
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
"downloads": {
|
||||
"program": {
|
||||
"xpack_mcp_market": {
|
||||
"version": "v1.1.0",
|
||||
"version": "v1.1.1",
|
||||
"docker": "xpackai/xpack-mcp-market",
|
||||
"tar": "xpack-mcp-market.tar.gz",
|
||||
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from fastapi import APIRouter, Depends, Query, Body
|
||||
from pydantic import config
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from services.common.database import get_db
|
||||
from services.common.utils.response_utils import ResponseUtils
|
||||
from services.admin_service.services.payment_channel_service import PaymentChannelService
|
||||
from services.common.utils.secure_config import decrypt_config, encrypt_config
|
||||
from services.admin_service.tasks.order_monitor_task import OrderMonitorTask
|
||||
import json
|
||||
|
||||
router = APIRouter()
|
||||
@@ -27,10 +28,9 @@ async def payment_channel_list(
|
||||
result = []
|
||||
for item in list:
|
||||
enable = item.status == 1
|
||||
try:
|
||||
cfg = json.loads(item.config)
|
||||
except:
|
||||
cfg = {}
|
||||
cfg = {}
|
||||
if item.config:
|
||||
cfg = decrypt_config(item.config) or {}
|
||||
result.append(
|
||||
{
|
||||
"id": item.id,
|
||||
@@ -56,14 +56,16 @@ async def payment_channel_enable(
|
||||
|
||||
if data:
|
||||
if id == "alipay":
|
||||
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask
|
||||
alipay_monitor_task = AlipayOrderMonitorTask.get_instance()
|
||||
alipay_monitor_task.start_monitor()
|
||||
monitor = OrderMonitorTask.get_instance()
|
||||
# Ensure monitor is running and enable Alipay client
|
||||
monitor.start_monitor()
|
||||
monitor.enable_client("alipay")
|
||||
cfg = decrypt_config(data.config) if data.config else {}
|
||||
return ResponseUtils.success(
|
||||
{
|
||||
"id": id,
|
||||
"name": data.name,
|
||||
"config": data.config,
|
||||
"config": cfg or {},
|
||||
"is_enabled": data.status == 1,
|
||||
"updated_time": data.updated_at,
|
||||
}
|
||||
@@ -83,14 +85,15 @@ async def payment_channel_disable(
|
||||
data = payment_channel_service.update_status(id, 0)
|
||||
if data:
|
||||
if id == "alipay":
|
||||
from services.admin_service.tasks.alipay_order_monitor_task import AlipayOrderMonitorTask
|
||||
alipay_monitor_task = AlipayOrderMonitorTask.get_instance()
|
||||
alipay_monitor_task.stop_monitor()
|
||||
monitor = OrderMonitorTask.get_instance()
|
||||
# Disable Alipay client without stopping global monitor
|
||||
monitor.disable_client("alipay")
|
||||
cfg = decrypt_config(data.config) if data.config else {}
|
||||
return ResponseUtils.success(
|
||||
{
|
||||
"id": id,
|
||||
"name": data.name,
|
||||
"config": data.config,
|
||||
"config": cfg or {},
|
||||
"is_enabled": data.status == 1,
|
||||
"updated_time": data.updated_at,
|
||||
}
|
||||
@@ -109,8 +112,12 @@ async def payment_channel_config(
|
||||
return ResponseUtils.error("Payment channel id cannot be empty")
|
||||
config = body.get("config")
|
||||
if config:
|
||||
configStr = json.dumps(config)
|
||||
data = payment_channel_service.update_config(id=id, config=configStr)
|
||||
# Encrypt config before persisting to DB
|
||||
try:
|
||||
encrypted = encrypt_config(config)
|
||||
except Exception as e:
|
||||
return ResponseUtils.error(f"Failed to encrypt config: {str(e)}")
|
||||
data = payment_channel_service.update_config(id=id, config=encrypted)
|
||||
if data:
|
||||
# Convert str to object
|
||||
return ResponseUtils.success(
|
||||
|
||||
@@ -172,8 +172,8 @@ class McpManagerService:
|
||||
new_api.response_examples = temp_api.response_examples
|
||||
new_api.response_headers = temp_api.response_headers
|
||||
new_api.operation_examples = temp_api.operation_examples
|
||||
new_api.enabled = temp_api.enabled
|
||||
new_api.is_deleted = temp_api.is_deleted
|
||||
new_api.enabled = True
|
||||
# new_api.is_deleted = temp_api.is_deleted
|
||||
|
||||
# Save new API record
|
||||
self.mcp_tool_api_repository.create(new_api)
|
||||
|
||||
@@ -6,6 +6,7 @@ from services.common.database import SessionLocal
|
||||
from datetime import datetime
|
||||
from services.admin_service.repositories.payment_channel_repository import PaymentChannelRepository
|
||||
from services.common.models.payment_channel import PaymentChannel
|
||||
from services.common.utils.secure_config import decrypt_config
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -35,13 +36,14 @@ class PaymentChannelService:
|
||||
channel_config = self.payment_channel_repository.payment_channel_get(id)
|
||||
if channel_config is None:
|
||||
return None
|
||||
# Support encrypted-at-rest config
|
||||
config = {}
|
||||
if channel_config.config is not None:
|
||||
try:
|
||||
config = json.loads(channel_config.config)
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Failed to parse payment channel config JSON: {str(e)}")
|
||||
if channel_config.config:
|
||||
cfg = decrypt_config(channel_config.config)
|
||||
if cfg is None:
|
||||
logger.error("Failed to decrypt or parse payment channel config")
|
||||
return None
|
||||
config = cfg
|
||||
config["enable"] = channel_config.status == 1
|
||||
config["id"] = channel_config.id
|
||||
return config
|
||||
@@ -64,19 +66,17 @@ class PaymentChannelService:
|
||||
logger.warning("Stripe payment channel config is empty")
|
||||
return None
|
||||
|
||||
# Parse config JSON
|
||||
try:
|
||||
config = json.loads(stripe_channel.config)
|
||||
# Validate required fields
|
||||
if not config.get("secret") or not config.get("webhook_secret"):
|
||||
logger.warning("Stripe config missing required fields: secret or webhook_secret")
|
||||
return None
|
||||
|
||||
logger.info("Successfully retrieved Stripe config")
|
||||
return config
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"Failed to parse Stripe config JSON: {str(e)}")
|
||||
# Decrypt/parse config JSON
|
||||
config = decrypt_config(stripe_channel.config)
|
||||
if config is None:
|
||||
logger.error("Failed to decrypt Stripe config")
|
||||
return None
|
||||
# Validate required fields
|
||||
if not config.get("secret") or not config.get("webhook_secret"):
|
||||
logger.warning("Stripe config missing required fields: secret or webhook_secret")
|
||||
return None
|
||||
logger.info("Successfully retrieved Stripe config")
|
||||
return config
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get Stripe config: {str(e)}")
|
||||
|
||||
@@ -18,6 +18,7 @@ from services.api_service.repositories.user_apikey_repository import UserApiKeyR
|
||||
from services.api_service.utils.connection_manager import connection_manager
|
||||
from services.common.database import get_db
|
||||
from services.common.config import Config
|
||||
from services.api_service.utils.mcp_event_store import RedisEventStore
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -27,12 +28,12 @@ class McpController:
|
||||
|
||||
def __init__(self):
|
||||
self.sse = SseServerTransport("/messages/")
|
||||
# StreamableHTTP 会话持久化:按 (service_id, user_id) 维度保存传输与服务器任务
|
||||
# StreamableHTTP session persistence: store transport and server task keyed by (service_id, user_id)
|
||||
self._http_transports: dict[str, StreamableHTTPServerTransport] = {}
|
||||
self._http_server_tasks: dict[str, asyncio.Task] = {}
|
||||
self._http_servers: dict[str, Server] = {}
|
||||
self.server_factory = McpServerFactory()
|
||||
# 会话回收:空闲时间记录与后台清理任务
|
||||
# Session recycling: track idle time and run a background cleanup task
|
||||
self._session_last_activity: dict[str, float] = {}
|
||||
self._session_gc_task: Optional[asyncio.Task] = None
|
||||
self._session_gc_stop_event = asyncio.Event()
|
||||
@@ -128,8 +129,8 @@ class McpController:
|
||||
async def handle_sse_connection_asgi(self, request: Request):
|
||||
"""Return an ASGI app that handles MCP SSE connections.
|
||||
|
||||
Starlette 的 Route 会传入一个 Request,然后将返回的可调用对象视为 ASGI 应用。
|
||||
我们在返回的 ASGI 可调用中完成 SSE 握手,并运行 MCP 服务器。
|
||||
Starlette Route passes a Request and treats the returned callable as an ASGI app.
|
||||
The returned ASGI callable performs the SSE handshake and runs the MCP server.
|
||||
"""
|
||||
|
||||
async def asgi(scope, receive, send):
|
||||
@@ -234,8 +235,9 @@ class McpController:
|
||||
async def handle_streamable_http_asgi(self, request: Request):
|
||||
"""Return an ASGI app that handles MCP Streamable HTTP (GET/POST/DELETE).
|
||||
|
||||
将请求交由StreamableHTTP的handle_request处理;同时使用持久化的transport与server,
|
||||
让 GET/POST/DELETE 在同一会话中交互,保证工具/资源列表等请求能返回到相同的 SSE 流。
|
||||
Delegate requests to StreamableHTTP's handle_request; use persistent transport and server
|
||||
so GET/POST/DELETE interact within the same session, ensuring tools/resources queries
|
||||
return to the same SSE stream.
|
||||
"""
|
||||
|
||||
async def asgi(scope, receive, send):
|
||||
@@ -263,6 +265,69 @@ class McpController:
|
||||
|
||||
logger.info(f"Received StreamableHTTP request - Service ID: {service_id}, Client: {client_ip}, UA: {user_agent[:50]}...")
|
||||
|
||||
# Security: validate Origin to prevent DNS rebinding (MCP spec requirement)
|
||||
origin = req.headers.get('origin')
|
||||
if origin and not self._is_origin_allowed(origin, req):
|
||||
logger.warning(f"Rejected by Origin validation - Origin: {origin}, Host: {req.url.scheme}://{req.url.netloc}")
|
||||
response_body = b"Forbidden: invalid Origin"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 403,
|
||||
'headers': [
|
||||
[b'content-type', b'text/plain'],
|
||||
[b'content-length', str(len(response_body)).encode()],
|
||||
],
|
||||
})
|
||||
await send({'type': 'http.response.body', 'body': response_body})
|
||||
return
|
||||
|
||||
# Validate Accept & Content-Type headers according to MCP Streamable HTTP rules
|
||||
accept = (req.headers.get('accept') or '').lower()
|
||||
content_type = (req.headers.get('content-type') or '').lower()
|
||||
if req.method == 'GET':
|
||||
if 'text/event-stream' not in accept:
|
||||
logger.warning("Missing Accept: text/event-stream for GET SSE")
|
||||
response_body = b"Not Acceptable: require text/event-stream"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 406,
|
||||
'headers': [
|
||||
[b'content-type', b'text/plain'],
|
||||
[b'content-length', str(len(response_body)).encode()],
|
||||
],
|
||||
})
|
||||
await send({'type': 'http.response.body', 'body': response_body})
|
||||
return
|
||||
elif req.method == 'POST':
|
||||
# Client should list both application/json and text/event-stream
|
||||
if not ('application/json' in accept and 'text/event-stream' in accept):
|
||||
logger.warning("POST Accept must include application/json and text/event-stream")
|
||||
response_body = b"Not Acceptable: require application/json and text/event-stream"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 406,
|
||||
'headers': [
|
||||
[b'content-type', b'text/plain'],
|
||||
[b'content-length', str(len(response_body)).encode()],
|
||||
],
|
||||
})
|
||||
await send({'type': 'http.response.body', 'body': response_body})
|
||||
return
|
||||
# Body must be JSON-RPC payload
|
||||
if 'application/json' not in content_type:
|
||||
logger.warning("Unsupported Media Type for POST; require application/json")
|
||||
response_body = b"Unsupported Media Type: require application/json"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 415,
|
||||
'headers': [
|
||||
[b'content-type', b'text/plain'],
|
||||
[b'content-length', str(len(response_body)).encode()],
|
||||
],
|
||||
})
|
||||
await send({'type': 'http.response.body', 'body': response_body})
|
||||
return
|
||||
|
||||
# Extract user ID (for billing) - must provide valid apikey
|
||||
user_info = self._extract_user_info(req)
|
||||
if not user_info:
|
||||
@@ -281,28 +346,62 @@ class McpController:
|
||||
user_id, apikey_id = user_info
|
||||
session_key = f"{service_id}:{user_id}"
|
||||
|
||||
# Register connection to manager(便于统计与审计)
|
||||
connection_key = connection_manager.register_connection(service_id, user_id, client_ip)
|
||||
# Build connection key for lifecycle management
|
||||
connection_key = f"{service_id}:{user_id}:{client_ip}"
|
||||
|
||||
try:
|
||||
# 获取(或创建)持久化的 StreamableHTTP 传输与 MCP 服务器
|
||||
# Get or create a persistent StreamableHTTP transport and MCP server
|
||||
transport = await self._ensure_http_session(session_key, service_id, user_id, apikey_id)
|
||||
|
||||
# 记录会话活跃
|
||||
logger.debug(f'method: {req.method},request body: {req.body}')
|
||||
# Note session activity
|
||||
self._note_session_activity(session_key)
|
||||
|
||||
# 将请求交由 transport 处理;若是 GET,将建立 SSE;若是 POST,将发送 JSON-RPC;DELETE 将终止会话
|
||||
# Ensure transport is connected before delegating (avoid race on first request)
|
||||
try:
|
||||
await self._wait_transport_ready(transport, timeout_seconds=2.0)
|
||||
except TimeoutError:
|
||||
logger.error("Transport not ready within timeout; refusing request")
|
||||
response_body = b"Service Unavailable: transport not ready"
|
||||
await send({
|
||||
'type': 'http.response.start',
|
||||
'status': 503,
|
||||
'headers': [
|
||||
[b'content-type', b'text/plain'],
|
||||
[b'content-length', str(len(response_body)).encode()],
|
||||
],
|
||||
})
|
||||
await send({'type': 'http.response.body', 'body': response_body})
|
||||
return
|
||||
|
||||
# Register only on GET (SSE handshake); POST just updates activity
|
||||
if req.method == "GET":
|
||||
connection_manager.register_connection(service_id, user_id, client_ip)
|
||||
logger.debug(f'register connection successful.connection_key is {connection_key}')
|
||||
elif req.method == "POST":
|
||||
connection_manager.update_activity(connection_key)
|
||||
|
||||
# Delegate request to transport; GET establishes SSE; POST sends JSON-RPC; DELETE terminates the session
|
||||
await transport.handle_request(scope, receive, send)
|
||||
|
||||
# 处理完成后更新活跃时间(避免长处理后的误清理)
|
||||
# Update activity after handling (avoid cleaning long-running processing)
|
||||
self._note_session_activity(session_key)
|
||||
|
||||
# 如果会话已经终止,立即清理资源
|
||||
if transport.is_terminated or req.method == "DELETE":
|
||||
# If the session has terminated, clean up and unregister
|
||||
if transport.is_terminated:
|
||||
await self._cleanup_http_session(session_key)
|
||||
connection_manager.unregister_connection(connection_key)
|
||||
# For explicit DELETE, ensure termination cleanup
|
||||
if req.method == "DELETE" and not transport.is_terminated:
|
||||
try:
|
||||
await transport.terminate()
|
||||
except Exception:
|
||||
logger.exception("Error terminating transport on DELETE")
|
||||
await self._cleanup_http_session(session_key)
|
||||
connection_manager.unregister_connection(connection_key)
|
||||
finally:
|
||||
# Unregister connection(按请求粒度解除登记)
|
||||
connection_manager.unregister_connection(connection_key)
|
||||
# Unregister when SSE GET disconnects
|
||||
if req.method == "GET":
|
||||
connection_manager.unregister_connection(connection_key)
|
||||
|
||||
except ConnectionError as e:
|
||||
logger.warning(f"Connection error - Service ID: {service_id or 'unknown'}, Client: {client_ip}: {str(e)}")
|
||||
@@ -331,23 +430,74 @@ class McpController:
|
||||
|
||||
return asgi
|
||||
|
||||
async def _wait_transport_ready(self, transport: StreamableHTTPServerTransport, timeout_seconds: float = 2.0) -> None:
|
||||
"""Wait until StreamableHTTP transport is connected and streams are initialized.
|
||||
|
||||
The underlying transport sets internal streams in connect(); POST/GET handling
|
||||
requires these to exist. We poll briefly to avoid race on first request.
|
||||
"""
|
||||
deadline = time.time() + timeout_seconds
|
||||
# Access internal attributes defensively; break once read writer is present
|
||||
while True:
|
||||
# If terminated, break early
|
||||
if transport.is_terminated:
|
||||
raise TimeoutError("Transport terminated during wait")
|
||||
# Heuristic: writer/reader set once connect() started
|
||||
has_read_writer = getattr(transport, "_read_stream_writer", None) is not None
|
||||
has_write_reader = getattr(transport, "_write_stream_reader", None) is not None
|
||||
if has_read_writer and has_write_reader:
|
||||
return
|
||||
if time.time() > deadline:
|
||||
raise TimeoutError("Transport not ready")
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
def _is_origin_allowed(self, origin: str, req: Request) -> bool:
|
||||
"""Validate Origin header against request host to prevent DNS rebinding.
|
||||
|
||||
Policy:
|
||||
- If Origin is present, it must match the scheme+host of the request URL.
|
||||
- Allow common localhost variants (127.0.0.1, localhost) across http/https.
|
||||
- If Origin is missing, treat as allowed (native clients may omit it).
|
||||
"""
|
||||
try:
|
||||
if not origin:
|
||||
return True
|
||||
origin = origin.strip()
|
||||
request_host = f"{req.url.scheme}://{req.url.netloc}"
|
||||
if origin == request_host:
|
||||
return True
|
||||
allowed_local_origins = {
|
||||
"http://127.0.0.1",
|
||||
f"http://127.0.0.1:{req.url.port}" if req.url.port else "http://127.0.0.1",
|
||||
"http://localhost",
|
||||
f"http://localhost:{req.url.port}" if req.url.port else "http://localhost",
|
||||
"https://127.0.0.1",
|
||||
f"https://127.0.0.1:{req.url.port}" if req.url.port else "https://127.0.0.1",
|
||||
"https://localhost",
|
||||
f"https://localhost:{req.url.port}" if req.url.port else "https://localhost",
|
||||
}
|
||||
return origin in allowed_local_origins
|
||||
except Exception:
|
||||
logger.exception("Origin validation error")
|
||||
return False
|
||||
|
||||
async def _ensure_http_session(self, session_key: str, service_id: str, user_id: str, apikey_id: str) -> StreamableHTTPServerTransport:
|
||||
"""Ensure a persistent StreamableHTTP transport and MCP server exist for the session.
|
||||
|
||||
- 创建(或复用) transport
|
||||
- 若尚未启动服务器,则在 transport.connect() 上启动 mcp_server.run()
|
||||
- Create or reuse the transport
|
||||
- If the server has not been started, start mcp_server.run() within transport.connect()
|
||||
"""
|
||||
if session_key in self._http_transports:
|
||||
transport = self._http_transports[session_key]
|
||||
# 启动回收任务(若未启动)
|
||||
# Start GC task if not started
|
||||
self._ensure_session_gc_task()
|
||||
# 记录活跃
|
||||
# Note activity
|
||||
self._note_session_activity(session_key)
|
||||
# 服务器任务存在且未结束,直接复用
|
||||
# If the server task exists and is not finished, reuse it
|
||||
task = self._http_server_tasks.get(session_key)
|
||||
if task and not task.done():
|
||||
return transport
|
||||
# 没有任务或任务已结束,则重新启动
|
||||
# If no task or the task is finished, restart
|
||||
mcp_server = self._http_servers.get(session_key)
|
||||
if mcp_server is None:
|
||||
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
|
||||
@@ -368,8 +518,13 @@ class McpController:
|
||||
self._http_server_tasks[session_key] = task
|
||||
return transport
|
||||
|
||||
# 新建 transport 与 server
|
||||
transport = StreamableHTTPServerTransport(mcp_session_id=None, is_json_response_enabled=False)
|
||||
# Create transport and server
|
||||
event_store = RedisEventStore(namespace=session_key, ttl_seconds=Config.MCP_SESSION_IDLE_TTL_SECONDS)
|
||||
transport = StreamableHTTPServerTransport(
|
||||
mcp_session_id=None,
|
||||
is_json_response_enabled=False,
|
||||
event_store=event_store,
|
||||
)
|
||||
mcp_server = await self.server_factory.create_server(service_id, user_id, apikey_id)
|
||||
self._http_transports[session_key] = transport
|
||||
self._http_servers[session_key] = mcp_server
|
||||
@@ -378,7 +533,7 @@ class McpController:
|
||||
init_options.server_name = f"mcp-service-{service_id}"
|
||||
logger.info(f"StreamableHTTP session created - Service: {service_id}, User: {user_id}")
|
||||
|
||||
# 启动回收任务(若未启动)并记录活跃
|
||||
# Start GC task if not started and note activity
|
||||
self._ensure_session_gc_task()
|
||||
self._note_session_activity(session_key)
|
||||
|
||||
@@ -403,7 +558,7 @@ class McpController:
|
||||
|
||||
if task and not task.done():
|
||||
try:
|
||||
# 终止 transport,促使服务器退出
|
||||
# Terminate transport to prompt server exit
|
||||
if transport and not transport.is_terminated:
|
||||
await transport.terminate()
|
||||
await task
|
||||
@@ -500,8 +655,9 @@ class McpController:
|
||||
return self.sse.handle_post_message
|
||||
|
||||
def get_streamable_http_handler(self):
|
||||
# 不再返回持久化实例的 handler;如果确实需要,可改为在路由层调用 handle_streamable_http_asgi
|
||||
# 这里保留占位以避免外部引用报错,但不使用。
|
||||
# We no longer return a handler for a persistent instance; if needed, call
|
||||
# handle_streamable_http_asgi at the routing layer. This placeholder is
|
||||
# retained to avoid external references breaking, but it is not used.
|
||||
raise NotImplementedError("Use handle_streamable_http_asgi for StreamableHTTP routes")
|
||||
|
||||
|
||||
@@ -528,7 +684,7 @@ class McpController:
|
||||
idle = (now - last) > self._session_idle_ttl
|
||||
task = self._http_server_tasks.get(key)
|
||||
active_task = task is not None and not task.done()
|
||||
# 仅在无活动任务时才按TTL清理,避免中断活跃的SSE会话
|
||||
# Only TTL-clean when there is no active task, to avoid killing active SSE sessions
|
||||
if transport.is_terminated or (idle and not active_task):
|
||||
stale_keys.append(key)
|
||||
for key in stale_keys:
|
||||
|
||||
@@ -57,13 +57,13 @@ class McpServerFactory:
|
||||
|
||||
# Register tool call handler
|
||||
@app.call_tool()
|
||||
async def call_tool(name: str, arguments: dict) -> List[types.Content]:
|
||||
async def call_tool(name: str, arguments: dict) -> tuple[List[types.Content], dict]:
|
||||
"""Execute specified tool"""
|
||||
# user_id must exist, otherwise we shouldn't reach here
|
||||
if not user_id:
|
||||
error_msg = "Missing user authentication"
|
||||
logger.error(error_msg)
|
||||
return [types.TextContent(type="text", text=error_msg)]
|
||||
return [types.TextContent(type="text", text=error_msg)], {}
|
||||
|
||||
return await self._handle_call_tool_with_billing(service_id, name, arguments, user_id, apikey_id)
|
||||
|
||||
@@ -102,7 +102,7 @@ class McpServerFactory:
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def _handle_call_tool_with_billing(self, service_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> List[types.Content]:
|
||||
async def _handle_call_tool_with_billing(self, service_id: str, name: str, arguments: dict, user_id: str, apikey_id: Optional[str] = None) -> tuple[List[types.Content], dict]:
|
||||
"""
|
||||
Handle tool call with billing logic
|
||||
|
||||
@@ -145,7 +145,7 @@ class McpServerFactory:
|
||||
|
||||
# If tool API id not found, skip sending billing message to avoid FK errors
|
||||
if not api_id_for_log:
|
||||
return [types.TextContent(type="text", text=error_msg)]
|
||||
return [types.TextContent(type="text", text=error_msg)], {}
|
||||
|
||||
call_log = ApiCallLogInfo(
|
||||
user_id=user_id,
|
||||
@@ -163,7 +163,7 @@ class McpServerFactory:
|
||||
)
|
||||
await self.billing_service.send_billing_message(call_log, False, datetime.now(timezone.utc))
|
||||
|
||||
return [types.TextContent(type="text", text=error_msg)]
|
||||
return [types.TextContent(type="text", text=error_msg)],{}
|
||||
|
||||
logger.info(f"Pre-deduction successful - User ID: {user_id}, Deduction amount: {pre_deduct_result.service_price}")
|
||||
amount = pre_deduct_result.service_price
|
||||
@@ -171,6 +171,7 @@ class McpServerFactory:
|
||||
db = next(get_db())
|
||||
call_success = False
|
||||
result: List[types.ContentBlock] = []
|
||||
response_data: dict = {}
|
||||
# calculate input token amount
|
||||
input_token_amount = Decimal("0")
|
||||
output_token_amount = Decimal("0")
|
||||
@@ -195,7 +196,7 @@ class McpServerFactory:
|
||||
logger.error(error_msg)
|
||||
# Close DB and return error without sending billing message to avoid FK violation
|
||||
db.close()
|
||||
return [types.TextContent(type="text", text=error_msg)]
|
||||
return [types.TextContent(type="text", text=error_msg)],{}
|
||||
|
||||
try:
|
||||
logger.info(f"Found tool configuration: {tool_config.name}")
|
||||
@@ -205,7 +206,7 @@ class McpServerFactory:
|
||||
logger.debug(f"Call params: {call_params}")
|
||||
|
||||
# Execute tool
|
||||
result = await self.tool_service.execute_tool(tool_config, arguments, call_params)
|
||||
result,response_data = await self.tool_service.execute_tool(tool_config, arguments, call_params)
|
||||
call_success = True
|
||||
logger.info(f"Tool call successful - User ID: {user_id}, Tool: {name}")
|
||||
|
||||
@@ -254,7 +255,7 @@ class McpServerFactory:
|
||||
logger.info(f"Billing message sent - User ID: {user_id}, Tool: {name}, Success: {call_success}")
|
||||
|
||||
# Ensure return type is correct
|
||||
return result
|
||||
return result,response_data
|
||||
|
||||
def _create_mcp_service(self, db) -> McpService:
|
||||
"""
|
||||
|
||||
@@ -131,11 +131,15 @@ class McpService:
|
||||
"required": []
|
||||
}
|
||||
|
||||
return types.Tool(
|
||||
t = types.Tool(
|
||||
name=tool_api.name,
|
||||
description=tool_api.description,
|
||||
inputSchema=input_schema
|
||||
inputSchema=input_schema,
|
||||
)
|
||||
output_schema = self._build_output_schema(tool_api)
|
||||
if output_schema:
|
||||
t.outputSchema = output_schema
|
||||
return t
|
||||
except Exception as e:
|
||||
self.logger.error(f"Tool conversion failed for {tool_api.name}: {e}")
|
||||
return None
|
||||
@@ -271,4 +275,62 @@ class McpService:
|
||||
return {
|
||||
"base_url": service.base_url or "",
|
||||
"headers": headers
|
||||
}
|
||||
}
|
||||
|
||||
def _build_output_schema(self,tool_api:McpToolApi) -> Optional[dict]:
|
||||
"""
|
||||
Build output schema based on API configuration
|
||||
|
||||
Priority:
|
||||
1) Use explicit JSON Schema from `response_schema` if present
|
||||
2) Infer a reasonable JSON Schema from `response_examples`
|
||||
3) Fallback to a generic text content schema
|
||||
"""
|
||||
# 1) Try explicit response schema
|
||||
try:
|
||||
if tool_api.response_schema:
|
||||
parsed = json.loads(tool_api.response_schema)
|
||||
if isinstance(parsed, dict):
|
||||
return parsed
|
||||
except Exception as e:
|
||||
self.logger.warning(f"Failed to parse response_schema for {tool_api.name}: {e}")
|
||||
|
||||
# 2) Try to infer schema from examples
|
||||
try:
|
||||
if getattr(tool_api, "response_examples", None) and tool_api.response_examples.strip():
|
||||
examples = json.loads(tool_api.response_examples)
|
||||
# If multiple examples provided, use the first for inference
|
||||
example_value = examples[0] if isinstance(examples, list) and examples else examples
|
||||
inferred = self._infer_json_schema_from_example(example_value)
|
||||
if inferred:
|
||||
return inferred
|
||||
except Exception as e:
|
||||
self.logger.warning(f"Failed to infer schema from response_examples for {tool_api.name}: {e}")
|
||||
|
||||
# 3) Fallback to a generic text MCP content schema
|
||||
return None
|
||||
|
||||
# Helper: Infer a simple JSON Schema from a Python value
|
||||
def _infer_json_schema_from_example(self, value) -> dict:
|
||||
try:
|
||||
if isinstance(value, str):
|
||||
return {"type": "string"}
|
||||
if isinstance(value, bool):
|
||||
return {"type": "boolean"}
|
||||
if isinstance(value, int):
|
||||
return {"type": "integer"}
|
||||
if isinstance(value, float):
|
||||
return {"type": "number"}
|
||||
if isinstance(value, list):
|
||||
if not value:
|
||||
return {"type": "array", "items": {}}
|
||||
# Infer from first element; for heterogeneous arrays this is a best-effort
|
||||
return {"type": "array", "items": self._infer_json_schema_from_example(value[0])}
|
||||
if isinstance(value, dict):
|
||||
props = {k: self._infer_json_schema_from_example(v) for k, v in value.items()}
|
||||
required = list(value.keys())
|
||||
return {"type": "object", "properties": props, "required": required}
|
||||
# Fallback
|
||||
return {"type": "string"}
|
||||
except Exception:
|
||||
return {"type": "string"}
|
||||
@@ -17,7 +17,7 @@ class McpToolService:
|
||||
def __init__(self):
|
||||
self.http_builder = HttpRequestBuilder()
|
||||
|
||||
async def execute_tool(self, tool_config, arguments: dict, call_params: dict) -> List[types.Content]:
|
||||
async def execute_tool(self, tool_config, arguments: dict, call_params: dict) -> tuple[List[types.Content],dict]:
|
||||
"""
|
||||
Execute tool call
|
||||
|
||||
@@ -37,14 +37,17 @@ class McpToolService:
|
||||
|
||||
# Send HTTP request
|
||||
response_text = await self._send_http_request(request_info)
|
||||
|
||||
if response_text:
|
||||
response_data = json.loads(response_text)
|
||||
else:
|
||||
response_data = {}
|
||||
logger.info("Tool execution completed successfully")
|
||||
return [types.TextContent(type="text", text=response_text)]
|
||||
return [types.TextContent(type="text", text=response_text)],response_data
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f"Tool execution failed: {str(e)}"
|
||||
logger.error(f"Tool execution failed: {error_msg}", exc_info=True)
|
||||
return [types.TextContent(type="text", text=error_msg)]
|
||||
return [types.TextContent(type="text", text=error_msg)],{}
|
||||
|
||||
async def _send_http_request(self, request_info: Dict[str, Any]) -> str:
|
||||
"""
|
||||
@@ -81,6 +84,8 @@ class McpToolService:
|
||||
raise ValueError(f"Unsupported HTTP method: {method}")
|
||||
|
||||
logger.info(f"HTTP response status code: {response.status_code}")
|
||||
if response.text != "":
|
||||
logger.debug(f"Response content: {response.text}")
|
||||
response.raise_for_status()
|
||||
|
||||
response_text = response.text
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import json
|
||||
import asyncio
|
||||
from typing import List, cast
|
||||
|
||||
from mcp.server.streamable_http import (
|
||||
EventStore,
|
||||
EventId,
|
||||
StreamId,
|
||||
EventMessage,
|
||||
EventCallback,
|
||||
)
|
||||
from mcp.types import JSONRPCMessage
|
||||
from services.common.redis import redis_client
|
||||
|
||||
|
||||
class RedisEventStore(EventStore):
|
||||
def __init__(self, namespace: str, ttl_seconds: int = 900):
|
||||
self.namespace = namespace
|
||||
self.ttl_seconds = ttl_seconds
|
||||
|
||||
# Synchronous helpers; wrap at call sites to avoid blocking
|
||||
def _rpush(self, key: str, value: str) -> int:
|
||||
return cast(int, redis_client.client.rpush(key, value))
|
||||
|
||||
def _expire(self, key: str, seconds: int) -> None:
|
||||
try:
|
||||
redis_client.client.expire(key, int(seconds))
|
||||
except Exception:
|
||||
# Ignore expiration errors
|
||||
pass
|
||||
|
||||
def _llen(self, key: str) -> int:
|
||||
return cast(int, redis_client.client.llen(key))
|
||||
|
||||
def _lrange(self, key: str, start: int, end: int) -> List[str]:
|
||||
raw = redis_client.client.lrange(key, int(start), int(end))
|
||||
items: List[str] = cast(List[str], raw or [])
|
||||
return [str(i) for i in items]
|
||||
|
||||
def _key(self, stream_id: StreamId) -> str:
|
||||
return f"xpack:mcp:event:{self.namespace}:{stream_id}"
|
||||
|
||||
def _event_id(self, stream_id: StreamId, seq: int) -> EventId:
|
||||
return f"{stream_id}:{seq}"
|
||||
|
||||
async def store_event(self, stream_id: StreamId, message: JSONRPCMessage) -> EventId:
|
||||
key = self._key(stream_id)
|
||||
payload = json.dumps(message.model_dump(by_alias=True, exclude_none=True))
|
||||
# Append to Redis list via thread wrapper to avoid blocking
|
||||
seq: int = await asyncio.to_thread(self._rpush, key, payload)
|
||||
# rpush returns new length; sequence index is length-1
|
||||
await asyncio.to_thread(self._expire, key, self.ttl_seconds)
|
||||
return self._event_id(stream_id, seq - 1)
|
||||
|
||||
async def replay_events_after(
|
||||
self,
|
||||
last_event_id: EventId,
|
||||
send_callback: EventCallback,
|
||||
) -> StreamId | None:
|
||||
# Parse last_event_id of format "<stream_id>:<seq>"
|
||||
try:
|
||||
stream_id_str, seq_str = last_event_id.rsplit(":", 1)
|
||||
stream_id: StreamId = stream_id_str
|
||||
start_idx = int(seq_str) + 1
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
key = self._key(stream_id)
|
||||
try:
|
||||
length: int = await asyncio.to_thread(self._llen, key)
|
||||
except Exception:
|
||||
# Redis unavailable or key type mismatch; cannot replay
|
||||
return stream_id
|
||||
if length <= 0 or start_idx >= length:
|
||||
return stream_id
|
||||
|
||||
# Fetch range [start_idx, length-1]
|
||||
try:
|
||||
items: list[str] = await asyncio.to_thread(self._lrange, key, start_idx, length - 1)
|
||||
except Exception:
|
||||
return stream_id
|
||||
for item in items:
|
||||
try:
|
||||
data = json.loads(item)
|
||||
msg = JSONRPCMessage.model_validate(data)
|
||||
# send_callback is async and expects EventMessage
|
||||
await send_callback(EventMessage(message=msg, event_id=None))
|
||||
except Exception:
|
||||
# Skip malformed entries
|
||||
continue
|
||||
return stream_id
|
||||
@@ -14,5 +14,6 @@ class PaymentChannel(Base):
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, autoincrement=False, comment="Primary key")
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, comment="Channel name")
|
||||
status: Mapped[int] = mapped_column(Integer, nullable=False, comment="Channel status, 0: disabled, 1: enabled")
|
||||
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config")
|
||||
# Store encrypted JSON envelope for channel configuration (AES-GCM). See secure_config.py
|
||||
config: Mapped[str] = mapped_column(Text, nullable=True, comment="Channel config (encrypted envelope JSON)")
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="Update time")
|
||||
@@ -1,4 +1,5 @@
|
||||
import redis
|
||||
from redis import Redis
|
||||
from typing import Optional, Any
|
||||
from .config import Config
|
||||
|
||||
@@ -9,7 +10,7 @@ class RedisClient:
|
||||
def __init__(self):
|
||||
"""Initialize Redis connection"""
|
||||
try:
|
||||
self.client = redis.Redis(
|
||||
self.client: Redis = redis.Redis(
|
||||
host=Config.REDIS_HOST,
|
||||
port=Config.REDIS_PORT,
|
||||
password=Config.REDIS_PASSWORD,
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import os
|
||||
import json
|
||||
import base64
|
||||
import secrets
|
||||
from typing import Optional, Any, Dict
|
||||
|
||||
try:
|
||||
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||||
except Exception:
|
||||
AESGCM = None
|
||||
|
||||
|
||||
ENC_MARKER = "__enc__"
|
||||
|
||||
|
||||
def _get_key() -> bytes:
|
||||
"""Load symmetric encryption key from environment.
|
||||
|
||||
Expects `PAYMENT_CONFIG_SECRET_KEY` to be a base64-encoded 32-byte key.
|
||||
Optionally supports `PAYMENT_CONFIG_KEY_ID` for key rotation tracking.
|
||||
"""
|
||||
key_b64 = os.getenv("PAYMENT_CONFIG_SECRET_KEY", "")
|
||||
if not key_b64:
|
||||
raise RuntimeError("Missing PAYMENT_CONFIG_SECRET_KEY environment variable")
|
||||
try:
|
||||
key = base64.b64decode(key_b64)
|
||||
except Exception as e:
|
||||
raise RuntimeError("Invalid base64 in PAYMENT_CONFIG_SECRET_KEY") from e
|
||||
if len(key) != 32:
|
||||
raise RuntimeError("PAYMENT_CONFIG_SECRET_KEY must decode to 32 bytes (AES-256)")
|
||||
return key
|
||||
|
||||
|
||||
def is_encrypted(value: Optional[str]) -> bool:
|
||||
"""Heuristically determine if the stored config string is an encrypted envelope."""
|
||||
if not value:
|
||||
return False
|
||||
try:
|
||||
obj = json.loads(value)
|
||||
return isinstance(obj, dict) and obj.get(ENC_MARKER) is True and "ct" in obj and "nonce" in obj
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def encrypt_config(config_obj: Dict[str, Any]) -> str:
|
||||
"""Encrypt config object using AES-GCM and return envelope JSON string.
|
||||
|
||||
Envelope format:
|
||||
{
|
||||
"__enc__": true,
|
||||
"v": 1,
|
||||
"alg": "AESGCM",
|
||||
"kid": "default",
|
||||
"nonce": base64,
|
||||
"ct": base64
|
||||
}
|
||||
"""
|
||||
if AESGCM is None:
|
||||
raise RuntimeError("cryptography library is required for encryption")
|
||||
key = _get_key()
|
||||
aesgcm = AESGCM(key)
|
||||
nonce = secrets.token_bytes(12) # AES-GCM standard 96-bit nonce
|
||||
plaintext = json.dumps(config_obj, ensure_ascii=False).encode("utf-8")
|
||||
ciphertext = aesgcm.encrypt(nonce, plaintext, None)
|
||||
envelope = {
|
||||
ENC_MARKER: True,
|
||||
"v": 1,
|
||||
"alg": "AESGCM",
|
||||
"kid": os.getenv("PAYMENT_CONFIG_KEY_ID", "default"),
|
||||
"nonce": base64.b64encode(nonce).decode("ascii"),
|
||||
"ct": base64.b64encode(ciphertext).decode("ascii"),
|
||||
}
|
||||
return json.dumps(envelope, ensure_ascii=False)
|
||||
|
||||
|
||||
def decrypt_config(value: Optional[str]) -> Optional[Dict[str, Any]]:
|
||||
"""Decrypt envelope if present; otherwise parse plaintext JSON.
|
||||
|
||||
Returns dict on success, or None on failure.
|
||||
"""
|
||||
if not value:
|
||||
return None
|
||||
# If looks like encrypted envelope, decrypt.
|
||||
if is_encrypted(value):
|
||||
if AESGCM is None:
|
||||
# Cannot decrypt without cryptography
|
||||
return None
|
||||
try:
|
||||
obj = json.loads(value)
|
||||
key = _get_key()
|
||||
aesgcm = AESGCM(key)
|
||||
nonce = base64.b64decode(obj["nonce"])
|
||||
ct = base64.b64decode(obj["ct"])
|
||||
plaintext = aesgcm.decrypt(nonce, ct, None)
|
||||
return json.loads(plaintext.decode("utf-8"))
|
||||
except Exception:
|
||||
return None
|
||||
# Fallback: treat as plaintext JSON
|
||||
try:
|
||||
return json.loads(value)
|
||||
except Exception:
|
||||
return None
|
||||
Reference in new issue
Block a user