130 lines
7.7 KiB
Python
130 lines
7.7 KiB
Python
from pathlib import Path
|
|
import re
|
|
from urllib.parse import urlsplit
|
|
from pydantic import model_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True)
|
|
environment: str = "production"
|
|
role: str = "connector"
|
|
redis_url: str = "redis://127.0.0.1:6379/0"
|
|
data_dir: Path = Path("runtime/connector")
|
|
database_url: str = ""
|
|
admin_secret: str = ""
|
|
internal_secret: str = ""
|
|
ca_cert: Path = Path("runtime/pki/ca.pem")
|
|
connector_key: Path = Path("runtime/pki/connector.key")
|
|
connector_public_key: Path = Path("runtime/pki/connector.pub")
|
|
public_url: str = "https://relay.example.invalid"
|
|
router_url: str = "wss://relay.example.invalid/device/ws"
|
|
device_domain: str = ""
|
|
feishu_api_base: str = "https://open.feishu.cn"
|
|
feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize"
|
|
feishu_oauth_scopes: str = ""
|
|
feishu_app_id: str = ""
|
|
feishu_app_secret: str = ""
|
|
feishu_encrypt_key: str = ""
|
|
feishu_verification_token: str = ""
|
|
feishu_enabled: bool = False
|
|
oauth_enabled: bool = True
|
|
feishu_event_source: str = "longconn"
|
|
proxy_enabled: bool = False
|
|
# A fixed allowlist, never a user supplied upstream URL.
|
|
proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents"
|
|
request_timeout: int = 45
|
|
task_tracking_timeout: int = 1800
|
|
max_body: int = 262144
|
|
max_pending: int = 1000
|
|
max_devices: int = 1000
|
|
max_connections: int = 1000
|
|
max_result: int = 16000
|
|
work_files_enabled: bool = False
|
|
work_task_cards_enabled: bool = False
|
|
work_remote_approvals_enabled: bool = False
|
|
feishu_tenant_key: str = ''
|
|
approval_retention_enabled: bool = False
|
|
approval_payload_retention_days: int = 30
|
|
approval_audit_retention_days: int = 90
|
|
work_file_max_bytes: int = 10*1024*1024
|
|
work_file_batch_limit: int = 20
|
|
work_file_retention: int = 86400
|
|
work_file_owner_quota_bytes: int = 512*1024*1024
|
|
work_file_global_quota_bytes: int = 2*1024*1024*1024
|
|
allow_insecure_local: bool = False
|
|
enrollment_signer_url: str = ""
|
|
credential_mode: str = "legacy"
|
|
credential_keyring_file: Path | None = None
|
|
credential_permission_profiles: dict[str, list[str]] = {}
|
|
connector_internal_url: str = "http://connector:8000"
|
|
router_admission_secret: str = ""
|
|
|
|
@model_validator(mode="after")
|
|
def validate_runtime(self):
|
|
if not 1<=self.approval_payload_retention_days<=self.approval_audit_retention_days<=365:
|
|
raise ValueError('approval retention requires 1 <= payload days <= audit days <= 365')
|
|
if self.approval_retention_enabled and not self.work_remote_approvals_enabled:
|
|
raise ValueError('approval retention requires remote approvals')
|
|
if self.work_remote_approvals_enabled:
|
|
if (self.credential_mode!='broker'or self.feishu_event_source!='longconn'
|
|
or not self.feishu_enabled or not self.feishu_verification_token
|
|
or not re.fullmatch(r'[A-Za-z0-9_-]{1,128}',self.feishu_tenant_key)):
|
|
raise ValueError('remote approvals require broker, longconn and explicit callback identity')
|
|
if self.work_task_cards_enabled and self.credential_mode!='broker':raise ValueError('task cards require broker mode')
|
|
if not 1024<=self.work_file_max_bytes<=100*1024*1024 or not 1<=self.work_file_batch_limit<=50 or not 300<=self.work_file_retention<=7*86400:
|
|
raise ValueError('invalid work file deployment limits')
|
|
if self.work_files_enabled and self.credential_mode!='broker':raise ValueError('work files require broker mode')
|
|
if not self.work_file_max_bytes<=self.work_file_owner_quota_bytes<=self.work_file_global_quota_bytes:
|
|
raise ValueError('invalid work file storage quotas')
|
|
if not 30 <= self.task_tracking_timeout <= 3600:
|
|
raise ValueError("task tracking timeout must be 30-3600 seconds")
|
|
if self.credential_mode not in {"legacy", "broker"}:
|
|
raise ValueError("invalid credential mode")
|
|
if self.credential_mode == "broker" and self.role in {"connector","router"}:
|
|
if (len(self.router_admission_secret) < 32 or self.router_admission_secret in {
|
|
self.internal_secret,self.admin_secret,self.feishu_app_secret}):
|
|
raise ValueError("broker mode requires an independent router admission credential")
|
|
for profile, scopes in self.credential_permission_profiles.items():
|
|
if (not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", profile) or not scopes or len(scopes) > 128
|
|
or any(not re.fullmatch(r"[A-Za-z0-9_:.-]{1,256}", scope) for scope in scopes)):
|
|
raise ValueError("invalid credential permission profile")
|
|
if self.credential_mode == "broker" and self.role == "connector":
|
|
if self.environment == "production" and not self.database_url:
|
|
raise ValueError("production broker mode requires PostgreSQL")
|
|
if (not self.credential_keyring_file or not self.credential_permission_profiles
|
|
or not self.feishu_enabled or not self.oauth_enabled):
|
|
raise ValueError("broker mode requires keyring, product permissions and enabled platform OAuth")
|
|
authorize = urlsplit(self.feishu_authorize_url)
|
|
if (authorize.scheme != "https" or not authorize.hostname or authorize.username or authorize.password
|
|
or authorize.query or authorize.fragment):
|
|
raise ValueError("broker mode requires a fixed HTTPS authorization endpoint")
|
|
if self.credential_mode == "broker" and self.role == "router":
|
|
target = urlsplit(self.connector_internal_url)
|
|
if (not self.feishu_app_id or target.scheme not in {"http","https"}
|
|
or not target.hostname or target.username or target.password or target.query or target.fragment
|
|
or target.path not in {"","/"}):
|
|
raise ValueError("broker router requires authenticated fixed internal connector")
|
|
if self.environment not in {"production", "test", "development"}:
|
|
raise ValueError("invalid environment")
|
|
if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32:
|
|
raise ValueError("admin/internal secrets must each be at least 32 characters")
|
|
if self.role == "longconn" and len(self.internal_secret) < 32:
|
|
raise ValueError("internal credential must be at least 32 characters")
|
|
if self.role == "connector" and self.admin_secret == self.internal_secret:
|
|
raise ValueError("admin and internal credentials must be different")
|
|
if self.environment == "production":
|
|
if self.allow_insecure_local or not self.public_url.startswith("https://"):
|
|
raise ValueError("production requires HTTPS")
|
|
if not self.router_url.startswith("wss://"):
|
|
raise ValueError("production requires WSS")
|
|
if not self.feishu_api_base.startswith("https://"):
|
|
raise ValueError("platform API requires HTTPS")
|
|
if self.feishu_event_source not in {"longconn", "webhook"}:
|
|
raise ValueError("invalid platform event source")
|
|
if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)):
|
|
raise ValueError("platform app ID and secret are required")
|
|
if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)):
|
|
raise ValueError("webhook requires encryption key and verification token")
|
|
return self
|