```
feat(feishu): 添加飞书入站事件inbox和混合数据库协调功能 - 实现飞书入站事件持久化inbox机制,支持状态管理、租约锁定和重试退避 - 添加混合数据库基线协调工具,确保平台PostgreSQL结构安全对齐 - 增加运行组件心跳检测和readiness就绪检查机制 - 实现app_ticket事件的安全轮换和验证处理 - 添加生产环境运行编排和fail-closed安全机制 - 支持webhook快速确认和长连接独立进程处理 - 完善个人数据擦除时的待处理事件清理功能 ```
This commit is contained in:
@@ -3,7 +3,7 @@ import os
|
||||
from functools import lru_cache
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
from pydantic import Field, field_validator, model_validator
|
||||
from pydantic import Field, ValidationInfo, field_validator, model_validator
|
||||
from pydantic_settings import BaseSettings, NoDecode, SettingsConfigDict
|
||||
|
||||
from app.core.constants import (
|
||||
@@ -12,6 +12,7 @@ from app.core.constants import (
|
||||
DEFAULT_MODEL_PROVIDER,
|
||||
DEFAULT_OPENCLAW_ACTION_JSON,
|
||||
)
|
||||
from app.core.database.safety import database_password, database_target
|
||||
|
||||
_DOTENV_DISABLED = os.getenv("COMPANY_AI_DISABLE_DOTENV", "").strip().lower() in {
|
||||
"1",
|
||||
@@ -19,15 +20,43 @@ _DOTENV_DISABLED = os.getenv("COMPANY_AI_DISABLE_DOTENV", "").strip().lower() in
|
||||
"yes",
|
||||
"on",
|
||||
}
|
||||
_DOTENV_FILE = None if _DOTENV_DISABLED else ".env"
|
||||
_PRODUCTION_SECRET_MIN_LENGTH = 24
|
||||
_DATABASE_PASSWORD_MIN_LENGTH = 10
|
||||
_PLACEHOLDER_SECRET_PARTS = (
|
||||
"change-me",
|
||||
"changeme",
|
||||
"example",
|
||||
"placeholder",
|
||||
"replace-with",
|
||||
"your-",
|
||||
)
|
||||
|
||||
|
||||
def _is_unsafe_production_secret(value: str) -> bool:
|
||||
normalized = value.strip().casefold()
|
||||
return (
|
||||
len(normalized) < _PRODUCTION_SECRET_MIN_LENGTH
|
||||
or any(part in normalized for part in _PLACEHOLDER_SECRET_PARTS)
|
||||
)
|
||||
|
||||
|
||||
def _is_unsafe_database_password(value: str) -> bool:
|
||||
normalized = value.strip().casefold()
|
||||
return (
|
||||
len(normalized) < _DATABASE_PASSWORD_MIN_LENGTH
|
||||
or any(part in normalized for part in _PLACEHOLDER_SECRET_PARTS)
|
||||
)
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
"""Runtime settings loaded from environment variables and `.env`."""
|
||||
"""Runtime settings loaded from the environment and optional `.env` file."""
|
||||
|
||||
model_config = SettingsConfigDict(
|
||||
env_file=None if _DOTENV_DISABLED else ".env",
|
||||
env_file=_DOTENV_FILE,
|
||||
env_file_encoding="utf-8",
|
||||
extra="ignore",
|
||||
hide_input_in_errors=True,
|
||||
)
|
||||
|
||||
app_name: str = "Company AI Management Platform"
|
||||
@@ -63,6 +92,11 @@ class Settings(BaseSettings):
|
||||
feishu_encrypt_key: str | None = None
|
||||
feishu_default_chat_id: str | None = None
|
||||
feishu_default_tenant_key: str | None = None
|
||||
feishu_event_transport: Literal[
|
||||
"disabled",
|
||||
"webhook",
|
||||
"long_connection",
|
||||
] = "disabled"
|
||||
feishu_user_features_enabled: bool = False
|
||||
feishu_admin_identities: Annotated[list[str], NoDecode] = Field(default_factory=list)
|
||||
model_provider: str = DEFAULT_MODEL_PROVIDER
|
||||
@@ -189,11 +223,13 @@ class Settings(BaseSettings):
|
||||
|
||||
@field_validator(
|
||||
"feishu_app_type",
|
||||
"feishu_event_transport",
|
||||
mode="before",
|
||||
)
|
||||
@classmethod
|
||||
def normalize_feishu_app_type(cls, value: Any) -> str:
|
||||
return str(value or "self").strip().lower()
|
||||
def normalize_feishu_choice(cls, value: Any, info: ValidationInfo) -> str:
|
||||
default = "self" if info.field_name == "feishu_app_type" else "disabled"
|
||||
return str(value or default).strip().lower()
|
||||
|
||||
@field_validator(
|
||||
"openclaw_allowed_tools",
|
||||
@@ -291,12 +327,34 @@ class Settings(BaseSettings):
|
||||
errors: list[str] = []
|
||||
api_key_values = _enabled_keys(self.api_key, self.api_keys)
|
||||
audit_key_values = _enabled_keys(self.audit_api_key, self.audit_api_keys)
|
||||
if self.database_url.startswith("sqlite"):
|
||||
platform_target = database_target(self.database_url)
|
||||
legacy_target = database_target(self.legacy_database_url)
|
||||
if platform_target is None or platform_target[0] != "postgresql":
|
||||
errors.append("DATABASE_URL must use PostgreSQL in production")
|
||||
platform_password = database_password(self.database_url)
|
||||
if platform_password and _is_unsafe_database_password(platform_password):
|
||||
errors.append(
|
||||
"DATABASE_URL password must use a non-placeholder value of at least "
|
||||
f"{_DATABASE_PASSWORD_MIN_LENGTH} characters in production"
|
||||
)
|
||||
if platform_target is not None and platform_target == legacy_target:
|
||||
errors.append(
|
||||
"DATABASE_URL and LEGACY_DATABASE_URL must target different databases"
|
||||
)
|
||||
if not api_key_values:
|
||||
errors.append("API_KEY or API_KEYS is required in production")
|
||||
elif any(_is_unsafe_production_secret(value) for value in api_key_values):
|
||||
errors.append(
|
||||
"API_KEY/API_KEYS must use non-placeholder values of at least "
|
||||
f"{_PRODUCTION_SECRET_MIN_LENGTH} characters in production"
|
||||
)
|
||||
if not audit_key_values:
|
||||
errors.append("AUDIT_API_KEY or AUDIT_API_KEYS is required in production")
|
||||
elif any(_is_unsafe_production_secret(value) for value in audit_key_values):
|
||||
errors.append(
|
||||
"AUDIT_API_KEY/AUDIT_API_KEYS must use non-placeholder values of at least "
|
||||
f"{_PRODUCTION_SECRET_MIN_LENGTH} characters in production"
|
||||
)
|
||||
if api_key_values & audit_key_values:
|
||||
errors.append(
|
||||
"API_KEY/API_KEYS and AUDIT_API_KEY/AUDIT_API_KEYS cannot overlap in production"
|
||||
@@ -309,10 +367,52 @@ class Settings(BaseSettings):
|
||||
errors.append("MASK_SENSITIVE_RESPONSES must be true in production")
|
||||
if not self.read_only_mode:
|
||||
errors.append("READ_ONLY_MODE must be true in production")
|
||||
if self.feishu_user_features_enabled and not self.feishu_admin_identities:
|
||||
errors.append(
|
||||
"FEISHU_ADMIN_IDENTITIES is required when Feishu user features are enabled"
|
||||
)
|
||||
if self.feishu_event_transport != "disabled":
|
||||
if not self.feishu_app_id or not self.feishu_app_secret:
|
||||
errors.append(
|
||||
"FEISHU_APP_ID and FEISHU_APP_SECRET are required when "
|
||||
"Feishu event transport is enabled"
|
||||
)
|
||||
elif _is_unsafe_production_secret(self.feishu_app_secret):
|
||||
errors.append(
|
||||
"FEISHU_APP_SECRET must use a non-placeholder value of at least "
|
||||
f"{_PRODUCTION_SECRET_MIN_LENGTH} characters in production"
|
||||
)
|
||||
if (
|
||||
self.feishu_event_transport == "webhook"
|
||||
and not self.feishu_verification_token
|
||||
):
|
||||
errors.append(
|
||||
"FEISHU_VERIFICATION_TOKEN is required for Feishu webhook transport"
|
||||
)
|
||||
elif (
|
||||
self.feishu_event_transport == "webhook"
|
||||
and self.feishu_verification_token
|
||||
and _is_unsafe_production_secret(self.feishu_verification_token)
|
||||
):
|
||||
errors.append(
|
||||
"FEISHU_VERIFICATION_TOKEN must use a non-placeholder value of at "
|
||||
f"least {_PRODUCTION_SECRET_MIN_LENGTH} characters in production"
|
||||
)
|
||||
if self.feishu_user_features_enabled:
|
||||
if not self.feishu_admin_identities:
|
||||
errors.append(
|
||||
"FEISHU_ADMIN_IDENTITIES is required when Feishu user features are enabled"
|
||||
)
|
||||
else:
|
||||
try:
|
||||
from app.modules.feishu_users.constants import (
|
||||
parse_admin_identities,
|
||||
)
|
||||
|
||||
parse_admin_identities(self.feishu_admin_identities)
|
||||
except ValueError as exc:
|
||||
errors.append(str(exc))
|
||||
if self.feishu_event_transport == "disabled":
|
||||
errors.append(
|
||||
"FEISHU_EVENT_TRANSPORT must be webhook or long_connection "
|
||||
"when Feishu user features are enabled"
|
||||
)
|
||||
if errors:
|
||||
raise ValueError("; ".join(errors))
|
||||
return self
|
||||
|
||||
Reference in New Issue
Block a user