Files
knowledge-governance/internal/governance/ui_settings.go
T
姚章浩 1db21fb899 feat(settings): 服务配置四项化并保存时真校验
- 生产/测试 WeKnora 地址、租户 ID、知识库 ID 由静态 env 收敛为版本化 UI 设置,保留修订号与操作人审计
- 新增 serviceverify:保存前真调目标端点探测,端点无效才拒绝;网络抖动或无凭据仅告警
- 迁移 000042(生产 WeKnora 端点)/000043(服务目标);补齐设置候选与校验契约用例
2026-09-30 10:54:07 +08:00

123 lines
4.2 KiB
Go

package governance
import (
"strconv"
"strings"
"time"
)
type UISettings struct {
ID string `json:"id"`
State string `json:"state"`
DocumentRefreshSeconds int `json:"document_refresh_seconds"`
ResourceRefreshSeconds int `json:"resource_refresh_seconds"`
StalledAfterSeconds int `json:"stalled_after_seconds"`
FinalizingAfterSeconds int `json:"finalizing_after_seconds"`
IngestionConcurrency int `json:"ingestion_concurrency"`
TestWeKnoraBaseURL string `json:"test_weknora_base_url"`
ProductionWeKnoraBaseURL string `json:"production_weknora_base_url"`
ProductionTenantID string `json:"production_tenant_id"`
ProductionKnowledgeBaseID string `json:"production_knowledge_base_id"`
UpdatedBy string `json:"updated_by"`
CreatedAt time.Time `json:"created_at"`
ActivatedAt *time.Time `json:"activated_at"`
RetiredAt *time.Time `json:"retired_at"`
ErrorCode string `json:"error_code"`
}
type CreateUISettingsCandidateRequest struct {
DocumentRefreshSeconds int
ResourceRefreshSeconds int
StalledAfterSeconds int
FinalizingAfterSeconds int
IngestionConcurrency int
TestWeKnoraBaseURL string
ProductionWeKnoraBaseURL string
ProductionTenantID string
ProductionKnowledgeBaseID string
UpdatedBy string
Now time.Time
}
func (request CreateUISettingsCandidateRequest) Valid() bool {
return oneOfInt(request.DocumentRefreshSeconds, 15, 30, 60) &&
oneOfInt(request.ResourceRefreshSeconds, 30, 60) &&
oneOfInt(request.StalledAfterSeconds, 1800, 3600) &&
oneOfInt(request.FinalizingAfterSeconds, 7200, 14400) &&
request.IngestionConcurrency >= 1 && request.IngestionConcurrency <= 16 &&
ValidTestWeKnoraBaseURL(request.TestWeKnoraBaseURL) &&
ValidProductionWeKnoraBaseURL(request.ProductionWeKnoraBaseURL) &&
ValidProductionTenantID(request.ProductionTenantID) &&
ValidKnowledgeBaseID(request.ProductionKnowledgeBaseID) &&
validSettingsActor(request.UpdatedBy) && !request.Now.IsZero()
}
// ValidProductionWeKnoraBaseURL accepts the operator-configured production
// WeKnora endpoint for the console: empty means "not configured" (clearing the
// field is legal and honest), anything else must be a bare http(s) origin.
// Credentials stay file references — never part of this value.
func ValidProductionWeKnoraBaseURL(value string) bool {
if value == "" {
return true
}
return ValidHTTPOrigin(value)
}
// ValidTestWeKnoraBaseURL is the same rule as the production endpoint: the
// test environment is also operator-provided, empty means "not configured".
func ValidTestWeKnoraBaseURL(value string) bool {
if value == "" {
return true
}
return ValidHTTPOrigin(value)
}
// ValidProductionTenantID accepts the WeKnora tenant of the production
// environment (the release contract's tenant_id). It is a decimal identifier,
// so anything with a sign, separator or letter is a typing mistake. Empty means
// "not configured".
func ValidProductionTenantID(value string) bool {
if value == "" {
return true
}
if len(value) > 20 {
return false
}
for _, char := range value {
if char < '0' || char > '9' {
return false
}
}
parsed, err := strconv.ParseUint(value, 10, 64)
return err == nil && parsed > 0
}
// ValidKnowledgeBaseID accepts the target knowledge base identifier of the
// production environment. It is an opaque id (letters, digits, '.', '_', '-',
// ':'), never a path or URL fragment: whitespace, '/' , '?', '#' and '\\' are
// rejected so a pasted URL cannot masquerade as an id. Empty means "not
// configured".
func ValidKnowledgeBaseID(value string) bool {
if value == "" {
return true
}
if len(value) > 128 {
return false
}
return !strings.ContainsAny(value, "\x00\r\n /?#\\")
}
func oneOfInt(value int, allowed ...int) bool {
for _, candidate := range allowed {
if value == candidate {
return true
}
}
return false
}
func validSettingsActor(value string) bool {
trimmed := strings.TrimSpace(value)
return trimmed == value && len(trimmed) >= 2 && len(trimmed) <= 128 && !strings.ContainsAny(trimmed, "\x00\r\n\t")
}