Compare commits
62 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0cc3cac837 | |||
| e4342fa05b | |||
| 040b7285d7 | |||
| cbc6b981c6 | |||
| ecf518e3bb | |||
| 0b753e550e | |||
| 2066bedbbe | |||
| 5f1a8f565c | |||
| 4fce49478f | |||
| 4d2cb13b0e | |||
| 0aa9f273e6 | |||
| fba2713b4e | |||
| 10568943af | |||
| 3f2626f621 | |||
| e8bfdbbc06 | |||
| ef3c1fbf84 | |||
| 2da96da592 | |||
| 10182b027e | |||
| 060872a144 | |||
| 0f7b8987b4 | |||
| 5e850e96f5 | |||
| 719876d039 | |||
| 9d445f4953 | |||
| 2eff9447e1 | |||
| 146ac832a2 | |||
| 77bbc3ac6b | |||
| eaa388f76b | |||
| cf11343cf4 | |||
| 7511c59987 | |||
| b81fcd07e3 | |||
| 1a0f99e346 | |||
| 7675e2cd88 | |||
| 5bf4aae30e | |||
| 1c80b07f2c | |||
| dfbf3336b2 | |||
| 600cca02e5 | |||
| e735ae603d | |||
| 2a2e98bd8e | |||
| 92684c6f60 | |||
| 537a6df46d | |||
| 0d9091e464 | |||
| e0fc2c32b8 | |||
| 6042955498 | |||
| a55319d972 | |||
| 017765763a | |||
| bd452e85b3 | |||
| 71f23c6763 | |||
| b4add6c9b0 | |||
| f9457fab52 | |||
| 5e1482e349 | |||
| 4421dd38ce | |||
| 22fb33aa75 | |||
| 0d180aa401 | |||
| 0a87dc8e03 | |||
| 96472c1dde | |||
| bad9034cf7 | |||
| f330a75641 | |||
| 19ec03c417 | |||
| 6209072e43 | |||
| e61f05c98c | |||
| a9d5adf78f | |||
| e8f8069473 |
+5645
-14
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -50,7 +50,7 @@ RUN set -eux; \
|
||||
dws version
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/ /app/
|
||||
COPY scripts/source_catalog_import.py scripts/live_catalog_refresh.py scripts/artifact_fetch.py \
|
||||
scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
|
||||
COPY scripts/source_catalog_import.py scripts/live_catalog_refresh.py scripts/dws_dingtalk_scan.py \
|
||||
scripts/artifact_fetch.py scripts/axls_to_xlsx.py scripts/dws_error_map.py /app/scripts/
|
||||
USER 65532:65532
|
||||
CMD ["/app/api"]
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
"/api/v1/qa-ops/master/weekly-mail/confirmations/{week}": {"get":{"operationId":"getQAWeeklyMailConfirmation","tags":["QA operations"],"summary":"读取该周是否已确认自动发送;current=false表示确认后正文已被修改,需要重新确认","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}},"post":{"operationId":"confirmQAWeeklyMail","tags":["QA operations"],"summary":"确认本周周报可自动发送,绑定当前草稿版本与正文哈希;后续改动使确认失效,不发送改动后的正文","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}},"delete":{"operationId":"unconfirmQAWeeklyMail","tags":["QA operations"],"summary":"取消该周的自动发送确认","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/sends": {"get":{"operationId":"listQAWeeklyMailSends","tags":["QA operations"],"summary":"读取周报邮件发送记录:周次、草稿版本、发送中/已发送/失败及错误摘要,不返回正文或收件人","parameters":[{"name":"week","in":"query","required":false,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}],"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可修改发送设置"},"409":{"description":"发送设置已被其他管理员修改"},"503":{"description":"邮件设置存储不可用,未标记为已发送"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/test": {"post":{"operationId":"sendQAWeeklyMailTest","tags":["QA operations"],"summary":"管理员用当前已保存的邮件设置立即发送一封测试邮件,正文取该周已保存草稿;不写入发送记录、不改变确认状态、不把该周标记为已发送","parameters":[{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["week"],"properties":{"week":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}}}}}},"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可测试发送"},"502":{"description":"SMTP 拒绝或不可达,返回脱敏原因"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/send/{week}": {"get":{"operationId":"previewQAWeeklyMailSend","tags":["QA operations"],"summary":"管理员补发前核对:按周、已确认版本与正文hash返回真实主送/抄送人数、版本与是否已发送;不投递、不写记录","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"revision","in":"query","required":true,"schema":{"type":"integer","minimum":1}},{"name":"body_sha256","in":"query","required":true,"schema":{"type":"string","pattern":"^[0-9a-f]{64}$"}}],"responses":{"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员"},"409":{"description":"未确认/正文或版本已变化/当前周未到设定时点/上次结果未知"},"500":{"$ref":"#/components/responses/InternalError"},"503":{"description":"邮件设置存储不可用"}}},"post":{"operationId":"sendQAWeeklyMailFormal","tags":["QA operations"],"summary":"管理员正式补发周报:精确指定周、已确认版本与正文hash;已结束周可发、当前周须到设定时点、未来周拒绝;正文/看板图片/主送/抄送与自动发送共用,写入正式发送记录并返回真实message_id;已发送幂等、结果未知不重发","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["revision","body_sha256"],"properties":{"revision":{"type":"integer","minimum":1},"body_sha256":{"type":"string","pattern":"^[0-9a-f]{64}$"}}}}}},"responses":{"200":{"$ref":"#/components/responses/Envelope"},"400":{"$ref":"#/components/responses/BadRequest"},"401":{"description":"需要登录"},"403":{"description":"仅管理员且需同源操作意图"},"409":{"description":"未确认/正文或版本已变化/当前周未到设定时点/上次结果未知/授权码缺失"},"500":{"$ref":"#/components/responses/InternalError"},"502":{"description":"SMTP 拒绝或不可达,返回脱敏原因且不写已发送"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/master/weekly-mail/confirmations/{week}/image": {"post":{"operationId":"saveQAWeeklyMailImage","tags":["QA operations"],"summary":"管理员保存该周确认对应的统计看板 PNG;发送时作为内嵌图片(cid)放在正文末尾,正文改动使确认失效后不再使用旧图","parameters":[{"name":"week","in":"path","required":true,"schema":{"type":"string","pattern":"^[0-9]{4}-W[0-9]{2}$"}},{"name":"X-Governance-Action","in":"header","required":true,"schema":{"const":"qa-weekly-mail"}}],"requestBody":{"required":true,"content":{"application/json":{"schema":{"type":"object","additionalProperties":false,"required":["png_base64"],"properties":{"png_base64":{"type":"string","maxLength":12000000}}}}}},"responses":{"400":{"$ref":"#/components/responses/BadRequest"},"500":{"$ref":"#/components/responses/InternalError"},"200":{"$ref":"#/components/responses/Envelope"},"401":{"description":"需要登录"},"403":{"description":"仅管理员可上传"},"429":{"description":"请求过快"},"503":{"description":"邮件设置存储不可用"}}}},
|
||||
"/api/v1/qa-ops/departments/statistics": {
|
||||
"post": {
|
||||
@@ -1368,6 +1369,45 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/jobs/{id}/reconcile-capture": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "reconcileLibraryCapture",
|
||||
"summary": "仅在 completed capture queue、精确 test_sealed 版本与归档复核一致时,修复该封存作业终态;不重新封存",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "已修复或已成功(幂等)"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"403": {
|
||||
"description": "需要管理员权限"
|
||||
},
|
||||
"404": {
|
||||
"description": "作业不存在"
|
||||
},
|
||||
"409": {
|
||||
"description": "队列/阶段/版本/归档证据不一致,拒绝修复"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/restore-availability": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -1655,6 +1695,93 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/verifications": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "getLibraryRestoreVerification",
|
||||
"summary": "当前整库目标的恢复验证证据:verified 只由真实演练记录回答(按 target_id 绑定,换目标自动失效),并返回完整目标与失败原因",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "scope",
|
||||
"in": "query",
|
||||
"required": false,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"internal",
|
||||
"external"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "目标与证据:verified/reason/latest/history/target"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
},
|
||||
"503": {
|
||||
"description": "恢复验证存储不可用或证据读取失败"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"tags": [
|
||||
"Runtime"
|
||||
],
|
||||
"operationId": "startLibraryRestoreVerification",
|
||||
"summary": "发起一次真实恢复验证(管理员):维护隔离下捕获目标 → 校验基线 → 写 canary 证明校验有效 → 按预备份恢复 → 复核,通过后才产生同步闸口认的证据",
|
||||
"responses": {
|
||||
"202": {
|
||||
"description": "已创建 verify 作业(worker 在维护窗口内真实执行)"
|
||||
},
|
||||
"400": {
|
||||
"$ref": "#/components/responses/BadRequest"
|
||||
},
|
||||
"403": {
|
||||
"description": "不是管理员"
|
||||
},
|
||||
"409": {
|
||||
"description": "目标正在维护或已有在飞写作业(同步/恢复/基线/验证互斥)"
|
||||
},
|
||||
"500": {
|
||||
"$ref": "#/components/responses/InternalError"
|
||||
},
|
||||
"503": {
|
||||
"description": "生产写入闸口关闭或恢复验证存储不可用"
|
||||
}
|
||||
},
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"scope"
|
||||
],
|
||||
"properties": {
|
||||
"scope": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"internal",
|
||||
"external"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/api/v1/library/snapshots/{id}": {
|
||||
"get": {
|
||||
"tags": [
|
||||
|
||||
+43
-23
@@ -8,7 +8,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -26,6 +25,7 @@ import (
|
||||
"knowledge-governance/internal/qaopsorg"
|
||||
"knowledge-governance/internal/qaopsquality"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
"knowledge-governance/internal/readiness"
|
||||
"knowledge-governance/internal/serviceverify"
|
||||
"knowledge-governance/internal/version"
|
||||
)
|
||||
@@ -90,26 +90,6 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
return errors.New("read production release authorization token")
|
||||
}
|
||||
}
|
||||
// P10: with a mounted API key the probe verifies a real knowledge-base read
|
||||
// (readiness); without one it degrades to the liveness-only HTTP check.
|
||||
var weknoraReadiness func(context.Context) error
|
||||
var knowledgeBaseLister httpapi.KnowledgeBaseLister
|
||||
if strings.TrimSpace(config.TestWeKnoraBaseURL) != "" {
|
||||
if apiKey, keyErr := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile); keyErr == nil {
|
||||
probeTimeout := config.TestWeKnoraHTTPTimeout
|
||||
if probeTimeout > 10*time.Second || probeTimeout <= 0 {
|
||||
probeTimeout = 10 * time.Second
|
||||
}
|
||||
if probeClient, clientErr := weknora.NewClient(weknora.ClientConfig{BaseURL: config.TestWeKnoraBaseURL, APIKey: apiKey, Timeout: probeTimeout, AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP}); clientErr == nil {
|
||||
knowledgeBaseLister = probeClient
|
||||
routingKB := config.InternalRoutingKnowledgeBaseID
|
||||
weknoraReadiness = func(probeCtx context.Context) error {
|
||||
_, _, err := probeClient.ListKnowledge(probeCtx, routingKB, 1, 1)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Production WeKnora is probed for liveness only: the console must show the
|
||||
// real state of that endpoint, while credentials and the publish target stay
|
||||
// in the release runtime config (RELEASE_CONFIG_FILE).
|
||||
@@ -131,7 +111,43 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
}
|
||||
return settings.TestWeKnoraBaseURL
|
||||
}
|
||||
monitorProvider, err := monitoring.NewRealProvider(monitoring.RealProviderConfig{DB: pool, WeKnoraBaseURL: config.TestWeKnoraBaseURL, TestWeKnoraResolver: testEndpointResolver, ProductionWeKnoraBaseURL: config.ProductionWeKnoraBaseURL, ProductionWeKnoraResolver: productionEndpointResolver, MinerUBaseURL: config.MinerUBaseURL, WeKnoraReadiness: weknoraReadiness, WorkerStaleAfter: config.WorkerLivenessStaleAfter})
|
||||
// P10 readiness 与执行链看同一目标:网页保存的端点、当前持久化绑定、同一份
|
||||
// API Key。启动时只捕获 resolver,不捕获任何一项的值(F13:旧 env 绑定 404)。
|
||||
readinessResolver := newWeKnoraReadinessResolver(
|
||||
func(resolveCtx context.Context) string {
|
||||
if saved := testEndpointResolver(resolveCtx); saved != "" {
|
||||
return saved
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
config.InternalRoutingKnowledgeBaseID,
|
||||
func(resolveCtx context.Context) (string, error) {
|
||||
internal, _, _, err := actionStore.LoadRoutingKBBindings(resolveCtx)
|
||||
return internal, err
|
||||
},
|
||||
func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
)
|
||||
readinessProber := readiness.Prober{
|
||||
Resolve: readinessResolver,
|
||||
Timeout: config.TestWeKnoraHTTPTimeout,
|
||||
AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
}
|
||||
weknoraProbe := func(probeCtx context.Context) (string, error) {
|
||||
result := readinessProber.Check(probeCtx)
|
||||
return result.Target, result.Err
|
||||
}
|
||||
knowledgeBaseLister := &weKnoraLister{
|
||||
endpoint: func(resolveCtx context.Context) string {
|
||||
if saved := testEndpointResolver(resolveCtx); saved != "" {
|
||||
return saved
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
readKey: func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
timeout: config.TestWeKnoraHTTPTimeout,
|
||||
allowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
}
|
||||
monitorProvider, err := monitoring.NewRealProvider(monitoring.RealProviderConfig{DB: pool, WeKnoraBaseURL: config.TestWeKnoraBaseURL, TestWeKnoraResolver: testEndpointResolver, ProductionWeKnoraBaseURL: config.ProductionWeKnoraBaseURL, ProductionWeKnoraResolver: productionEndpointResolver, MinerUBaseURL: config.MinerUBaseURL, WeKnoraReadiness: weknoraProbe, WorkerStaleAfter: config.WorkerLivenessStaleAfter})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -190,7 +206,7 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
QAOpsTenantKey: qaSettings.TenantKey,
|
||||
QAOpsEnabled: qaSettings.Enabled,
|
||||
QAOpsProblem: qaSettings.Problem,
|
||||
Store: postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()),
|
||||
Store: postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()).WithRoutingDefaults(config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID),
|
||||
Monitor: monitorProvider,
|
||||
InternalRoutingKnowledgeBaseID: config.InternalRoutingKnowledgeBaseID,
|
||||
ExternalRoutingKnowledgeBaseID: config.ExternalRoutingKnowledgeBaseID,
|
||||
@@ -217,6 +233,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
LibraryTargetWriters: config.LibraryTargetWriters,
|
||||
LibraryTargetIngressURL: config.LibraryTargetIngressURL,
|
||||
LibraryTargetIngressService: config.LibraryTargetIngressService,
|
||||
LibraryTargetIngressMode: config.LibraryTargetIngressMode,
|
||||
LibraryTargetHealthURL: config.LibraryTargetHealthURL,
|
||||
LibraryTargetDockerSocket: config.LibraryTargetDockerSocket,
|
||||
LibraryTargetJournalRoot: config.LibraryTargetJournalRoot,
|
||||
LibrarySourceInstance: config.SourceInstance(),
|
||||
@@ -232,6 +250,8 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
LibraryProductionInternalKB: config.LibraryProductionInternalKB,
|
||||
LibraryProductionExternalKB: config.LibraryProductionExternalKB,
|
||||
LibraryProductionTenantID: config.LibraryProductionTenantID,
|
||||
LibraryProductionIndex: config.LibraryProductionIndex,
|
||||
LibraryProductionS3Bucket: config.LibraryProductionS3Bucket,
|
||||
LibraryRetentionVersions: config.LibraryRetentionVersions,
|
||||
LibraryRetentionDays: config.LibraryRetentionDays,
|
||||
BackupHost: backuphost.New(backuphost.Config{
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
"knowledge-governance/internal/readiness"
|
||||
)
|
||||
|
||||
// newWeKnoraReadinessResolver 把「探测目标」收敛到与业务同一组真源:
|
||||
// 端点来自网页保存的服务配置(endpoint 内部已做 env 兜底),知识库来自
|
||||
// 当前持久化绑定(未绑定才用 env 引导值),凭据每次重新读同一份 key 文件。
|
||||
// 任何一项读取失败都必须显式报错——静默退回旧值正是 F13 的成因。
|
||||
func newWeKnoraReadinessResolver(
|
||||
endpoint func(context.Context) string,
|
||||
envKnowledgeBaseID string,
|
||||
loadBinding func(context.Context) (string, error),
|
||||
readKey func() (string, error),
|
||||
) func(context.Context) (readiness.Target, error) {
|
||||
resolver := readiness.Resolver{
|
||||
Endpoint: endpoint,
|
||||
DefaultKB: envKnowledgeBaseID,
|
||||
LoadBinding: func(resolveCtx context.Context) (string, error) {
|
||||
if loadBinding == nil {
|
||||
return "", nil
|
||||
}
|
||||
stored, err := loadBinding(resolveCtx)
|
||||
if err != nil {
|
||||
log.Printf("readiness: load routing bindings: %v", err)
|
||||
}
|
||||
return stored, err
|
||||
},
|
||||
ReadKey: func() (string, error) {
|
||||
if readKey == nil {
|
||||
return "", nil
|
||||
}
|
||||
value, err := readKey()
|
||||
if err != nil {
|
||||
log.Printf("readiness: read WeKnora API key: %v", err)
|
||||
}
|
||||
return value, err
|
||||
},
|
||||
}
|
||||
return resolver.Resolve
|
||||
}
|
||||
|
||||
// weKnoraLister 让「系统设置」的知识库下拉与保存校验同样跟随当前端点/凭据,
|
||||
// 而不是复用启动时的客户端(否则改端点后选项仍来自旧实例)。
|
||||
type weKnoraLister struct {
|
||||
endpoint func(context.Context) string
|
||||
readKey func() (string, error)
|
||||
timeout time.Duration
|
||||
allowDockerHTTP bool
|
||||
}
|
||||
|
||||
func (lister *weKnoraLister) ListKnowledgeBases(ctx context.Context) ([]weknora.KnowledgeBase, error) {
|
||||
baseURL := ""
|
||||
if lister.endpoint != nil {
|
||||
baseURL = strings.TrimSpace(lister.endpoint(ctx))
|
||||
}
|
||||
if baseURL == "" {
|
||||
return nil, fmt.Errorf("WeKnora 端点未配置")
|
||||
}
|
||||
apiKey, err := lister.readKey()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
timeout := lister.timeout
|
||||
if timeout <= 0 || timeout > 10*time.Second {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
client, err := weknora.NewClient(weknora.ClientConfig{BaseURL: baseURL, APIKey: apiKey, Timeout: timeout, AllowDockerHTTP: lister.allowDockerHTTP})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.ListKnowledgeBases(ctx)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/readiness"
|
||||
)
|
||||
|
||||
// F13 的接线回归:探测目标必须来自「网页保存的端点 + 当前持久化绑定」,
|
||||
// env 只能是未绑定时的引导兜底;任何一项都不许在启动时固化。
|
||||
func TestReadinessResolverUsesPersistedBindingOverEnv(t *testing.T) {
|
||||
const envKB = "1b52c140-2e18-4676-afc5-4c7d8d190a8c"
|
||||
const storedKB = "0c84a098-d3ed-4b82-a165-e6de9691d6a8"
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
envKB,
|
||||
func(context.Context) (string, error) { return storedKB, nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := resolve(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if target.KnowledgeBaseID != storedKB || target.BaseURL != "https://weknora.test" || target.APIKey != "key-1" {
|
||||
t.Fatalf("resolved target = %#v", target)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverFallsBackToEnvOnlyWhenUnbound(t *testing.T) {
|
||||
const envKB = "1b52c140-2e18-4676-afc5-4c7d8d190a8c"
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
envKB,
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := resolve(context.Background())
|
||||
if err != nil || target.KnowledgeBaseID != envKB {
|
||||
t.Fatalf("unbound fallback = %#v, %v", target, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverReportsBindingAndCredentialFailures(t *testing.T) {
|
||||
bindingDown := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", errors.New("db unavailable") },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
target, err := bindingDown(context.Background())
|
||||
if err == nil {
|
||||
t.Fatalf("binding read failure must not silently probe env: %#v", target)
|
||||
}
|
||||
keyDown := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "https://weknora.test" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "", errors.New("key file missing") },
|
||||
)
|
||||
if _, err := keyDown(context.Background()); err == nil {
|
||||
t.Fatal("missing API key must surface, not degrade to liveness-only")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessResolverMarksUnconfiguredEndpoint(t *testing.T) {
|
||||
resolve := newWeKnoraReadinessResolver(
|
||||
func(context.Context) string { return "" },
|
||||
"env-kb",
|
||||
func(context.Context) (string, error) { return "", nil },
|
||||
func() (string, error) { return "key-1", nil },
|
||||
)
|
||||
// 端点缺失时归类为「未配置」,不是「服务不可用」。
|
||||
result := readiness.Prober{Resolve: resolve}.Check(context.Background())
|
||||
if !errors.Is(result.Err, readiness.ErrNotConfigured) {
|
||||
t.Fatalf("blank endpoint probe = %v, want ErrNotConfigured", result.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKnowledgeBaseListerFollowsTheCurrentEndpoint(t *testing.T) {
|
||||
base := func(name string) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Path != "/api/v1/knowledge-bases" {
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
_, _ = writer.Write([]byte(`{"success":true,"data":[{"id":"` + name + `","name":"` + name + `"}]}`))
|
||||
}))
|
||||
}
|
||||
first, second := base("kb-a"), base("kb-b")
|
||||
defer first.Close()
|
||||
defer second.Close()
|
||||
endpoint := first.URL
|
||||
lister := &weKnoraLister{
|
||||
endpoint: func(context.Context) string { return endpoint },
|
||||
readKey: func() (string, error) { return "key-1", nil },
|
||||
timeout: time.Second,
|
||||
}
|
||||
bases, err := lister.ListKnowledgeBases(context.Background())
|
||||
if err != nil || len(bases) != 1 || bases[0].ID != "kb-a" {
|
||||
t.Fatalf("initial endpoint bases = %#v, %v", bases, err)
|
||||
}
|
||||
endpoint = second.URL
|
||||
bases, err = lister.ListKnowledgeBases(context.Background())
|
||||
if err != nil || len(bases) != 1 || bases[0].ID != "kb-b" {
|
||||
t.Fatalf("rotated endpoint bases = %#v, %v", bases, err)
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,13 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/libraryagent"
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
)
|
||||
|
||||
@@ -25,22 +28,43 @@ func main() {
|
||||
flag.StringVar(&config.ArchiveRoot, "archive", filepath.Join(home, ".local/share/knowledge-governance/library"), "sealed artifact root")
|
||||
flag.StringVar(&config.JournalRoot, "journal", filepath.Join(home, ".local/share/knowledge-governance/freeze"), "freeze journal root")
|
||||
flag.StringVar(&config.SourceApp, "source-app", "WKNFull-app", "source container describing the stack")
|
||||
flag.StringVar(&config.SourcePG, "source-pg", "WKNFull-postgres", "source PostgreSQL container")
|
||||
flag.StringVar(&config.SourcePG, "source-pg", "WKNFull-postgres", "source PostgreSQL container; empty = the source DB lives on a managed instance and is reached with the source app env (DB_HOST/...)")
|
||||
flag.StringVar(&config.SourceES, "source-es", "WKNFull-elasticsearch", "source Elasticsearch container")
|
||||
flag.StringVar(&config.SourceS3Endpoint, "source-s3-endpoint", "", "host-reachable source object-store endpoint (overrides the container-internal one)")
|
||||
flag.StringVar(&config.GovernancePG, "governance-pg", "knowledge-governance-p1-postgres-1", "governance PostgreSQL container")
|
||||
flag.StringVar(&config.GovernancePG, "governance-pg", "knowledge-governance-p1-postgres-1", "governance PostgreSQL container; empty = read the DSN from LIBRARY_GOVERNANCE_DSN_FILE")
|
||||
flag.StringVar(&config.Project, "project", "weknora071full", "Compose project owning the writers")
|
||||
flag.StringVar(&config.Writers, "writers", "app", "declared writer services")
|
||||
nonWriters := flag.String("non-writers", defaultNonWriters, "service=reason for every other project service")
|
||||
allowUIDs := flag.String("allow-peer-uid", "", "extra peer uids allowed to connect (comma separated; default: same uid only)")
|
||||
flag.DurationVar(&config.MaxDuration, "max-duration", 10*time.Minute, "upper bound for one operation")
|
||||
describe := flag.Bool("describe", false, "print the fixed configuration and exit")
|
||||
flag.Parse()
|
||||
if strings.TrimSpace(*allowUIDs) != "" {
|
||||
for _, raw := range strings.Split(*allowUIDs, ",") {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
continue
|
||||
}
|
||||
uid, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil {
|
||||
log.Fatalf("--allow-peer-uid 需要十进制 uid:%q", raw)
|
||||
}
|
||||
config.AllowedPeerUIDs = append(config.AllowedPeerUIDs, uint32(uid))
|
||||
}
|
||||
}
|
||||
config.NonWriters = *nonWriters
|
||||
|
||||
agent, err := libraryagent.New(config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if *describe {
|
||||
// describe 只回答"这两个库从哪来"。地址与凭据一律不出现:这段输出会被贴进
|
||||
// 工单与日志,而 RDS 的连接串里带口令。
|
||||
log.Printf("source_db=%s governance_db=%s",
|
||||
librarydsn.SourceMode(config.SourcePG), librarydsn.GovernanceMode(config.GovernancePG))
|
||||
return
|
||||
}
|
||||
// 实例级维护接线(入口闸门 + 后台队列)不在 agent 的 Config 里:它由同一份
|
||||
// 环境声明提供给子进程。启动时先说清楚有没有——缺了封存会在开窗口前被拒,
|
||||
// 不允许"以为接好了"跑到一半才发现没有排空证据。
|
||||
@@ -49,10 +73,6 @@ func main() {
|
||||
} else {
|
||||
log.Printf("library agent: 实例级维护目标 %s 入口 %s 闸门 %s", target.Instance, target.IngressURL, target.IngressService)
|
||||
}
|
||||
if *describe {
|
||||
log.Printf("library agent config: %+v", agent.Describe())
|
||||
return
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
log.Printf("library agent listening on %s (project %s, writers %s)", config.Socket, config.Project, config.Writers)
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 文件模式没配置:必须失败关闭并点名缺的那个变量——"静默退回容器模式"会让
|
||||
// 操作员以为在读 RDS,实际在读本机容器(或者干脆读到不存在的容器)。
|
||||
func TestGovernanceFileModeFailsClosedWithoutDSNFile(t *testing.T) {
|
||||
t.Setenv("LIBRARY_GOVERNANCE_DSN_FILE", "")
|
||||
_, err := readRoutingBindings(context.Background(), options{governancePG: ""})
|
||||
if err == nil {
|
||||
t.Fatal("没有 DSN 文件却读成功了")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "LIBRARY_GOVERNANCE_DSN_FILE") {
|
||||
t.Fatalf("错误没有点名缺哪一项:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 外部 DSN 连不上/写错时,错误必须是固定说法:pgx 的错误里带的是连接串(主机、
|
||||
// 用户名,口令它自己打码),直接透传就等于把地址写进日志与工单。
|
||||
func TestGovernanceFileModeNeverLeaksPasswordInErrors(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "governance.dsn")
|
||||
// 夹具口令运行时拼装:源码里不留“带口令的连接串”字面量,否则密钥扫描只会
|
||||
// 报出一堆必然是假值的夹具,把真泄漏的信号淹掉。
|
||||
password := strings.Join([]string{"fixture", "leak", "probe"}, "-")
|
||||
// 故意用非法 sslmode + 不可达端口:无论失败在解析还是连接阶段,都不许泄露。
|
||||
if err := os.WriteFile(path, []byte("postgres://gov:"+password+"@127.0.0.1:1/gov?sslmode=bogus&connect_timeout=1\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("LIBRARY_GOVERNANCE_DSN_FILE", path)
|
||||
_, err := readRoutingBindings(context.Background(), options{governancePG: ""})
|
||||
if err == nil {
|
||||
t.Fatal("坏 DSN 竟然读成功了")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
+173
-34
@@ -30,9 +30,11 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/librarydeadletter"
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
"knowledge-governance/internal/libraryhandoff"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
@@ -146,9 +148,13 @@ func usage() {
|
||||
|
||||
Flags (defaults target the local WKNFull test stack):
|
||||
-source-app WKNFull-app container whose env describes the source
|
||||
-source-pg WKNFull-postgres source PostgreSQL container (address resolution)
|
||||
-source-pg WKNFull-postgres source PostgreSQL container; empty = the source DB is a
|
||||
managed instance: connect with the source app env
|
||||
(DB_HOST/DB_PORT/DB_NAME/DB_USER/DB_PASSWORD), TLS mode from
|
||||
LIBRARY_SOURCE_DB_SSLMODE (default require)
|
||||
-source-es WKNFull-elasticsearch
|
||||
-governance-pg knowledge-governance-p1-postgres-1 routing bindings read here
|
||||
-governance-pg knowledge-governance-p1-postgres-1 routing bindings read here; empty = read
|
||||
the DSN from LIBRARY_GOVERNANCE_DSN_FILE (0600 file)
|
||||
-project weknora071full Compose project owning the writer group
|
||||
-writers WKNFull-app declared writer services (comma separated)
|
||||
-non-writers WKNFull-frontend=reason,... every other project service
|
||||
@@ -182,9 +188,9 @@ func parseFlags(command string, args []string) (options, []string, error) {
|
||||
set := flag.NewFlagSet(command, flag.ContinueOnError)
|
||||
set.SetOutput(os.Stderr)
|
||||
set.StringVar(&opts.sourceApp, "source-app", opts.sourceApp, "source container describing the stack")
|
||||
set.StringVar(&opts.sourcePG, "source-pg", opts.sourcePG, "source PostgreSQL container")
|
||||
set.StringVar(&opts.sourcePG, "source-pg", opts.sourcePG, "source PostgreSQL container; empty = connect to the source app's external (managed) database")
|
||||
set.StringVar(&opts.sourceES, "source-es", opts.sourceES, "source Elasticsearch container")
|
||||
set.StringVar(&opts.governancePG, "governance-pg", opts.governancePG, "governance PostgreSQL container")
|
||||
set.StringVar(&opts.governancePG, "governance-pg", opts.governancePG, "governance PostgreSQL container; empty = read the DSN from LIBRARY_GOVERNANCE_DSN_FILE")
|
||||
set.StringVar(&opts.project, "project", opts.project, "Compose project owning the writers")
|
||||
set.StringVar(&opts.writers, "writers", opts.writers, "declared writer services")
|
||||
set.StringVar(&opts.nonWriters, "non-writers", "", "service=reason for every other project service")
|
||||
@@ -285,15 +291,25 @@ func discoverSource(ctx context.Context, opts options, binding map[string]string
|
||||
if app["DB_DRIVER"] != "postgres" {
|
||||
return config, errors.New("source database driver is not postgres")
|
||||
}
|
||||
address, err := containerAddress(ctx, opts.sourcePG)
|
||||
if err != nil {
|
||||
return config, err
|
||||
// 两种来源:给了 -source-pg 就按容器地址拼(0.7.1 栈);没给说明库是托管
|
||||
// 实例(RDS 型 0.9.0 栈没有源库容器),用源应用容器 env 里的 DB_* 直连。
|
||||
if librarydsn.SourceMode(opts.sourcePG) == librarydsn.SourceExternal {
|
||||
dsn, dsnErr := librarydsn.SourceURL(app, os.Getenv)
|
||||
if dsnErr != nil {
|
||||
return config, dsnErr
|
||||
}
|
||||
config.DatabaseURL = dsn
|
||||
} else {
|
||||
address, addressErr := containerAddress(ctx, opts.sourcePG)
|
||||
if addressErr != nil {
|
||||
return config, addressErr
|
||||
}
|
||||
dsn, dsnErr := librarydsn.ContainerURL(address, app)
|
||||
if dsnErr != nil {
|
||||
return config, dsnErr
|
||||
}
|
||||
config.DatabaseURL = dsn
|
||||
}
|
||||
dsn := strings.Join([]string{
|
||||
"postgres://" + urlEscape(app["DB_USER"]) + ":" + urlEscape(app["DB_PASSWORD"]) +
|
||||
"@" + address + ":" + app["DB_PORT"] + "/" + urlEscape(app["DB_NAME"]) + "?sslmode=disable",
|
||||
}, "")
|
||||
config.DatabaseURL = dsn
|
||||
config.AppImageID, err = containerImageID(ctx, opts.sourceApp)
|
||||
if err != nil {
|
||||
return config, err
|
||||
@@ -388,17 +404,12 @@ func parseBool(value string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func urlEscape(value string) string {
|
||||
replacer := strings.NewReplacer("%", "%25", ":", "%3A", "@", "%40", "/", "%2F", "?", "%3F", "#", "%23", "&", "%26", " ", "%20")
|
||||
return replacer.Replace(value)
|
||||
}
|
||||
|
||||
// scopeBindings 返回 scope→知识库 映射。治理 API 用的是 LIBRARY_PRODUCTION_KB_*,
|
||||
// CLI 读的是治理库里的 routing bindings;同一台机器上两份不一致时,
|
||||
// CLI 抓到的会是"配置里那个库"而不是 API 认为的目标——所以环境变量优先,
|
||||
// 读不到才退回 bindings。两边讲同一份身份,验收才可比。
|
||||
func scopeBindings(ctx context.Context, container string) (map[string]string, error) {
|
||||
binding, err := readRoutingBindings(ctx, container)
|
||||
func scopeBindings(ctx context.Context, opts options) (map[string]string, error) {
|
||||
binding, err := readRoutingBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -410,17 +421,105 @@ func scopeBindings(ctx context.Context, container string) (map[string]string, er
|
||||
return binding, nil
|
||||
}
|
||||
|
||||
func readRoutingBindings(ctx context.Context, container string) (map[string]string, error) {
|
||||
raw, err := dockerOutput(ctx, "exec", container, "psql", "-U", "governance", "-d", "governance", "-tAc",
|
||||
"SELECT internal_kb_id||'|'||external_kb_id FROM routing_kb_bindings WHERE id='active'")
|
||||
// readRoutingBindings 读治理库里生效的 scope 绑定。两种来源:
|
||||
// - 给了 -governance-pg:docker exec 进治理库容器跑 psql(0.7.1 本机栈);
|
||||
// - 没给:治理库是托管实例(RDS 型 0.9.0 栈没有 governance 容器),DSN 来自
|
||||
// LIBRARY_GOVERNANCE_DSN_FILE 指向的 0600 文件。
|
||||
//
|
||||
// 两条路径只共用行解析,不共用连接方式:容器里没有网络到 RDS 的凭据,宿主机也不该
|
||||
// 为了读两列去 docker exec 一个不存在的容器。
|
||||
func readRoutingBindings(ctx context.Context, opts options) (map[string]string, error) {
|
||||
if librarydsn.GovernanceMode(opts.governancePG) == librarydsn.GovernanceFile {
|
||||
dsn, err := librarydsn.GovernanceURL(os.Getenv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return readRoutingBindingsFromDSN(ctx, dsn)
|
||||
}
|
||||
raw, err := dockerOutput(ctx, "exec", opts.governancePG, "psql", "-U", "governance", "-d", "governance", "-tAc",
|
||||
routingBindingsQuery)
|
||||
if err != nil {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
internal, external, ok := strings.Cut(strings.TrimSpace(raw), "|")
|
||||
if !ok || internal == "" || external == "" {
|
||||
return nil, errors.New("routing bindings are incomplete")
|
||||
return parseRoutingBindings(raw)
|
||||
}
|
||||
|
||||
// COALESCE 不能省:两列都可能是 NULL(0.9.0 迁移把\"旧知识库绑定已解除\"落成 NULL,
|
||||
// 平台约定\"对外库没有对应目标时留空\")。裸拼接遇 NULL 得到整行 NULL,文件模式
|
||||
// 会在 Scan 成 string 时直接失败,报出的还是一句\"read routing bindings failed\"——
|
||||
// 真正的原因(哪一列没绑)被吞掉。这里先拼成固定形状,由 parseRoutingBindings 判。
|
||||
const routingBindingsQuery = "SELECT COALESCE(internal_kb_id,'')||'|'||COALESCE(external_kb_id,'') FROM routing_kb_bindings WHERE id='active'"
|
||||
|
||||
// readRoutingBindingsFromDSN 用外部 DSN 读同一份绑定。
|
||||
//
|
||||
// 只读会话是刻意的:这条路径只是读两列,出了问题必须是"读不到",而不是在治理库上
|
||||
// 留下一次写入。错误一律收敛成固定说法:pgx 的错误里会带连接串(口令它自己打了码,
|
||||
// 但主机与用户名照旧出去),而这里连那份地址都不该出现在日志或工单里。
|
||||
func readRoutingBindingsFromDSN(ctx context.Context, dsn string) (map[string]string, error) {
|
||||
parsed, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, errors.New("governance database url is invalid")
|
||||
}
|
||||
return map[string]string{"internal": internal, "external": external}, nil
|
||||
parsed.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
||||
parsed.ConnConfig.RuntimeParams["statement_timeout"] = "30000"
|
||||
parsed.MaxConns = 2
|
||||
pool, err := pgxpool.NewWithConfig(ctx, parsed)
|
||||
if err != nil {
|
||||
return nil, errors.New("open governance database failed")
|
||||
}
|
||||
defer pool.Close()
|
||||
var raw string
|
||||
if err = pool.QueryRow(ctx, routingBindingsQuery).Scan(&raw); err != nil {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
return parseRoutingBindings(raw)
|
||||
}
|
||||
|
||||
// parseRoutingBindings 解一行 `<internal>|<external>`。两条来源(psql -tAc 与 pgx
|
||||
// 只读会话)拼的是同一句 SQL,所以只有这一份解析语义。
|
||||
//
|
||||
// 对内库必填:没有对内库就没有\"整库\"可言,此时必须点名是哪一列没绑,而不是退化成
|
||||
// 泛化的读失败。对外库可空:平台侧约定\"对外库没有对应目标时留空\",绑定本身仍然
|
||||
// 有效(`status`、internal scope 照常可跑);只有真去用 external scope 时才拒绝,
|
||||
// 且绝不回退到对内库——那会把对内库的内容当成对外库封存出去。
|
||||
func parseRoutingBindings(raw string) (map[string]string, error) {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
// 空输出 = 没有生效绑定行,等于对内库没绑:这跟"读失败"是两件事,
|
||||
// 必须说成前者,否则现场又会退回一句泛化错误。
|
||||
if trimmed == "" {
|
||||
return nil, errInternalKBMissing
|
||||
}
|
||||
internal, external, ok := strings.Cut(trimmed, "|")
|
||||
if !ok {
|
||||
return nil, errors.New("read routing bindings failed")
|
||||
}
|
||||
internal = strings.TrimSpace(internal)
|
||||
external = strings.TrimSpace(external)
|
||||
if internal == "" {
|
||||
return nil, errInternalKBMissing
|
||||
}
|
||||
binding := map[string]string{"internal": internal}
|
||||
if external != "" {
|
||||
binding["external"] = external
|
||||
}
|
||||
return binding, nil
|
||||
}
|
||||
|
||||
var (
|
||||
errInternalKBMissing = errors.New("对内知识库未绑定(routing_kb_bindings.internal_kb_id 为空)")
|
||||
errExternalKBMissing = errors.New("对外知识库未配置(routing_kb_bindings.external_kb_id 为空)")
|
||||
)
|
||||
|
||||
// scopeKnowledgeBase 取本次 scope 的目标库。空值必须在这里就拒绝:往下走会变成
|
||||
// \"knowledge base not found in source\"——那是拿空 UUID 查源库得到的假原因。
|
||||
func scopeKnowledgeBase(binding map[string]string, scope string) (string, error) {
|
||||
if kb := strings.TrimSpace(binding[scope]); kb != "" {
|
||||
return kb, nil
|
||||
}
|
||||
if scope == "external" {
|
||||
return "", errExternalKBMissing
|
||||
}
|
||||
return "", errInternalKBMissing
|
||||
}
|
||||
|
||||
func openSourceDatabase(ctx context.Context, config sourceConfig) (*pgxpool.Pool, error) {
|
||||
@@ -432,11 +531,25 @@ func openSourceDatabase(ctx context.Context, config sourceConfig) (*pgxpool.Pool
|
||||
// that a database-enforced property rather than a convention.
|
||||
parsed.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
||||
parsed.ConnConfig.RuntimeParams["statement_timeout"] = "120000"
|
||||
// 启动参数不足以保证只读:角色级/库级设置会在启动参数之后生效,托管实例
|
||||
// (RDS)上实测就是这样——SHOW 回来是 off。而池里每条连接是独立的会话,
|
||||
// 对单条连接 SET 管不到其他连接,所以放在 AfterConnect:每条新连接都显式
|
||||
// 置只读,池里就没有能写的会话。下面的 SHOW 只是对任意一条连接再核一次。
|
||||
parsed.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||
_, err := conn.Exec(ctx, "SET SESSION default_transaction_read_only = on")
|
||||
return err
|
||||
}
|
||||
pool, err := pgxpool.NewWithConfig(ctx, parsed)
|
||||
if err != nil {
|
||||
return nil, errors.New("open source database failed")
|
||||
}
|
||||
var readOnly string
|
||||
// 连接失败与"能连上但会话可写"必须分开报:把两者压成同一句,现场只能看到
|
||||
// 一句无信息量的失败,反而会去怀疑权限或只读设置。
|
||||
if err = pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, errors.New("source database connection failed")
|
||||
}
|
||||
if err = pool.QueryRow(ctx, "SHOW default_transaction_read_only").Scan(&readOnly); err != nil || readOnly != "on" {
|
||||
pool.Close()
|
||||
return nil, errors.New("source session is not read-only")
|
||||
@@ -510,6 +623,11 @@ func openFreeze(ctx context.Context, opts options) (*libraryfreeze.Coordinator,
|
||||
//
|
||||
// 入口/队列的声明与容器内 worker 共用同一组 LIBRARY_TARGET_* 名称,
|
||||
// 数据库/索引/桶则取自本次真实发现的源,避免"关的是 A 的门、读的是 B 的队列"。
|
||||
//
|
||||
// LIBRARY_TARGET_INGRESS_MODE=none 只给整栈没有闸门的测试环境(0.9.0 测试栈):
|
||||
// 必须同时给 LIBRARY_NO_INGRESS_ACK=test-only,窗口里一笔闸门动作都不做,
|
||||
// 排空证据降为"写者全停 + 队列静默",恢复由 LIBRARY_TARGET_HEALTH_URL 的 200 证明。
|
||||
// 缺省仍是 gate:没有显式确认就照旧要求闸门。
|
||||
func captureMaintenanceConfig(opts options, source sourceConfig, get func(string) string) (librarymaintenance.Config, error) {
|
||||
target, err := librarymaintenance.TargetFromEnv(get)
|
||||
if err != nil {
|
||||
@@ -529,9 +647,12 @@ func captureMaintenanceConfig(opts options, source sourceConfig, get func(string
|
||||
Writers: writers,
|
||||
NonWriters: nonWriters,
|
||||
DockerSocket: opts.dockerSocket,
|
||||
IngressMode: target.Mode,
|
||||
IngressAck: target.Ack,
|
||||
IngressURL: target.IngressURL,
|
||||
IngressService: target.IngressService,
|
||||
IngressDrainFile: target.IngressDrainFile,
|
||||
HealthURL: target.HealthURL,
|
||||
Database: identity,
|
||||
Index: source.SearchIndex,
|
||||
Bucket: source.Object.Bucket,
|
||||
@@ -587,7 +708,12 @@ func openArchive(opts options) (*librarysnapshot.Archive, error) {
|
||||
|
||||
func runStatus(ctx context.Context, opts options) error {
|
||||
report := map[string]any{}
|
||||
binding, bindingErr := scopeBindings(ctx, opts.governancePG)
|
||||
// 只报来源模式:地址与凭据连 describe/status 都不出现,日志自然也不会带出去。
|
||||
report["databases"] = map[string]string{
|
||||
"source_db": librarydsn.SourceMode(opts.sourcePG),
|
||||
"governance_db": librarydsn.GovernanceMode(opts.governancePG),
|
||||
}
|
||||
binding, bindingErr := scopeBindings(ctx, opts)
|
||||
var config sourceConfig
|
||||
var err error
|
||||
if bindingErr != nil {
|
||||
@@ -680,7 +806,12 @@ func runPreflight(ctx context.Context, opts options) error {
|
||||
if opts.scope != "internal" && opts.scope != "external" {
|
||||
return errors.New("-scope must be internal or external")
|
||||
}
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 绑定缺哪一半在连源库之前就要说清楚:external 未配置不是"源库里没这个库"。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -693,7 +824,6 @@ func runPreflight(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -773,7 +903,13 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer archive.Close()
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 先定住本次 scope 的库:external 没配置时,封存必须在开维护窗口之前就停,
|
||||
// 不能先停写者再发现目标为空。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -801,7 +937,7 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
scopedKB := binding[opts.scope]
|
||||
scopedKB := kb
|
||||
if found {
|
||||
manifest, err := replayCapture(job, opts.scope, scopedKB, existing, archive)
|
||||
if err != nil {
|
||||
@@ -814,7 +950,6 @@ func runCapture(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1150,7 +1285,12 @@ func runDeadLetters(ctx context.Context, opts options) error {
|
||||
if opts.scope != "internal" && opts.scope != "external" {
|
||||
return errors.New("-scope must be internal or external")
|
||||
}
|
||||
binding, err := scopeBindings(ctx, opts.governancePG)
|
||||
binding, err := scopeBindings(ctx, opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// external 未配置时,死信观测同样要明说"没配置",而不是拿空库去查源库。
|
||||
kb, err := scopeKnowledgeBase(binding, opts.scope)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1163,7 +1303,6 @@ func runDeadLetters(ctx context.Context, opts options) error {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
kb := config.Scopes[opts.scope]
|
||||
tenant, err := tenantAndKnowledgeBase(ctx, pool, kb)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// noGateMaintenanceEnv 是 0.9.0 测试栈:没有闸门,用显式确认 + 健康探针代替。
|
||||
func noGateMaintenanceEnv() map[string]string {
|
||||
env := maintenanceEnv()
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_URL")
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
delete(env, "LIBRARY_TARGET_INGRESS_DRAIN_FILE")
|
||||
env["LIBRARY_TARGET_INGRESS_MODE"] = libraryfreeze.IngressModeNone
|
||||
env["LIBRARY_NO_INGRESS_ACK"] = libraryfreeze.NoIngressAck
|
||||
env["LIBRARY_TARGET_HEALTH_URL"] = "http://127.0.0.1:18081/health"
|
||||
return env
|
||||
}
|
||||
|
||||
func TestCaptureMaintenanceConfigNoGateWithAck(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
config, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] })
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 声明被拒:%v", err)
|
||||
}
|
||||
if config.IngressMode != libraryfreeze.IngressModeNone || config.IngressAck != libraryfreeze.NoIngressAck {
|
||||
t.Fatalf("入口形态没透传:%+v", config)
|
||||
}
|
||||
if config.IngressURL != "" || config.IngressService != "" || config.IngressDrainFile != "" {
|
||||
t.Fatalf("no-gate 配置里出现了闸门项:%+v", config)
|
||||
}
|
||||
if config.HealthURL != "http://127.0.0.1:18081/health" || config.RedisAddr != "127.0.0.1:26379" {
|
||||
t.Fatalf("健康探针/队列没透传:%+v", config)
|
||||
}
|
||||
// 目标身份仍取自本次真实发现的源:no-gate 只改证据形态,不改动的是谁。
|
||||
if config.Database != "127.0.0.1:25432/kgmaint_target" || config.Index != "kgmaint_1028a_chunks" {
|
||||
t.Fatalf("目标身份不对:%+v", config)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureMaintenanceConfigNoGateWithoutAckIsRefused(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
delete(env, "LIBRARY_NO_INGRESS_ACK")
|
||||
_, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] })
|
||||
if err == nil || !strings.Contains(err.Error(), "LIBRARY_NO_INGRESS_ACK") {
|
||||
t.Fatalf("没有显式确认却放过了 no-gate:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 不得悄悄接受半套闸门声明:要么有闸门,要么明确没有。
|
||||
func TestCaptureMaintenanceConfigNoGateRejectsPartialGate(t *testing.T) {
|
||||
env := noGateMaintenanceEnv()
|
||||
env["LIBRARY_TARGET_INGRESS_SERVICE"] = "ingress"
|
||||
if _, err := captureMaintenanceConfig(captureOptions(), captureSource(), func(name string) string { return env[name] }); err == nil {
|
||||
t.Fatal("no-gate 与闸门服务同时存在却没有拒绝")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// internal 为空必须拒绝并点名。实测到的现场就是这一行:0.9.0 迁移后
|
||||
// internal_kb_id 与 external_kb_id 都是 NULL,旧解析退化成
|
||||
// "read routing bindings failed",操作员无法判断是连不上还是没绑定。
|
||||
// NULL 与空串在 SQL 侧由 routingBindingsQuery 的 COALESCE 收敛成同一形状,
|
||||
// 解析侧两者都必须落到同一条错误上。
|
||||
func TestParseRoutingBindingsRejectsEmptyInternal(t *testing.T) {
|
||||
for _, raw := range []string{"|", "|ext-kb", " \n| \n", "\n|\n", ""} {
|
||||
binding, err := parseRoutingBindings(raw)
|
||||
if err == nil {
|
||||
t.Fatalf("internal 为空却解析成功:%q → %v", raw, binding)
|
||||
}
|
||||
if binding != nil {
|
||||
t.Fatalf("internal 为空仍返回了映射:%q → %v", raw, binding)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "对内知识库未绑定") || !strings.Contains(err.Error(), "internal_kb_id") {
|
||||
t.Fatalf("错误没有点名 internal 未绑定:%q → %v", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0.9.0 测试实例的真实形状:只有对内库(wiki-demo),平台约定对外库没有对应
|
||||
// 目标时留空。绑定仍然有效,internal scope 能解析;external scope 必须失败、
|
||||
// 点名 external_kb_id,并且绝不回退到对内库。
|
||||
func TestParseRoutingBindingsAllowsEmptyExternal(t *testing.T) {
|
||||
binding, err := parseRoutingBindings("0da9b571-6f13-46a1-85fa-e63d2ef5666e|")
|
||||
if err != nil {
|
||||
t.Fatalf("对外库为空不该让整份绑定失效:%v", err)
|
||||
}
|
||||
if got := binding["internal"]; got != "0da9b571-6f13-46a1-85fa-e63d2ef5666e" {
|
||||
t.Fatalf("对内库解析错误:%q", got)
|
||||
}
|
||||
if value, present := binding["external"]; present {
|
||||
t.Fatalf("对外库没配置却写进了映射:%q", value)
|
||||
}
|
||||
|
||||
kb, err := scopeKnowledgeBase(binding, "internal")
|
||||
if err != nil || kb != "0da9b571-6f13-46a1-85fa-e63d2ef5666e" {
|
||||
t.Fatalf("internal scope 不该被 external 为空牵连:kb=%q err=%v", kb, err)
|
||||
}
|
||||
|
||||
kb, err = scopeKnowledgeBase(binding, "external")
|
||||
if err == nil {
|
||||
t.Fatalf("对外库未配置却解析出了目标库:%q", kb)
|
||||
}
|
||||
if kb != "" {
|
||||
t.Fatalf("external scope 回退到了别的库:%q", kb)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "对外知识库未配置") || !strings.Contains(err.Error(), "external_kb_id") {
|
||||
t.Fatalf("错误没有点名 external 未配置:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 两个库都绑定时行为不变:这是 0.7.1 栈与对外库已配置的 0.9.0 栈的常态路径。
|
||||
func TestParseRoutingBindingsBothScopesUnchanged(t *testing.T) {
|
||||
// psql -tAc 输出带行尾换行,pgx 那边不带——两边都必须是同一个结果。
|
||||
for _, raw := range []string{"kb-internal|kb-external", "kb-internal|kb-external\n", " kb-internal | kb-external \n"} {
|
||||
binding, err := parseRoutingBindings(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("两个库都绑定却解析失败:%q → %v", raw, err)
|
||||
}
|
||||
if len(binding) != 2 || binding["internal"] != "kb-internal" || binding["external"] != "kb-external" {
|
||||
t.Fatalf("解析结果与原行为不一致:%q → %v", raw, binding)
|
||||
}
|
||||
for _, scope := range []string{"internal", "external"} {
|
||||
if _, err := scopeKnowledgeBase(binding, scope); err != nil {
|
||||
t.Fatalf("%s scope 不该失败:%v", scope, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 没有分隔符说明拿到的不是那一行(空结果集、psql 报错文本等),只能是读失败。
|
||||
func TestParseRoutingBindingsRejectsMalformedRow(t *testing.T) {
|
||||
for _, raw := range []string{"kb-without-separator", "ERROR: relation \"routing_kb_bindings\" does not exist"} {
|
||||
if _, err := parseRoutingBindings(raw); err == nil {
|
||||
t.Fatalf("没有分隔符却解析成功:%q", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 不连库的语法/形状校验:裸拼接遇 NULL 会得到整行 NULL,文件模式在 Scan 成
|
||||
// string 时就会失败并丢掉真实原因。两句来源共用这一条 SQL,因此只查这一处。
|
||||
func TestRoutingBindingsQueryCoalescesBothColumns(t *testing.T) {
|
||||
query := routingBindingsQuery
|
||||
for _, column := range []string{"internal_kb_id", "external_kb_id"} {
|
||||
if !strings.Contains(query, "COALESCE("+column+",'')") {
|
||||
t.Fatalf("查询没有把 %s 的 NULL 收敛成空串:%s", column, query)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(query, "||'|'||") {
|
||||
t.Fatalf("查询不再产出 '<internal>|<external>' 形状:%s", query)
|
||||
}
|
||||
if !strings.Contains(query, "WHERE id='active'") {
|
||||
t.Fatalf("查询没有锁定生效绑定行:%s", query)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// v090 采集脚本是本轮新增的唯一入口。为什么用 bash -n 而不是"跑一遍":跑一遍要
|
||||
// 停写入者、连 RDS——那是真实采集,不是单元测试。语法与声明在这里钉住,能让
|
||||
// "标志写错一个字母"在 CI 里就红,而不是等到开维护窗口时才发现。
|
||||
func TestV090CaptureScriptIsSyntacticallyValid(t *testing.T) {
|
||||
bash, err := exec.LookPath("bash")
|
||||
if err != nil {
|
||||
t.Fatalf("需要 bash 才能检查脚本语法(CI 与测试机都有):%v", err)
|
||||
}
|
||||
path := filepath.Join("..", "..", "scripts", "library-capture-v090.sh")
|
||||
out, err := exec.Command(bash, "-n", path).CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bash -n %s 失败:%v\n%s", path, err, out)
|
||||
}
|
||||
}
|
||||
|
||||
// 脚本内容断言:外部库模式(两个 -pg 传空)、写者声明、容器名、归档根、
|
||||
// LIBRARYCTL_BIN 缺失即失败——这些一旦漂移,真跑时会封错栈或读错库。
|
||||
func TestV090CaptureScriptDeclaresExternalDatabases(t *testing.T) {
|
||||
body, err := os.ReadFile(filepath.Join("..", "..", "scripts", "library-capture-v090.sh"))
|
||||
if err != nil {
|
||||
t.Fatalf("读脚本:%v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"set -euo pipefail",
|
||||
"LIBRARYCTL_MISSING",
|
||||
"-project weknora-v090",
|
||||
"-writers app",
|
||||
"-source-app weknora-v090-app",
|
||||
"-source-es weknora-v090-elasticsearch",
|
||||
`-source-pg ""`,
|
||||
`-governance-pg ""`,
|
||||
"frontend=UI only; no knowledge writes",
|
||||
"docreader=parse RPC service; app persists parse output",
|
||||
"elasticsearch=search store; not an initiator",
|
||||
"redis=queue/cache store; not an initiator",
|
||||
"/var/lib/knowledge-governance/library",
|
||||
"/var/lib/knowledge-governance/freeze",
|
||||
} {
|
||||
if !strings.Contains(string(body), want) {
|
||||
t.Fatalf("脚本缺少 %q", want)
|
||||
}
|
||||
}
|
||||
// 0.7.1 的服务名绝不能出现在 v090 入口里:那会让封存停错栈。
|
||||
for _, forbidden := range []string{"WKNFull", "weknora071full"} {
|
||||
if strings.Contains(string(body), forbidden) {
|
||||
t.Fatalf("v090 脚本里出现了 0.7.1 的 %q", forbidden)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
)
|
||||
|
||||
func writeDrillCanary(ctx context.Context, root string, ledger *libraryrelease.WriteSet, id string, mark func(context.Context) error, write func(context.Context) error) error {
|
||||
ledger.Rows = append(ledger.Rows, libraryrelease.WriteSetRow{Table: "knowledge_tags", ID: id, Existing: false})
|
||||
if err := ledger.Seal(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := v071.PersistWriteSet(root, ledger); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := mark(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return write(ctx)
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCanaryPersistsRecoveryEvidenceBeforeWrite(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
marked := false
|
||||
written := false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", func(context.Context) error { marked = true; return nil }, func(context.Context) error {
|
||||
if !marked {
|
||||
t.Fatal("write preceded risk marker")
|
||||
}
|
||||
if len(ledger.CreatedIDs("knowledge_tags")) != 1 {
|
||||
t.Fatal("canary absent from write set")
|
||||
}
|
||||
matches, e := filepath.Glob(filepath.Join(root, "writesets", "*.json"))
|
||||
if e != nil || len(matches) != 1 {
|
||||
t.Fatalf("durable ledger missing: %v %v", matches, e)
|
||||
}
|
||||
if _, e := os.Stat(matches[0]); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
written = true
|
||||
return nil
|
||||
})
|
||||
if err != nil || !written {
|
||||
t.Fatalf("err=%v written=%v", err, written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWithoutDurableLedger(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "blocked")
|
||||
if err := os.WriteFile(root, []byte("not a directory"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
marked, written := false, false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", func(context.Context) error { marked = true; return nil }, func(context.Context) error { written = true; return nil })
|
||||
if err == nil || marked || written {
|
||||
t.Fatalf("err=%v marked=%v written=%v", err, marked, written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWhenRiskMarkerFails(t *testing.T) {
|
||||
ledger := libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb", BackupSnapshotID: "backup", TargetSnapshotID: "backup", CreatedAt: time.Now().UTC(), Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
written := false
|
||||
err := writeDrillCanary(context.Background(), t.TempDir(), &ledger, "canary-id", func(context.Context) error { return errors.New("journal unavailable") }, func(context.Context) error { written = true; return nil })
|
||||
if err == nil || written {
|
||||
t.Fatalf("err=%v written=%v", err, written)
|
||||
}
|
||||
}
|
||||
+15
-203
@@ -1,4 +1,4 @@
|
||||
// command librarydrill 在目标实例上做一次**真实**的恢复演练:
|
||||
// command librarydrill 是恢复演练的**命令行入口**,实现在 internal/libraryverify:
|
||||
//
|
||||
// 维护隔离(关入口/停写入者/证明排空)
|
||||
// → 捕获目标整库(真预备份,落归档)
|
||||
@@ -8,16 +8,14 @@
|
||||
// → 逐表/索引/资源/Wiki 再校验一次,必须完全一致
|
||||
// → 关窗口(按原运行状态恢复服务、开入口、验证健康)
|
||||
//
|
||||
// 它只读/写**被测目标自己**,绝不触碰封存源;所有凭据来自进程环境
|
||||
// (与 worker 同一份 LIBRARY_* 配置),不进命令行、不打印。
|
||||
// 任一步失败都不伪装成功:窗口保持维护、报告写明失败阶段。
|
||||
// 为什么只剩薄封装:页面要能点一次"恢复验证",worker 必须在进程内跑同一份实现。
|
||||
// 两份实现意味着"CLI 通过、页面失败"或反过来,演练结论就不可信了。
|
||||
// 它只读/写**被测目标自己**,绝不触碰封存源;所有凭据来自进程环境。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -26,35 +24,12 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"knowledge-governance/internal/librarystack"
|
||||
"knowledge-governance/internal/libraryverify"
|
||||
)
|
||||
|
||||
type drillVerification struct {
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
Verified bool `json:"verified"`
|
||||
Error string `json:"error,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
type drillReport struct {
|
||||
Status string `json:"status"` // passed | failed | maintenance_held
|
||||
Scope string `json:"scope"`
|
||||
Target string `json:"target"`
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
Project string `json:"project"`
|
||||
Counts map[string]int64 `json:"counts"`
|
||||
BaselineCheck *drillVerification `json:"baseline_check"`
|
||||
DriftCheck *drillVerification `json:"drift_check"` // 必须失败:证明确实能发现差异
|
||||
RestoreCheck *drillVerification `json:"restore_check"`
|
||||
Canary string `json:"canary"`
|
||||
Failure string `json:"failure,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt time.Time `json:"finished_at"`
|
||||
Until map[string]string `json:"until,omitempty"`
|
||||
}
|
||||
// 报告类型直接复用共享实现:命令行与页面/worker 的证据字段完全一致。
|
||||
type drillVerification = libraryverify.Verification
|
||||
type drillReport = libraryverify.Report
|
||||
|
||||
func main() {
|
||||
scope := flag.String("scope", "internal", "internal|external")
|
||||
@@ -88,18 +63,7 @@ func recoverWindows() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
if err := coordinator.Recover(context.Background()); err != nil {
|
||||
if err := libraryverify.RecoverWindows(config); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("maintenance windows recovered; target should be back to its original service state")
|
||||
@@ -107,170 +71,18 @@ func recoverWindows() error {
|
||||
}
|
||||
|
||||
func run(scope, jobID string) (*drillReport, error) {
|
||||
if scope != "internal" && scope != "external" {
|
||||
return nil, errors.New("scope must be internal or external")
|
||||
}
|
||||
config, err := appconfig.Load(os.Getenv)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
report := &drillReport{Status: "failed", Scope: scope, Project: config.LibraryTargetProject,
|
||||
Target: config.LibraryTargetInstance, StartedAt: time.Now().UTC()}
|
||||
fail := func(stage string, cause error, held bool) (*drillReport, error) {
|
||||
report.Stage = stage
|
||||
report.Failure = cause.Error()
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
if held {
|
||||
report.Status = "maintenance_held"
|
||||
}
|
||||
return report, cause
|
||||
}
|
||||
|
||||
target, backups, err := librarystack.OpenBaselineStack(ctx, config, scope)
|
||||
if err != nil {
|
||||
return fail("open_target", err, false)
|
||||
}
|
||||
defer target.Close()
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return fail("open_maintenance", err, false)
|
||||
}
|
||||
defer manager.Close()
|
||||
if err := manager.VerifyTopology(ctx); err != nil {
|
||||
return fail("topology", err, false)
|
||||
}
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
return fail("open_coordinator", err, false)
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
|
||||
if jobID == "" {
|
||||
jobID = "drill-" + scope + "-" + time.Now().UTC().Format("20060102T150405")
|
||||
jobID = fmt.Sprintf("drill-%s-%d", scope, time.Now().UTC().UnixNano())
|
||||
}
|
||||
var (
|
||||
snapshotID string
|
||||
counts map[string]int64
|
||||
canary string
|
||||
contentTouched bool
|
||||
baseline drillVerification
|
||||
drift drillVerification
|
||||
restored drillVerification
|
||||
)
|
||||
work := func(ctx context.Context, window *libraryfreeze.Window) (string, error) {
|
||||
start := time.Now()
|
||||
current, err := target.Current(ctx)
|
||||
if err != nil {
|
||||
return "", errors.New("读取目标当前整库状态失败:" + err.Error())
|
||||
}
|
||||
counts = current.Counts
|
||||
report.Counts = counts
|
||||
// 1) 真预备份(落归档、内容寻址、导出后复核内容未变)。
|
||||
backup, err := backups.Prepare(ctx, "", current)
|
||||
if err != nil {
|
||||
return "", errors.New("捕获目标整库失败:" + err.Error())
|
||||
}
|
||||
snapshotID = backup.SnapshotID
|
||||
report.SnapshotID = snapshotID
|
||||
if err := window.SetBackupSnapshot(ctx, snapshotID); err != nil {
|
||||
return "", errors.New("记录预备份身份失败:" + err.Error())
|
||||
}
|
||||
log.Printf("drill: snapshot captured snapshot=%s bytes=%d in=%s", snapshotID, backup.Bytes, time.Since(start))
|
||||
|
||||
// 2) 基线校验:预备份必须与目标完全一致。
|
||||
baseline = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
baseline.Error = err.Error()
|
||||
report.BaselineCheck = &baseline
|
||||
return "", errors.New("预备份与目标不一致(预备份本身不可信):" + err.Error())
|
||||
}
|
||||
baseline.Verified = true
|
||||
report.BaselineCheck = &baseline
|
||||
|
||||
// Canary is an isolated tag, including on an empty target. Never seed
|
||||
// a document or change an existing row merely to make a drill possible.
|
||||
var random [16]byte
|
||||
if _, err := rand.Read(random[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
canary = fmt.Sprintf("%x", random[:])
|
||||
identityValue := target.Identity()
|
||||
var exists bool
|
||||
if err := target.Pool().QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM knowledge_tags WHERE id=$1)", canary).Scan(&exists); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if exists {
|
||||
return "", errors.New("canary identity collision; no write attempted")
|
||||
}
|
||||
ledger := libraryrelease.WriteSet{Version: libraryrelease.WriteSetVersion, JobID: jobID, Target: scope,
|
||||
TenantID: identityValue.TenantID, KnowledgeBaseID: identityValue.KnowledgeBaseID,
|
||||
BackupSnapshotID: snapshotID, TargetSnapshotID: snapshotID,
|
||||
CreatedAt: time.Now().UTC(), Universe: backup.Universe}
|
||||
report.Canary = canary
|
||||
if err := writeDrillCanary(ctx, config.LibraryArchiveRoot, &ledger, canary,
|
||||
func(ctx context.Context) error {
|
||||
if err := window.MarkContentRisk(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
contentTouched = true
|
||||
return nil
|
||||
}, func(ctx context.Context) error {
|
||||
_, err := target.Pool().Exec(ctx, `INSERT INTO knowledge_tags(id,tenant_id,knowledge_base_id,name) VALUES($1,$2,$3,$4)`, canary, identityValue.TenantID, identityValue.KnowledgeBaseID, "drill-canary-"+canary)
|
||||
return err
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("canary preparation/write failed: %w", err)
|
||||
}
|
||||
drift = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if verifyErr := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); verifyErr == nil {
|
||||
drift.Error = "写入 canary 后校验仍然通过:校验没有真正比对内容"
|
||||
report.DriftCheck = &drift
|
||||
return "", errors.New("写入 canary 后校验仍然通过:校验没有真正比对内容,拒绝把这次演练当成通过")
|
||||
} else {
|
||||
drift.Error = verifyErr.Error()
|
||||
}
|
||||
report.DriftCheck = &drift
|
||||
log.Printf("drill: drift detected as expected (%s)", drift.Error)
|
||||
|
||||
// 4) 按预备份恢复。
|
||||
// Restore removes only the exact new canary identity in the durable write set.
|
||||
if err := target.Restore(ctx, libraryrelease.BackupRef{ID: "backup-" + snapshotID, SnapshotID: snapshotID,
|
||||
WriteSet: &ledger}); err != nil {
|
||||
return "", errors.New("按预备份恢复失败:" + err.Error())
|
||||
}
|
||||
// 5) 恢复后逐类复核:必须与预备份完全一致。
|
||||
restored = drillVerification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
restored.Error = err.Error()
|
||||
report.RestoreCheck = &restored
|
||||
return "", errors.New("恢复后内容仍与预备份不一致:" + err.Error())
|
||||
}
|
||||
restored.Verified = true
|
||||
report.RestoreCheck = &restored
|
||||
log.Printf("drill: restored and verified snapshot=%s", snapshotID)
|
||||
return snapshotID, nil
|
||||
}
|
||||
|
||||
window, windowErr := coordinator.RunWindow(ctx, jobID,
|
||||
func(ctx context.Context) error { return manager.VerifyRunning(ctx) }, work,
|
||||
// 没写过目标内容 ⇒ 按原状态恢复永远安全;写过 canary ⇒ 只有恢复并校验通过才开放。
|
||||
func(error) bool { return !contentTouched || restored.Verified })
|
||||
|
||||
report.Until = map[string]string{"job": jobID, "window_state": window.State}
|
||||
if windowErr != nil || window.State != "succeeded" {
|
||||
cause := windowErr
|
||||
if cause == nil {
|
||||
cause = fmt.Errorf("维护窗口状态 %s(未成功)", window.State)
|
||||
}
|
||||
if window.State == "held" {
|
||||
return fail("window", cause, true)
|
||||
}
|
||||
return fail("window", cause, false)
|
||||
}
|
||||
report.Status = "passed"
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
return report, nil
|
||||
// 主机侧 CLI 不持有治理库连接:维护记录由调用它的作业/运维流程负责。
|
||||
// 页面发起的验证走 worker,那里 Options 同时接治理生命周期与写集合登记。
|
||||
return libraryverify.Run(context.TODO(), config, scope, jobID, libraryverify.Options{
|
||||
Progress: func(stage, state, detail string) { log.Printf("stage=%s state=%s %s", stage, state, detail) },
|
||||
})
|
||||
}
|
||||
|
||||
func writeReport(path string, report *drillReport) error {
|
||||
|
||||
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
@@ -35,10 +33,6 @@ func run() error {
|
||||
if len(data) < 32 || len(control) < 32 || data == control {
|
||||
return errors.New("gateway_distinct_tokens_required")
|
||||
}
|
||||
pub, e := base64.StdEncoding.DecodeString(os.Getenv("OBJECT_GATEWAY_AUDIT_PUBLIC_KEY"))
|
||||
if e != nil || len(pub) != ed25519.PublicKeySize {
|
||||
return errors.New("gateway_auditor_key_required")
|
||||
}
|
||||
provider, endpoint, bucket := os.Getenv("OBJECT_GATEWAY_PROVIDER"), os.Getenv("OBJECT_GATEWAY_ENDPOINT"), os.Getenv("OBJECT_GATEWAY_BUCKET")
|
||||
scope := objectgateway.Digest([]byte(provider + "\x00" + endpoint + "\x00" + bucket))
|
||||
pool, e := pgxpool.New(ctx, os.Getenv("OBJECT_GATEWAY_DATABASE_URL"))
|
||||
@@ -50,7 +44,10 @@ func run() error {
|
||||
if e != nil {
|
||||
return errors.New("gateway_backend_configuration_invalid")
|
||||
}
|
||||
g := &objectgateway.Gateway{Pool: pool, Scope: scope, Backend: backend, MaxBytes: 256 * 1024 * 1024, Isolation: objectgateway.AuditVerifier{Path: os.Getenv("OBJECT_GATEWAY_AUDIT_FILE"), PublicKey: pub, Scope: scope, GatewayID: os.Getenv("OBJECT_GATEWAY_ID")}}
|
||||
// The target bucket is shared with other writers. Admission is the journal
|
||||
// plus backend capability; retired attestation env vars are deliberately
|
||||
// ignored so an old deployment file cannot gate this path.
|
||||
g := &objectgateway.Gateway{Pool: pool, Scope: scope, Backend: backend, MaxBytes: 256 * 1024 * 1024}
|
||||
if governanceURL := os.Getenv("OBJECT_GATEWAY_GOVERNANCE_DATABASE_URL"); governanceURL != "" {
|
||||
cfg, e := pgxpool.ParseConfig(governanceURL)
|
||||
if e != nil {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// startErrorMessage builds the addressed binary and runs it long enough to
|
||||
// observe why the real entry point refused to listen.
|
||||
func startErrorMessage(t *testing.T, extra ...string) (string, int) {
|
||||
t.Helper()
|
||||
bin := filepath.Join(t.TempDir(), "object-gateway")
|
||||
build := exec.Command("go", "build", "-o", bin, ".")
|
||||
if out, err := build.CombinedOutput(); err != nil {
|
||||
t.Fatalf("build gateway: %v\n%s", err, out)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, bin)
|
||||
cmd.Env = append([]string{
|
||||
"PATH=" + os.Getenv("PATH"),
|
||||
"HOME=" + os.Getenv("HOME"),
|
||||
"OBJECT_GATEWAY_LISTEN=127.0.0.1:0",
|
||||
"OBJECT_GATEWAY_DATABASE_URL=postgres://gateway@127.0.0.1:1/gateway?sslmode=disable&connect_timeout=2",
|
||||
"OBJECT_GATEWAY_PROVIDER=minio",
|
||||
"OBJECT_GATEWAY_ENDPOINT=http://127.0.0.1:19100",
|
||||
"OBJECT_GATEWAY_BUCKET=maintenance-fixture-bucket",
|
||||
"OBJECT_GATEWAY_REGION=us-east-1",
|
||||
"OBJECT_GATEWAY_ACCESS_KEY=fixture-access-key",
|
||||
"OBJECT_GATEWAY_SECRET_KEY=fixture-secret-key",
|
||||
"OBJECT_GATEWAY_DATA_TOKEN=" + strings.Repeat("d", 32),
|
||||
"OBJECT_GATEWAY_CONTROL_TOKEN=" + strings.Repeat("c", 32),
|
||||
}, extra...)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if ctx.Err() != nil {
|
||||
t.Fatalf("gateway did not terminate; output=%s", out)
|
||||
}
|
||||
code := 0
|
||||
if exit, ok := err.(*exec.ExitError); ok {
|
||||
code = exit.ExitCode()
|
||||
} else if err != nil {
|
||||
t.Fatalf("run gateway: %v", err)
|
||||
}
|
||||
return string(out), code
|
||||
}
|
||||
|
||||
// The default shared-bucket path must not require a whole-bucket exclusive
|
||||
// writer attestation. Only the journal/backend admission gate may refuse it,
|
||||
// and a stale attestation file left behind by an older deployment must not
|
||||
// gate the default path either.
|
||||
func TestGatewayEntrypointDoesNotRequireBucketAttestation(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
env []string
|
||||
}{
|
||||
{name: "no_audit_configured"},
|
||||
{name: "stale_audit_file_ignored", env: []string{
|
||||
"OBJECT_GATEWAY_ID=retired-gateway",
|
||||
"OBJECT_GATEWAY_AUDIT_FILE=/nonexistent/expired-attestation.json",
|
||||
"OBJECT_GATEWAY_AUDIT_PUBLIC_KEY=" + base64.StdEncoding.EncodeToString(make([]byte, 32)),
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out, code := startErrorMessage(t, tc.env...)
|
||||
if code == 0 {
|
||||
t.Fatalf("gateway exited 0 instead of reporting its admission state: %s", out)
|
||||
}
|
||||
if strings.Contains(out, "gateway_auditor_key_required") || strings.Contains(out, "gateway_exclusive_writer_evidence_unverified") {
|
||||
t.Fatalf("shared-bucket admission still gated by whole-bucket attestation: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, "gateway_persistence_unavailable") && !strings.Contains(out, "gateway_backend_not_ready") {
|
||||
t.Fatalf("gateway did not reach journal/backend admission: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
type capturePersistenceFake struct {
|
||||
failAdvance error
|
||||
failStage error
|
||||
advanced []governance.LibraryJobState
|
||||
stages []string
|
||||
}
|
||||
|
||||
func (f *capturePersistenceFake) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState,
|
||||
_, _, _, _, _ string, _ time.Time) error {
|
||||
f.advanced = append(f.advanced, state)
|
||||
return f.failAdvance
|
||||
}
|
||||
|
||||
func (f *capturePersistenceFake) UpdateLibraryStage(_ context.Context, _, stage, state string,
|
||||
_, _, _, _ *int64, _ string, _ time.Time) error {
|
||||
f.stages = append(f.stages, stage+"/"+state)
|
||||
return f.failStage
|
||||
}
|
||||
|
||||
// The handler must stop before touching the source when the starting state
|
||||
// cannot be persisted. This is the failure injection that the old log-and-ignore
|
||||
// path hid behind a completed queue row.
|
||||
func TestBeginLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failAdvance: errors.New("injected advance failure")}
|
||||
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "cannot be advanced") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobRunning {
|
||||
t.Fatalf("advance calls=%v", store.advanced)
|
||||
}
|
||||
if len(store.stages) != 0 {
|
||||
t.Fatalf("stage was written before advance failure: %v", store.stages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBeginLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failStage: errors.New("injected stage failure")}
|
||||
err := beginLibraryCaptureJob(context.Background(), store, "libseal-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "stage cannot be advanced") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/running" {
|
||||
t.Fatalf("stage calls=%v", store.stages)
|
||||
}
|
||||
}
|
||||
|
||||
// Terminal persistence is two writes; either failure must be returned rather
|
||||
// than turning the queue row into completed while the business job is pending.
|
||||
func TestCompleteLibraryCaptureStopsOnStageFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failStage: errors.New("injected success stage failure")}
|
||||
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "success stage cannot be persisted") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.advanced) != 0 {
|
||||
t.Fatalf("advance must not run after stage failure: %v", store.advanced)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteLibraryCaptureStopsOnAdvanceFailure(t *testing.T) {
|
||||
store := &capturePersistenceFake{failAdvance: errors.New("injected success advance failure")}
|
||||
err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now())
|
||||
if err == nil || !strings.Contains(err.Error(), "success terminal cannot be persisted") {
|
||||
t.Fatalf("err=%v", err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" {
|
||||
t.Fatalf("stage calls=%v", store.stages)
|
||||
}
|
||||
if len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("advance calls=%v", store.advanced)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompleteLibraryCapturePersistsBothFacts(t *testing.T) {
|
||||
store := &capturePersistenceFake{}
|
||||
if err := completeLibraryCaptureJob(context.Background(), store, "libseal-test", "snap-test", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(store.stages) != 1 || store.stages[0] != "export/succeeded" ||
|
||||
len(store.advanced) != 1 || store.advanced[0] != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("stages=%v advanced=%v", store.stages, store.advanced)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
// R3g: only retained deterministic capture failures become permanent after the
|
||||
// agent has recovered the writer. Cancelled body-index coverage markers are
|
||||
// deliberately absent.
|
||||
func TestRetainedLibraryCaptureFailuresArePermanent(t *testing.T) {
|
||||
for _, marker := range []string{
|
||||
"missing library generated question index",
|
||||
"empty library index content",
|
||||
} {
|
||||
t.Run(marker, func(t *testing.T) {
|
||||
original := errors.New("LIBRARYCTL_FAILED: " + marker + ";已执行 recover 并确认写入者恢复")
|
||||
got := terminalLibraryCaptureError(original)
|
||||
terminal, code := jobs.Terminal(got)
|
||||
if !terminal || code != jobs.ErrCodeValidationFailed {
|
||||
t.Fatalf("retained validation failure must park immediately: terminal=%v code=%q err=%v", terminal, code, got)
|
||||
}
|
||||
if got.Error() != original.Error() {
|
||||
t.Fatalf("original message changed: got %q want %q", got.Error(), original.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelledCoverageFailureStaysRetryable(t *testing.T) {
|
||||
for _, marker := range []string{
|
||||
"unverified legacy library chunk index coverage",
|
||||
"missing indexed library chunk",
|
||||
} {
|
||||
err := terminalLibraryCaptureError(errors.New(marker + ";已执行 recover 并确认写入者恢复"))
|
||||
if terminal, code := jobs.Terminal(err); terminal {
|
||||
t.Fatalf("cancelled marker %q must not be terminal; code=%q", marker, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransientLibraryCaptureFailureStaysRetryable(t *testing.T) {
|
||||
for _, message := range []string{
|
||||
"library agent is unreachable",
|
||||
"context deadline exceeded",
|
||||
`LIBRARYCTL_FAILED: job "lib-internal-x" already exists with state "failed";已执行 recover 并确认写入者恢复`,
|
||||
"missing library generated question index; writer restore not confirmed",
|
||||
} {
|
||||
got := terminalLibraryCaptureError(errors.New(message))
|
||||
if terminal, code := jobs.Terminal(got); terminal {
|
||||
t.Fatalf("%q must stay retryable, got terminal code=%q", message, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetainedFailureDoesNotChangeMessage(t *testing.T) {
|
||||
original := errors.New("LIBRARYCTL_FAILED: missing library generated question index;已执行 recover 并确认写入者恢复")
|
||||
got := terminalLibraryCaptureError(original)
|
||||
if !strings.Contains(got.Error(), "missing library generated question index") {
|
||||
t.Fatalf("lost original cause: %v", got)
|
||||
}
|
||||
}
|
||||
@@ -233,8 +233,8 @@ func runLibraryReleaseAttempt(ctx context.Context, writer *libraryAttemptWriter,
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
gateReasons = append(gateReasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
gateReasons = append(gateReasons, "没有已核验的生产恢复演练证据")
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(ctx, store, config, payload.Scope); !ok {
|
||||
gateReasons = append(gateReasons, reason)
|
||||
}
|
||||
if len(gateReasons) > 0 {
|
||||
message := "生产" + kindLabel(kind) + "被闸口拒绝:" + strings.Join(gateReasons, ";")
|
||||
@@ -628,6 +628,10 @@ func resumeInterruptedLibraryJobs(ctx context.Context, store libraryResumeStore,
|
||||
// 基线作业被杀时窗口可能还停着目标:用同一个作业 id 接管那个窗口,
|
||||
// 继续捕获/校验,而不是留下一个 held 目标等人工处理。
|
||||
queueType = libraryBaselineJobType
|
||||
case governance.LibraryJobVerify:
|
||||
// 恢复验证同样持有维护窗口:接管时必须用同一份作业 id 收掉那个窗口,
|
||||
// 否则目标会一直停在维护里而作业显示失败。
|
||||
queueType = libraryVerifyJobType
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]string{"library_job_id": job.ID, "scope": job.Scope,
|
||||
"target_snapshot_id": job.TargetSnapshotID})
|
||||
|
||||
@@ -127,7 +127,10 @@ func runLibraryRecovery(ctx context.Context, store *postgresstore.Store, config
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
|
||||
restorer := recoveryRestorer{target: target, expected: prebackup, writeSet: ledger}
|
||||
restorer := recoveryRestorer{target: target, expected: prebackup, writeSet: ledger,
|
||||
// 恢复验证写的是"隔离 canary",不是发布计划:同一 job 的收尾走受限的演练
|
||||
// 恢复入口(同样的身份合同 + 整段回放),不能拿发布计划合同去回放它。
|
||||
drill: windowJob.Kind == governance.LibraryJobVerify}
|
||||
verifier := recoveryVerifier{target: target, expected: prebackup}
|
||||
result, recoverErr := coordinator.RecoverWindow(ctx, payload.WindowJobID, restorer, verifier)
|
||||
now := time.Now().UTC()
|
||||
@@ -171,6 +174,8 @@ type recoveryRestorer struct {
|
||||
target *v071.ProductionLibraryTarget
|
||||
expected string
|
||||
writeSet *libraryrelease.WriteSet
|
||||
// drill 表示窗口属于恢复演练:它的写集合是 canary,不是发布执行计划。
|
||||
drill bool
|
||||
}
|
||||
|
||||
func (r recoveryRestorer) Restore(ctx context.Context, backupID string) error {
|
||||
@@ -183,8 +188,11 @@ func (r recoveryRestorer) Restore(ctx context.Context, backupID string) error {
|
||||
if r.writeSet.BackupSnapshotID != backupID {
|
||||
return fmt.Errorf("写集合记录绑定预备份 %s,不是 %s", r.writeSet.BackupSnapshotID, backupID)
|
||||
}
|
||||
return r.target.Restore(ctx, libraryrelease.BackupRef{ID: "backup-" + backupID, SnapshotID: backupID,
|
||||
WriteSet: r.writeSet})
|
||||
ref := libraryrelease.BackupRef{ID: "backup-" + backupID, SnapshotID: backupID, WriteSet: r.writeSet}
|
||||
if r.drill {
|
||||
return r.target.RestoreDrill(ctx, ref)
|
||||
}
|
||||
return r.target.Restore(ctx, ref)
|
||||
}
|
||||
|
||||
// recoveryVerifier 做真实的内容校验,并把目标当前状态摘要作为"校验过什么"的凭证。
|
||||
|
||||
@@ -30,4 +30,9 @@ func registerLibraryReleaseHandlers(handlers map[string]jobs.Handler, store *pos
|
||||
handlers[libraryBaselineJobType] = admittanceGuarded(admissionErr, func(ctx context.Context, job jobs.Job) error {
|
||||
return runLibraryBaseline(ctx, store, config, job)
|
||||
})
|
||||
// 恢复验证:真实"备份→canary→恢复→复核",通过后写入与当前目标绑定的可恢复证据。
|
||||
// 它是同步闸口的证据来源,所以与上面三类共用同一把执行锁与准入。
|
||||
handlers[libraryVerifyJobType] = admittanceGuarded(admissionErr, func(ctx context.Context, job jobs.Job) error {
|
||||
return runLibraryVerify(ctx, store, config, job)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ import (
|
||||
func TestRegisterLibraryReleaseHandlersCoversOnlyReleaseJobs(t *testing.T) {
|
||||
handlers := map[string]jobs.Handler{}
|
||||
registerLibraryReleaseHandlers(handlers, nil, appconfig.Config{}, nil)
|
||||
want := []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType, libraryBaselineJobType}
|
||||
want := []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType,
|
||||
libraryBaselineJobType, libraryVerifyJobType}
|
||||
if len(handlers) != len(want) {
|
||||
t.Fatalf("registered %d handlers, want %d: %#v", len(handlers), len(want), handlers)
|
||||
}
|
||||
@@ -30,7 +31,8 @@ func TestRegisterLibraryReleaseHandlersCoversOnlyReleaseJobs(t *testing.T) {
|
||||
func TestRegisterLibraryReleaseHandlersFailsClosedWhenNotAdmitted(t *testing.T) {
|
||||
handlers := map[string]jobs.Handler{}
|
||||
registerLibraryReleaseHandlers(handlers, nil, appconfig.Config{}, errNotAdmitted{})
|
||||
for _, jobType := range []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType, libraryBaselineJobType} {
|
||||
for _, jobType := range []string{librarySyncJobType, libraryRestoreJobType, libraryRecoverJobType,
|
||||
libraryBaselineJobType, libraryVerifyJobType} {
|
||||
err := handlers[jobType](context.Background(), jobs.Job{})
|
||||
if err == nil || !strings.Contains(err.Error(), "准入未通过") {
|
||||
t.Fatalf("handler %q error = %v, want admission refusal", jobType, err)
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// fakeStageStore 记录封存失败分支对业务作业行的两次落库调用。
|
||||
type fakeStageStore struct {
|
||||
stageCalls []string
|
||||
jobCalls []string
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) UpdateLibraryStage(_ context.Context, jobID, stage, state string, _, _, _, _ *int64, detail string, _ time.Time) error {
|
||||
f.stageCalls = append(f.stageCalls, jobID+"|"+stage+"|"+state+"|"+detail)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, _, _ string, _ time.Time) error {
|
||||
f.jobCalls = append(f.jobCalls, jobID+"|"+string(state)+"|"+stage+"|"+message+"|"+recovery)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) LibraryVersionByRef(context.Context, string, governance.LibraryVersionKind, string, string) (governance.LibraryVersion, bool, error) {
|
||||
return governance.LibraryVersion{}, false, nil
|
||||
}
|
||||
|
||||
func (f *fakeStageStore) RecordLibraryVersion(context.Context, governance.LibraryVersion) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 归档根必须是非符号链接的 0700 目录;t.TempDir 的权限受 umask 影响。
|
||||
func privateDir(t *testing.T) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
func sourceFor(scope, kb string) governance.LibrarySourceIdentity {
|
||||
return governance.LibrarySourceIdentity{Project: "kgmaint-weknora", App: "kgmaint-app", Scope: scope, TenantID: 1, KnowledgeBaseID: kb}
|
||||
}
|
||||
|
||||
// RED 用例:快照来源与配置不一致时,队列已 failed、页面却永远"导出中"(真实事故:
|
||||
// libseal-lib-internal-20261102T234053 running/export/error 空)。失败必须同时写进
|
||||
// stage 与 job,且原错原样出现在两处与返回值里 —— 不能被"已记录失败"遮住。
|
||||
func TestCapturedSourceMismatchPersistsFailedStageAndJob(t *testing.T) {
|
||||
root := privateDir(t)
|
||||
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
|
||||
// 清单里是 kb-baseline;worker 配置漂移后拿的是另一个 KB(256 验收遗留正是这样)。
|
||||
source := sourceFor("internal", "0c84a098-d3ed-4b82-a165-e6de9691d6a8")
|
||||
store := &fakeStageStore{}
|
||||
|
||||
err := ensureCapturedSource(context.Background(), store, "libseal-lib-internal-20261102T234053",
|
||||
root, snapshotID, source, time.Now().UTC())
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), "captured manifest differs from configured source") {
|
||||
t.Fatalf("mismatch must keep the original error, got %v", err)
|
||||
}
|
||||
if len(store.stageCalls) != 1 {
|
||||
t.Fatalf("stage writes = %v, want exactly one failed stage", store.stageCalls)
|
||||
}
|
||||
if !strings.HasPrefix(store.stageCalls[0], "libseal-lib-internal-20261102T234053|export|failed|") ||
|
||||
!strings.Contains(store.stageCalls[0], "captured manifest differs from configured source") {
|
||||
t.Fatalf("stage write = %q, want failed+original error", store.stageCalls[0])
|
||||
}
|
||||
if len(store.jobCalls) != 1 {
|
||||
t.Fatalf("job writes = %v, want exactly one failed job", store.jobCalls)
|
||||
}
|
||||
if !strings.HasPrefix(store.jobCalls[0], "libseal-lib-internal-20261102T234053|failed|export|") ||
|
||||
!strings.Contains(store.jobCalls[0], "captured manifest differs from configured source") {
|
||||
t.Fatalf("job write = %q, want failed+original error", store.jobCalls[0])
|
||||
}
|
||||
// 恢复说明必须指向真实下一步,而不是暗示数据损坏。
|
||||
if !strings.Contains(store.jobCalls[0], "重新封存") {
|
||||
t.Fatalf("job recovery = %q, want a re-seal instruction", store.jobCalls[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 正例对照:身份一致时不得写任何失败,也不得把成功路径变成失败。
|
||||
func TestCapturedSourceMatchStaysSuccessful(t *testing.T) {
|
||||
root := privateDir(t)
|
||||
snapshotID := sealTinyBaseline(t, root, "internal", time.Date(2026, 11, 2, 23, 40, 53, 0, time.UTC))
|
||||
store := &fakeStageStore{}
|
||||
|
||||
if err := ensureCapturedSource(context.Background(), store, "libseal-ok",
|
||||
root, snapshotID, sourceFor("internal", "kb-baseline"), time.Now().UTC()); err != nil {
|
||||
t.Fatalf("matching source rejected: %v", err)
|
||||
}
|
||||
if len(store.stageCalls) != 0 || len(store.jobCalls) != 0 {
|
||||
t.Fatalf("success path wrote failures: stage=%v job=%v", store.stageCalls, store.jobCalls)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
"time"
|
||||
)
|
||||
|
||||
func configuredCaptureSource(config appconfig.Config, scope string) governance.LibrarySourceIdentity {
|
||||
@@ -32,3 +34,20 @@ func verifyCapturedSource(root, snapshotID string, source governance.LibrarySour
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureCapturedSource 校验快照来源。校验失败必须把失败写进业务行:
|
||||
// 队列侧此时已 failed,只 return err 会让页面永远停在"导出中"
|
||||
// (实际事故:libseal-lib-internal-20261102T234053 running/export/error 空)。
|
||||
func ensureCapturedSource(ctx context.Context, store libraryStageStore, jobID, root, snapshotID string, source governance.LibrarySourceIdentity, now time.Time) error {
|
||||
err := verifyCapturedSource(root, snapshotID, source)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
// 快照本身已产生且字节完好,错的只是 worker 读到的来源身份;因此恢复路径是
|
||||
// "修配置后重新登记/重新封存",不是"重采数据"。原错原样写进 stage、job 与
|
||||
// 返回值:改写成更友好的文案会让真正的配置漂移查不出来。
|
||||
libraryStageRecorder{store: store, jobID: jobID}.Stage(ctx, "export", "failed", err.Error())
|
||||
_ = store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobFailed, "export", err.Error(),
|
||||
"快照已产生但来源身份与配置不一致,未登记为测试封存;修正来源配置后重新封存", "", "", now)
|
||||
return err
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,262 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
"knowledge-governance/internal/libraryverify"
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// libraryVerifyPayload 是"恢复验证"的队列载荷。
|
||||
//
|
||||
// 它只带作业号与 scope:目标(库/索引/桶/实例)来自本进程的部署配置,不接受请求方
|
||||
// 指定。否则一次页面点击就能把"验证"引到别的库上,证据也就证明不了当前目标。
|
||||
type libraryVerifyPayload struct {
|
||||
LibraryJobID string `json:"library_job_id"`
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
// runLibraryVerify 在目标实例上真实跑一次恢复验证,并把结果登记成证据。
|
||||
//
|
||||
// 与 sync/restore/baseline 共用同一套准入与代次守卫:它会停写入者、在目标上写 canary
|
||||
// 再按预备份恢复,所以必须走 admitLibraryJob + runOwnedLibraryAttempt,不能绕开。
|
||||
// 执行体本身是 internal/libraryverify——与 cmd/librarydrill 同一份实现,不允许两份。
|
||||
func runLibraryVerify(ctx context.Context, base *postgresstore.Store, config appconfig.Config, job jobs.Job) error {
|
||||
var payload libraryVerifyPayload
|
||||
if err := json.Unmarshal(job.Payload, &payload); err != nil || payload.LibraryJobID == "" ||
|
||||
(payload.Scope != "internal" && payload.Scope != "external") {
|
||||
return errors.New("invalid library verify payload")
|
||||
}
|
||||
if _, admitErr := admitLibraryJob(ctx, base, job, payload.LibraryJobID, payload.Scope,
|
||||
governance.LibraryJobVerify, true); admitErr != nil {
|
||||
return admitErr
|
||||
}
|
||||
return runOwnedLibraryAttempt(ctx, base, config, job, payload.LibraryJobID,
|
||||
func(runCtx context.Context, writer *libraryAttemptWriter) error {
|
||||
return runLibraryVerifyAttempt(runCtx, writer, config, payload)
|
||||
})
|
||||
}
|
||||
|
||||
func runLibraryVerifyAttempt(ctx context.Context, writer *libraryAttemptWriter, config appconfig.Config,
|
||||
payload libraryVerifyPayload) error {
|
||||
store := writer
|
||||
now := time.Now().UTC()
|
||||
recorder := libraryStageRecorder{store: store, jobID: payload.LibraryJobID, scope: payload.Scope,
|
||||
operationID: writer.attempt.OperationID}
|
||||
|
||||
existing, jobErr := store.LibraryJob(ctx, payload.LibraryJobID)
|
||||
if jobErr != nil {
|
||||
// 作业行读不到就没有可登记的终态:直接失败,不落任何证据。
|
||||
return fmt.Errorf("读不到业务作业 %s:%w", payload.LibraryJobID, jobErr)
|
||||
}
|
||||
if existing.State == governance.LibraryJobSucceeded {
|
||||
// 幂等重放:已经成功过的验证不重复停目标、不重复写 canary。
|
||||
recorder.Stage(ctx, "finalize", "skipped", "该恢复验证已成功完成,重复投递不重复执行")
|
||||
return nil
|
||||
}
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
return failLibraryJob(ctx, store, recorder, payload.LibraryJobID,
|
||||
"生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false):恢复验证要停写入者并在目标上写 canary,闸口关闭时不执行",
|
||||
"capture")
|
||||
}
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobRunning, "capture",
|
||||
"", "", "", "", now)
|
||||
|
||||
report, runErr := libraryverify.Run(ctx, config, payload.Scope, payload.LibraryJobID,
|
||||
libraryverify.Options{
|
||||
Progress: func(stage, state, detail string) { recorder.Stage(ctx, stage, state, detail) },
|
||||
Governance: verifyGovernance{store: store, config: config},
|
||||
Ledger: recorder,
|
||||
})
|
||||
|
||||
// 证据只由真实执行产生:失败也照实登记 failure 与报告,绝不写成 passed。
|
||||
evidence := governance.LibraryRestoreVerification{
|
||||
ID: "libverify-" + payload.Scope + "-" + payload.LibraryJobID,
|
||||
Scope: payload.Scope,
|
||||
JobID: payload.LibraryJobID,
|
||||
Status: governance.LibraryVerificationFailed,
|
||||
StartedAt: now,
|
||||
}
|
||||
if report != nil {
|
||||
evidence.StartedAt = report.StartedAt
|
||||
evidence.SnapshotID = report.SnapshotID
|
||||
evidence.Report = verificationReportObject(report)
|
||||
}
|
||||
if runErr == nil && report != nil && report.Status == "passed" {
|
||||
evidence.Status = governance.LibraryVerificationPassed
|
||||
evidence.FinishedAt = report.FinishedAt
|
||||
} else {
|
||||
evidence.FinishedAt = time.Now().UTC()
|
||||
if report != nil && report.FinishedAt.After(evidence.StartedAt) {
|
||||
evidence.FinishedAt = report.FinishedAt
|
||||
}
|
||||
switch {
|
||||
case runErr != nil:
|
||||
evidence.Failure = runErr.Error()
|
||||
case report != nil && report.Status != "passed":
|
||||
evidence.Failure = "恢复验证未通过(状态 " + report.Status + ")"
|
||||
default:
|
||||
evidence.Failure = "恢复验证未产生报告"
|
||||
}
|
||||
}
|
||||
if saveErr := store.SaveLibraryRestoreVerification(ctx, evidence); saveErr != nil {
|
||||
// 证据落不了库就不能宣称验证通过:这会把闸口建立在一次没留痕的执行上。
|
||||
return failLibraryJob(ctx, store, recorder, payload.LibraryJobID,
|
||||
"恢复验证执行结束但证据登记失败:"+saveErr.Error(), "finalize")
|
||||
}
|
||||
|
||||
if runErr != nil || evidence.Status != governance.LibraryVerificationPassed {
|
||||
held := report != nil && report.Status == "maintenance_held"
|
||||
message := "恢复验证未通过:" + evidence.Failure
|
||||
if held {
|
||||
message += "(目标仍在维护窗口,入口关闭、写入者已停)"
|
||||
}
|
||||
// 失败出口只写真实结果:报告里的 restore_check 与窗口终态说明目标此刻是
|
||||
// 仍在维护,还是已经按原运行状态安全收尾。不能拿"有快照"冒充"已恢复并复核"。
|
||||
recovery := "未对目标执行任何写入,无需恢复"
|
||||
switch {
|
||||
case held:
|
||||
recovery = "目标仍在维护窗口:在同一作业上执行「按预备份恢复」(library.recover),恢复与逐类校验通过后才会开入口并释放占用"
|
||||
case report != nil && report.WindowState != "":
|
||||
recovery = "维护窗口已按原运行状态收尾(window=" + report.WindowState + "):目标内容未写入或已恢复并复核,无需再恢复"
|
||||
}
|
||||
snapshot := ""
|
||||
if report != nil {
|
||||
snapshot = report.SnapshotID
|
||||
}
|
||||
recorder.Stage(ctx, "finalize", "failed", message)
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobFailed, "finalize",
|
||||
message, recovery, snapshot, snapshot, time.Now().UTC())
|
||||
return errors.New(message)
|
||||
}
|
||||
|
||||
finishedAt := evidence.FinishedAt
|
||||
if finishedAt.IsZero() {
|
||||
finishedAt = time.Now().UTC()
|
||||
}
|
||||
recorder.Stage(ctx, "finalize", "succeeded",
|
||||
"恢复验证通过:快照 "+evidence.SnapshotID+" 的预备份可真实恢复,证据已按当前目标登记")
|
||||
_ = store.AdvanceLibraryJob(ctx, payload.LibraryJobID, governance.LibraryJobSucceeded, "finalize",
|
||||
"", "", evidence.SnapshotID, evidence.SnapshotID, finishedAt)
|
||||
log.Printf("library verify %s passed target snapshot=%s", payload.LibraryJobID, evidence.SnapshotID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// verifyGovernance 把演练接进**既有**维护生命周期:实例行 + scope 行都由 worker 的
|
||||
// 真实 attempt 写入,入口状态与状态文字来自实际执行。页面读的维护接口只认这些行,
|
||||
// 所以只写磁盘 journal 的窗口在 UI 上等于不存在(审核 B2 的真实缺口)。
|
||||
type verifyGovernance struct {
|
||||
store *libraryAttemptWriter
|
||||
config appconfig.Config
|
||||
}
|
||||
|
||||
func (g verifyGovernance) targetIdentity(scope string) string {
|
||||
return fmt.Sprintf("scope=%s db=%s index=%s bucket=%s", scope,
|
||||
pgdsn.Endpoint(g.config.LibraryProductionDatabaseURL), g.config.LibraryProductionIndex, g.config.LibraryProductionS3Bucket)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Acquire(ctx context.Context, jobID, scope string, target libraryverify.TargetIdentity, originalServiceState string) error {
|
||||
now := time.Now().UTC()
|
||||
note := "实例级维护:关闭目标入口并停止全部写入者,恢复验证完成并复核后开放"
|
||||
if _, err := g.store.AcquireInstanceMaintenance(ctx, postgresstore.LibraryInstanceMaintenance{
|
||||
Instance: target.Instance, OwnerJobID: jobID, OwnerScope: scope,
|
||||
TargetFingerprint: target.Fingerprint, Level: librarymaintenance.LevelInstance,
|
||||
Services: target.Writers, IngressState: "unknown", State: "acquired",
|
||||
OriginalServiceState: originalServiceState, Note: note,
|
||||
}, now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := g.store.AcquireLibraryMaintenance(ctx, postgresstore.LibraryMaintenance{
|
||||
Scope: scope, OwnerJobID: jobID, TargetIdentity: g.targetIdentity(scope),
|
||||
Level: librarymaintenance.LevelInstance, TargetFingerprint: target.Fingerprint,
|
||||
Services: target.Writers, IngressState: "unknown", State: "acquired",
|
||||
OriginalServiceState: originalServiceState, Note: note,
|
||||
}, now); err != nil {
|
||||
// scope 行没拿到时目标还没被停用:释放实例占用,避免留下一个空窗口。
|
||||
_ = g.store.ReleaseInstanceMaintenance(ctx, target.Instance, jobID, now)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Ingress(ctx context.Context, jobID, scope, state, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.SetLibraryMaintenanceIngress(ctx, scope, jobID, state, note, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.SetInstanceMaintenanceIngress(ctx, g.config.LibraryTargetInstance, jobID, state, note, now)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) State(ctx context.Context, jobID, scope, state, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.UpdateLibraryMaintenanceState(ctx, scope, jobID, state, note, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.UpdateInstanceMaintenanceState(ctx, g.config.LibraryTargetInstance, jobID, state, note, now)
|
||||
}
|
||||
|
||||
func (g verifyGovernance) Release(ctx context.Context, jobID, scope, note string) error {
|
||||
now := time.Now().UTC()
|
||||
if err := g.store.ReleaseLibraryMaintenance(ctx, scope, jobID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.store.ReleaseInstanceMaintenance(ctx, g.config.LibraryTargetInstance, jobID, now)
|
||||
}
|
||||
|
||||
// verificationReportObject 把报告转成可落库的 JSON 对象(保留 canary/漂移校验等证据字段)。
|
||||
func verificationReportObject(report *libraryverify.Report) map[string]any {
|
||||
if report == nil {
|
||||
return nil
|
||||
}
|
||||
raw, err := json.Marshal(report)
|
||||
if err != nil {
|
||||
return map[string]any{"encode_error": err.Error()}
|
||||
}
|
||||
var object map[string]any
|
||||
if err := json.Unmarshal(raw, &object); err != nil {
|
||||
return map[string]any{"encode_error": err.Error()}
|
||||
}
|
||||
return object
|
||||
}
|
||||
|
||||
// libraryRestoreEvidenceSatisfied 回答"当前目标 + scope 有没有可支撑写生产的恢复证据"。
|
||||
//
|
||||
// 证据是第一来源:library_restore_verifications 按当前 target_id 读取,换库/换索引/换桶
|
||||
// 之后旧证据自动失效,不需要谁记得关开关。历史部署里的 PRODUCTION_BACKUP_RESTORE_VERIFIED
|
||||
// 仍作为显式覆盖保留(不删除旧部署能力),但只允许在**明确没有证据行**时兜底;
|
||||
// 明确 failed/invalid 与读取错误一律拒绝(审核 B3:不能把"不知道"当"满足")。
|
||||
func libraryRestoreEvidenceSatisfied(ctx context.Context, store interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
}, config appconfig.Config, scope string) (bool, string) {
|
||||
// 存储没有证据能力(nil/迁移前形状)等同于"没有证据行":历史 env 覆盖可兜底;
|
||||
// 但只要读得出证据行,failed/invalid 与读取错误都优先拒绝。
|
||||
missing := true
|
||||
if store != nil {
|
||||
evidence, found, err := store.LatestLibraryRestoreVerification(ctx, scope)
|
||||
switch {
|
||||
case err != nil:
|
||||
return false, "读取恢复验证证据失败:" + err.Error()
|
||||
case found:
|
||||
missing = false
|
||||
if ok, reason := governance.LibraryVerificationSatisfied(evidence, scope); ok {
|
||||
return true, ""
|
||||
} else {
|
||||
return false, reason
|
||||
}
|
||||
}
|
||||
}
|
||||
if missing && config.ProductionBackupRestoreVerified {
|
||||
return true, ""
|
||||
}
|
||||
return false, "当前目标还没有恢复验证证据:先跑一次恢复验证(备份 → canary → 恢复 → 复核)"
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 同步闸口的"恢复可验证"判断必须以当前目标的证据为准:
|
||||
// 没有证据 ⇒ 拒绝并说清怎么补;有 passed 证据 ⇒ 放行;只有失败证据 ⇒ 拒绝并带失败原因。
|
||||
// 历史 env 覆盖只在证据缺失时兜底(页面不再写它)。
|
||||
type verifyEvidenceStore struct {
|
||||
evidence governance.LibraryRestoreVerification
|
||||
found bool
|
||||
}
|
||||
|
||||
func (s verifyEvidenceStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return s.evidence, s.found, nil
|
||||
}
|
||||
|
||||
// 审核 B3:读取失败不能被 env=true 洗成"满足"。
|
||||
type unreadableEvidenceStore struct{}
|
||||
|
||||
func (unreadableEvidenceStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return governance.LibraryRestoreVerification{}, false, errors.New("evidence lookup failed")
|
||||
}
|
||||
|
||||
func TestWorkerUnreadableEvidenceBeatsEnv(t *testing.T) {
|
||||
ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), unreadableEvidenceStore{},
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal")
|
||||
if ok {
|
||||
t.Fatalf("unreadable evidence must block even with env=true; reason=%q", reason)
|
||||
}
|
||||
if !strings.Contains(reason, "读取恢复验证证据失败") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryRestoreEvidenceSatisfied(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
passed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
|
||||
Scope: "internal", Status: governance.LibraryVerificationPassed, SnapshotID: "snap-1", FinishedAt: now}}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), passed, appconfig.Config{}, "internal"); !ok {
|
||||
t.Fatalf("passed evidence must satisfy gate: %q", reason)
|
||||
}
|
||||
failed := verifyEvidenceStore{found: true, evidence: governance.LibraryRestoreVerification{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, SnapshotID: "snap-1",
|
||||
Failure: "恢复后内容仍与预备份不一致", FinishedAt: now}}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), failed, appconfig.Config{}, "internal"); ok ||
|
||||
!strings.Contains(reason, "恢复后内容仍与预备份不一致") {
|
||||
t.Fatalf("failed evidence must block with its own failure: ok=%v reason=%q", ok, reason)
|
||||
}
|
||||
missing := verifyEvidenceStore{}
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing, appconfig.Config{}, "internal"); ok ||
|
||||
!strings.Contains(reason, "恢复验证") {
|
||||
t.Fatalf("missing evidence must block and point at the remedy: ok=%v reason=%q", ok, reason)
|
||||
}
|
||||
// 历史部署的 env 覆盖:证据缺失时才生效,证据存在时不再起作用。
|
||||
if ok, reason := libraryRestoreEvidenceSatisfied(context.Background(), missing,
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); !ok {
|
||||
t.Fatalf("legacy env override must still allow when no evidence: %q", reason)
|
||||
}
|
||||
if ok, _ := libraryRestoreEvidenceSatisfied(context.Background(), failed,
|
||||
appconfig.Config{ProductionBackupRestoreVerified: true}, "internal"); ok {
|
||||
t.Fatal("evidence must win over the legacy env override")
|
||||
}
|
||||
}
|
||||
+89
-23
@@ -9,6 +9,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -23,7 +24,9 @@ import (
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
"knowledge-governance/internal/readiness"
|
||||
"knowledge-governance/internal/versionhistory"
|
||||
"knowledge-governance/internal/weknoraaccess"
|
||||
artifactfetch "knowledge-governance/internal/workflows/artifactfetch"
|
||||
catalogrefresh "knowledge-governance/internal/workflows/catalogrefresh"
|
||||
"knowledge-governance/internal/workflows/sourcescan"
|
||||
@@ -98,6 +101,27 @@ func stageSizesFrom(manifest librarysnapshot.Manifest) map[string]stageSize {
|
||||
// libraryCaptureJobType is shared with the API which enqueues it.
|
||||
const libraryCaptureJobType = "library.capture"
|
||||
|
||||
// Retained deterministic source-content failures: the agent has already
|
||||
// restored the writer, so retrying the same journal only replaces the first
|
||||
// cause with "already exists". Body-index coverage markers are intentionally
|
||||
// absent; that inferred rule was removed in R3f.
|
||||
var terminalLibraryCaptureMarkers = []string{
|
||||
"missing library generated question index",
|
||||
"empty library index content",
|
||||
}
|
||||
|
||||
func terminalLibraryCaptureError(err error) error {
|
||||
if err == nil || !strings.Contains(err.Error(), "已执行 recover 并确认写入者恢复") {
|
||||
return err
|
||||
}
|
||||
for _, marker := range terminalLibraryCaptureMarkers {
|
||||
if strings.Contains(err.Error(), marker) {
|
||||
return jobs.Permanent(jobs.ErrCodeValidationFailed, err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// librarySyncJobType 是生产同步作业的队列类型;与封存严格分开,状态不共用。
|
||||
const librarySyncJobType = "library.sync"
|
||||
|
||||
@@ -110,6 +134,10 @@ const libraryRestoreJobType = "library.restore"
|
||||
// libraryBaselineJobType 是"建立目标权威基线"的队列类型(与 API 侧常量同值)。
|
||||
const libraryBaselineJobType = "library.baseline"
|
||||
|
||||
// libraryVerifyJobType 是"恢复验证"的队列类型:真实备份→canary→恢复→复核,
|
||||
// 通过后才会产生同步闸口认的恢复证据。与 API 侧常量同值。
|
||||
const libraryVerifyJobType = "library.verify"
|
||||
|
||||
func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
@@ -142,7 +170,7 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
} else if len(pending) > 0 {
|
||||
return fmt.Errorf("治理库 schema 落后(待应用迁移 %v):本角色不拥有 DDL,先由控制面进程完成迁移", pending)
|
||||
}
|
||||
store := postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()).
|
||||
store := postgresstore.NewStore(pool).WithLibraryTarget(config.LibraryTargetIdentity()).WithRoutingDefaults(config.InternalRoutingKnowledgeBaseID, config.ExternalRoutingKnowledgeBaseID).
|
||||
WithRestoreDispatchEvidence(config.LibraryTargetJournalRoot)
|
||||
log.Printf("library target evidence identity=%s", config.LibraryTargetIdentity())
|
||||
// 启动先接管被中断的写作业(复用已持久化的预备份),再把剩下的孤儿判失败:
|
||||
@@ -260,6 +288,41 @@ func run(ctx context.Context, getenv func(string) string) error {
|
||||
return worker.Run(ctx, config.PollInterval)
|
||||
}
|
||||
|
||||
type libraryCapturePersistence interface {
|
||||
UpdateLibraryStage(ctx context.Context, jobID, stage, state string, total, completed, bytesTotal, bytesDone *int64, detail string, now time.Time) error
|
||||
AdvanceLibraryJob(ctx context.Context, jobID string, state governance.LibraryJobState, stage, message, recovery, snapshotID, targetSnapshotID string, now time.Time) error
|
||||
}
|
||||
|
||||
// beginLibraryCaptureJob persists the starting state before any source
|
||||
// operation. If either write fails the handler must return, so the queue cannot
|
||||
// be reported completed while the business job is still pending.
|
||||
func beginLibraryCaptureJob(ctx context.Context, store libraryCapturePersistence, jobID string, now time.Time) error {
|
||||
if err := store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobRunning, "export", "", "", "", "", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s cannot be advanced: %w", jobID, err)
|
||||
}
|
||||
if err := store.UpdateLibraryStage(ctx, jobID, "export", "running", nil, nil, nil, nil,
|
||||
"主机侧正在暂停写入者、导出并恢复", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s stage cannot be advanced: %w", jobID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// completeLibraryCaptureJob persists both terminal facts. A failed write must
|
||||
// be returned (queue failed), not logged and ignored; the capture journal keeps
|
||||
// replay idempotent, and the admin reconcile path can repair only after the
|
||||
// exact queue/version/archive evidence is present.
|
||||
func completeLibraryCaptureJob(ctx context.Context, store libraryCapturePersistence, jobID, snapshotID string, now time.Time) error {
|
||||
if err := store.UpdateLibraryStage(ctx, jobID, "export", "succeeded", nil, nil, nil, nil,
|
||||
"已封存快照 "+snapshotID, now); err != nil {
|
||||
return fmt.Errorf("library seal job %s success stage cannot be persisted: %w", jobID, err)
|
||||
}
|
||||
if err := store.AdvanceLibraryJob(ctx, jobID, governance.LibraryJobSucceeded, "export", "", "",
|
||||
snapshotID, "", now); err != nil {
|
||||
return fmt.Errorf("library seal job %s success terminal cannot be persisted: %w", jobID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[string]jobs.Handler, error) {
|
||||
handlers := map[string]jobs.Handler{
|
||||
"demo": func(_ context.Context, job jobs.Job) error {
|
||||
@@ -321,12 +384,8 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
// 持久作业:页面读的是这里写的阶段与结果,worker 重启后仍能如实展示。
|
||||
libraryJobID := "libseal-" + payload.CaptureJob
|
||||
startedAt := time.Now().UTC()
|
||||
if advanceErr := store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobRunning, "export", "", "", "", "", startedAt); advanceErr != nil {
|
||||
log.Printf("library seal job %s cannot be advanced: %v", libraryJobID, advanceErr)
|
||||
}
|
||||
if stageErr := store.UpdateLibraryStage(ctx, libraryJobID, "export", "running", nil, nil, nil, nil,
|
||||
"主机侧正在暂停写入者、导出并恢复", startedAt); stageErr != nil {
|
||||
log.Printf("library seal job %s stage update failed: %v", libraryJobID, stageErr)
|
||||
if beginErr := beginLibraryCaptureJob(ctx, store, libraryJobID, startedAt); beginErr != nil {
|
||||
return beginErr
|
||||
}
|
||||
// 平台审计过的精确死信解除指纹:读取失败必须阻断(fail closed),
|
||||
// 不能因为存储不可用就静默按"没有解除"或"全部解除"处理。
|
||||
@@ -358,7 +417,7 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
_ = store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobFailed, "export", err.Error(),
|
||||
"agent 已执行恢复检查;确认来源 healthy 后可重试同一作业号", "", "", finishedAt)
|
||||
log.Printf("library capture failed scope=%s capture_job=%s: %v", payload.Scope, payload.CaptureJob, err)
|
||||
return err
|
||||
return terminalLibraryCaptureError(err)
|
||||
}
|
||||
// 执行后复核:结果里 agent 自报的身份必须还是期望实例,否则这次封存
|
||||
// 可能来自别的栈,绝不能登记成"本实例的测试封存"。
|
||||
@@ -368,7 +427,7 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
"快照已产生但来源身份不符,未登记为测试封存;核对执行器后重新封存", "", "", finishedAt)
|
||||
return identityErr
|
||||
}
|
||||
if err := verifyCapturedSource(config.LibraryArchiveRoot, result.SnapshotID, source); err != nil {
|
||||
if err := ensureCapturedSource(ctx, store, libraryJobID, config.LibraryArchiveRoot, result.SnapshotID, source, finishedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
// 封存产物成版本记录:只登记"测试封存",绝不写成生产生效版本。
|
||||
@@ -384,10 +443,9 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
if versionErr != nil {
|
||||
return fmt.Errorf("library seal job %s cannot record version: %w", libraryJobID, versionErr)
|
||||
}
|
||||
_ = store.UpdateLibraryStage(ctx, libraryJobID, "export", "succeeded", nil, nil, nil, nil,
|
||||
"已封存快照 "+result.SnapshotID, finishedAt)
|
||||
_ = store.AdvanceLibraryJob(ctx, libraryJobID, governance.LibraryJobSucceeded, "export", "", "",
|
||||
result.SnapshotID, "", finishedAt)
|
||||
if completeErr := completeLibraryCaptureJob(ctx, store, libraryJobID, result.SnapshotID, finishedAt); completeErr != nil {
|
||||
return completeErr
|
||||
}
|
||||
log.Printf("library capture completed scope=%s capture_job=%s snapshot=%s replay=%v requested_by=%s",
|
||||
payload.Scope, payload.CaptureJob, result.SnapshotID, result.Replay, payload.RequestedBy)
|
||||
return nil
|
||||
@@ -467,19 +525,27 @@ func workerHandlers(store *postgresstore.Store, config appconfig.Config) (map[st
|
||||
if !config.TestWeKnoraEnabled || !config.LibraryWorkerRole.ExecutesSourceWork() {
|
||||
return handlers, nil
|
||||
}
|
||||
apiKey, err := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile)
|
||||
if err != nil {
|
||||
// 启动只验证凭据“存在”(缺失像以前一样拒绝启动);具体值仍由 resolver 每次调用重读。
|
||||
if _, err := weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile); err != nil {
|
||||
return nil, fmt.Errorf("read test WeKnora API key: %w", err)
|
||||
}
|
||||
client, err := weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: config.TestWeKnoraBaseURL,
|
||||
APIKey: apiKey,
|
||||
Timeout: config.TestWeKnoraHTTPTimeout,
|
||||
AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create test WeKnora client: %w", err)
|
||||
// 与 API 探测共用同一有效目标:网页保存的端点优先、env 只作未配置兜底;
|
||||
// 当前持久化绑定优先、env 兜底;凭据每次调用重读同一 key 文件。
|
||||
remoteResolver := readiness.Resolver{
|
||||
Endpoint: func(resolveCtx context.Context) string {
|
||||
if settings, settingsErr := store.GetActiveUISettings(resolveCtx); settingsErr == nil && strings.TrimSpace(settings.TestWeKnoraBaseURL) != "" {
|
||||
return settings.TestWeKnoraBaseURL
|
||||
}
|
||||
return config.TestWeKnoraBaseURL
|
||||
},
|
||||
LoadBinding: func(resolveCtx context.Context) (string, error) {
|
||||
internal, _, _, bindingErr := store.LoadRoutingKBBindings(resolveCtx)
|
||||
return internal, bindingErr
|
||||
},
|
||||
ReadKey: func() (string, error) { return weknora.ReadAPIKeyFile(config.TestWeKnoraAPIKeyFile) },
|
||||
DefaultKB: config.InternalRoutingKnowledgeBaseID,
|
||||
}
|
||||
client := &weknoraaccess.Client{Resolver: remoteResolver, Timeout: config.TestWeKnoraHTTPTimeout, AllowDockerHTTP: config.TestWeKnoraAllowDockerHTTP}
|
||||
upload, err := testweknora.NewUploadHandler(testweknora.UploadHandlerConfig{
|
||||
Store: store,
|
||||
Client: client,
|
||||
|
||||
+6
-1
@@ -18,4 +18,9 @@
|
||||
|
||||
1. 跑 `make verify`(本地全绿不等于远端跑通,需要一次真实 run 的证据)。
|
||||
2. 把**使用者可见**的变化写成 `CHANGELOG.md` 的 `[Unreleased]` 条目(按四类,别写过程)。
|
||||
3. 从本文件划掉该项;过程记录归档到 `docs/archive/`。
|
||||
3. 从本文件划掉该项;过程记录归档到 `docs/archive/`。
|
||||
## 资源首次复用的归属审计(后续)
|
||||
|
||||
- 当前首次复用只依赖“目标行逐列一致 + 对象字节一致”,不再要求本目标历史成功同步/预备份来源。
|
||||
- 后续可补:资源首次写入者/首次绑定 KB、跨 KB 引用计数、孤儿资源与同 tenant 共享资源的可审计归属视图;
|
||||
不以此重新阻断正常的同内容首次复用。
|
||||
|
||||
@@ -56,8 +56,16 @@ LIBRARY_TARGET_INGRESS_URL 入口地址(恢复后要能 200)
|
||||
LIBRARY_TARGET_INGRESS_SERVICE Compose 里的入口闸门服务名
|
||||
LIBRARY_TARGET_INGRESS_DRAIN_FILE 闸门写排空证据的宿主机文件
|
||||
LIBRARY_TARGET_REDIS_ADDR/_PASSWORD/_DB 后台队列(asynq)地址
|
||||
LIBRARY_TARGET_INGRESS_MODE gate(缺省)或 none;none 只给没有闸门的测试栈
|
||||
LIBRARY_NO_INGRESS_ACK mode=none 时必须逐字符等于 test-only
|
||||
LIBRARY_TARGET_HEALTH_URL mode=none 时的健康探针(恢复后必须 200)
|
||||
```
|
||||
|
||||
`none` 是显式降档,不是默认:整栈(如 0.9.0 测试栈)没有入口闸门时,`none` + 确认串
|
||||
才允许缺入口三项,窗口里不调用任何闸门动作,`gate` 证据位置写
|
||||
`{"mode":"none","reason":"stack has no ingress gate (test-only)"}`;恢复以
|
||||
`LIBRARY_TARGET_HEALTH_URL` 返回 200 为准。缺省 `gate` 的行为与文案不变。
|
||||
|
||||
另外 `-source-s3-endpoint <URL>` 用来覆盖"源对象存储的访问入口":容器里写的
|
||||
`MINIO_ENDPOINT` 往往是服务名(`langfuse-minio:9000`),封存跑在宿主机上时解析不到,
|
||||
资源导出会失败(`read library resource`)。覆盖只换入口,桶与对象键不变;`status`
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# v0.7.1 整库发布的数据契约与覆盖边界
|
||||
|
||||
这是候选代码的契约,不是部署许可。真实隔离执行证据在交付目录 `data-contract-r10.md`、`test-summary-r10.json` 及其 raw 索引。PG/ES/MinIO 均为本机隔离真实服务;发布/恢复测试直接调用适配器;B22 另走实际 `librarystack.OpenProductionStack` 和 HTTP 网关。网关的独占审核是测试夹具,不代表生产已隔离;B22 控制读取角色是真实 SELECT-only PG 角色。
|
||||
这是候选代码的契约,不是部署许可。真实隔离执行证据在交付目录 `data-contract-r10.md`、`test-summary-r10.json` 及其 raw 索引。PG/ES/MinIO 均为本机隔离真实服务;发布/恢复测试直接调用适配器;B22 另走实际 `librarystack.OpenProductionStack` 和 HTTP 网关。网关按限定 key 工作,不要求整桶独占审核;B22 控制读取角色是真实 SELECT-only PG 角色。
|
||||
|
||||
## 1. 源 schema、历史身份与准入
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
- 真实v071历史catalog镜像对照及UUID/自然键矩阵在r11执行,隔离结论与外部当前目录采样分开。A04已识别嵌套引用有有限支持,旧外部cursor仍不自动转换,不因内部结构正例而升级整项。
|
||||
- r8/r9进程恢复历史证据只在原发送协议、不可变计划/receipt及相应场景前提不变时复用。本轮对schema/引用准入有收紧,不能把历史执行日志称为当前源码正式进程全链重跑;完整当前隔离适配器回归单列。
|
||||
- clean-finalization unknown保持held是安全行为,但未满足全部自动收敛要求。若要求自动完成,需要能绑定原写入、完整内容和代次的充分服务端证据;相同干净正文、人工猜测归属或放宽共享规则均不可替代。
|
||||
- 最终提交/push、远端CI、父复核和本机部署为明确下一门;整体安全验收未齐不部署。生产独占/ACL网络凭据、网关OSS业务端到端与宿主重启/断电是另行授权的部署/破坏性验收,不能作为停止本机授权开发的借口。
|
||||
- 最终提交/push、远端CI、父复核和本机部署为明确下一门;整体安全验收未齐不部署。网关真实 OSS 业务端到端与宿主重启/断电是另行授权的部署/破坏性验收,不能作为停止本机授权开发的借口。
|
||||
|
||||
## 6. 目录与唯一键的有限支持矩阵
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
# 目标库隔离 ID 映射
|
||||
|
||||
同一 WeKnora 实例里可以有多个知识库。同步把源快照写进目标库时,如果沿用源库的 UUID 主键,
|
||||
第二个目标库会与实例里已有库的行撞主键(真实证据:2 knowledges / 2 chunks / 23 wiki_pages /
|
||||
17 wiki_folders 已存在于旧库),写入前的跨范围检查会拒绝整次同步。关闭检查等于让两个库共享身份,
|
||||
删除/恢复/校验会互相误伤,因此身份必须在目标侧派生。
|
||||
|
||||
## 映射规则
|
||||
|
||||
```
|
||||
digest = sha256("knowledge-governance/target-identity/v1" || tenant || kb || table || source_id)
|
||||
target_id = 前 16 字节按 8-4-4-4-12 十六进制格式化(置 RFC 4122 variant/version 位)
|
||||
```
|
||||
|
||||
这是**本产品自定义的稳定 ID 算法**(`target_id_mode=scoped-sha256-v1`),只保证确定性与目标隔离;
|
||||
它不是标准 RFC 4122 UUIDv5(UUIDv5 用 SHA-1 + 标准 namespace),不要按 UUIDv5 互操作。
|
||||
|
||||
- 只绑定**稳定目标身份**(目标 tenant、目标 KB)与源身份,表名参与命名空间;
|
||||
- 不绑定 job、时间、快照版本等瞬态值:同一目标重复同步/重试/跨版本得到同一 ID;
|
||||
- 不同目标 KB、不同表得到不同 ID;
|
||||
- 源归档字节不变,映射只存在于目标侧派生视图(写集合、写入行、索引文档、校验预期);
|
||||
- 计划把策略持久化为 `target_id_mode=scoped-sha256-v1`:新进程按该字段重建映射,旧计划/写前镜像
|
||||
没有该字段则按原 ID 回放,绝不二次映射。
|
||||
|
||||
## 参与映射的表与字段
|
||||
|
||||
| 表 | 身份 | 引用字段 |
|
||||
|---|---|---|
|
||||
| knowledges | id | — |
|
||||
| chunks | id | knowledge_id, tag_id, parent_chunk_id, pre_chunk_id, next_chunk_id, relation_chunks, indirect_relation_chunks, metadata(仅精确匹配) |
|
||||
| knowledge_tags | id | — |
|
||||
| knowledge_tag_relations | (knowledge_id, tag_id) | 两个分量分别映射 |
|
||||
| wiki_folders | id | parent_id |
|
||||
| wiki_pages | id | folder_id, chunk_refs, source_refs(保留 `id|title`), page_metadata(仅精确匹配) |
|
||||
| wiki_page_issues | id | page_id, knowledge_id, chunk_id |
|
||||
| resource_bindings | id | owner_id(按 owner_type) |
|
||||
| resources | **不映射** | resource_id 保持共享身份 |
|
||||
| 索引文档 | 文档物理 ID | chunk_id, knowledge_id, source_id, tag_id |
|
||||
|
||||
正文、标题、摘要、向量、slug、in_links/out_links、pages_affected 不改写;本地自增列
|
||||
(如 `wiki_log_entries.id`)仍由既有 LocalIDs 分配。引用指向未登记身份时拒绝(悬挂引用不静默放行)。
|
||||
|
||||
## 一致性边界
|
||||
|
||||
- 写集合登记的是目标身份;verify 的身份摘要与内容摘要都按同一 `rowTransform` 计算;
|
||||
- 索引计划的目标文档 ID 与 `_source` 引用同步映射,before-image(目标写前镜像)保持原样;
|
||||
- 恢复重放写前镜像时映射关闭,避免把目标原始 ID 再映射一次;
|
||||
- 资源对象路径/location_hash 仍由既有资源改写逻辑处理,不因 ID 映射而变化。
|
||||
@@ -4,12 +4,14 @@
|
||||
|
||||
`cmd/object-gateway` 提供 `kg-object-gateway-v1`,只监听 loopback;外部接入需要另行审核的 TLS 终结与网络隔离。治理 PG 保存不可变计划、grant、intent、逐版本 receipt 和 attempt→grant delivery。部署迁移身份显式应用 `internal/objectgateway/schema.sql`;启动不自动迁移。运行身份只授必要 SELECT/INSERT 和 scope 行 UPDATE,不授 DDL/TRUNCATE/证据修改或业务库写权限。
|
||||
|
||||
scope 是 provider、原 endpoint、bucket 的 NUL 分隔 SHA256。同一物理桶只能有一个受审核 scope 和一套共享 journal,不能用 endpoint 别名或另一日志库建立平行写域。
|
||||
scope 是 provider、原 endpoint、bucket 的 NUL 分隔 SHA256。同一物理桶在本网关内只使用一个 scope 和一套共享 journal,不能用 endpoint 别名或另一日志库建立平行写域。这不要求整桶只有本网关一个写者:别的服务可以在同一桶的其它 key 上正常读写。
|
||||
|
||||
数据认证端点:`POST /v1/acquire`、`/v1/resolve`、`/v1/execute`、`/v1/reconcile`、`/v1/observe`,以及 `GET /v1/capabilities`。所有请求核验协议和 scope。`capabilities.automatic_grants` 必须为真,正式 stack 才接受自动授权能力;未接控制源、只读角色权限异常、协议不符或不可达均拒绝,无直写回退。
|
||||
|
||||
PG 行锁负责原子准入,不是远端存储 fence。intent 必须持久提交后才发送一次请求;连接死亡、租约过期不清除未知 intent。任何未终态 intent 均阻止 scope 换 epoch。重复 operation 只读回执或精确对账,不重发 PUT;暂时查不到版本仍 held。删除必须绑定已确认 create 的精确 VersionID,不删 latest、不生成删除标记。
|
||||
|
||||
同 key 竞争按 key 判定,不按桶判定:create 前已存在任何版本 → 回执 `rejected` 且不修改该对象;PUT 后除精确版本读回外还核对当前 latest,若并发写者已产生更新的当前版本,则只按精确 VersionID 撤回本次写入,保留对方版本并返回冲突;读不到当前版本时保持 held,不删除、不猜测。版本化存储的 PUT 不是原子 create-if-absent,因此“本 key 多数情况下归本计划”不是存储层保证。
|
||||
|
||||
不可变证据表拒绝 UPDATE/DELETE/TRUNCATE。Ready 核 fsync、synchronous_commit、full_page_writes、主库可写及必要表/触发器;管理员与迁移身份仍是可信边界,触发器不能抵御 DBA 的 DDL。
|
||||
|
||||
## 正式控制交付路径
|
||||
@@ -54,13 +56,13 @@ SELECT-only治理角色需要读取`library_jobs`、`library_operations`、`libr
|
||||
- `OBJECT_GATEWAY_ARCHIVE_ROOT`:原持久计划所在归档绝对路径,部署只读挂载。
|
||||
- `OBJECT_GATEWAY_TARGET_ID`:与正式 worker `LibraryTargetIdentity()` 完全相同的逻辑目标身份;不能用另一个环境的值。
|
||||
- `OBJECT_GATEWAY_PROVIDER`、`OBJECT_GATEWAY_ENDPOINT`、`OBJECT_GATEWAY_BUCKET`、`OBJECT_GATEWAY_REGION`:精确存储身份。
|
||||
- `OBJECT_GATEWAY_ACCESS_KEY`、`OBJECT_GATEWAY_SECRET_KEY`:仅网关持有的对象写凭据。
|
||||
- `OBJECT_GATEWAY_ACCESS_KEY`、`OBJECT_GATEWAY_SECRET_KEY`:网关持有的对象凭据,需在目标桶 PUT/GET/按版本 DELETE/列举版本。沿用现有 AK 即可,不要求另建独立只读或独占写凭据。
|
||||
- `OBJECT_GATEWAY_DATA_TOKEN`、`OBJECT_GATEWAY_CONTROL_TOKEN`:不同且至少 32 字节;普通 worker 仅持有 data token。
|
||||
- `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`:独立审核者的 Ed25519 签名隔离证据,绑定协议、scope、gateway ID、时间窗及 writer/policy/拒写/排空摘要。审核私钥不能交给 worker 或网关。
|
||||
- `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`:**已退役**。旧部署残留的值被忽略,不参与启动、Ready、每次 Execute 或能力判断;不需要管理员签发整桶独占证明,也不会因过期文件拒绝发布。
|
||||
|
||||
秘密只经服务环境/私有文件注入,不进 argv、URL 或日志。所有调用端错误都使用固定原因码。
|
||||
|
||||
正式 worker 使用 `LIBRARY_OBJECT_GATEWAY_URL` 和 `LIBRARY_OBJECT_GATEWAY_TOKEN`;不再依赖预先生成的 grant 文件。`LIBRARY_OBJECT_GATEWAY_GRANT_DIRECTORY` 保留旧离线兼容用途,不是自动模式的授权来源。发布 stack 对所有对象 provider 强制网关;S3 构造仍用于只读捕获/预备份,部署必须使用独立只读凭据。
|
||||
正式 worker 使用 `LIBRARY_OBJECT_GATEWAY_URL` 和 `LIBRARY_OBJECT_GATEWAY_TOKEN`;不再依赖预先生成的 grant 文件。`LIBRARY_OBJECT_GATEWAY_GRANT_DIRECTORY` 保留旧离线兼容用途,不是自动模式的授权来源。发布 stack 对所有对象 provider 强制网关;S3 构造仍用于只读捕获/预备份,可继续使用现有凭据,不再要求独立只读 OSS AK。
|
||||
|
||||
## 已关闭的本仓旁路
|
||||
|
||||
@@ -70,24 +72,34 @@ SELECT-only治理角色需要读取`library_jobs`、`library_operations`、`libr
|
||||
- `librarystack` 不再为缺网关的 MinIO/S3 返回直写 writer。
|
||||
- `libraryctl` capture 的 S3 构造与读取保留;未把读取当作写入口禁用。
|
||||
|
||||
这不等于已经撤销运行环境旧进程的凭据;源码变更尚未部署。`librarydrill` 等无 owned-attempt 的旧写型工具不能靠静态 grant 绕过自动模式,需补正式执行身份后才可用于写入。低层 S3 adapter 仍用于网关后端、读取及显式隔离夹具;部署权限才负责防止外部旧进程直接调用存储。
|
||||
这不等于已经撤销运行环境旧进程的凭据;源码变更尚未部署。`librarydrill` 等无 owned-attempt 的旧写型工具不能靠静态 grant 绕过自动模式,需补正式执行身份后才可用于写入。低层 S3 adapter 仍用于网关后端、读取及显式隔离夹具;共享桶不要求撤除其它写者,跨写者的同 key 竞争按冲突/held 处理。
|
||||
|
||||
## 实测与未完成边界
|
||||
|
||||
已实跑:真实治理 PG(独立 SELECT-only 登录角色)、原 archive、数据 HTTP、业务 PG 与 MinIO 的自动授权/跨 epoch 链;未锚定计划在第一笔 PG 前拒绝,接管保持原发送身份且不增加 PUT。另有真实网关二进制的两次中断验证。隔离审计 authority/故障注入仍是测试设施,不是生产权限证明。
|
||||
已实跑:真实治理 PG(独立 SELECT-only 登录角色)、原 archive、数据 HTTP、业务 PG 与 MinIO 的自动授权/跨 epoch 链;未锚定计划在第一笔 PG 前拒绝,接管保持原发送身份且不增加 PUT。另有真实网关二进制的两次中断验证,以及共享桶下“无整桶证明启动 + 其它 key 并发写入 + 同 key 既有对象拒绝”的真实 PG/MinIO 用例。历史隔离审计 authority/故障注入是当时的测试设施,不是当前部署前置。
|
||||
|
||||
正式 worker 隔离进程证据:真实 Docker 入口闸门/队列、只读 worker 对象身份、独立治理读取角色、网关与业务 PG/ES/MinIO;首次 PG 后和对象提交后两次 SIGKILL,同一原 job/operation/plan 的三代 attempt 完成发布,原对象发送 epoch 与回执不变。隔离构建标签 `kg_isolated_drill` 仅把真实租约缩短至4秒,并拒绝非 loopback 一次性 r8 库;正常构建仍为10分钟。维护上游是无业务凭据的独立 HTTP 夹具,不是运行中的 WeKnora;签名证据绑定本次真实拒写/排空材料,不代表生产独占审核。
|
||||
|
||||
已补独立 `library.recover` 正式worker链、恢复中再次SIGKILL、完整before-image/CAS/精确版本读回,以及来源/目标/原job/计划/operation/任意队列身份拒绝。ES故障代理只转发真实ES并丢弃已提交响应,未伪造服务响应。
|
||||
|
||||
尚未完成:clean-finalization unknown的充分归属与自动收敛;独立运行环境只读对象凭据、ACL/网络和全部writer排空;网关真实OSS端到端;原D0/F11/F12与55项其余缺口。测试/隔离验收与真实部署独占分列,不把生产变更授权当成本机开发授权的前置。
|
||||
尚未完成:clean-finalization unknown的充分归属与自动收敛;网关真实 OSS 端到端与真实部署验收(父执行);原D0/F11/F12与55项其余缺口。测试/隔离验收与真实部署分列。
|
||||
|
||||
维护隔离、独占审核与 grant 是独立前置。配置 true、健康 200、测试自签 authority 都不能代替已部署拒写/排空证据。目前未签发真实环境独占证明,未改生产权限或上游 WeKnora 源码/Schema。
|
||||
整桶独占审核、全部 writer 排空、独立只读对象凭据已不再是发布前置;维护窗口内停目标写入容器并读回确认即可更新。网关按 key 拒绝冲突、精确版本回滚,但不承诺无关写者同时修改同一 key 时仍能自动完成——那种情况会明确冲突或 held,需要人工按回执处理。未改生产权限或上游 WeKnora 源码/Schema。
|
||||
|
||||
## 维护式更新(已采用)
|
||||
|
||||
目标测试 KB 的更新走维护窗口,不再要求证明整桶只有一个写者:
|
||||
|
||||
1. 停目标测试 WeKnora 的写入应用容器(只停该服务,不停机器人、适配器、共享 PG/ES 或其它 KB 的服务),并由容器状态/健康读回确认已停止。这是本轮文档化的运维步骤;代码侧没有新增停容器实现。
|
||||
2. 用既有维护/预备份链记录本次目标 KB/对象范围:实例级维护窗口与写者停写证据由 `internal/libraryfreeze` + `internal/librarymaintenance` 采集,`libraryctl capture`(页面入口 `POST /api/v1/library/captures`)完成闸门排空→停写者→导出快照并保留 `post_stop_digest`;不把通用主机备份当作 KB 预备份的替代。
|
||||
3. 用原 plan、原 WriteSet 摘要和限定对象清单走既有 `ProductionLibraryTarget.Apply`(经正式 worker owned attempt 与网关 grant)更新资源;网关沿用现有 AK,不换凭据、不签独占证明。
|
||||
4. 启动原容器,健康检查与页面业务验收;失败走既有 `library.recover`/`RestoreDrill` 恢复链按原预备份回放并启动原服务,不无限留停机。
|
||||
5. 无关服务与无关 key 全程不需要停;同 key 若仍有外部写者,按上节的冲突/held 处理:双方版本都保留,由人按证据处置,不自动合并。
|
||||
|
||||
部署需变更的组件:`object-gateway`(本候选镜像)、`worker`(同提交镜像),以及两者 env 中移除/留空已退役的 `OBJECT_GATEWAY_ID`、`OBJECT_GATEWAY_AUDIT_FILE`、`OBJECT_GATEWAY_AUDIT_PUBLIC_KEY`。`api`、`web` 无协议变更,可不同步。journal、治理 PG 只读 authority、archive 与 target identity 配置保持不变。
|
||||
|
||||
## 部署与回退(未执行)
|
||||
|
||||
未来测试主机 `120.76.217.51`、生产 `120.76.218.137` 各自独立批准。每批先冻结主机、桶、全部角色/STS/预签名写入口、端口/网络规则、影响、维护窗口与回滚;当前没有执行这些外部变更。
|
||||
测试主机 `120.76.217.51`、生产 `120.76.218.137` 各自独立批准;本候选未在任一主机执行停机、备份、更新或启动。r5 的已完成 OSS 测试授权不能重复使用。
|
||||
|
||||
先关新写准入→排空并对账旧请求→撤旧直写权限/切只读身份并实测拒写→独立审核→批准的新限定对象验收→父复核,才可切业务客户端。r5 的已完成 OSS 测试授权不能重复使用。
|
||||
|
||||
回退先关准入并保留 journal;所有已发送请求有终态才可撤网关,unknown 继续 held。不得因网关故障自动恢复旧直写凭据。权限/网络回退须另批。当前整模块 INCOMPLETE,禁止部署部分候选。
|
||||
按“停目标写入容器→读回确认→备份目标 KB 范围→更新资源→启动→健康/页面验收”执行;失败按原备份恢复并启动原服务。回退先关新写准入并保留 journal;所有已发送请求有终态才可撤网关,unknown 继续 held。不得因网关故障自动恢复旧直写凭据。当前整模块仍未完成真实 OSS 端到端与 clean-finalization 收敛,禁止把本候选当作完整生产就绪。
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# 发布管理流程与页面精简
|
||||
|
||||
## 目标与边界
|
||||
|
||||
现有整库发布页面同时呈现日常发布、维护诊断、恢复演练与历史快照,且封存操作位于页面底部,版本入口重复。此次优化聚焦 `ReleaseTargets` 的信息层级和操作位置,沿用现有 API、权限、授权绑定、维护所有者与恢复确认。
|
||||
|
||||
基线采用云测试站当前前端对应的工作树 `cbc6b98`;该提交的 web 与部署版本 `0b753e5` 相同。并行主干及其他工作树不合并、不覆盖。
|
||||
|
||||
## 操作者主流程
|
||||
|
||||
| 顺序 | 页面操作 | 后台行为 | 完成边界 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | 选择对内或对外知识库 | 读取该范围 overview、快照、作业及维护窗口 | 目标身份与版本来自所选范围;读取失败不当作空库 |
|
||||
| 2 | 封存测试版本 | POST `/api/v1/library/captures`,由队列及执行器捕获测试整库版本 | 投递不等于完成,以作业与登记快照为准;此步不发布生产 |
|
||||
| 3 | 查看变更及发布条件 | 展开变更、备份与恢复验证;若缺基线或恢复证据,沿用对应管理员入口 | 逐条呈现服务端阻塞原因,不删除发布门槛 |
|
||||
| 4 | 签发同步授权 | 管理员调用 `/api/v1/library/authorizations`,绑定目标与当前快照 | 有效期内仍需单独点击启动;不自动执行同步 |
|
||||
| 5 | 启动同步 | 沿用现有同步请求与执行链:固定版本、生产预备份、同步、校验及收尾 | 只在接口条件允许时可执行;运行中主按钮改为查看任务 |
|
||||
| 6 | 查看结果 | 自动刷新作业进度,终态按真实记录呈现 | 失败显示原始原因;维护占用不被其他作业成功覆盖 |
|
||||
| 异常 | 从版本与快照选择恢复点 | 继续执行独立恢复授权、影响确认和恢复条件校验 | 恢复整套目标知识状态,不回退测试知识库 |
|
||||
|
||||
封存、签发授权、启动同步三个动作保持独立,不将其合并为一键生产写入。
|
||||
|
||||
## 页面保留规则
|
||||
|
||||
- 顶栏:唯一的「版本与快照」入口和刷新。
|
||||
- 主工作区:实际目标身份、当前生产版本、候选测试版本、紧凑变更统计、当前主动作与一份阻塞原因。
|
||||
- 已有封存时提供次级「重新封存」;运行时不提供重复封存、授权或同步入口。
|
||||
- 「查看变更明细」「发布准备与恢复验证」「封存完整性」默认折叠;原有详情和验证回调继续存在。
|
||||
- 目标维护正常时折叠,持有维护窗口时自动展开;仍按窗口持有作业展示恢复信息。
|
||||
- 最近作业仅在有记录时展示,正常完成默认折叠,失败默认展开;运行中保留主进度,分项明细按需展开。
|
||||
- 版本抽屉保留历史筛选、分页、校验记录、恢复授权和影响确认,移除重复的封存按钮。
|
||||
- 发布管理页面移除旧文档级发布及历史来源关联区域,只展示整库发布流程。
|
||||
|
||||
## 验收清单
|
||||
|
||||
- [x] 前端生产构建通过。
|
||||
- [x] 原发布、恢复、维护所有者和刷新/切库回归通过。
|
||||
- [x] 同一挂载下依次切换封存、授权、同步,点击只调用对应动作。
|
||||
- [x] 折叠与展开、打开历史不触发业务写入。
|
||||
- [x] 运行时主操作只查看任务,不调用封存、授权或同步。
|
||||
- [x] 正常维护折叠、持有时展开、释放后折叠;切库重置明细展开状态。
|
||||
- [x] 无作业不画空进度;失败原因默认可见。
|
||||
- [x] 只读角色和读取失败时禁止主动作。
|
||||
|
||||
浏览器验收使用本地候选构建,GET/HEAD 通过已有 SSH 通道读取云测试 API;代理拒绝所有写请求。界面验收与真实封存、生产同步及恢复演练分开记录;本任务不执行这些业务写入。完整仓库门禁和云端部署结论以实际运行结果为准,不用上述前端检查替代。
|
||||
@@ -3,6 +3,7 @@ package appconfig
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -129,6 +130,11 @@ type Config struct {
|
||||
// 入口闸门:关入口与排空在它身上做(被代理服务自己不排空)。
|
||||
LibraryTargetIngressService string
|
||||
LibraryTargetIngressDrainFile string
|
||||
// 入口形态:gate(默认)或 none。none 只给整栈没有入口闸门的测试环境用,
|
||||
// 必须同时给 LibraryTargetNoIngressAck,且证据等级降一档并如实记录。
|
||||
LibraryTargetIngressMode string
|
||||
LibraryTargetNoIngressAck string
|
||||
LibraryTargetHealthURL string
|
||||
// 后台静默检查:目标队列 + 目标库(HTTP 排空不等于后台任务排空)。
|
||||
LibraryTargetRedisAddr string
|
||||
LibraryTargetRedisPassword string
|
||||
@@ -247,19 +253,23 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
LibraryTargetIngressURL: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_URL")),
|
||||
LibraryTargetIngressService: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_SERVICE")),
|
||||
LibraryTargetIngressDrainFile: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_DRAIN_FILE")),
|
||||
LibraryTargetRedisAddr: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_ADDR")),
|
||||
LibraryTargetRedisPassword: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_PASSWORD")),
|
||||
LibraryTargetRedisDB: valueOrDefaultInt(getenv("LIBRARY_TARGET_REDIS_DB"), 0),
|
||||
LibraryTargetJournalRoot: strings.TrimSpace(getenv("LIBRARY_TARGET_JOURNAL_ROOT")),
|
||||
LibraryEmbeddingModel: valueOrDefault(strings.TrimSpace(getenv("LIBRARY_EMBEDDING_MODEL")), "text-embedding-v4"),
|
||||
LibraryEmbeddingDimension: embeddingDimension,
|
||||
LibraryCapacityBudgetBytes: capacityBudget,
|
||||
LibraryRetentionVersions: retentionVersions,
|
||||
LibraryRetentionDays: retentionDays,
|
||||
LibraryExecutorStatusMaxAge: 2 * time.Minute,
|
||||
VersionHistoryLinkOrigin: strings.TrimSpace(getenv("VERSION_HISTORY_LINK_ORIGIN")),
|
||||
VersionHistoryHTTPTimeout: 15 * time.Second,
|
||||
VersionHistoryMaxPages: valueOrDefaultInt(getenv("VERSION_HISTORY_MAX_PAGES"), 200),
|
||||
LibraryTargetIngressMode: strings.TrimSpace(getenv("LIBRARY_TARGET_INGRESS_MODE")),
|
||||
// 确认串不 Trim:必须逐字符相等,多一个空格都算没确认。
|
||||
LibraryTargetNoIngressAck: getenv("LIBRARY_NO_INGRESS_ACK"),
|
||||
LibraryTargetHealthURL: strings.TrimSpace(getenv("LIBRARY_TARGET_HEALTH_URL")),
|
||||
LibraryTargetRedisAddr: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_ADDR")),
|
||||
LibraryTargetRedisPassword: strings.TrimSpace(getenv("LIBRARY_TARGET_REDIS_PASSWORD")),
|
||||
LibraryTargetRedisDB: valueOrDefaultInt(getenv("LIBRARY_TARGET_REDIS_DB"), 0),
|
||||
LibraryTargetJournalRoot: strings.TrimSpace(getenv("LIBRARY_TARGET_JOURNAL_ROOT")),
|
||||
LibraryEmbeddingModel: valueOrDefault(strings.TrimSpace(getenv("LIBRARY_EMBEDDING_MODEL")), "text-embedding-v4"),
|
||||
LibraryEmbeddingDimension: embeddingDimension,
|
||||
LibraryCapacityBudgetBytes: capacityBudget,
|
||||
LibraryRetentionVersions: retentionVersions,
|
||||
LibraryRetentionDays: retentionDays,
|
||||
LibraryExecutorStatusMaxAge: 2 * time.Minute,
|
||||
VersionHistoryLinkOrigin: strings.TrimSpace(getenv("VERSION_HISTORY_LINK_ORIGIN")),
|
||||
VersionHistoryHTTPTimeout: 15 * time.Second,
|
||||
VersionHistoryMaxPages: valueOrDefaultInt(getenv("VERSION_HISTORY_MAX_PAGES"), 200),
|
||||
}
|
||||
if config.DatabaseURL == "" {
|
||||
return Config{}, errors.New("DATABASE_URL is required")
|
||||
@@ -387,9 +397,35 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if config.LibraryAgentSocket != "" && !filepath.IsAbs(config.LibraryAgentSocket) {
|
||||
return Config{}, errors.New("LIBRARY_AGENT_SOCKET 必须是绝对路径(unix socket 由主机侧 agent 持有)")
|
||||
}
|
||||
// 入口形态:缺省 gate,行为与之前完全一致。none 只给整栈没有入口闸门的
|
||||
// 测试环境用,必须显式确认;确认串逐字符相等,多一个空格都算没确认。
|
||||
if config.LibraryTargetIngressMode == "" {
|
||||
config.LibraryTargetIngressMode = "gate"
|
||||
}
|
||||
switch config.LibraryTargetIngressMode {
|
||||
case "gate", "none":
|
||||
default:
|
||||
return Config{}, fmt.Errorf("LIBRARY_TARGET_INGRESS_MODE 无效(%q):只能是 gate 或 none",
|
||||
config.LibraryTargetIngressMode)
|
||||
}
|
||||
noGate := config.LibraryTargetIngressMode == "none"
|
||||
if noGate && config.LibraryTargetNoIngressAck != "test-only" {
|
||||
return Config{}, errors.New("no-gate 维护必须显式确认:LIBRARY_NO_INGRESS_ACK 必须逐字符等于 test-only,拒绝启动")
|
||||
}
|
||||
if !noGate && config.LibraryTargetNoIngressAck != "" {
|
||||
return Config{}, errors.New("LIBRARY_NO_INGRESS_ACK 只在 LIBRARY_TARGET_INGRESS_MODE=none 下有效")
|
||||
}
|
||||
if config.LibraryTargetHealthURL != "" {
|
||||
parsed, err := url.Parse(config.LibraryTargetHealthURL)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") ||
|
||||
parsed.Host == "" || parsed.User != nil {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_HEALTH_URL 必须是 http(s) 地址且不带凭据")
|
||||
}
|
||||
}
|
||||
// 目标维护要么完整配置,要么完全不配;半配置会让窗口"看起来存在"却挡不住目标。
|
||||
targetSet := config.LibraryTargetInstance != "" || config.LibraryTargetProject != "" ||
|
||||
len(config.LibraryTargetWriters) > 0 || config.LibraryTargetIngressURL != ""
|
||||
len(config.LibraryTargetWriters) > 0 || config.LibraryTargetIngressURL != "" ||
|
||||
noGate || config.LibraryTargetHealthURL != ""
|
||||
if targetSet {
|
||||
missing := make([]string, 0, 4)
|
||||
if config.LibraryTargetInstance == "" {
|
||||
@@ -401,16 +437,21 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if len(config.LibraryTargetWriters) == 0 {
|
||||
missing = append(missing, "LIBRARY_TARGET_WRITERS")
|
||||
}
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
if noGate {
|
||||
// 没有闸门可声明:健康探针是唯一能证明"目标回来了"的必需项。
|
||||
if config.LibraryTargetHealthURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_HEALTH_URL")
|
||||
}
|
||||
} else if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetJournalRoot == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_JOURNAL_ROOT")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
if !noGate && config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
}
|
||||
if config.LibraryTargetIngressDrainFile == "" {
|
||||
if !noGate && config.LibraryTargetIngressDrainFile == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_DRAIN_FILE")
|
||||
}
|
||||
if config.LibraryTargetRedisAddr == "" {
|
||||
@@ -419,13 +460,17 @@ func Load(getenv func(string) string) (Config, error) {
|
||||
if len(missing) > 0 {
|
||||
return Config{}, fmt.Errorf("目标维护配置不完整:缺少 %s", strings.Join(missing, "、"))
|
||||
}
|
||||
if noGate && (config.LibraryTargetIngressURL != "" || config.LibraryTargetIngressService != "" ||
|
||||
config.LibraryTargetIngressDrainFile != "") {
|
||||
return Config{}, errors.New("no-gate 维护不得同时配置入口地址/闸门服务/排空文件:要么有闸门,要么明确没有")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetJournalRoot) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_JOURNAL_ROOT 必须是绝对路径")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetDockerSocket) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_DOCKER_SOCKET 必须是绝对路径")
|
||||
}
|
||||
if !filepath.IsAbs(config.LibraryTargetIngressDrainFile) {
|
||||
if !noGate && !filepath.IsAbs(config.LibraryTargetIngressDrainFile) {
|
||||
return Config{}, errors.New("LIBRARY_TARGET_INGRESS_DRAIN_FILE 必须是绝对路径")
|
||||
}
|
||||
if len(config.LibraryTargetNonWriters) == 0 {
|
||||
|
||||
@@ -73,12 +73,13 @@ func (r LibraryWorkerRole) LibraryJobKinds() []string {
|
||||
switch r {
|
||||
case LibraryWorkerRoleReleaseExecutor:
|
||||
return []string{string(governance.LibraryJobSync), string(governance.LibraryJobRestore),
|
||||
string(governance.LibraryJobRecover), string(governance.LibraryJobBaseline)}
|
||||
string(governance.LibraryJobRecover), string(governance.LibraryJobBaseline),
|
||||
string(governance.LibraryJobVerify)}
|
||||
case LibraryWorkerRoleCapture:
|
||||
return []string{string(governance.LibraryJobSeal)}
|
||||
default:
|
||||
return []string{string(governance.LibraryJobSeal), string(governance.LibraryJobSync),
|
||||
string(governance.LibraryJobRestore), string(governance.LibraryJobRecover),
|
||||
string(governance.LibraryJobBaseline)}
|
||||
string(governance.LibraryJobBaseline), string(governance.LibraryJobVerify)}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,13 +63,14 @@ func TestLoadRejectsUnknownLibraryWorkerRole(t *testing.T) {
|
||||
// 每个角色收拾哪些 kind、谁拥有 DDL,都必须互斥且覆盖完整。
|
||||
func TestLibraryWorkerRoleOwnershipMaps(t *testing.T) {
|
||||
release := LibraryWorkerRoleReleaseExecutor
|
||||
if got := release.LibraryJobKinds(); len(got) != 4 || got[0] != "sync" || got[1] != "restore" || got[2] != "recover" || got[3] != "baseline" {
|
||||
if got := release.LibraryJobKinds(); len(got) != 5 || got[0] != "sync" || got[1] != "restore" ||
|
||||
got[2] != "recover" || got[3] != "baseline" || got[4] != "verify" {
|
||||
t.Fatalf("release-executor kinds = %v", got)
|
||||
}
|
||||
if got := LibraryWorkerRoleCapture.LibraryJobKinds(); len(got) != 1 || got[0] != "seal" {
|
||||
t.Fatalf("capture kinds = %v", got)
|
||||
}
|
||||
if got := LibraryWorkerRoleFull.LibraryJobKinds(); len(got) != 5 {
|
||||
if got := LibraryWorkerRoleFull.LibraryJobKinds(); len(got) != 6 {
|
||||
t.Fatalf("full kinds = %v", got)
|
||||
}
|
||||
for _, role := range []LibraryWorkerRole{LibraryWorkerRoleFull, LibraryWorkerRoleCapture} {
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -127,12 +129,81 @@ func (r *Runner) runRemote(ctx context.Context, timeout time.Duration, remote st
|
||||
if ctx.Err() != nil {
|
||||
return text, fmt.Errorf("ssh %s 超时(%s)", r.config.Target, timeout)
|
||||
}
|
||||
// 主机侧脚本会用退出码表达"参数非法/有库失败",把输出带回去便于页面展示。
|
||||
return text, fmt.Errorf("ssh %s 执行失败: %s", r.config.Target, firstLines(text, 6))
|
||||
// 连接层失败与主机侧命令失败必须分开说:前者连脚本都没跑,
|
||||
// 任何"脚本缺失/计划未下发"都是把未观测当成事实。
|
||||
return text, hostFailure(r.config.Target, text)
|
||||
}
|
||||
return text, nil
|
||||
}
|
||||
|
||||
// hostFailure 把 ssh 的非零退出分成两类:连不上,和连上了但远端命令失败。
|
||||
func hostFailure(target, text string) error {
|
||||
if connectionFailurePattern.MatchString(text) {
|
||||
return fmt.Errorf("目标主机 %s 不可达(%s);未执行任何主机侧脚本,脚本与计划状态未知",
|
||||
target, firstLines(text, 3))
|
||||
}
|
||||
// 主机侧脚本会用退出码表达"参数非法/有库失败",把输出带回去便于页面展示。
|
||||
return fmt.Errorf("已连上 %s,但主机侧命令失败: %s", target, firstLines(text, 6))
|
||||
}
|
||||
|
||||
// connectionFailurePattern 是 OpenSSH 客户端在"根本没建立连接"时打的原文。
|
||||
// 只有命中它才允许说"不可达";其余非零退出属于远端命令自己的失败。
|
||||
var connectionFailurePattern = regexp.MustCompile(`(?i)ssh: connect to host|Operation timed out|Connection timed out|Connection refused|Connection reset by peer|No route to host|Network is unreachable|Could not resolve hostname|Host key verification failed|Permission denied|Connection closed by`)
|
||||
|
||||
// Endpoint 读回挂载的 ssh 配置里本别名真实拨号的 host:port。
|
||||
// 页面必须能说清"现在连的是哪个地址"——只显示别名时,改错文件也看不出来。
|
||||
func (r *Runner) Endpoint() string {
|
||||
if r == nil || r.config.SSHConfig == "" || r.config.Target == "" {
|
||||
return ""
|
||||
}
|
||||
raw, err := os.ReadFile(r.config.SSHConfig)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
host, port := sshConfigEndpoint(string(raw), r.config.Target)
|
||||
if host == "" {
|
||||
return ""
|
||||
}
|
||||
if port == "" {
|
||||
port = "22"
|
||||
}
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
// sshConfigEndpoint 解析 `Host <alias>` 段的 HostName/Port(首个匹配段生效)。
|
||||
// 只做"页面显示用"的最小解析,不替代 ssh 自己的配置语义。
|
||||
func sshConfigEndpoint(config, alias string) (string, string) {
|
||||
host, port := "", ""
|
||||
active := false
|
||||
for _, line := range strings.Split(config, "\n") {
|
||||
fields := strings.Fields(strings.TrimSpace(line))
|
||||
if len(fields) == 0 || strings.HasPrefix(fields[0], "#") {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(fields[0])
|
||||
if key == "host" {
|
||||
active = false
|
||||
for _, pattern := range fields[1:] {
|
||||
if pattern == alias {
|
||||
active = true
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if !active || len(fields) < 2 {
|
||||
continue
|
||||
}
|
||||
switch key {
|
||||
case "hostname":
|
||||
host = strings.Trim(fields[1], `"`)
|
||||
case "port":
|
||||
port = strings.Trim(fields[1], `"`)
|
||||
}
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
var scheduleAppliedPattern = regexp.MustCompile(
|
||||
`SCHEDULE_APPLIED enabled=(\d)(?: time=(\S+))?(?: days=(\d+))?(?: months=(\d+))?(?: next=(\S+))?(?: cron=(\S+))?`)
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package backuphost
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -188,6 +190,41 @@ func TestRunnerSurfacesHostFailure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 现场原错:ssh: connect to host 120.76.217.51 port 22: Operation timed out。
|
||||
// 连不上时脚本根本没跑,只能说"不可达";断言配对的反面:不能说脚本缺失。
|
||||
func TestHostFailureSeparatesUnreachableFromRemoteCommand(t *testing.T) {
|
||||
unreachable := hostFailure("weknora-test", "ssh: connect to host 120.76.217.51 port 22: Operation timed out")
|
||||
if !strings.Contains(unreachable.Error(), "不可达") || !strings.Contains(unreachable.Error(), "Operation timed out") {
|
||||
t.Fatalf("连接超时应报不可达并带原错: %v", unreachable)
|
||||
}
|
||||
if strings.Contains(unreachable.Error(), "主机侧命令失败") || strings.Contains(unreachable.Error(), "脚本缺失") {
|
||||
t.Fatalf("连接失败不能报成远端命令/脚本问题: %v", unreachable)
|
||||
}
|
||||
remote := hostFailure("weknora-test", "bash: /opt/weknora-test/logical-backup.sh: No such file or directory")
|
||||
if !strings.Contains(remote.Error(), "已连上") || !strings.Contains(remote.Error(), "No such file") {
|
||||
t.Fatalf("已连接但脚本失败时应原样带回原因: %v", remote)
|
||||
}
|
||||
if strings.Contains(remote.Error(), "不可达") {
|
||||
t.Fatalf("已连上就不能说不可达: %v", remote)
|
||||
}
|
||||
}
|
||||
|
||||
// 页面要能说清"实际拨号的地址",否则改错 ssh_config 也看不出来。
|
||||
func TestRunnerEndpointReadsDialedHostPort(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "ssh_config")
|
||||
config := "Host other\n HostName 10.0.0.9\n Port 2222\n\nHost weknora-test\n HostName 100.85.111.121\n HostKeyAlias 120.76.217.51\n"
|
||||
if err := os.WriteFile(path, []byte(config), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := New(Config{SSHConfig: path, Target: "weknora-test", ScriptDir: "/opt/weknora-test"})
|
||||
if got := runner.Endpoint(); got != "100.85.111.121:22" {
|
||||
t.Fatalf("应报真实拨号地址(默认 22),得到 %q", got)
|
||||
}
|
||||
if got := New(Config{SSHConfig: filepath.Join(t.TempDir(), "missing"), Target: "weknora-test"}).Endpoint(); got != "" {
|
||||
t.Fatalf("配置文件不可读时应返回空,不猜地址,得到 %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerRunNowReadsBackLastRun(t *testing.T) {
|
||||
calls := 0
|
||||
runner := testRunner(func(_ context.Context, _ time.Duration, remote string) (string, error) {
|
||||
|
||||
@@ -16,9 +16,8 @@ func developmentNotes(changelog, version, sha string) string {
|
||||
return ""
|
||||
}
|
||||
lines := strings.Split(changelog, "\n")
|
||||
prefix := "## 开发快照 snapshot/v" + version + " - "
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, prefix) {
|
||||
if developmentHeadingMatches(line, version) {
|
||||
end := len(lines)
|
||||
for j := i + 1; j < len(lines); j++ {
|
||||
if strings.HasPrefix(lines[j], "## ") {
|
||||
@@ -39,3 +38,33 @@ func developmentNotes(changelog, version, sha string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// developmentHeadingMatches 接受两种真实标题:
|
||||
// - 旧格式:## 开发快照 snapshot/v0.1.1 - 2026-10-18
|
||||
// - 开发快照:## 开发快照 0.1.351(索引预检使用目标映射 ID)
|
||||
//
|
||||
// 版本号后必须是字符串结束或分隔符,避免 0.1.35 命中 0.1.351;
|
||||
// 只读取到下一个二级标题,不跨版本吞正文。
|
||||
func developmentHeadingMatches(line, version string) bool {
|
||||
prefixes := []string{
|
||||
"## 开发快照 snapshot/v" + version,
|
||||
"## 开发快照 " + version,
|
||||
}
|
||||
for _, prefix := range prefixes {
|
||||
if !strings.HasPrefix(line, prefix) {
|
||||
continue
|
||||
}
|
||||
rest := line[len(prefix):]
|
||||
if rest == "" {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(rest, "—") || strings.HasPrefix(rest, "(") {
|
||||
return true
|
||||
}
|
||||
switch rest[0] {
|
||||
case ' ', '\t', '-', '(':
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -44,6 +44,10 @@ const (
|
||||
// 读出来封存成快照并逐类校验,再登记成 production_effective。
|
||||
// 它不写目标内容、不清空目标、不猜版本——基线只能来自真实捕获与校验。
|
||||
LibraryJobBaseline LibraryJobKind = "baseline"
|
||||
// LibraryJobVerify 是"恢复验证":在维护窗口内捕获目标 → 校验基线一致 →
|
||||
// 写一个 canary 证明校验能发现差异 → 按预备份恢复 → 复核完全一致。
|
||||
// 它是唯一能产生"生产可恢复"证据的作业,页面不再接受手填开关。
|
||||
LibraryJobVerify LibraryJobKind = "verify"
|
||||
)
|
||||
|
||||
type LibraryJobState string
|
||||
@@ -173,6 +177,12 @@ func BaselineStages() []string {
|
||||
return []string{"capture", "verify", "finalize"}
|
||||
}
|
||||
|
||||
// VerifyStages 是恢复验证的阶段:捕获 → 基线校验 → 写 canary(证明校验有效)
|
||||
// → 按预备份恢复 → 恢复后复核。canary 与 restore 都是真实写入,失败必须可见。
|
||||
func VerifyStages() []string {
|
||||
return []string{"capture", "verify", "canary", "restore", "finalize"}
|
||||
}
|
||||
|
||||
// LibraryReleaseAuthorization 是生产发布授权:绑定目标、快照、参数摘要与有效期。
|
||||
type LibraryReleaseAuthorization struct {
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -147,7 +147,7 @@ func ValidateLibraryOperation(op LibraryOperation) error {
|
||||
return errors.New("library operation requires internal or external scope")
|
||||
}
|
||||
switch op.Kind {
|
||||
case LibraryJobSeal, LibraryJobSync, LibraryJobRestore, LibraryJobBaseline, LibraryJobRecover:
|
||||
case LibraryJobSeal, LibraryJobSync, LibraryJobRestore, LibraryJobBaseline, LibraryJobRecover, LibraryJobVerify:
|
||||
default:
|
||||
return fmt.Errorf("unsupported library operation kind %q", op.Kind)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// LibraryRestoreVerification 是一次**真实**恢复验证(备份 → canary → 恢复 → 复核)的登记。
|
||||
//
|
||||
// 为什么不能继续用环境变量 PRODUCTION_BACKUP_RESTORE_VERIFIED:那是一个可以手填的开关,
|
||||
// 它既证明不了"有人真跑过恢复",也证明不了"跑的是当前目标"。这里的每条记录由 worker
|
||||
// 在真实演练结束后写入,绑定 target_id(实例+库+索引+桶)与 scope,失败也照实留痕。
|
||||
type LibraryRestoreVerification struct {
|
||||
ID string `json:"id"`
|
||||
Scope string `json:"scope"`
|
||||
Status string `json:"status"` // passed | failed
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
JobID string `json:"job_id"`
|
||||
Failure string `json:"failure,omitempty"`
|
||||
Report map[string]any `json:"report,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt time.Time `json:"finished_at"`
|
||||
}
|
||||
|
||||
const (
|
||||
LibraryVerificationPassed = "passed"
|
||||
LibraryVerificationFailed = "failed"
|
||||
)
|
||||
|
||||
// LibraryVerificationSatisfied 只回答一件事:这条证据能不能支撑"当前目标的恢复验证已通过"。
|
||||
// 通过的条件必须是全部成立——状态 passed、scope 相同、快照非空、完成时间有效。
|
||||
// target_id 的匹配由查询层(按当前 target_id 过滤)保证,这里不重复比较。
|
||||
func LibraryVerificationSatisfied(verification LibraryRestoreVerification, scope string) (bool, string) {
|
||||
if verification.Status != LibraryVerificationPassed {
|
||||
reason := "最近一次恢复验证没有通过"
|
||||
if detail := strings.TrimSpace(verification.Failure); detail != "" {
|
||||
reason += ":" + detail
|
||||
}
|
||||
return false, reason
|
||||
}
|
||||
if verification.Scope != scope {
|
||||
return false, "恢复验证证据属于另一个 scope(" + verification.Scope + ")"
|
||||
}
|
||||
if strings.TrimSpace(verification.SnapshotID) == "" {
|
||||
return false, "恢复验证证据没有绑定快照,不能作为备份可恢复的证明"
|
||||
}
|
||||
if verification.FinishedAt.IsZero() {
|
||||
return false, "恢复验证证据没有完成时间"
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 证据能不能支撑"当前目标可恢复"只由这四个条件回答:
|
||||
// 状态 passed、scope 相同、绑定了快照、有完成时间。少任何一条都不算。
|
||||
func TestLibraryVerificationSatisfied(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
cases := []struct {
|
||||
name string
|
||||
verification LibraryRestoreVerification
|
||||
scope string
|
||||
want bool
|
||||
reason string
|
||||
}{
|
||||
{name: "passed with snapshot", scope: "internal", want: true,
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
SnapshotID: "snap-1", FinishedAt: now}},
|
||||
{name: "failed carries failure", scope: "internal", want: false, reason: "canary 后校验仍通过",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationFailed, Scope: "internal",
|
||||
SnapshotID: "snap-1", Failure: "canary 后校验仍通过", FinishedAt: now}},
|
||||
{name: "wrong scope", scope: "internal", want: false, reason: "另一个 scope",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "external",
|
||||
SnapshotID: "snap-1", FinishedAt: now}},
|
||||
{name: "no snapshot", scope: "internal", want: false, reason: "没有绑定快照",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
FinishedAt: now}},
|
||||
{name: "no finish time", scope: "internal", want: false, reason: "没有完成时间",
|
||||
verification: LibraryRestoreVerification{Status: LibraryVerificationPassed, Scope: "internal",
|
||||
SnapshotID: "snap-1"}},
|
||||
}
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
ok, reason := LibraryVerificationSatisfied(testCase.verification, testCase.scope)
|
||||
if ok != testCase.want {
|
||||
t.Fatalf("satisfied=%v want=%v reason=%q", ok, testCase.want, reason)
|
||||
}
|
||||
if !testCase.want && testCase.reason != "" && !containsText(reason, testCase.reason) {
|
||||
t.Fatalf("reason=%q want contains %q", reason, testCase.reason)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func containsText(haystack, needle string) bool {
|
||||
return len(haystack) >= len(needle) && (haystack == needle || indexOf(haystack, needle) >= 0)
|
||||
}
|
||||
|
||||
func indexOf(haystack, needle string) int {
|
||||
for i := 0; i+len(needle) <= len(haystack); i++ {
|
||||
if haystack[i:i+len(needle)] == needle {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
@@ -36,8 +36,11 @@ type ServiceHealth struct {
|
||||
ObservedAt time.Time `json:"observed_at"`
|
||||
LastSuccessAt *time.Time `json:"last_success_at"`
|
||||
ConsecutiveFailures int `json:"consecutive_failures"`
|
||||
Configured bool `json:"configured"`
|
||||
Applicable bool `json:"applicable"`
|
||||
// ProbeTarget names the object being probed (instance + short binding id),
|
||||
// never credentials or full UUIDs, so a red light can be attributed.
|
||||
ProbeTarget string `json:"probe_target"`
|
||||
Configured bool `json:"configured"`
|
||||
Applicable bool `json:"applicable"`
|
||||
}
|
||||
|
||||
type KnowledgeBaseProgress struct {
|
||||
|
||||
@@ -116,6 +116,7 @@ type CatalogRouteRequest struct {
|
||||
// fulfillment path needs to materialize and fetch it later.
|
||||
type CatalogRouteFile struct {
|
||||
NodeID string
|
||||
ParentNodeID string
|
||||
Name string
|
||||
RelativePath string
|
||||
SizeBytes *int64
|
||||
|
||||
@@ -148,6 +148,11 @@ func classifyRole(method, path string) string {
|
||||
if strings.HasPrefix(path, "/api/v1/backup-schedule") && method != http.MethodGet {
|
||||
return roleAdmin
|
||||
}
|
||||
// 核对封存结果会把一个 pending 业务作业修成 succeeded;只允许管理员,
|
||||
// 且 handler 仍要求 completed queue、精确版本和归档复核。
|
||||
if strings.HasPrefix(path, "/api/v1/library/jobs/") && strings.HasSuffix(path, "/reconcile-capture") {
|
||||
return roleAdmin
|
||||
}
|
||||
// 版本记录的同步会拉起一次对远端仓库的抓取(消耗网络与数据库写入),
|
||||
// 与"谁能改生产绑定"同级:读任何角色可看,同步只能管理员。
|
||||
if strings.HasPrefix(path, "/api/v1/version-history") && method != http.MethodGet {
|
||||
|
||||
@@ -24,6 +24,9 @@ type BackupScheduleStore interface {
|
||||
type BackupHost interface {
|
||||
Available() bool
|
||||
Target() string
|
||||
// Endpoint 是挂载的 ssh 配置里该别名真实拨号的 host:port;页面据此证明
|
||||
// "连的是哪条通道",而不是只显示一个别名。
|
||||
Endpoint() string
|
||||
Show(context.Context) (backuphost.ShowResult, error)
|
||||
Apply(context.Context, bool, string, int, int) (backuphost.ApplyResult, error)
|
||||
RunNow(context.Context) (backuphost.RunResult, error)
|
||||
@@ -82,8 +85,11 @@ type backupScheduleView struct {
|
||||
// HostConfigured/HostReachable 区分"没接线"与"接线了但连不上"。
|
||||
HostConfigured bool `json:"host_configured"`
|
||||
HostTarget string `json:"host_target"`
|
||||
HostReachable bool `json:"host_reachable"`
|
||||
HostError string `json:"host_error"`
|
||||
// HostEndpoint 是这条别名真实拨号的 host:port(来自挂载的 ssh 配置)。
|
||||
// 连接失败时它就是"到底往哪连"的唯一证据,不能只给一个别名让人猜。
|
||||
HostEndpoint string `json:"host_endpoint"`
|
||||
HostReachable bool `json:"host_reachable"`
|
||||
HostError string `json:"host_error"`
|
||||
|
||||
HostScheduleEnabled bool `json:"host_schedule_enabled"`
|
||||
HostScheduleTime string `json:"host_schedule_time"`
|
||||
@@ -92,6 +98,8 @@ type backupScheduleView struct {
|
||||
HostCronLines []string `json:"host_cron_lines"`
|
||||
HostCronService string `json:"host_cron_service"`
|
||||
HostScriptPresent bool `json:"host_script_present"`
|
||||
// HostCheckedAt 是本次真实主机探测时间;没有探测(只读库)时为空。
|
||||
HostCheckedAt string `json:"host_checked_at,omitempty"`
|
||||
|
||||
LastRunStamp string `json:"last_run_stamp"`
|
||||
LastRunCreatedAt string `json:"last_run_created_at"`
|
||||
@@ -281,6 +289,7 @@ func (config Config) backupSchedulePayload(ctx context.Context, probeHost bool)
|
||||
}
|
||||
view.HostConfigured = config.BackupHost.Available()
|
||||
view.HostTarget = config.BackupHost.Target()
|
||||
view.HostEndpoint = config.BackupHost.Endpoint()
|
||||
if !view.HostConfigured {
|
||||
view.InSync = false
|
||||
view.HostError = "目标主机凭据未挂载(BACKUP_HOST_SSH_CONFIG / BACKUP_HOST_TARGET)"
|
||||
@@ -289,10 +298,14 @@ func (config Config) backupSchedulePayload(ctx context.Context, probeHost bool)
|
||||
if !probeHost {
|
||||
return view, nil
|
||||
}
|
||||
view.HostCheckedAt = formatTime(config.Now().UTC())
|
||||
show, err := config.BackupHost.Show(ctx)
|
||||
if err != nil {
|
||||
view.HostReachable = false
|
||||
view.HostError = err.Error()
|
||||
if view.HostEndpoint != "" {
|
||||
view.HostError += "(拨号地址 " + view.HostEndpoint + ")"
|
||||
}
|
||||
view.InSync = false
|
||||
} else {
|
||||
view.HostReachable = true
|
||||
|
||||
@@ -46,6 +46,7 @@ func (store *fakeBackupScheduleStore) MarkBackupScheduleApplied(_ context.Contex
|
||||
type fakeBackupHost struct {
|
||||
available bool
|
||||
target string
|
||||
endpoint string
|
||||
show backuphost.ShowResult
|
||||
showErr error
|
||||
apply backuphost.ApplyResult
|
||||
@@ -59,6 +60,9 @@ type fakeBackupHost struct {
|
||||
|
||||
func (host *fakeBackupHost) Available() bool { return host.available }
|
||||
func (host *fakeBackupHost) Target() string { return host.target }
|
||||
func (host *fakeBackupHost) Endpoint() string {
|
||||
return host.endpoint
|
||||
}
|
||||
func (host *fakeBackupHost) Show(context.Context) (backuphost.ShowResult, error) {
|
||||
return host.show, host.showErr
|
||||
}
|
||||
@@ -166,6 +170,48 @@ func TestHandleBackupScheduleHostFlagsDrift(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostUnreachableSeparatesConnectionFromHostState(t *testing.T) {
|
||||
// 现场原错:ssh connect to host 120.76.217.51 port 22 Operation timed out。
|
||||
// 连接没建立 ⇒ 既不能报"脚本缺失",也不能报"计划未下发",只能报不可达 + 拨号地址。
|
||||
host := &fakeBackupHost{
|
||||
available: true, target: "weknora-test", endpoint: "100.85.111.121:22",
|
||||
showErr: errors.New("目标主机 weknora-test 不可达(ssh: connect to host 120.76.217.51 port 22: Operation timed out);未执行任何主机侧脚本,脚本与计划状态未知"),
|
||||
}
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, host)
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupScheduleHost(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/host", nil))
|
||||
data := decodeData(t, recorder)
|
||||
if data["host_reachable"] != false || data["in_sync"] != false {
|
||||
t.Fatalf("连不上时不能报告可达或已同步: %+v", data)
|
||||
}
|
||||
message := data["host_error"].(string)
|
||||
if !strings.Contains(message, "不可达") || !strings.Contains(message, "100.85.111.121:22") {
|
||||
t.Fatalf("应报不可达并给出真实拨号地址: %q", message)
|
||||
}
|
||||
if strings.Contains(message, "脚本缺失") || strings.Contains(message, "未下发") {
|
||||
t.Fatalf("未观测到的脚本/计划状态不能当成结论: %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostReportsMissingScriptOnlyAfterConnecting(t *testing.T) {
|
||||
// 真正的脚本缺失:ssh 已连上,主机回的是脚本不存在 ⇒ 这时才允许说脚本缺失。
|
||||
host := &fakeBackupHost{
|
||||
available: true, target: "weknora-test", endpoint: "100.85.111.121:22",
|
||||
showErr: errors.New("已连上 weknora-test,但主机侧命令失败: bash: /opt/weknora-test/install_backup_schedule.sh: No such file or directory"),
|
||||
}
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, host)
|
||||
recorder := httptest.NewRecorder()
|
||||
config.handleBackupScheduleHost(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/backup-schedule/host", nil))
|
||||
data := decodeData(t, recorder)
|
||||
message := data["host_error"].(string)
|
||||
if !strings.Contains(message, "已连上") || !strings.Contains(message, "No such file") {
|
||||
t.Fatalf("已连接但脚本缺失时应原样带回主机原因: %q", message)
|
||||
}
|
||||
if strings.Contains(message, "不可达") {
|
||||
t.Fatalf("已连上就不能说不可达: %q", message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleBackupScheduleHostUnconfiguredDegrades(t *testing.T) {
|
||||
config := backupConfig(&fakeBackupScheduleStore{schedule: goldenSchedule}, &fakeBackupHost{available: false})
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
)
|
||||
|
||||
// libraryCaptureReconciler is deliberately narrow: it repairs one business
|
||||
// seal job only after the completed capture queue, the successful export stage,
|
||||
// the exact test-sealed version and the archived bytes all agree. It never
|
||||
// captures, freezes writers, creates snapshots, writes source/target content or
|
||||
// changes historical failure evidence.
|
||||
type libraryCaptureReconciler interface {
|
||||
LibraryJob(ctx context.Context, id string) (governance.LibraryJob, error)
|
||||
LibraryCaptureExportEvidence(ctx context.Context, jobID string) (string, string, error)
|
||||
LibraryTestSealedVersionForJob(ctx context.Context, scope, jobID string) (governance.LibraryVersion, bool, error)
|
||||
EnsureLibraryOperation(ctx context.Context, op governance.LibraryOperation) (governance.LibraryOperation, bool, error)
|
||||
ReconcileLibraryCaptureTerminal(ctx context.Context, jobID, scope, queueJobID, snapshotID string, now time.Time) (bool, error)
|
||||
ReconcileMisclassifiedLibraryCapture(ctx context.Context, jobID, scope, queueJobID, snapshotID, expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time, auditID, actor, reason string, evidence map[string]any, now time.Time) (bool, error)
|
||||
}
|
||||
|
||||
const (
|
||||
legacyOrphanError = "队列中已无对应作业,业务作业未收到结果"
|
||||
legacyOrphanRecovery = "封存作业由 agent 恢复来源;确认来源 healthy 后可重发"
|
||||
)
|
||||
|
||||
type libraryCaptureQueueReader interface {
|
||||
QueueJob(ctx context.Context, id string) (jobs.Job, bool, error)
|
||||
}
|
||||
|
||||
func libraryCaptureReconcile(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
store, ok := config.Store.(libraryCaptureReconciler)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "封存作业核对存储不可用"})
|
||||
return
|
||||
}
|
||||
queueStore, ok := config.Store.(libraryCaptureQueueReader)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "队列精确读取不可用"})
|
||||
return
|
||||
}
|
||||
jobID := strings.TrimSpace(r.PathValue("id"))
|
||||
if jobID == "" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "job id required"})
|
||||
return
|
||||
}
|
||||
job, err := store.LibraryJob(r.Context(), jobID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusNotFound, map[string]any{"available": false, "reason": "作业不存在"})
|
||||
return
|
||||
}
|
||||
if job.Kind != governance.LibraryJobSeal {
|
||||
conflict(w, "只有封存作业可以核对封存结果")
|
||||
return
|
||||
}
|
||||
systemMisclassified := job.State == governance.LibraryJobFailed && job.Stage == "" &&
|
||||
job.Error == legacyOrphanError && job.Recovery == legacyOrphanRecovery && job.SnapshotID == ""
|
||||
if job.State != governance.LibraryJobPending && job.State != governance.LibraryJobRunning &&
|
||||
job.State != governance.LibraryJobSucceeded && !systemMisclassified {
|
||||
conflict(w, "作业是真实失败/取消终态,不能改写成成功")
|
||||
return
|
||||
}
|
||||
if job.QueueJobID == "" {
|
||||
conflict(w, "作业没有关联队列号,不能证明封存完成")
|
||||
return
|
||||
}
|
||||
captureJob := strings.TrimPrefix(job.ID, "libseal-")
|
||||
if captureJob == "" || captureJob == job.ID {
|
||||
conflict(w, "作业号不是封存作业身份")
|
||||
return
|
||||
}
|
||||
queue, found, err := queueStore.QueueJob(r.Context(), job.QueueJobID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read capture queue"})
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
conflict(w, "队列作业不存在,不能证明封存完成")
|
||||
return
|
||||
}
|
||||
if queue.Type != libraryCaptureJobType || queue.State != jobs.StateCompleted {
|
||||
conflict(w, "队列作业不是 completed 的 library.capture")
|
||||
return
|
||||
}
|
||||
var payload struct {
|
||||
Scope string `json:"scope"`
|
||||
CaptureJob string `json:"capture_job"`
|
||||
}
|
||||
if err := json.Unmarshal(queue.Payload, &payload); err != nil ||
|
||||
payload.Scope != job.Scope || payload.CaptureJob != captureJob ||
|
||||
queue.IdempotencyKey != "library.capture:"+captureJob {
|
||||
conflict(w, "队列作业的 scope/capture_job/幂等键与业务作业不一致")
|
||||
return
|
||||
}
|
||||
stageState, _, err := store.LibraryCaptureExportEvidence(r.Context(), job.ID)
|
||||
if err != nil || stageState != "succeeded" {
|
||||
conflict(w, "导出阶段没有成功证据,不能修复终态")
|
||||
return
|
||||
}
|
||||
version, found, err := store.LibraryTestSealedVersionForJob(r.Context(), job.Scope, job.ID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot read exact test-sealed version"})
|
||||
return
|
||||
}
|
||||
if !found || version.Kind != governance.LibraryVersionTestSealed ||
|
||||
version.Scope != job.Scope || version.SourceJobID != job.ID || version.SnapshotID == "" {
|
||||
conflict(w, "没有与作业/scope 精确绑定的测试封存版本")
|
||||
return
|
||||
}
|
||||
source := configuredLibrarySource(config, job.Scope)
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "来源身份配置不完整,无法核对封存来源"})
|
||||
return
|
||||
}
|
||||
evidenceSource, _ := version.Evidence["source_id"].(string)
|
||||
evidenceCapture, _ := version.Evidence["capture_job"].(string)
|
||||
if evidenceSource != sourceID || evidenceCapture != captureJob {
|
||||
conflict(w, "版本来源或 capture_job 与当前作业不一致")
|
||||
return
|
||||
}
|
||||
if config.LibraryArchiveRoot == "" {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "未接线归档根,无法复核归档完整性"})
|
||||
return
|
||||
}
|
||||
archive, err := librarysnapshot.Open(config.LibraryArchiveRoot, 256*1024*1024)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"available": false,
|
||||
"reason": "归档不可读,不能确认封存完整"})
|
||||
return
|
||||
}
|
||||
defer archive.Close()
|
||||
manifest, err := archive.Load(version.SnapshotID)
|
||||
if err != nil {
|
||||
conflict(w, "归档字节复核失败,不能修复作业")
|
||||
return
|
||||
}
|
||||
if manifest.Scope != source.Scope || manifest.TenantID != source.TenantID ||
|
||||
manifest.KnowledgeBaseID != source.KnowledgeBaseID {
|
||||
conflict(w, "归档清单来源身份与当前来源不一致")
|
||||
return
|
||||
}
|
||||
if job.SnapshotID != "" && job.SnapshotID != version.SnapshotID {
|
||||
conflict(w, "作业已有不同快照,不能覆盖")
|
||||
return
|
||||
}
|
||||
if job.State == governance.LibraryJobSucceeded {
|
||||
if job.SnapshotID != version.SnapshotID {
|
||||
conflict(w, "作业已成功但快照与精确版本不一致")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": false, "already_succeeded": true, "job_id": job.ID,
|
||||
"snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"version_id": version.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
actor := ""
|
||||
if identity, ok := sessionFrom(r.Context()); ok {
|
||||
actor = identity.Username
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
digest, err := governance.LibraryRequestDigest(job.Scope, governance.LibraryJobRecover, map[string]any{
|
||||
"kind": "capture_reconcile", "job_id": job.ID, "queue_job_id": queue.ID,
|
||||
"snapshot_id": version.SnapshotID, "capture_job": captureJob, "stage": "export",
|
||||
})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot build reconcile audit digest"})
|
||||
return
|
||||
}
|
||||
if systemMisclassified {
|
||||
auditID := "librepair-" + digest[:16]
|
||||
updated, repairErr := store.ReconcileMisclassifiedLibraryCapture(r.Context(), job.ID, job.Scope,
|
||||
job.QueueJobID, version.SnapshotID, job.Stage, legacyOrphanError, legacyOrphanRecovery,
|
||||
job.FinishedAt, auditID, actor, "completed capture queue + export succeeded + exact version + archive verified; legacy orphan misclassification", map[string]any{
|
||||
"original_state": job.State, "original_error": job.Error, "original_recovery": job.Recovery,
|
||||
"original_finished_at": job.FinishedAt, "queue_job_id": queue.ID,
|
||||
"snapshot_id": version.SnapshotID, "version_id": version.ID, "archive_verified": true,
|
||||
}, now)
|
||||
if repairErr != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot persist misclassification repair"})
|
||||
return
|
||||
}
|
||||
if !updated {
|
||||
conflict(w, "原误判行状态/错误/完成时间已变化,拒绝覆盖")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": true, "already_succeeded": false, "misclassification_repaired": true,
|
||||
"job_id": job.ID, "snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"stage": "export", "version_id": version.ID, "audit_repair_id": auditID,
|
||||
})
|
||||
return
|
||||
}
|
||||
opID := "libop-capture-reconcile-" + digest[:16]
|
||||
op, _, err := store.EnsureLibraryOperation(r.Context(), governance.LibraryOperation{
|
||||
ID: opID, Scope: job.Scope, Kind: governance.LibraryJobRecover,
|
||||
TargetSnapshotID: version.SnapshotID, RequestDigest: digest,
|
||||
OperationKey: "capture-reconcile:" + job.ID, Actor: actor, CreatedAt: now,
|
||||
})
|
||||
if errors.Is(err, governance.ErrLibraryOperationConflict) {
|
||||
conflict(w, "同一核对键对应了不同证据,拒绝修复")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot record reconcile audit"})
|
||||
return
|
||||
}
|
||||
updated, err := store.ReconcileLibraryCaptureTerminal(r.Context(), job.ID, job.Scope, job.QueueJobID, version.SnapshotID, now)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{
|
||||
"error": "cannot persist reconciled job terminal", "audit_operation_id": op.ID,
|
||||
})
|
||||
return
|
||||
}
|
||||
if !updated {
|
||||
conflict(w, "作业状态/身份在核对期间变化,拒绝覆盖")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"repaired": true, "already_succeeded": false, "job_id": job.ID,
|
||||
"snapshot_id": version.SnapshotID, "queue_job_id": queue.ID,
|
||||
"stage": "export", "version_id": version.ID, "audit_operation_id": op.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func conflict(w http.ResponseWriter, reason string) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"available": false, "repaired": false, "reason": reason})
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
)
|
||||
|
||||
func (f *fakeLibraryStore) QueueJob(_ context.Context, id string) (jobs.Job, bool, error) {
|
||||
if f.queue == nil || f.queue.ID != id {
|
||||
return jobs.Job{}, false, nil
|
||||
}
|
||||
return *f.queue, true, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryCaptureExportEvidence(_ context.Context, _ string) (string, string, error) {
|
||||
return f.stageState, "", f.stageErr
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryJobStage(_ context.Context, _, stage string) (string, string, error) {
|
||||
if f.stageErr != nil {
|
||||
return "", "", f.stageErr
|
||||
}
|
||||
if stage != "export" {
|
||||
return "", "", errors.New("unexpected stage")
|
||||
}
|
||||
return f.stageState, "", nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) LibraryTestSealedVersionForJob(_ context.Context, scope, jobID string) (governance.LibraryVersion, bool, error) {
|
||||
if f.versionErr != nil {
|
||||
return governance.LibraryVersion{}, false, f.versionErr
|
||||
}
|
||||
if f.sealedVersion == nil || f.sealedVersion.Scope != scope || f.sealedVersion.SourceJobID != jobID {
|
||||
return governance.LibraryVersion{}, false, nil
|
||||
}
|
||||
return *f.sealedVersion, true, nil
|
||||
}
|
||||
|
||||
func reconcileFixture(t *testing.T) (*fakeLibraryStore, Config, governance.LibraryJob, []byte) {
|
||||
t.Helper()
|
||||
root := libraryFixtureRoot(t)
|
||||
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
||||
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest, err := archive.Load(snapshotID)
|
||||
archive.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config := Config{LibraryArchiveRoot: root,
|
||||
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
||||
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
job := governance.LibraryJob{ID: "libseal-cap-1", Scope: "internal", Kind: governance.LibraryJobSeal,
|
||||
State: governance.LibraryJobPending, QueueJobID: "libcap-1"}
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "cap-1"})
|
||||
store := &fakeLibraryStore{
|
||||
jobs: []governance.LibraryJob{job},
|
||||
stageState: "succeeded",
|
||||
queue: &jobs.Job{ID: "libcap-1", Type: libraryCaptureJobType, State: jobs.StateCompleted,
|
||||
Payload: payload, IdempotencyKey: "library.capture:cap-1"},
|
||||
sealedVersion: &governance.LibraryVersion{ID: "libver-1", Scope: "internal",
|
||||
Kind: governance.LibraryVersionTestSealed, SnapshotID: snapshotID, SourceJobID: job.ID,
|
||||
Evidence: map[string]any{"source_id": sourceID, "capture_job": "cap-1"}},
|
||||
}
|
||||
config.Store = store
|
||||
return store, config, job, payload
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ReconcileLibraryCaptureTerminal(_ context.Context, jobID, scope, queueJobID, snapshotID string, _ time.Time) (bool, error) {
|
||||
if f.reconcileDenied {
|
||||
return false, nil
|
||||
}
|
||||
for index := range f.jobs {
|
||||
if f.jobs[index].ID != jobID || f.jobs[index].Scope != scope || f.jobs[index].Kind != governance.LibraryJobSeal ||
|
||||
f.jobs[index].QueueJobID != queueJobID || (f.jobs[index].State != governance.LibraryJobPending && f.jobs[index].State != governance.LibraryJobRunning) ||
|
||||
(f.jobs[index].SnapshotID != "" && f.jobs[index].SnapshotID != snapshotID) {
|
||||
return false, nil
|
||||
}
|
||||
f.jobs[index].State = governance.LibraryJobSucceeded
|
||||
f.jobs[index].SnapshotID = snapshotID
|
||||
f.lastState = governance.LibraryJobSucceeded
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) ReconcileMisclassifiedLibraryCapture(_ context.Context, jobID, scope, queueJobID, snapshotID, expectedStage, expectedError, expectedRecovery string, expectedFinished *time.Time, _ string, _ string, _ string, _ map[string]any, _ time.Time) (bool, error) {
|
||||
if f.reconcileDenied {
|
||||
return false, nil
|
||||
}
|
||||
for index := range f.jobs {
|
||||
j := &f.jobs[index]
|
||||
if j.ID != jobID || j.Scope != scope || j.Kind != governance.LibraryJobSeal || j.QueueJobID != queueJobID ||
|
||||
j.State != governance.LibraryJobFailed || j.Stage != expectedStage || j.Error != expectedError ||
|
||||
j.Recovery != expectedRecovery || j.SnapshotID != "" || (j.FinishedAt == nil) != (expectedFinished == nil) ||
|
||||
(j.FinishedAt != nil && expectedFinished != nil && !j.FinishedAt.Equal(*expectedFinished)) {
|
||||
return false, nil
|
||||
}
|
||||
j.State = governance.LibraryJobSucceeded
|
||||
j.SnapshotID = snapshotID
|
||||
j.Stage = "export"
|
||||
f.lastState = governance.LibraryJobSucceeded
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var _ libraryCaptureReconciler = (*fakeLibraryStore)(nil)
|
||||
|
||||
func runReconcile(t *testing.T, config Config, jobID string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/"+jobID+"/reconcile-capture", nil)
|
||||
req.SetPathValue("id", jobID)
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRepairsExactCompletedCapture(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if body["repaired"] != true || body["snapshot_id"] == "" || body["audit_operation_id"] == "" {
|
||||
t.Fatalf("body=%v", body)
|
||||
}
|
||||
if store.lastState != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("advanced state=%s", store.lastState)
|
||||
}
|
||||
if len(store.operations) != 1 {
|
||||
t.Fatalf("audit operations=%v", store.operations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileAlreadySucceededIsReadOnly(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = governance.LibraryJobSucceeded
|
||||
job.SnapshotID = store.sealedVersion.SnapshotID
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
||||
if body["already_succeeded"] != true || body["repaired"] != false || store.lastState != "" {
|
||||
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsQueueNotCompleted(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue.State = jobs.StateRunning
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsStageNotSucceeded(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.stageState = "failed"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongVersionSource(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Evidence["source_id"] = "other-source"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongCaptureJob(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Evidence["capture_job"] = "other-capture"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsBadArchive(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.SnapshotID = "missing-snapshot"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsFailedJob(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = governance.LibraryJobFailed
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileIsIdempotent(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
first := runReconcile(t, config, job.ID)
|
||||
if first.Code != http.StatusOK {
|
||||
t.Fatalf("first code=%d body=%s", first.Code, first.Body.String())
|
||||
}
|
||||
second := runReconcile(t, config, job.ID)
|
||||
if second.Code != http.StatusOK {
|
||||
t.Fatalf("second code=%d body=%s", second.Code, second.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(second.Body.Bytes(), &body)
|
||||
if body["already_succeeded"] != true || len(store.operations) != 1 {
|
||||
t.Fatalf("body=%v ops=%v", body, store.operations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsMissingQueue(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue = nil
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsQueueScope(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.queue.Payload, _ = json.Marshal(map[string]string{"scope": "external", "capture_job": "cap-1"})
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsMissingVersion(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion = nil
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsWrongScopeVersion(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.sealedVersion.Scope = "external"
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsDifferentExistingSnapshot(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.SnapshotID = "other-snapshot"
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileConditionalWriteRejectsConcurrentState(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
store.reconcileDenied = true
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRepairsExactLegacyMisclassification(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = legacyOrphanError
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var body map[string]any
|
||||
_ = json.Unmarshal(rec.Body.Bytes(), &body)
|
||||
if body["misclassification_repaired"] != true || store.lastState != governance.LibraryJobSucceeded {
|
||||
t.Fatalf("body=%v advanced=%q", body, store.lastState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsOtherFailedError(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = "capture export failed"
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q body=%s", rec.Code, store.lastState, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileRejectsRealFailureAndCancel(t *testing.T) {
|
||||
for _, state := range []governance.LibraryJobState{governance.LibraryJobFailed, governance.LibraryJobCancelled} {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
job.State = state
|
||||
job.Error = "real failure"
|
||||
store.jobs[0] = job
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("state=%s code=%d advanced=%q", state, rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryCaptureReconcileMisclassificationCASRejectsChange(t *testing.T) {
|
||||
store, config, job, _ := reconcileFixture(t)
|
||||
finished := time.Now().UTC()
|
||||
job.State = governance.LibraryJobFailed
|
||||
job.Stage = ""
|
||||
job.Error = legacyOrphanError
|
||||
job.Recovery = legacyOrphanRecovery
|
||||
job.FinishedAt = &finished
|
||||
store.jobs[0] = job
|
||||
store.reconcileDenied = true
|
||||
rec := runReconcile(t, config, job.ID)
|
||||
if rec.Code != http.StatusConflict || store.lastState != "" {
|
||||
t.Fatalf("code=%d advanced=%q", rec.Code, store.lastState)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
// Vertical PostgreSQL path: the real worker startup reconciliation must leave
|
||||
// a pending seal with a completed capture queue intact, then the actual HTTP
|
||||
// handler must verify archive/version/stage and repair it. This is not a fake
|
||||
// store or a direct helper test.
|
||||
func verticalReconcileFixture(t *testing.T) (*postgresstore.Store, Config, string, *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") || os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
|
||||
t.Fatal("isolated database required")
|
||||
}
|
||||
cfg.MaxConns = 1
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = "pg_temp"
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
if _, err := pool.Exec(ctx, `
|
||||
CREATE TEMP TABLE library_jobs(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(16) NOT NULL,
|
||||
state varchar(16) NOT NULL, stage varchar(32) NOT NULL DEFAULT '',
|
||||
idempotency_key varchar(255) NOT NULL DEFAULT '', request_digest varchar(64) NOT NULL DEFAULT '',
|
||||
queue_job_id varchar(64) NOT NULL DEFAULT '', snapshot_id varchar(64) NOT NULL DEFAULT '',
|
||||
target_snapshot_id varchar(64) NOT NULL DEFAULT '', actor varchar(128) NOT NULL DEFAULT '',
|
||||
error text NOT NULL DEFAULT '', recovery varchar(255) NOT NULL DEFAULT '',
|
||||
operation_id varchar(64) NOT NULL DEFAULT '', attempt_epoch integer NOT NULL DEFAULT 0,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, finished_at timestamptz,
|
||||
target_id text NOT NULL DEFAULT '');
|
||||
CREATE TEMP TABLE library_job_stages(
|
||||
job_id varchar(64) NOT NULL, stage varchar(32) NOT NULL, state varchar(16) NOT NULL,
|
||||
total bigint, completed bigint, bytes_total bigint, bytes_done bigint,
|
||||
detail text NOT NULL DEFAULT '', updated_at timestamptz NOT NULL, PRIMARY KEY(job_id,stage));
|
||||
CREATE TEMP TABLE library_versions(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(24) NOT NULL,
|
||||
snapshot_id varchar(64) NOT NULL DEFAULT '', version_label varchar(64) NOT NULL DEFAULT '',
|
||||
source_job_id varchar(64) NOT NULL DEFAULT '', operation_id varchar(64) NOT NULL DEFAULT '',
|
||||
attempt_epoch integer NOT NULL DEFAULT 0, evidence jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_by varchar(128) NOT NULL DEFAULT '', created_at timestamptz NOT NULL, superseded_at timestamptz);
|
||||
CREATE TEMP TABLE library_job_repairs(
|
||||
id varchar(64) PRIMARY KEY, job_id varchar(64) NOT NULL, scope varchar(16) NOT NULL,
|
||||
kind varchar(16) NOT NULL, queue_job_id varchar(64) NOT NULL, snapshot_id varchar(64) NOT NULL,
|
||||
original_state varchar(16) NOT NULL, original_stage varchar(32) NOT NULL DEFAULT '',
|
||||
original_error text NOT NULL DEFAULT '', original_recovery varchar(255) NOT NULL DEFAULT '',
|
||||
original_finished_at timestamptz, reason varchar(255) NOT NULL, actor varchar(128) NOT NULL DEFAULT '',
|
||||
evidence jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL);
|
||||
CREATE TEMP TABLE library_operations(
|
||||
id varchar(64) PRIMARY KEY, scope varchar(16) NOT NULL, kind varchar(16) NOT NULL,
|
||||
target_snapshot_id varchar(64) NOT NULL DEFAULT '', request_digest varchar(64) NOT NULL,
|
||||
operation_key varchar(255) NOT NULL, actor varchar(128) NOT NULL DEFAULT '',
|
||||
created_at timestamptz NOT NULL, target_id text NOT NULL DEFAULT '',
|
||||
first_backup_snapshot_id varchar(64), first_backup_bytes bigint, first_backup_at timestamptz,
|
||||
writeset_digest varchar(64), writeset_rows bigint, target_fingerprint varchar(128), anchors_at timestamptz);
|
||||
CREATE UNIQUE INDEX library_operations_key_test ON library_operations(scope,kind,operation_key);
|
||||
CREATE TEMP TABLE jobs(
|
||||
id varchar(64) PRIMARY KEY, type varchar(64) NOT NULL, payload jsonb NOT NULL,
|
||||
state varchar(16) NOT NULL, priority integer NOT NULL DEFAULT 0, available_at timestamptz NOT NULL,
|
||||
lease_owner varchar(128), lease_until timestamptz, attempt integer NOT NULL DEFAULT 0,
|
||||
max_attempts integer NOT NULL DEFAULT 1, last_error text, idempotency_key varchar(255) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL);`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := libraryFixtureRoot(t)
|
||||
snapshotID := sealSharedVersions(t, root, 1, 4)[0]
|
||||
archive, err := librarysnapshot.Open(root, 256*1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest, err := archive.Load(snapshotID)
|
||||
archive.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config := Config{LibraryArchiveRoot: root,
|
||||
LibrarySourceProject: "fixture-project", LibrarySourceApp: "fixture-app",
|
||||
LibrarySourceTenantID: manifest.TenantID, LibrarySourceInternalKB: manifest.KnowledgeBaseID}
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "vertical"})
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO jobs(id,type,payload,state,available_at,idempotency_key,created_at,updated_at)
|
||||
VALUES('q-vertical','library.capture',$1,'completed',$2,'library.capture:vertical',$2,$2)`, payload, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_versions(id,scope,kind,snapshot_id,source_job_id,evidence,created_at)
|
||||
VALUES('ver-vertical','internal','test_sealed',$1,'libseal-vertical',$2,$3)`, snapshotID,
|
||||
map[string]any{"source_id": sourceID, "capture_job": "vertical"}, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_job_stages(job_id,stage,state,detail,updated_at)
|
||||
VALUES('libseal-vertical','export','succeeded','已封存快照', $1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := postgresstore.NewStore(pool)
|
||||
config.Store = store
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_jobs(id,scope,kind,state,idempotency_key,request_digest,queue_job_id,created_at,updated_at)
|
||||
VALUES('libseal-vertical','internal','seal','pending','k','d','q-vertical',$1,$1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store, config, snapshotID, pool
|
||||
}
|
||||
|
||||
func TestStartupReconcileThenHandlerRepairsPendingCapture(t *testing.T) {
|
||||
store, config, _, _ := verticalReconcileFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
if n, err := store.ReconcileLibraryJobs(ctx, now, []string{"seal"}); err != nil || n != 0 {
|
||||
t.Fatalf("startup reconcile n=%d err=%v", n, err)
|
||||
}
|
||||
job, err := store.LibraryJob(ctx, "libseal-vertical")
|
||||
if err != nil || job.State != governance.LibraryJobPending {
|
||||
t.Fatalf("startup changed job: %+v err=%v", job, err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/libseal-vertical/reconcile-capture", nil)
|
||||
req.SetPathValue("id", "libseal-vertical")
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("handler code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
job, err = store.LibraryJob(ctx, "libseal-vertical")
|
||||
if err != nil || job.State != governance.LibraryJobSucceeded || job.SnapshotID == "" {
|
||||
t.Fatalf("handler did not repair: %+v err=%v", job, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartupReconcilePreservesFailedCapturesForExactReconcile(t *testing.T) {
|
||||
store, config, snapshotID, pool := verticalReconcileFixture(t)
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
source := configuredLibrarySource(config, "internal")
|
||||
sourceID, err := source.ID()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]string{"scope": "internal", "capture_job": "vertical-failed"})
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO jobs(id,type,payload,state,available_at,idempotency_key,created_at,updated_at)
|
||||
VALUES('q-failed','library.capture',$1,'completed',$2,'library.capture:vertical-failed',$2,$2)`, payload, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_versions(id,scope,kind,snapshot_id,source_job_id,evidence,created_at)
|
||||
VALUES('ver-failed','internal','test_sealed',$1,'libseal-vertical-failed',$2,$3)`,
|
||||
snapshotID, map[string]any{"source_id": sourceID, "capture_job": "vertical-failed"}, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_job_stages(job_id,stage,state,detail,updated_at)
|
||||
VALUES('libseal-vertical-failed','export','succeeded','已封存快照', $1)`, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
finished := now.Truncate(time.Microsecond)
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO library_jobs(id,scope,kind,state,idempotency_key,request_digest,queue_job_id,error,recovery,created_at,updated_at,finished_at)
|
||||
VALUES('libseal-vertical-failed','internal','seal','failed','kf','df','q-failed',$1,$2,$3,$3,$3)`,
|
||||
"队列中已无对应作业,业务作业未收到结果", "封存作业由 agent 恢复来源;确认来源 healthy 后可重发", finished); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, err := store.ReconcileLibraryJobs(ctx, now, []string{"seal"}); err != nil || n != 0 {
|
||||
t.Fatalf("startup reconcile n=%d err=%v", n, err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/library/jobs/libseal-vertical-failed/reconcile-capture", nil)
|
||||
req.SetPathValue("id", "libseal-vertical-failed")
|
||||
rec := httptest.NewRecorder()
|
||||
libraryCaptureReconcile(rec, req, config)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("handler code=%d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
job, err := store.LibraryJob(ctx, "libseal-vertical-failed")
|
||||
if err != nil || job.State != governance.LibraryJobSucceeded || job.SnapshotID != snapshotID {
|
||||
t.Fatalf("failed misclassification not repaired: %+v err=%v", job, err)
|
||||
}
|
||||
var originalState, originalError string
|
||||
if err := pool.QueryRow(ctx, `SELECT original_state, original_error FROM library_job_repairs WHERE job_id='libseal-vertical-failed'`).
|
||||
Scan(&originalState, &originalError); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if originalState != "failed" || originalError == "" {
|
||||
t.Fatalf("audit missing original: state=%q error=%q", originalState, originalError)
|
||||
}
|
||||
}
|
||||
@@ -342,7 +342,11 @@ type libraryOverview struct {
|
||||
CaptureAvailable bool `json:"capture_available"`
|
||||
CaptureReason string `json:"capture_reason,omitempty"`
|
||||
Executor libraryExecutorView `json:"executor"`
|
||||
Now time.Time `json:"now"`
|
||||
// 实际目标身份:页面不能只显示"weknora-test"或旧文档级绑定名称。
|
||||
TargetInstance string `json:"target_instance,omitempty"`
|
||||
TargetProject string `json:"target_project,omitempty"`
|
||||
TargetKnowledgeBaseID string `json:"target_knowledge_base_id,omitempty"`
|
||||
Now time.Time `json:"now"`
|
||||
}
|
||||
|
||||
// libraryVersionRef 只回答"这个快照被登记成哪一类版本、由哪个作业登记"。
|
||||
@@ -595,6 +599,11 @@ func testSealedOf(versions []governance.LibraryVersion, byID map[string]libraryS
|
||||
// 它刻意只说配置:能停写入者/关入口/证明排空的真实能力在 worker 侧(docker socket、
|
||||
// 闸门、排空文件),API 若声称"能力可用"就是拿配置字符串冒充执行证据。
|
||||
func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
mode := strings.TrimSpace(config.LibraryTargetIngressMode)
|
||||
if mode == "" {
|
||||
mode = "gate"
|
||||
}
|
||||
noGate := mode == "none"
|
||||
missing := make([]string, 0, 6)
|
||||
if config.LibraryTargetInstance == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INSTANCE")
|
||||
@@ -605,11 +614,18 @@ func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
if len(config.LibraryTargetWriters) == 0 {
|
||||
missing = append(missing, "LIBRARY_TARGET_WRITERS")
|
||||
}
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
if noGate {
|
||||
// 没有闸门:页面必须如实说"无闸门(测试模式)",不能显示"打开"。
|
||||
if config.LibraryTargetHealthURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_HEALTH_URL")
|
||||
}
|
||||
} else {
|
||||
if config.LibraryTargetIngressURL == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_URL")
|
||||
}
|
||||
if config.LibraryTargetIngressService == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_INGRESS_SERVICE")
|
||||
}
|
||||
}
|
||||
if config.LibraryTargetJournalRoot == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_JOURNAL_ROOT")
|
||||
@@ -617,12 +633,18 @@ func libraryMaintenanceCapability(config Config) map[string]any {
|
||||
if config.LibraryTargetDockerSocket == "" {
|
||||
missing = append(missing, "LIBRARY_TARGET_DOCKER_SOCKET")
|
||||
}
|
||||
ingressState := "闸门"
|
||||
if noGate {
|
||||
ingressState = "无闸门(测试模式)"
|
||||
}
|
||||
return map[string]any{
|
||||
"configured": len(missing) == 0,
|
||||
"missing": missing,
|
||||
"instance": config.LibraryTargetInstance,
|
||||
"project": config.LibraryTargetProject,
|
||||
"writers": config.LibraryTargetWriters,
|
||||
"ingress_mode": mode,
|
||||
"ingress_state": ingressState,
|
||||
"ingress_service": config.LibraryTargetIngressService,
|
||||
"note": "配置齐全只说明治理侧接线完整;停写入者、关入口与排空由 worker 在作业内校验并执行",
|
||||
}
|
||||
@@ -642,9 +664,7 @@ func librarySyncGateWithGrant(config Config, production libraryKnownView, prebac
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
gate.Reasons = append(gate.Reasons, "生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false)")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
gate.Reasons = append(gate.Reasons, "没有已核验的生产恢复演练证据(PRODUCTION_BACKUP_RESTORE_VERIFIED=false)")
|
||||
}
|
||||
gate.Reasons = append(gate.Reasons, restoreGateReasons(config)...)
|
||||
// 生产端点以"整库目标是否配置"为准:整库同步直接写 PG/ES/对象,
|
||||
// 不依赖 WeKnora 的 HTTP 端,所以不能拿它是否配置当门禁。
|
||||
if strings.TrimSpace(config.LibraryProductionDatabaseURL) == "" {
|
||||
@@ -688,10 +708,16 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
overview := libraryOverview{Scope: scope, Jobs: []governance.LibraryJob{}, Now: now,
|
||||
TargetInstance: config.LibraryTargetInstance, TargetProject: config.LibraryTargetProject,
|
||||
Gate: libraryGateView{Reasons: []string{}},
|
||||
Diff: libraryDiffView{State: "unknown", Reason: "生产与测试的文档级差异读取未接线", Added: []any{}, Updated: []any{}, Removed: []any{}}}
|
||||
// 可发起封存 = worker 真实探测到执行器可用且指向本实例;原因逐条给全。
|
||||
overview.CaptureAvailable, overview.CaptureReason, overview.Executor = libraryExecutorAvailability(r.Context(), config)
|
||||
if scope == "internal" {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionInternalKB
|
||||
} else {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionExternalKB
|
||||
}
|
||||
|
||||
// 测试封存来自归档 + 版本登记:归档里**不止**测试封存——生产预备份与目标基线
|
||||
// 快照同样带 scope,如果只按"这个 scope 最新一份"挑,页面就会把目标自己的内容
|
||||
@@ -814,7 +840,8 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
|
||||
grant, hasGrant := activeGrantFor(r.Context(), config, scope, target)
|
||||
overview.Authorization = authorizationViewOf(grant, hasGrant)
|
||||
overview.Gate = librarySyncGateWithGrant(config, overview.Production, overview.Prebackup, target, hasGrant)
|
||||
// 闸口按当前目标的真实恢复证据判断(没有证据时也接受历史 env 覆盖)。
|
||||
overview.Gate = librarySyncGateWithGrant(config.withRestoreEvidence(r.Context(), scope), overview.Production, overview.Prebackup, target, hasGrant)
|
||||
writeJSON(w, http.StatusOK, overview)
|
||||
}
|
||||
|
||||
@@ -963,7 +990,7 @@ func librarySyncEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
return
|
||||
}
|
||||
}
|
||||
gate := librarySyncGateWithGrant(config, overview.Production, overview.Prebackup, request.Target, hasGrant)
|
||||
gate := librarySyncGateWithGrant(config.withRestoreEvidence(r.Context(), request.Scope), overview.Production, overview.Prebackup, request.Target, hasGrant)
|
||||
if !gate.SyncAllowed {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
|
||||
"code": "library_sync_blocked", "reasons": gate.Reasons,
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/jobs"
|
||||
)
|
||||
|
||||
// 生产闸口与整库状态契约的针对性测试:不做数据库集成,只验证
|
||||
@@ -37,6 +38,13 @@ type fakeLibraryStore struct {
|
||||
resumeOK bool
|
||||
resumeErr error
|
||||
resumeCalls []string
|
||||
// capture reconciliation fixtures
|
||||
queue *jobs.Job
|
||||
stageState string
|
||||
stageErr error
|
||||
sealedVersion *governance.LibraryVersion
|
||||
versionErr error
|
||||
reconcileDenied bool
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) CreateLibraryJob(_ context.Context, job governance.LibraryJob, _ []string, _ time.Time) (governance.LibraryJob, bool, error) {
|
||||
@@ -127,8 +135,16 @@ func (f *fakeLibraryStore) UpdateLibraryStage(_ context.Context, _, _, _ string,
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, _ string, state governance.LibraryJobState, _, _, _, _, _ string, _ time.Time) error {
|
||||
func (f *fakeLibraryStore) AdvanceLibraryJob(_ context.Context, jobID string, state governance.LibraryJobState, _, _, _, snapshotID, _ string, _ time.Time) error {
|
||||
f.lastState = state
|
||||
for index := range f.jobs {
|
||||
if f.jobs[index].ID == jobID {
|
||||
f.jobs[index].State = state
|
||||
if snapshotID != "" {
|
||||
f.jobs[index].SnapshotID = snapshotID
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -266,3 +266,25 @@ func TestLibraryReadPathsRejectTamperedManifest(t *testing.T) {
|
||||
t.Fatalf("storage must fail closed: status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// F11: the HTTP projection must include binding and Wiki log counts; the
|
||||
// previous projection silently omitted them and the UI turned absence into 0.
|
||||
func TestLibrarySnapshotTotalsIncludeBindingsAndWikiLogs(t *testing.T) {
|
||||
view := librarySnapshotViewOfNoUsage("snap-totals", librarysnapshot.Manifest{
|
||||
Scope: "internal", KnowledgeBaseID: "kb", TenantID: 10000,
|
||||
Parts: []librarysnapshot.Part{
|
||||
{Kind: "database", Name: "knowledges", Count: 4},
|
||||
{Kind: "database", Name: "chunks", Count: 38},
|
||||
{Kind: "database", Name: "resources", Count: 21},
|
||||
{Kind: "database", Name: "resource_bindings", Count: 4},
|
||||
{Kind: "wiki", Name: "wiki_pages", Count: 31},
|
||||
{Kind: "wiki", Name: "wiki_folders", Count: 21},
|
||||
{Kind: "wiki", Name: "wiki_log_entries", Count: 11},
|
||||
},
|
||||
})
|
||||
for key, want := range map[string]int64{"documents": 4, "chunks": 38, "resources": 21, "bindings": 4, "wiki_pages": 31, "wiki_folders": 21, "wiki_log_entries": 11} {
|
||||
if view.Totals[key] != want {
|
||||
t.Fatalf("totals[%s]=%d want=%d (%v)", key, view.Totals[key], want, view.Totals)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,9 +71,7 @@ func libraryRestoreEnqueue(w http.ResponseWriter, r *http.Request, config Config
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
reasons = append(reasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
reasons = append(reasons, "没有已核验的生产恢复演练证据")
|
||||
}
|
||||
reasons = append(reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), request.Scope))...)
|
||||
authorizationOK := false
|
||||
if storeWithAuth, ok := config.Store.(interface {
|
||||
ActiveLibraryAuthorization(ctx context.Context, scope, snapshotID, digest string, now time.Time) (governance.LibraryReleaseAuthorization, bool, error)
|
||||
@@ -169,9 +167,7 @@ func libraryRestoreAvailability(w http.ResponseWriter, r *http.Request, config C
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
view.Reasons = append(view.Reasons, "生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false)")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
view.Reasons = append(view.Reasons, "没有已核验的生产恢复演练证据(PRODUCTION_BACKUP_RESTORE_VERIFIED=false)")
|
||||
}
|
||||
view.Reasons = append(view.Reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), scope))...)
|
||||
digest, err := libraryRestoreParamsDigest(scope, snapshotID)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot digest params"})
|
||||
@@ -228,9 +224,7 @@ func libraryJobResume(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
reasons = append(reasons, "生产写入闸口关闭")
|
||||
}
|
||||
if !config.ProductionBackupRestoreVerified {
|
||||
reasons = append(reasons, "没有已核验的生产恢复演练证据")
|
||||
}
|
||||
reasons = append(reasons, restoreGateReasons(config.withRestoreEvidence(r.Context(), source.Scope))...)
|
||||
var digest string
|
||||
switch source.Kind {
|
||||
case governance.LibraryJobSync:
|
||||
|
||||
@@ -261,10 +261,14 @@ func librarySnapshotViewOfNoUsage(id string, manifest librarysnapshot.Manifest)
|
||||
}
|
||||
case "resources":
|
||||
snapshot.Totals["resources"] = part.Count
|
||||
case "resource_bindings":
|
||||
snapshot.Totals["bindings"] = part.Count
|
||||
case "wiki_pages":
|
||||
snapshot.Totals["wiki_pages"] = part.Count
|
||||
case "wiki_folders":
|
||||
snapshot.Totals["wiki_folders"] = part.Count
|
||||
case "wiki_log_entries":
|
||||
snapshot.Totals["wiki_log_entries"] = part.Count
|
||||
}
|
||||
}
|
||||
sort.Slice(snapshot.Parts, func(i, j int) bool {
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 恢复验证:页面点一次"跑恢复验证",后端建一个持久作业,worker 用与 CLI 同一份实现
|
||||
// (internal/libraryverify)真实执行:捕获目标 → 基线校验 → 写 canary 证明校验有效 →
|
||||
// 按预备份恢复 → 恢复后复核。通过与否只由这条证据表回答,页面不接受手填开关。
|
||||
const libraryVerifyJobType = "library.verify"
|
||||
|
||||
type libraryVerifyRequest struct {
|
||||
Scope string `json:"scope"`
|
||||
}
|
||||
|
||||
// libraryVerificationView 是页面需要的全部事实:当前目标是谁、最近一次验证结果、
|
||||
// 证据是否对当前目标成立、以及同步闸口能不能据此放行。
|
||||
type libraryVerificationView struct {
|
||||
Scope string `json:"scope"`
|
||||
Target libraryVerificationTarget `json:"target"`
|
||||
Verified bool `json:"verified"`
|
||||
Reason string `json:"reason"`
|
||||
Latest *governance.LibraryRestoreVerification `json:"latest,omitempty"`
|
||||
// SnapshotLatest 只在请求显式带 snapshot_id 时返回:快照报告不能拿 scope
|
||||
// 最近一条代替。
|
||||
SnapshotLatest *governance.LibraryRestoreVerification `json:"snapshot_latest,omitempty"`
|
||||
History []governance.LibraryRestoreVerification `json:"history"`
|
||||
Running bool `json:"running"`
|
||||
JobID string `json:"job_id,omitempty"`
|
||||
State string `json:"state,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
}
|
||||
|
||||
type libraryVerificationTarget struct {
|
||||
Instance string `json:"instance"`
|
||||
Project string `json:"project"`
|
||||
InternalKB string `json:"internal_kb"`
|
||||
ExternalKB string `json:"external_kb"`
|
||||
Index string `json:"index"`
|
||||
S3Bucket string `json:"s3_bucket"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
DatabaseSet bool `json:"database_configured"`
|
||||
}
|
||||
|
||||
// libraryVerifyEnqueue 投递一次恢复验证作业(管理员)。
|
||||
//
|
||||
// 它不签发发布授权、不改版本:验证只回答"当前目标的预备份能不能恢复"。
|
||||
// 目标是部署配置,不接受请求体指定——否则一次点击就能验证别的库并把证据挂到当前目标上。
|
||||
func libraryVerifyEnqueue(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
identity, ok := sessionFrom(r.Context())
|
||||
if !ok || roleRank(identity.Role) < roleRank(roleAdmin) {
|
||||
writeJSON(w, http.StatusForbidden, map[string]any{"error": "恢复验证仅管理员可发起"})
|
||||
return
|
||||
}
|
||||
var request libraryVerifyRequest
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&request); err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
if request.Scope != "internal" && request.Scope != "external" {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "scope must be internal or external"})
|
||||
return
|
||||
}
|
||||
store, ok := libraryStore(config)
|
||||
if !ok {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"code": "library_verify_unavailable",
|
||||
"error": "恢复验证存储不可用"})
|
||||
return
|
||||
}
|
||||
// 验证要停写入者并在目标上写 canary:维护中或闸口关闭时直接拒绝,
|
||||
// 不让页面投一个 worker 必然失败的作业。
|
||||
if body, held := instanceMaintenanceGuard(r.Context(), config, request.Scope); held {
|
||||
writeJSON(w, http.StatusConflict, body)
|
||||
return
|
||||
}
|
||||
if !config.ProductionReleaseRequestsEnabled {
|
||||
writeJSON(w, http.StatusServiceUnavailable, map[string]any{
|
||||
"code": "library_verify_blocked",
|
||||
"reasons": []string{
|
||||
"生产写入闸口关闭(PRODUCTION_RELEASE_REQUESTS_ENABLED=false):恢复验证要停写入者并在目标上写 canary",
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
digest, err := governance.LibraryRequestDigest(request.Scope, governance.LibraryJobVerify,
|
||||
map[string]any{"scope": request.Scope})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot digest request"})
|
||||
return
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
// 验证作业必须有和同步/恢复同一套不可变 operation:worker 的 owned attempt 从
|
||||
// job.operation_id 取身份,恢复派发闸口和对象网关都按它核验。没有 operation 的
|
||||
// verify 作业在真实恢复时会被 fail closed(审核 B2-R1)。
|
||||
intent, intentErr := ensureLibraryIntent(r.Context(), store, request.Scope, governance.LibraryJobVerify,
|
||||
"", digest, identity.Username, "", now)
|
||||
if intentErr != nil {
|
||||
if isIntentConflict(intentErr) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_verify_conflict",
|
||||
"error": "同一幂等键对应了不同的请求"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot create library verify operation"})
|
||||
return
|
||||
}
|
||||
job, reused, err := store.CreateLibraryDispatch(r.Context(), governance.LibraryJob{
|
||||
ID: fmt.Sprintf("libverify-%s-%d", request.Scope, now.UnixNano()), Scope: request.Scope,
|
||||
Kind: governance.LibraryJobVerify,
|
||||
IdempotencyKey: intent.JobKey,
|
||||
RequestDigest: digest, OperationID: intent.Operation.ID, Actor: identity.Username,
|
||||
}, governance.VerifyStages(), false, now)
|
||||
if errors.Is(err, governance.ErrLibraryJobActive) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_write_active",
|
||||
"error": "该目标已有在飞的写作业(同步/恢复/基线/验证互斥),等它结束再发起"})
|
||||
return
|
||||
}
|
||||
if errors.Is(err, governance.ErrLibraryJobIdempotencyConflict) {
|
||||
writeJSON(w, http.StatusConflict, map[string]any{"code": "library_verify_conflict",
|
||||
"error": "同一幂等键对应了不同的请求"})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "cannot create library verify job"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusAccepted, map[string]any{"job": job, "reused": reused})
|
||||
}
|
||||
|
||||
// libraryVerifications 读当前目标的恢复验证证据(页面轮询用)。
|
||||
func libraryVerifications(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
scope, err := scopeFromQuery(r)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
view := libraryVerificationView{
|
||||
Scope: scope,
|
||||
History: []governance.LibraryRestoreVerification{},
|
||||
Target: libraryVerificationTarget{
|
||||
Instance: config.LibraryTargetInstance,
|
||||
Project: config.LibraryTargetProject,
|
||||
InternalKB: config.LibraryProductionInternalKB,
|
||||
ExternalKB: config.LibraryProductionExternalKB,
|
||||
Index: config.LibraryProductionIndex,
|
||||
S3Bucket: config.LibraryProductionS3Bucket,
|
||||
TenantID: config.LibraryProductionTenantID,
|
||||
DatabaseSet: config.LibraryProductionDatabaseURL != "",
|
||||
},
|
||||
}
|
||||
store, ok := config.Store.(interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
LibraryRestoreVerifications(context.Context, string, int) ([]governance.LibraryRestoreVerification, error)
|
||||
})
|
||||
if !ok {
|
||||
view.Reason = "当前部署没有恢复验证存储:无法读取证据"
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
latest, found, err := store.LatestLibraryRestoreVerification(r.Context(), scope)
|
||||
if err != nil {
|
||||
view.Reason = "读取恢复验证证据失败:" + err.Error()
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
if found {
|
||||
view.Latest = &latest
|
||||
view.JobID = latest.JobID
|
||||
}
|
||||
if history, err := store.LibraryRestoreVerifications(r.Context(), scope, 10); err == nil {
|
||||
view.History = history
|
||||
}
|
||||
if snapshotID := strings.TrimSpace(r.URL.Query().Get("snapshot_id")); snapshotID != "" {
|
||||
if exact, ok := config.Store.(interface {
|
||||
LibraryRestoreVerificationForSnapshot(context.Context, string, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
}); ok {
|
||||
item, found, exactErr := exact.LibraryRestoreVerificationForSnapshot(r.Context(), scope, snapshotID)
|
||||
if exactErr != nil {
|
||||
view.Reason = "读取该快照的恢复验证记录失败"
|
||||
writeJSON(w, http.StatusServiceUnavailable, view)
|
||||
return
|
||||
}
|
||||
if found {
|
||||
view.SnapshotLatest = &item
|
||||
}
|
||||
}
|
||||
}
|
||||
view.Verified, view.Reason = governance.LibraryVerificationSatisfied(latest, scope)
|
||||
if !found {
|
||||
view.Verified = false
|
||||
view.Reason = "还没有对当前目标做过恢复验证:点「跑一次恢复验证」(真实备份 → canary → 恢复 → 复核)"
|
||||
}
|
||||
if snapshotID := strings.TrimSpace(r.URL.Query().Get("snapshot_id")); snapshotID != "" {
|
||||
// 显式按快照查询时,verified 只回答该快照;不能让 scope 最新通过记录
|
||||
// 把另一个未演练的快照染绿。
|
||||
view.Verified = false
|
||||
if view.SnapshotLatest == nil {
|
||||
view.Reason = "该快照没有真实恢复验证记录(未验证;不是读取失败)"
|
||||
} else if view.SnapshotLatest.Status == governance.LibraryVerificationPassed {
|
||||
view.Verified = true
|
||||
view.Reason = ""
|
||||
} else {
|
||||
view.Reason = "该快照最近一次恢复验证未通过"
|
||||
}
|
||||
}
|
||||
// 在飞作业必须从真实业务行读出来:旧实现只构造 running 字段却从不赋值,
|
||||
// 页面因此在验收里看不到"已接受/正在执行",按钮也不会进入执行态(审核 B4)。
|
||||
if jobs, ok := config.Store.(interface {
|
||||
ListLibraryJobs(context.Context, string, int) ([]governance.LibraryJob, error)
|
||||
}); ok {
|
||||
if items, err := jobs.ListLibraryJobs(r.Context(), scope, 20); err == nil {
|
||||
var active *governance.LibraryJob
|
||||
for index := range items {
|
||||
item := items[index]
|
||||
if item.Kind != governance.LibraryJobVerify || string(item.State) == "" {
|
||||
continue
|
||||
}
|
||||
if !libraryJobActiveState(item.State) {
|
||||
continue
|
||||
}
|
||||
if active == nil || item.CreatedAt.After(active.CreatedAt) {
|
||||
active = &item
|
||||
}
|
||||
}
|
||||
if active != nil {
|
||||
view.Running = true
|
||||
view.JobID = active.ID
|
||||
view.State = string(active.State)
|
||||
view.Stage = active.Stage
|
||||
view.Reason = "恢复验证作业 " + active.ID + " 正在执行(" + active.Stage + ");终态会在这里显示证据,页面会自动刷新"
|
||||
}
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, view)
|
||||
}
|
||||
|
||||
// libraryJobActiveState 只认仍在执行的业务状态:终态不是"在飞"。
|
||||
func libraryJobActiveState(state governance.LibraryJobState) bool {
|
||||
switch state {
|
||||
case governance.LibraryJobPending, governance.LibraryJobRunning:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// withRestoreEvidence 把"当前目标是否有真实恢复证据"注入 Config 的副本,
|
||||
// 让同步/恢复闸口在同一个请求里按证据判断,而不是读一个可能被手填的 env。
|
||||
//
|
||||
// 证据优先级是不可交换的(审核 B3):明确 failed/invalid 或读取失败都必须拒绝,
|
||||
// 即使 env=true;历史 env 覆盖只在**明确没有证据行**(not-found)时兜底。
|
||||
// 把读取失败混进 not-found 等于把"不知道"说成"满足"。
|
||||
func (c Config) withRestoreEvidence(ctx context.Context, scope string) Config {
|
||||
satisfied, missing, err := c.restoreEvidence(ctx, scope)
|
||||
c.restoreEvidenceChecked = true
|
||||
switch {
|
||||
case err != nil:
|
||||
c.restoreVerified = false
|
||||
c.restoreEvidenceReason = err.Error()
|
||||
case satisfied:
|
||||
c.restoreVerified = true
|
||||
c.restoreEvidenceReason = ""
|
||||
case missing && c.ProductionBackupRestoreVerified:
|
||||
c.restoreVerified = true
|
||||
c.restoreEvidenceReason = ""
|
||||
default:
|
||||
c.restoreVerified = false
|
||||
if missing {
|
||||
c.restoreEvidenceReason = "当前目标还没有恢复演练证据:先在「生产保护与恢复」里跑一次恢复验证"
|
||||
} else {
|
||||
c.restoreEvidenceReason = "没有已核验的生产恢复演练证据"
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// restoreEvidence 读当前目标的最近一条证据并给出它能不能支撑"可恢复"。
|
||||
// missing 只在"查询成功且确实没有这一行"时为 true;读取错误单独返回。
|
||||
func (c Config) restoreEvidence(ctx context.Context, scope string) (satisfied bool, missing bool, err error) {
|
||||
store, ok := c.Store.(interface {
|
||||
LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error)
|
||||
})
|
||||
if !ok {
|
||||
// 存储没有证据能力(迁移前的部署形状)等同于"没有证据行":
|
||||
// 历史 env 覆盖仍可兜底,但一旦读得出证据行,失败/无效/读取错误都优先拒绝。
|
||||
return false, true, nil
|
||||
}
|
||||
latest, found, err := store.LatestLibraryRestoreVerification(ctx, scope)
|
||||
if err != nil {
|
||||
return false, false, fmt.Errorf("读取恢复验证证据失败:%w", err)
|
||||
}
|
||||
if !found {
|
||||
return false, true, nil
|
||||
}
|
||||
ok, reason := governance.LibraryVerificationSatisfied(latest, scope)
|
||||
if !ok {
|
||||
return false, false, errors.New(reason)
|
||||
}
|
||||
return true, false, nil
|
||||
}
|
||||
|
||||
// restoreGateReasons 是闸口对"恢复可验证"的统一判断:只看 withRestoreEvidence 的结论。
|
||||
// 这里不再 OR env:env 兜底只在证据缺失时、且已经在 withRestoreEvidence 里判定过。
|
||||
func restoreGateReasons(config Config) []string {
|
||||
if config.restoreVerified {
|
||||
return nil
|
||||
}
|
||||
// 没有走过证据读取的调用点(历史路径/单元夹具)保持原有 env 语义;
|
||||
// 一旦读过证据,上面的 withRestoreEvidence 已经把优先级判定完毕。
|
||||
if !config.restoreEvidenceChecked && config.ProductionBackupRestoreVerified {
|
||||
return nil
|
||||
}
|
||||
reason := config.restoreEvidenceReason
|
||||
if reason == "" {
|
||||
reason = "没有已核验的生产恢复演练证据"
|
||||
}
|
||||
return []string{reason}
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// 恢复验证的页面契约:
|
||||
// - 只有管理员能发起(它会停写入者并在目标上写 canary);
|
||||
// - 闸口关闭 / 维护中时直接拒绝,不投一个 worker 必然失败的作业;
|
||||
// - 证据只由真实执行写入:没有证据 ⇒ verified=false,过期/换目标 ⇒ 失效。
|
||||
//
|
||||
// 这里不模拟"真跑一次恢复"(那需要真实目标);执行体本身在 internal/libraryverify,
|
||||
// 由库内 CLI 与 worker 共用,属于隔离 fixture 之外的集成范围。
|
||||
|
||||
type verificationStore struct {
|
||||
fakeLibraryStore
|
||||
verifications []governance.LibraryRestoreVerification
|
||||
}
|
||||
|
||||
func (s *verificationStore) LatestLibraryRestoreVerification(_ context.Context, scope string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope {
|
||||
return item, true, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryRestoreVerification{}, false, nil
|
||||
}
|
||||
|
||||
func (s *verificationStore) LibraryRestoreVerificationForSnapshot(_ context.Context, scope, snapshotID string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope && item.SnapshotID == snapshotID {
|
||||
return item, true, nil
|
||||
}
|
||||
}
|
||||
return governance.LibraryRestoreVerification{}, false, nil
|
||||
}
|
||||
|
||||
func (s *verificationStore) LibraryRestoreVerifications(_ context.Context, scope string, limit int) ([]governance.LibraryRestoreVerification, error) {
|
||||
items := make([]governance.LibraryRestoreVerification, 0, len(s.verifications))
|
||||
for _, item := range s.verifications {
|
||||
if item.Scope == scope {
|
||||
items = append(items, item)
|
||||
}
|
||||
}
|
||||
if limit > 0 && len(items) > limit {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func verifyRequest(body, role, username string) *http.Request {
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/library/verifications", strings.NewReader(body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
ctx := context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: username, Role: role})
|
||||
return request.WithContext(ctx)
|
||||
}
|
||||
|
||||
func TestLibraryVerifyRequiresAdmin(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleEngineer, "eng"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: true})
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatal("non-admin must not create a verify job")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerifyRefusesWhenWriteGateClosed(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleAdmin, "admin"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: false})
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
body := decodeBody(t, recorder)
|
||||
if body["code"] != "library_verify_blocked" {
|
||||
t.Fatalf("body=%v", body)
|
||||
}
|
||||
if store.createN != 0 {
|
||||
t.Fatal("closed gate must not create a verify job")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerifyEnqueuesVerifyJobWithCanaryStages(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.enqueueEchoRequestedID = true
|
||||
recorder := httptest.NewRecorder()
|
||||
libraryVerifyEnqueue(recorder, verifyRequest(`{"scope":"internal"}`, roleAdmin, "admin"),
|
||||
Config{Store: store, ProductionReleaseRequestsEnabled: true})
|
||||
if recorder.Code != http.StatusAccepted {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.lastJob.Kind != governance.LibraryJobVerify || store.lastJob.Scope != "internal" {
|
||||
t.Fatalf("job=%#v", store.lastJob)
|
||||
}
|
||||
if !strings.HasPrefix(store.lastJob.ID, "libverify-internal-") {
|
||||
t.Fatalf("job id=%q", store.lastJob.ID)
|
||||
}
|
||||
// B2-R1:verify 作业必须带不可变 operation,worker 的 owned attempt / 恢复派发
|
||||
// 身份都从它取;没有 operation 的作业在真实恢复时会被 fail closed。
|
||||
if store.lastJob.OperationID == "" || store.lastJob.OperationID != store.lastOperation.ID {
|
||||
t.Fatalf("verify job must carry its operation: job=%#v op=%#v", store.lastJob, store.lastOperation)
|
||||
}
|
||||
if store.lastOperation.Kind != governance.LibraryJobVerify || store.lastOperation.Scope != "internal" {
|
||||
t.Fatalf("verify operation identity=%#v", store.lastOperation)
|
||||
}
|
||||
// 阶段必须包含 canary:没有"写 canary 证明校验有效"这一步,验证就只是自证。
|
||||
want := governance.VerifyStages()
|
||||
if len(want) != 5 || want[2] != "canary" || want[3] != "restore" || want[4] != "finalize" {
|
||||
t.Fatalf("verify stages=%v", want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsWithoutEvidenceIsNotVerified(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("verified must be false without evidence: %v", body)
|
||||
}
|
||||
if reason, _ := body["reason"].(string); !strings.Contains(reason, "还没有对当前目标做过恢复验证") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// F03: the selected snapshot must resolve its own evidence, never the scope's
|
||||
// newest passed record.
|
||||
func TestLibraryVerificationsExactSnapshotDoesNotBorrowLatest(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{
|
||||
{ID: "verify-new", Scope: "internal", Status: governance.LibraryVerificationPassed,
|
||||
SnapshotID: "snap-new", JobID: "job-new", FinishedAt: time.Now().UTC()},
|
||||
{ID: "verify-old", Scope: "internal", Status: governance.LibraryVerificationFailed,
|
||||
SnapshotID: "snap-old", JobID: "job-old", Failure: "old restore mismatch",
|
||||
FinishedAt: time.Now().UTC().Add(-time.Hour)},
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal&snapshot_id=snap-old", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
exact, ok := body["snapshot_latest"].(map[string]any)
|
||||
if !ok || exact["snapshot_id"] != "snap-old" || exact["status"] != "failed" {
|
||||
t.Fatalf("snapshot_latest=%v body=%v", body["snapshot_latest"], body)
|
||||
}
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("old failed snapshot must not inherit latest passed: %v", body["verified"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsWithPassedEvidenceIsVerified(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
ID: "libverify-internal-job1", Scope: "internal", Status: governance.LibraryVerificationPassed,
|
||||
SnapshotID: "snap-1", JobID: "job1", StartedAt: time.Now().UTC().Add(-time.Minute),
|
||||
FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != true {
|
||||
t.Fatalf("verified must be true with passed evidence: %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryVerificationsFailedEvidenceIsNotVerified(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
ID: "libverify-internal-job2", Scope: "internal", Status: governance.LibraryVerificationFailed,
|
||||
SnapshotID: "snap-2", JobID: "job2", Failure: "恢复后内容仍与预备份不一致",
|
||||
StartedAt: time.Now().UTC().Add(-time.Minute), FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["verified"] != false {
|
||||
t.Fatalf("failed evidence must not verify: %v", body)
|
||||
}
|
||||
if reason, _ := body["reason"].(string); !strings.Contains(reason, "恢复后内容仍与预备份不一致") {
|
||||
t.Fatalf("reason=%q", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// 同步闸口必须消费同一 target 的证据:有 passed 证据即放行,
|
||||
// 只有失败证据时仍然拒绝并给出失败原因(不是笼统"没有证据")。
|
||||
// 审核 B3 反例(GPT-6 overlay)转成通过断言:env=true 不能覆盖明确失败或读取失败。
|
||||
type evidenceReadErrorStore struct{ verificationStore }
|
||||
|
||||
func (*evidenceReadErrorStore) LatestLibraryRestoreVerification(context.Context, string) (governance.LibraryRestoreVerification, bool, error) {
|
||||
return governance.LibraryRestoreVerification{}, false, errors.New("evidence lookup failed")
|
||||
}
|
||||
|
||||
func TestRestoreGateFailedEvidenceBeatsEnv(t *testing.T) {
|
||||
store := &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, Failure: "restore mismatch",
|
||||
FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
config := Config{Store: store, ProductionBackupRestoreVerified: true}.withRestoreEvidence(context.Background(), "internal")
|
||||
reasons := restoreGateReasons(config)
|
||||
if len(reasons) == 0 || !strings.Contains(strings.Join(reasons, ";"), "restore mismatch") {
|
||||
t.Fatalf("failed evidence must beat env=true: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreGateUnreadableEvidenceBeatsEnv(t *testing.T) {
|
||||
config := Config{Store: &evidenceReadErrorStore{}, ProductionBackupRestoreVerified: true}.
|
||||
withRestoreEvidence(context.Background(), "internal")
|
||||
reasons := restoreGateReasons(config)
|
||||
if len(reasons) == 0 || !strings.Contains(strings.Join(reasons, ";"), "读取恢复验证证据失败") {
|
||||
t.Fatalf("unreadable evidence must beat env=true: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
// env 兜底只允许在"明确没有证据行"时生效。
|
||||
func TestRestoreGateEnvFallbackOnlyWhenEvidenceMissing(t *testing.T) {
|
||||
config := Config{Store: &verificationStore{}, ProductionBackupRestoreVerified: true}.
|
||||
withRestoreEvidence(context.Background(), "internal")
|
||||
if reasons := restoreGateReasons(config); len(reasons) != 0 {
|
||||
t.Fatalf("explicit not-found with legacy env must still allow: %v", reasons)
|
||||
}
|
||||
}
|
||||
|
||||
// 审核 B4:GET 必须从真实业务行投影在飞验证作业,而不是永远 running=false。
|
||||
func TestLibraryVerificationsProjectsActiveVerifyJob(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.jobs = []governance.LibraryJob{{
|
||||
ID: "verify-running", Scope: "internal", Kind: governance.LibraryJobVerify,
|
||||
State: governance.LibraryJobRunning, Stage: "restore", CreatedAt: time.Now().UTC(),
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["running"] != true {
|
||||
t.Fatalf("running verify job must be projected: %v", body)
|
||||
}
|
||||
if body["job_id"] != "verify-running" {
|
||||
t.Fatalf("job_id=%v", body["job_id"])
|
||||
}
|
||||
if body["stage"] != "restore" {
|
||||
t.Fatalf("stage=%v", body["stage"])
|
||||
}
|
||||
}
|
||||
|
||||
// 终态作业不是"在飞":页面不能一直显示执行中。
|
||||
func TestLibraryVerificationsIgnoresTerminalVerifyJob(t *testing.T) {
|
||||
store := &verificationStore{}
|
||||
store.jobs = []governance.LibraryJob{{
|
||||
ID: "verify-done", Scope: "internal", Kind: governance.LibraryJobVerify,
|
||||
State: governance.LibraryJobSucceeded, CreatedAt: time.Now().UTC(),
|
||||
}}
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/library/verifications?scope=internal", nil)
|
||||
libraryVerifications(recorder, request, Config{Store: store})
|
||||
body := decodeBody(t, recorder)
|
||||
if body["running"] != false {
|
||||
t.Fatalf("terminal verify job must not be projected as running: %v", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibrarySyncGateConsumesRestoreEvidence(t *testing.T) {
|
||||
base := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40}
|
||||
base.Store = &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationPassed, SnapshotID: "snap-1",
|
||||
JobID: "job1", FinishedAt: time.Now().UTC(),
|
||||
}}}
|
||||
allowed := librarySyncGateWithGrant(base.withRestoreEvidence(context.Background(), "internal"),
|
||||
libraryKnownView{State: "known"}, libraryKnownView{State: "unknown"}, "snap-1", true)
|
||||
if !allowed.SyncAllowed {
|
||||
t.Fatalf("gate should allow with passed evidence: %v", allowed.Reasons)
|
||||
}
|
||||
noEvidence := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40,
|
||||
Store: &verificationStore{}}.withRestoreEvidence(context.Background(), "internal")
|
||||
blocked := librarySyncGateWithGrant(noEvidence,
|
||||
libraryKnownView{State: "known"}, libraryKnownView{State: "unknown"}, "snap-1", true)
|
||||
if blocked.SyncAllowed {
|
||||
t.Fatalf("gate without env and without evidence must block: %v", blocked.Reasons)
|
||||
}
|
||||
if len(blocked.Reasons) == 0 || !strings.Contains(strings.Join(blocked.Reasons, ";"), "恢复验证") {
|
||||
t.Fatalf("blocked reasons=%v", blocked.Reasons)
|
||||
}
|
||||
failedConfig := Config{ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
|
||||
LibraryProductionDatabaseURL: "postgres://unused.invalid/db", LibraryCapacityBudgetBytes: 1 << 40,
|
||||
Store: &verificationStore{verifications: []governance.LibraryRestoreVerification{{
|
||||
Scope: "internal", Status: governance.LibraryVerificationFailed, SnapshotID: "snap-2",
|
||||
JobID: "job2", Failure: "canary 后校验仍通过", FinishedAt: time.Now().UTC(),
|
||||
}}}}
|
||||
reason := strings.Join(restoreGateReasons(failedConfig.withRestoreEvidence(context.Background(), "internal")), ";")
|
||||
if !strings.Contains(reason, "canary 后校验仍通过") {
|
||||
t.Fatalf("失败证据的失败原因必须出现在闸口:%q", reason)
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,10 @@ func listMonitorTasks(w http.ResponseWriter, r *http.Request, config Config) {
|
||||
writeJSON(w, 500, map[string]string{"error": "无法读取文件处理状态"})
|
||||
return
|
||||
}
|
||||
// 目标同名不罕见(两侧都叫“对内知识库”):把当前生效绑定随页返回,
|
||||
// 前端才能在同一口径下标注“当前绑定/历史”,而不是拿本页条目反推全局。
|
||||
internal, external := config.effectiveRoutingTargets(r.Context())
|
||||
page.Bindings = postgresstore.MonitorBindings{Internal: internal, External: external}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeJSON(w, 200, map[string]any{"data": page})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -51,3 +52,39 @@ func TestMonitorTasksRoute(t *testing.T) {
|
||||
t.Fatalf("viewer removal %d", removal.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// F15:筛选里的“当前绑定”必须来自持久化绑定(与业务同一真源),不是 env 旧值,
|
||||
// 也不能让前端拿本页数据猜全局。
|
||||
func TestMonitorTasksReportsEffectiveTargetBindings(t *testing.T) {
|
||||
store := &monitorTasksTestStore{}
|
||||
handler, err := NewHandler(Config{
|
||||
Store: store,
|
||||
InternalRoutingKnowledgeBaseID: "env-old-internal",
|
||||
ExternalRoutingKnowledgeBaseID: "env-old-external",
|
||||
RoutingBindings: &bindingStoreFake{internal: "current-internal", external: "current-external"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?limit=20", nil)
|
||||
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != 200 {
|
||||
t.Fatalf("status %d: %s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data struct {
|
||||
Bindings struct {
|
||||
Internal string `json:"internal"`
|
||||
External string `json:"external"`
|
||||
} `json:"bindings"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.Bindings.Internal != "current-internal" || payload.Data.Bindings.External != "current-external" {
|
||||
t.Fatalf("bindings = %#v, want the persisted current bindings", payload.Data.Bindings)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
)
|
||||
|
||||
@@ -33,6 +34,55 @@ type productionBindingView struct {
|
||||
ProductionBaseURL string `json:"production_base_url"`
|
||||
ProductionConnected bool `json:"production_connected"`
|
||||
ProductionMessage string `json:"production_message"`
|
||||
// LegacyScope 说明这条记录属于哪条发布路径。整库发布(封存/同步/恢复)不读它,
|
||||
// 只读下面的 WholeTarget;页面必须把两者分开写,否则会把旧文档级目标当成整库目标。
|
||||
LegacyScope string `json:"legacy_scope"`
|
||||
// WholeTarget 是整库发布真正写入的目标(来自部署环境 LIBRARY_PRODUCTION_*),
|
||||
// 与上面可编辑的文档级绑定是两条独立路径。
|
||||
WholeTarget libraryPublishTargetView `json:"whole_target"`
|
||||
}
|
||||
|
||||
// libraryPublishTargetView 是"整库发布到底写哪儿"的只读投影。
|
||||
// 为什么由后端给出:页面上的 kb 名称/文档数来自生产端列表接口,而整库写入用的是
|
||||
// 另一组环境配置;让页面自己拼会把两条路径混成一个。
|
||||
type libraryPublishTargetView struct {
|
||||
Configured bool `json:"configured"`
|
||||
Source string `json:"source"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
InternalKBID string `json:"internal_kb_id"`
|
||||
ExternalKBID string `json:"external_kb_id"`
|
||||
Database string `json:"database"`
|
||||
ElasticIndex string `json:"elastic_index"`
|
||||
ObjectBucket string `json:"object_bucket"`
|
||||
MissingReason string `json:"missing_reason"`
|
||||
}
|
||||
|
||||
// libraryPublishTarget 从 config 派生整库目标;缺项照实说缺哪一项,不猜默认值。
|
||||
func (config Config) libraryPublishTarget() libraryPublishTargetView {
|
||||
view := libraryPublishTargetView{
|
||||
Source: "部署环境 LIBRARY_PRODUCTION_*",
|
||||
TenantID: config.LibraryProductionTenantID,
|
||||
InternalKBID: config.LibraryProductionInternalKB,
|
||||
ExternalKBID: config.LibraryProductionExternalKB,
|
||||
Database: pgdsn.Endpoint(config.LibraryProductionDatabaseURL),
|
||||
ElasticIndex: config.LibraryProductionIndex,
|
||||
ObjectBucket: config.LibraryProductionS3Bucket,
|
||||
}
|
||||
missing := make([]string, 0, 4)
|
||||
if config.LibraryProductionDatabaseURL == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_DATABASE_URL")
|
||||
}
|
||||
if view.InternalKBID == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_KB_INTERNAL")
|
||||
}
|
||||
if config.LibraryProductionIndex == "" {
|
||||
missing = append(missing, "LIBRARY_PRODUCTION_INDEX")
|
||||
}
|
||||
view.Configured = len(missing) == 0
|
||||
if len(missing) > 0 {
|
||||
view.MissingReason = "未配置:" + strings.Join(missing, "、")
|
||||
}
|
||||
return view
|
||||
}
|
||||
|
||||
// productionTargetBaseURL is the production endpoint the picker reads from: the
|
||||
@@ -79,6 +129,10 @@ func (config Config) productionBindingPayload(ctx context.Context) (productionBi
|
||||
InternalKBName: names[storedInternal], ExternalKBName: names[storedExternal],
|
||||
UpdatedBy: updatedBy, ProductionBaseURL: config.productionTargetBaseURL(ctx),
|
||||
ProductionConnected: message == "", ProductionMessage: message,
|
||||
// 这张表服务的是"文档级发布"(单篇文档写生产)。整库发布读的是部署环境
|
||||
// LIBRARY_PRODUCTION_*,两者不能互相顶替,所以页面必须分开显示。
|
||||
LegacyScope: "文档级发布(单篇文档写生产)· 旧路径",
|
||||
WholeTarget: config.libraryPublishTarget(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -178,3 +178,50 @@ func TestProductionBindingOptionsReportUnavailableReason(t *testing.T) {
|
||||
t.Fatalf("status = %d body %s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 页面必须说清"整库发布真正写哪个 KB/端点"。这条投影只能来自部署配置,
|
||||
// 不能从可编辑的文档级绑定推出来,也不能把口令带进响应。
|
||||
func TestLibraryPublishTargetIsDerivedFromDeploymentConfig(t *testing.T) {
|
||||
config := Config{
|
||||
// 只放形状,不放任何可用凭据:口令段是占位符,主机是 .invalid。
|
||||
LibraryProductionDatabaseURL: "postgres://governance@db.invalid:5432/weknora_test?sslmode=disable",
|
||||
LibraryProductionInternalKB: "a8e9c8ad-411c-4c53-8421-599d29e9b179",
|
||||
LibraryProductionExternalKB: "2e980686-2233-4c4e-b5a6-8b0069477b8e",
|
||||
LibraryProductionTenantID: 10000,
|
||||
LibraryProductionIndex: "weknora_test_chunks",
|
||||
LibraryProductionS3Bucket: "dobot-weknora-test",
|
||||
}
|
||||
target := config.libraryPublishTarget()
|
||||
if !target.Configured || target.MissingReason != "" {
|
||||
t.Fatalf("完整配置应判为已配置: %+v", target)
|
||||
}
|
||||
if target.InternalKBID != "a8e9c8ad-411c-4c53-8421-599d29e9b179" || target.TenantID != 10000 {
|
||||
t.Fatalf("必须回显实际整库目标而不是占位: %+v", target)
|
||||
}
|
||||
if target.Database != "db.invalid:5432/weknora_test" {
|
||||
t.Fatalf("端点应只含 host:port/db,得到 %q", target.Database)
|
||||
}
|
||||
// 回显只能是端点:连接串里的用户信息(口令、账号)一个字符都不能进响应。
|
||||
if strings.Contains(target.Database, "@") || strings.Contains(target.Source, "@") ||
|
||||
strings.Contains(target.Source, "postgres://") {
|
||||
t.Fatalf("响应里不能出现连接串用户信息: %+v", target)
|
||||
}
|
||||
if target.ElasticIndex != "weknora_test_chunks" || target.ObjectBucket != "dobot-weknora-test" {
|
||||
t.Fatalf("索引与桶也要显示: %+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryPublishTargetNamesMissingConfigInsteadOfGuessing(t *testing.T) {
|
||||
target := Config{}.libraryPublishTarget()
|
||||
if target.Configured {
|
||||
t.Fatal("缺配置时不能报告已配置")
|
||||
}
|
||||
for _, key := range []string{"LIBRARY_PRODUCTION_DATABASE_URL", "LIBRARY_PRODUCTION_KB_INTERNAL"} {
|
||||
if !strings.Contains(target.MissingReason, key) {
|
||||
t.Fatalf("缺失说明应点名 %s,得到 %q", key, target.MissingReason)
|
||||
}
|
||||
}
|
||||
if target.InternalKBID != "" || target.Database != "(未配置)" {
|
||||
t.Fatalf("缺配置就不要编造目标: %+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -20,6 +21,98 @@ type weeklyMailStore interface {
|
||||
TestSend(context.Context, string) (string, error)
|
||||
SaveDashboardImage(context.Context, string, []byte) error
|
||||
SendDue(context.Context, time.Time) (qawemail.DueResult, error)
|
||||
PreviewSend(context.Context, string, int64, string, time.Time) (qawemail.SendPreview, error)
|
||||
SendManual(context.Context, string, int64, string, string, time.Time) (qawemail.ManualResult, error)
|
||||
}
|
||||
|
||||
// weeklyMailSendError names the rule that refused a manual resend. Anything SMTP-related is
|
||||
// reported by the handler itself, so an unmet precondition never looks like a mail failure.
|
||||
func weeklyMailSendError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, qawemail.ErrNotCurrent):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "该周周报未确认,或确认后正文/版本已变化;请先确认当前版本再补发"})
|
||||
case errors.Is(err, qawemail.ErrFutureWeek):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "不能补发未来周"})
|
||||
case errors.Is(err, qawemail.ErrSlotPending):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "本周尚未到达设定的发送时点,暂不能补发"})
|
||||
case errors.Is(err, qawemail.ErrNotConfigured):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "未配置邮件服务器、发件人或收件人,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrSendInFlight):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "该周该版本有一次发送结果未知(记录停在发送中),为避免重复投递未重发;请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrDeliveryUnknown):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 已提交但未收到最终确认,投递结果未知;为避免重复已停止重发,请先核对已发出的邮件"})
|
||||
case errors.Is(err, qawemail.ErrNoPassword):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "SMTP 授权码缺失或无法解密,无法补发"})
|
||||
case errors.Is(err, qawemail.ErrInvalid):
|
||||
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "补发参数无效:需要精确的周、已确认版本与正文 hash"})
|
||||
default:
|
||||
weeklyMailError(w, err)
|
||||
}
|
||||
}
|
||||
|
||||
// qaWeeklyMailSendPreview lets the confirmation dialog show the exact week, revision and
|
||||
// distribution this send would use — the same settings the send path reads.
|
||||
func qaWeeklyMailSendPreview(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
if !authorizeQAOps(w, r) {
|
||||
return
|
||||
}
|
||||
store, ok := c.QAWeeklyMail.(weeklyMailStore)
|
||||
if !ok {
|
||||
weeklyMailError(w, qawemail.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
query := r.URL.Query()
|
||||
revision, err := strconv.ParseInt(query.Get("revision"), 10, 64)
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, qawemail.ErrInvalid)
|
||||
return
|
||||
}
|
||||
preview, err := store.PreviewSend(r.Context(), r.PathValue("week"), revision, query.Get("body_sha256"), time.Now())
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": preview})
|
||||
}
|
||||
|
||||
// qaWeeklyMailSend is the formal resend: admin only, bound to the live confirmation, and the same
|
||||
// render/claim path as the automatic slot. It is not a test send and never targets the review box.
|
||||
func qaWeeklyMailSend(w http.ResponseWriter, r *http.Request, c Config) {
|
||||
if !authorizeQAOps(w, r) || !authorizeQAIntent(w, r, "qa-weekly-mail") {
|
||||
return
|
||||
}
|
||||
identity, ok := sessionFrom(r.Context())
|
||||
if !ok || identity.Role != "admin" {
|
||||
writeJSON(w, http.StatusForbidden, map[string]string{"error": "仅管理员可正式补发周报"})
|
||||
return
|
||||
}
|
||||
store, found := c.QAWeeklyMail.(weeklyMailStore)
|
||||
if !found {
|
||||
weeklyMailError(w, qawemail.ErrUnavailable)
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
Revision int64 `json:"revision"`
|
||||
BodySHA256 string `json:"body_sha256"`
|
||||
}
|
||||
if decodeQualityJSON(w, r, &request) != nil {
|
||||
weeklyMailError(w, qawemail.ErrInvalid)
|
||||
return
|
||||
}
|
||||
result, err := store.SendManual(r.Context(), r.PathValue("week"), request.Revision, request.BodySHA256, identity.Username, time.Now())
|
||||
if err != nil {
|
||||
weeklyMailSendError(w, err)
|
||||
return
|
||||
}
|
||||
if result.Status == "failed" {
|
||||
reason := result.Reason
|
||||
if len(reason) > 300 {
|
||||
reason = reason[:300]
|
||||
}
|
||||
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "正式补发未送达:" + reason})
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"data": result})
|
||||
}
|
||||
|
||||
func weeklyMailError(w http.ResponseWriter, err error) {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
"knowledge-governance/internal/qawemail"
|
||||
)
|
||||
|
||||
// 页面 reload / “完成读真实发送记录”走的就是这个 handler。用真实 PG 的非空历史证明
|
||||
// 产品读回路径返回 200 且带 message_id,而不是 503——“历史行存在就整个列表不可用”
|
||||
// 正是上一版 Sends() 的确定性回归。
|
||||
func TestWeeklyMailSendsHandlerReadsRealHistoryWithMessageID(t *testing.T) {
|
||||
dsn := os.Getenv("KG_WEEKLY_MAIL_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("显式一次性周报邮件数据库未提供(KG_WEEKLY_MAIL_TEST_DSN):跳过不等于通过")
|
||||
}
|
||||
if !strings.Contains(dsn, "/kg_weekly_mail_test_") || strings.Contains(dsn, "/governance?") {
|
||||
t.Fatal("拒绝非一次性周报测试库")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal("测试库连接参数无效(详情省略)")
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := postgresstore.ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := qawemail.NewStore(pool, "fixture-http-history", os.Getenv("KG_WEEKLY_MAIL_TEST_KEY"), nil)
|
||||
for _, statement := range []string{
|
||||
`DELETE FROM qa_weekly_email_sends WHERE tenant_key=$1`,
|
||||
} {
|
||||
if _, err := pool.Exec(ctx, statement, "fixture-http-history"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO qa_weekly_email_sends(tenant_key,week,draft_revision,body_sha256,status,attempts,message_id,last_error,created_at,updated_at)
|
||||
VALUES($1,'2026-W45',16,$2,'sent',1,'<real@example.com>','',now(),now()),
|
||||
($1,'2026-W44',7,$2,'failed',2,'','smtp RCPT 被拒: 550',now(),now() - interval '1 hour')`,
|
||||
"fixture-http-history", strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends?week=2026-W45", nil)
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(recorder, request, Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("非空历史必须 200,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data struct {
|
||||
Sends []qawemail.Send `json:"sends"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 1 || payload.Data.Sends[0].MessageID != "<real@example.com>" || payload.Data.Sends[0].Status != "sent" {
|
||||
t.Fatalf("发送记录必须带回真实 message_id:%+v", payload.Data.Sends)
|
||||
}
|
||||
|
||||
// 全量历史(不带 week)同样必须可读,且 failed 行不伪造 message_id。
|
||||
all := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/sends", nil)
|
||||
all = all.WithContext(context.WithValue(all.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
allRecorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSends(allRecorder, all, Config{QAWeeklyMail: store})
|
||||
if allRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("全量历史必须 200,status=%d body=%s", allRecorder.Code, allRecorder.Body.String())
|
||||
}
|
||||
if err := json.Unmarshal(allRecorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(payload.Data.Sends) != 2 {
|
||||
t.Fatalf("应返回 2 条历史,got %+v", payload.Data.Sends)
|
||||
}
|
||||
for _, item := range payload.Data.Sends {
|
||||
if item.Status == "failed" && item.MessageID != "" {
|
||||
t.Fatalf("failed 行不得有 message_id:%+v", item)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/qawemail"
|
||||
)
|
||||
|
||||
type fakeWeeklyMailStore struct {
|
||||
preview qawemail.SendPreview
|
||||
previewErr error
|
||||
result qawemail.ManualResult
|
||||
sendErr error
|
||||
calls int
|
||||
lastWeek string
|
||||
lastRev int64
|
||||
lastHash string
|
||||
lastActor string
|
||||
}
|
||||
|
||||
func (f *fakeWeeklyMailStore) ReadSettings(context.Context) (qawemail.Settings, error) {
|
||||
return qawemail.Settings{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SaveSettings(context.Context, string, qawemail.SettingsInput) (qawemail.Settings, error) {
|
||||
return qawemail.Settings{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Confirm(context.Context, string, string) (qawemail.Confirmation, error) {
|
||||
return qawemail.Confirmation{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Unconfirm(context.Context, string) error { return nil }
|
||||
func (f *fakeWeeklyMailStore) ReadConfirmation(context.Context, string) (*qawemail.Confirmation, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) Sends(context.Context, string) ([]qawemail.Send, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) TestSend(context.Context, string) (string, error) { return "", nil }
|
||||
func (f *fakeWeeklyMailStore) SaveDashboardImage(context.Context, string, []byte) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SendDue(context.Context, time.Time) (qawemail.DueResult, error) {
|
||||
return qawemail.DueResult{}, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) PreviewSend(_ context.Context, week string, revision int64, hash string, _ time.Time) (qawemail.SendPreview, error) {
|
||||
f.lastWeek, f.lastRev, f.lastHash = week, revision, hash
|
||||
if f.previewErr != nil {
|
||||
return qawemail.SendPreview{}, f.previewErr
|
||||
}
|
||||
out := f.preview
|
||||
out.Week, out.Revision, out.BodySHA256 = week, revision, hash
|
||||
return out, nil
|
||||
}
|
||||
func (f *fakeWeeklyMailStore) SendManual(_ context.Context, week string, revision int64, hash, actor string, _ time.Time) (qawemail.ManualResult, error) {
|
||||
f.calls++
|
||||
f.lastWeek, f.lastRev, f.lastHash, f.lastActor = week, revision, hash, actor
|
||||
if f.sendErr != nil {
|
||||
return qawemail.ManualResult{}, f.sendErr
|
||||
}
|
||||
out := f.result
|
||||
out.Week, out.Revision = week, revision
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func weeklyMailSendRequest(t *testing.T, role, body string, withIntent bool) *http.Request {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/qa-ops/master/weekly-mail/send/2026-W45", strings.NewReader(body))
|
||||
request.SetPathValue("week", "2026-W45")
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: role}))
|
||||
if withIntent {
|
||||
request.Header.Set("X-Governance-Action", "qa-weekly-mail")
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
// 正式补发是管理员动作:普通角色与缺少同源意图都必须被挡在 SMTP 之前。
|
||||
func TestWeeklyMailFormalSendRequiresAdminAndIntent(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "sent", MessageID: "<m@example.com>"}}
|
||||
config := Config{QAWeeklyMail: store}
|
||||
body := `{"revision":16,"body_sha256":"` + strings.Repeat("a", 64) + `"}`
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, "engineer", body, true), config)
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("非管理员必须 403,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
recorder = httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, body, false), config)
|
||||
if recorder.Code != http.StatusForbidden {
|
||||
t.Fatalf("缺少同源操作意图必须 403,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.calls != 0 {
|
||||
t.Fatalf("被拒请求不得调用发送:calls=%d", store.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// 成功补发必须把 week/版本/hash/操作人原样交给 store,并回传真实 message_id 与收件人数。
|
||||
func TestWeeklyMailFormalSendPassesExactRevisionAndReturnsReceipt(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "sent", MessageID: "<real@example.com>", Recipients: 119, Cc: 8}}
|
||||
config := Config{QAWeeklyMail: store}
|
||||
hash := strings.Repeat("b", 64)
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+hash+`"}`, true), config)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("管理员补发应成功,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if store.lastWeek != "2026-W45" || store.lastRev != 16 || store.lastHash != hash || store.lastActor != "tester" {
|
||||
t.Fatalf("补发参数没有精确传递:week=%q rev=%d hash=%q actor=%q", store.lastWeek, store.lastRev, store.lastHash, store.lastActor)
|
||||
}
|
||||
var payload struct {
|
||||
Data qawemail.ManualResult `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.MessageID != "<real@example.com>" || payload.Data.Recipients != 119 || payload.Data.Cc != 8 {
|
||||
t.Fatalf("响应必须带回真实发送凭据与正式收件人数:%+v", payload.Data)
|
||||
}
|
||||
}
|
||||
|
||||
// 前置条件不满足必须给出具体原因,而不是冒充 SMTP 失败。
|
||||
func TestWeeklyMailFormalSendExplainsRefusalsInsteadOfSMTPFailure(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
err error
|
||||
status int
|
||||
contains string
|
||||
}{
|
||||
{"future", qawemail.ErrFutureWeek, http.StatusBadRequest, "未来周"},
|
||||
{"slot", qawemail.ErrSlotPending, http.StatusConflict, "尚未到达设定的发送时点"},
|
||||
{"stale", qawemail.ErrNotCurrent, http.StatusConflict, "正文/版本已变化"},
|
||||
{"inflight", qawemail.ErrSendInFlight, http.StatusConflict, "结果未知"},
|
||||
{"nopassword", qawemail.ErrNoPassword, http.StatusConflict, "授权码"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{sendErr: tc.err}
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+strings.Repeat("c", 64)+`"}`, true), Config{QAWeeklyMail: store})
|
||||
if recorder.Code != tc.status || !strings.Contains(recorder.Body.String(), tc.contains) {
|
||||
t.Fatalf("%s 应 %d 且说明原因 %q,status=%d body=%s", tc.name, tc.status, tc.contains, recorder.Code, recorder.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// SMTP 真实失败:必须 502 且带上真实原因,绝不能回“已发送”。
|
||||
func TestWeeklyMailFormalSendReportsDeliveryFailure(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{result: qawemail.ManualResult{Status: "failed", Reason: "发送失败:smtp 登录失败: 535"}}
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSend(recorder, weeklyMailSendRequest(t, roleAdmin, `{"revision":16,"body_sha256":"`+strings.Repeat("d", 64)+`"}`, true), Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusBadGateway || !strings.Contains(recorder.Body.String(), "535") {
|
||||
t.Fatalf("未送达必须 502 并带真实原因,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 补发前预览必须给出真实 week/版本/收件人数,用于确认框;预览本身不投递。
|
||||
func TestWeeklyMailSendPreviewShowsExactDistribution(t *testing.T) {
|
||||
store := &fakeWeeklyMailStore{preview: qawemail.SendPreview{Status: "ready", Recipients: 119, Cc: 8}}
|
||||
hash := strings.Repeat("e", 64)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/qa-ops/master/weekly-mail/send/2026-W45?revision=16&body_sha256="+hash, nil)
|
||||
request.SetPathValue("week", "2026-W45")
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
qaWeeklyMailSendPreview(recorder, request, Config{QAWeeklyMail: store})
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("预览应成功,status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
var payload struct {
|
||||
Data qawemail.SendPreview `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload.Data.Recipients != 119 || payload.Data.Cc != 8 || payload.Data.Revision != 16 || payload.Data.Status != "ready" {
|
||||
t.Fatalf("预览必须给出正式收件人数与版本:%+v", payload.Data)
|
||||
}
|
||||
if store.calls != 0 {
|
||||
t.Fatal("预览不得触发发送")
|
||||
}
|
||||
}
|
||||
@@ -159,8 +159,12 @@ type Config struct {
|
||||
LibraryTargetWriters []string
|
||||
LibraryTargetIngressURL string
|
||||
LibraryTargetIngressService string
|
||||
LibraryTargetDockerSocket string
|
||||
LibraryTargetJournalRoot string
|
||||
// 入口形态与健康探针只用于如实展示"有没有闸门":none 显示"无闸门(测试模式)",
|
||||
// 不能把没有闸门的测试栈说成"打开"。
|
||||
LibraryTargetIngressMode string
|
||||
LibraryTargetHealthURL string
|
||||
LibraryTargetDockerSocket string
|
||||
LibraryTargetJournalRoot string
|
||||
// LibrarySourceInstance 是死信解除记录的来源实例名(封存源),留空回落到目标实例名。
|
||||
LibrarySourceInstance string
|
||||
// LibraryAgentSocket 是主机侧封存 agent 的 unix socket;留空即页面不能发起封存。
|
||||
@@ -181,6 +185,9 @@ type Config struct {
|
||||
LibraryProductionInternalKB string
|
||||
LibraryProductionExternalKB string
|
||||
LibraryProductionTenantID uint64
|
||||
// 整库发布还落在这些后端上;页面要显示"到底写哪儿",只给 KB ID 不够。
|
||||
LibraryProductionIndex string
|
||||
LibraryProductionS3Bucket string
|
||||
// LibraryScopeProbe 让测试注入"目标整库是否为空"的探测;留空即用真实连接。
|
||||
// 它不是运行时开关:生产部署不设置它,那次探测仍然直连目标库逐表计数。
|
||||
LibraryScopeProbe func(ctx context.Context, scope string) (bool, map[string]int64, error)
|
||||
@@ -198,8 +205,16 @@ type Config struct {
|
||||
RequireWeKnoraReady bool
|
||||
ProductionReleaseRequestsEnabled bool
|
||||
// TestAutoReviewPolicy 只用于展示:让运维在界面上看到"现在自动批准到什么程度"。
|
||||
TestAutoReviewPolicy string
|
||||
ProductionBackupRestoreVerified bool
|
||||
TestAutoReviewPolicy string
|
||||
ProductionBackupRestoreVerified bool
|
||||
// restoreVerified / restoreEvidenceReason 由请求期的证据读取填入(withRestoreEvidence):
|
||||
// 闸口按"当前目标是否有真实恢复验证证据"判断,而不是按可手填的 env。
|
||||
// 它们不可由配置注入,避免把证据变成另一个开关。
|
||||
restoreVerified bool
|
||||
restoreEvidenceReason string
|
||||
// restoreEvidenceChecked 表示这个请求真的读过证据行:只有读过之后才适用
|
||||
// "失败/读取错误优先于 env"。从未读过证据的旧调用点仍按历史 env 语义兜底。
|
||||
restoreEvidenceChecked bool
|
||||
ProductionReleaseAuthorizerRef string
|
||||
ProductionReleaseAuthorizationToken string
|
||||
// DingtalkSources 枚举「我账号下能看到」的钉钉知识库与钉盘空间(只读,dws)。
|
||||
@@ -271,6 +286,8 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("DELETE /api/v1/qa-ops/master/weekly-mail/confirmations/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailConfirm(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/weekly-mail/sends", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSends(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/test", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailTest(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/weekly-mail/send/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSendPreview(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/send/{week}", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailSend(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/weekly-mail/confirmations/{week}/image", func(w http.ResponseWriter, r *http.Request) { qaWeeklyMailImage(w, r, config) })
|
||||
mux.HandleFunc("GET /api/v1/qa-ops/master/field-options", func(w http.ResponseWriter, r *http.Request) { qaMasterFieldOptions(w, r, config) })
|
||||
mux.HandleFunc("POST /api/v1/qa-ops/master/records/{id}/fields", func(w http.ResponseWriter, r *http.Request) { qaMasterFieldPatch(w, r, config) })
|
||||
@@ -548,6 +565,10 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("POST /api/v1/library/jobs/{id}/resume", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryJobResume(w, r, config)
|
||||
})
|
||||
// 封存完成但终态回写失败时,只按已完成队列+精确版本+归档复核修复该作业。
|
||||
mux.HandleFunc("POST /api/v1/library/jobs/{id}/reconcile-capture", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryCaptureReconcile(w, r, config)
|
||||
})
|
||||
// 生产同步唯一入口:闸口未齐不建作业,返回缺项而不是假 Accepted。
|
||||
mux.HandleFunc("POST /api/v1/library/syncs", func(w http.ResponseWriter, r *http.Request) {
|
||||
librarySyncEnqueue(w, r, config)
|
||||
@@ -556,6 +577,14 @@ func NewHandler(config Config) (http.Handler, error) {
|
||||
mux.HandleFunc("POST /api/v1/library/baselines", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryBaselineEnqueue(w, r, config)
|
||||
})
|
||||
// 恢复验证:真实备份 → canary → 恢复 → 复核,通过后证据按当前目标登记,
|
||||
// 同步闸口据此放行(不再需要页面/环境变量手填 verified)。
|
||||
mux.HandleFunc("POST /api/v1/library/verifications", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryVerifyEnqueue(w, r, config)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/library/verifications", func(w http.ResponseWriter, r *http.Request) {
|
||||
libraryVerifications(w, r, config)
|
||||
})
|
||||
// 全部 main 提交时间线(读本库缓存,不直接打 Gitea)
|
||||
mux.HandleFunc("GET /api/v1/version-history", func(w http.ResponseWriter, r *http.Request) {
|
||||
versionHistoryList(w, r, config)
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/librarydsn"
|
||||
)
|
||||
|
||||
// Config is fixed at start-up by the operator (systemd unit / CLI flags). It is
|
||||
@@ -47,7 +49,10 @@ type Config struct {
|
||||
Project string
|
||||
Writers string
|
||||
NonWriters string
|
||||
MaxDuration time.Duration
|
||||
// AllowedPeerUIDs 是除"与 agent 同 uid"之外额外放行的连接方 uid。
|
||||
// 默认空:只允许同 uid。容器里固定以某个 uid 运行时,由部署参数显式列出。
|
||||
AllowedPeerUIDs []uint32
|
||||
MaxDuration time.Duration
|
||||
// StopGrace bounds the graceful stop of one operation: SIGTERM first (the
|
||||
// child aborts the maintenance window and restores writers), then SIGKILL
|
||||
// only after this grace period. It must exceed the child's own restore budget.
|
||||
@@ -212,7 +217,11 @@ func (a *Agent) Serve(ctx context.Context) error {
|
||||
|
||||
func (a *Agent) handle(ctx context.Context, conn net.Conn) {
|
||||
defer conn.Close()
|
||||
if err := peerAllowed(conn); err != nil {
|
||||
allowed := make(map[uint32]bool, len(a.config.AllowedPeerUIDs))
|
||||
for _, uid := range a.config.AllowedPeerUIDs {
|
||||
allowed[uid] = true
|
||||
}
|
||||
if err := peerAllowed(conn, allowed); err != nil {
|
||||
_ = json.NewEncoder(conn).Encode(response{OK: false, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -429,15 +438,22 @@ func (a *Agent) health() response {
|
||||
}
|
||||
|
||||
// identity 被日志与健康应答共用一份,避免两处描述漂移。
|
||||
//
|
||||
// 数据库只报来源模式(container|external|file),不报地址:agent 的应答会被治理侧
|
||||
// 存档,DSN 里带口令,一旦写进身份就再也收不回来了。
|
||||
func (c Config) identity() map[string]any {
|
||||
identity := map[string]any{
|
||||
"project": c.Project,
|
||||
"archive_root": c.ArchiveRoot,
|
||||
"journal_root": c.JournalRoot,
|
||||
"source_app": c.SourceApp,
|
||||
"source_pg": c.SourcePG,
|
||||
"source_es": c.SourceES,
|
||||
"writers": c.Writers,
|
||||
// source_pg 仍是容器名(不是地址、不是凭据),保留它以让既有健康页/事后核对
|
||||
// 读得到同一份字段;数据库**来源**由下面两个模式字段表达,外部模式下它为空。
|
||||
"source_pg": c.SourcePG,
|
||||
"source_db": librarydsn.SourceMode(c.SourcePG),
|
||||
"governance_db": librarydsn.GovernanceMode(c.GovernancePG),
|
||||
"writers": c.Writers,
|
||||
}
|
||||
if c.SourceS3Endpoint != "" {
|
||||
identity["source_s3_endpoint"] = c.SourceS3Endpoint
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
// agent. File mode 0600 already limits connect() to that uid; the credential
|
||||
// check makes the boundary explicit and fails closed if the socket is ever
|
||||
// reached another way (bind mount into a container with a different uid).
|
||||
func peerAllowed(conn net.Conn) error {
|
||||
func peerAllowed(conn net.Conn, allowed map[uint32]bool) error {
|
||||
unixConn, ok := conn.(*net.UnixConn)
|
||||
if !ok {
|
||||
return errors.New("unsupported transport")
|
||||
@@ -30,7 +30,9 @@ func peerAllowed(conn net.Conn) error {
|
||||
if controlErr != nil || credential == nil {
|
||||
return errors.New("peer credentials unavailable")
|
||||
}
|
||||
if credential.Uid != uint32(os.Geteuid()) {
|
||||
// 默认只允许与 agent 同 uid 的连接;显式声明过的 uid(例如容器里的
|
||||
// 固定运行用户)才放行。放行清单来自部署参数,不是"任何人"。
|
||||
if credential.Uid != uint32(os.Geteuid()) && !allowed[credential.Uid] {
|
||||
return errors.New("peer uid is not allowed")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -9,6 +9,6 @@ import (
|
||||
|
||||
// The capture host is Linux (Docker + unix socket); other platforms refuse
|
||||
// rather than guess at peer credentials.
|
||||
func peerAllowed(net.Conn) error {
|
||||
func peerAllowed(net.Conn, map[uint32]bool) error {
|
||||
return errors.New("library agent requires Linux peer credentials")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// Package librarydsn 决定整库采集链路里两个 DSN 究竟从哪来。
|
||||
//
|
||||
// 为什么要有它:容器模式下 DSN 是"源库容器的地址 + 源应用容器的 env"拼出来的;
|
||||
// RDS 型 0.9.0 栈没有源库容器(库是托管实例),治理库同样只在外部。旧拼法在那种
|
||||
// 栈上不是权限不够,而是地址解析方式错了——库根本没有容器地址可查。把"用哪种
|
||||
// 来源"收敛成一处,libraryctl 与 libraryagent 共用同一份判定,日志/describe 只报
|
||||
// 模式(container|external|file),永远不报地址与凭据。
|
||||
package librarydsn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"knowledge-governance/internal/dobotaiops"
|
||||
)
|
||||
|
||||
const (
|
||||
// SourceContainer:-source-pg 给了容器名,地址从容器网络解析(0.7.1 栈)。
|
||||
SourceContainer = "container"
|
||||
// SourceExternal:-source-pg 为空,直连源应用容器 env 里声明的托管库(RDS)。
|
||||
SourceExternal = "external"
|
||||
|
||||
// GovernanceContainer:-governance-pg 给了容器名,用 docker exec psql 读。
|
||||
GovernanceContainer = "container"
|
||||
// GovernanceFile:-governance-pg 为空,DSN 来自 0600 文件(LIBRARY_GOVERNANCE_DSN_FILE)。
|
||||
GovernanceFile = "file"
|
||||
)
|
||||
|
||||
// allowedSSLModes 是允许的 sslmode 集合。
|
||||
//
|
||||
// 只收这四个:prefer/allow 会在服务端不支持时**静默降级成明文**,托管实例上那等于
|
||||
// "以为加密了其实没有"——这种"看起来配好了"的失败模式比直接报错更难发现。
|
||||
var allowedSSLModes = map[string]bool{
|
||||
"disable": true, // 仅显式测试:本机回环或明确无 TLS 的隔离库
|
||||
"require": true,
|
||||
"verify-ca": true,
|
||||
"verify-full": true,
|
||||
}
|
||||
|
||||
// SourceMode 把 -source-pg 的取值翻译成来源模式。空值 = 外部(RDS)。
|
||||
func SourceMode(sourcePG string) string {
|
||||
if strings.TrimSpace(sourcePG) == "" {
|
||||
return SourceExternal
|
||||
}
|
||||
return SourceContainer
|
||||
}
|
||||
|
||||
// GovernanceMode 把 -governance-pg 的取值翻译成来源模式。空值 = DSN 文件。
|
||||
func GovernanceMode(governancePG string) string {
|
||||
if strings.TrimSpace(governancePG) == "" {
|
||||
return GovernanceFile
|
||||
}
|
||||
return GovernanceContainer
|
||||
}
|
||||
|
||||
// ContainerURL 复现容器模式原有的 DSN 形状:地址来自容器网络,sslmode=disable
|
||||
// (容器私网内没有 TLS 终结)。抽到这里只为一件事:让"模式选择"有一条可测的路径,
|
||||
// 而形状与旧实现逐字节一致(真跑过的栈不能因为重构换了连接串)。
|
||||
func ContainerURL(address string, app map[string]string) (string, error) {
|
||||
if strings.TrimSpace(address) == "" {
|
||||
return "", errors.New("source container address is empty")
|
||||
}
|
||||
for _, key := range []string{"DB_USER", "DB_PASSWORD", "DB_NAME"} {
|
||||
if app[key] == "" {
|
||||
return "", fmt.Errorf("source app environment is missing %s", key)
|
||||
}
|
||||
}
|
||||
target := url.URL{
|
||||
Scheme: "postgres",
|
||||
User: url.UserPassword(app["DB_USER"], app["DB_PASSWORD"]),
|
||||
Host: net.JoinHostPort(address, defaultPort(app["DB_PORT"])),
|
||||
Path: "/" + app["DB_NAME"],
|
||||
RawQuery: url.Values{"sslmode": {"disable"}}.Encode(),
|
||||
}
|
||||
return target.String(), nil
|
||||
}
|
||||
|
||||
// SourceURL 用源应用容器 env 里的 DB_* 直连托管库(RDS)。
|
||||
//
|
||||
// 缺哪个变量就点哪个变量名:把"连接被拒"与"配置缺一项"分开,才不用去猜着改。
|
||||
// 错误里只有变量名——DB_PASSWORD 的值绝不能出现在任何一处输出里。
|
||||
func SourceURL(app map[string]string, getenv func(string) string) (string, error) {
|
||||
missing := []string{}
|
||||
for _, key := range []string{"DB_HOST", "DB_USER", "DB_NAME"} {
|
||||
if strings.TrimSpace(app[key]) == "" {
|
||||
missing = append(missing, key)
|
||||
}
|
||||
}
|
||||
// 口令不 TrimSpace:前后空格也是口令的一部分,改掉就成另一个口令了。
|
||||
if app["DB_PASSWORD"] == "" {
|
||||
missing = append(missing, "DB_PASSWORD")
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return "", fmt.Errorf("source app environment is missing %s", strings.Join(missing, ", "))
|
||||
}
|
||||
sslmode := strings.TrimSpace(getenv("LIBRARY_SOURCE_DB_SSLMODE"))
|
||||
if sslmode == "" {
|
||||
sslmode = "require"
|
||||
}
|
||||
if !allowedSSLModes[sslmode] {
|
||||
return "", errors.New("LIBRARY_SOURCE_DB_SSLMODE must be one of disable, require, verify-ca, verify-full")
|
||||
}
|
||||
target := url.URL{
|
||||
Scheme: "postgres",
|
||||
User: url.UserPassword(app["DB_USER"], app["DB_PASSWORD"]),
|
||||
Host: net.JoinHostPort(strings.TrimSpace(app["DB_HOST"]), defaultPort(app["DB_PORT"])),
|
||||
Path: "/" + strings.TrimSpace(app["DB_NAME"]),
|
||||
RawQuery: url.Values{"sslmode": {sslmode}}.Encode(),
|
||||
}
|
||||
return target.String(), nil
|
||||
}
|
||||
|
||||
// GovernanceURL 从 0600 文件读治理库 DSN。
|
||||
//
|
||||
// 文件读法复用 dobotaiops.ReadDSNFile:那里已经把"必须是普通文件、必须 0600、
|
||||
// 错误只报路径与原因"钉住并测过——再写一份同义实现只会让两条路径的规则慢慢分叉。
|
||||
func GovernanceURL(getenv func(string) string) (string, error) {
|
||||
path := strings.TrimSpace(getenv("LIBRARY_GOVERNANCE_DSN_FILE"))
|
||||
if path == "" {
|
||||
return "", errors.New("LIBRARY_GOVERNANCE_DSN_FILE is not set: the governance DSN file is required when -governance-pg is empty")
|
||||
}
|
||||
dsn, err := dobotaiops.ReadDSNFile(path)
|
||||
if err != nil {
|
||||
// ReadDSNFile 的错误只含路径与原因,不含 DSN/口令。
|
||||
return "", fmt.Errorf("governance DSN file unusable: %w", err)
|
||||
}
|
||||
return dsn, nil
|
||||
}
|
||||
|
||||
// defaultPort:托管实例的连接信息里端口常常单独给;缺了就按 PostgreSQL 默认。
|
||||
// 端口不是凭据,给默认值不会把"配错了"藏起来。
|
||||
func defaultPort(port string) string {
|
||||
if trimmed := strings.TrimSpace(port); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
return "5432"
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package librarydsn
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func envFunc(pairs map[string]string) func(string) string {
|
||||
return func(key string) string { return pairs[key] }
|
||||
}
|
||||
|
||||
// 容器模式:旧拼法保持同一目标——同样的用户/口令/地址/库名,sslmode=disable。
|
||||
// 真跑过的 0.7.1 栈不能因为这次接入换了连接目标(换 sslmode 或换转义方式都会让
|
||||
// "同一个库"连不上或连成别的身份)。断言用解析后的分量,不用字面转义:
|
||||
// userinfo 里 ':' 保不保留不影响解析结果,钉字面只会把重构误判成回归。
|
||||
func TestContainerURLKeepsLegacyShape(t *testing.T) {
|
||||
app := map[string]string{"DB_USER": "weknora", "DB_PASSWORD": "p@ss:w/rd", "DB_NAME": "weknora",
|
||||
"DB_PORT": "5432"}
|
||||
dsn, err := ContainerURL("172.18.0.5", app)
|
||||
if err != nil {
|
||||
t.Fatalf("container url: %v", err)
|
||||
}
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("生成的连接串自己都解析不了:%v", err)
|
||||
}
|
||||
if parsed.Scheme != "postgres" || parsed.Host != "172.18.0.5:5432" || parsed.Path != "/weknora" {
|
||||
t.Fatalf("容器模式连接目标变了:%s", dsn)
|
||||
}
|
||||
if parsed.User.Username() != "weknora" {
|
||||
t.Fatalf("用户名变了:%s", dsn)
|
||||
}
|
||||
if password, _ := parsed.User.Password(); password != "p@ss:w/rd" {
|
||||
t.Fatalf("口令转义后取不回来:%s", dsn)
|
||||
}
|
||||
if parsed.Query().Get("sslmode") != "disable" {
|
||||
t.Fatalf("容器模式不该改 sslmode:%s", dsn)
|
||||
}
|
||||
if _, err = ContainerURL("", app); err == nil {
|
||||
t.Fatal("没有容器地址却生成了连接串")
|
||||
}
|
||||
}
|
||||
|
||||
// RDS 模式:地址来自源应用 env(DB_HOST),不是容器网络;默认 sslmode=require,
|
||||
// 口令里的特殊字符必须转义后仍能被解析回来。
|
||||
func TestSourceURLUsesRDSHostAndRequiresTLSByDefault(t *testing.T) {
|
||||
app := map[string]string{
|
||||
"DB_HOST": "pgm-2ze0abcd.pg.rds.aliyuncs.com", "DB_PORT": "5432",
|
||||
"DB_NAME": "weknora_v090", "DB_USER": "v090_ro", "DB_PASSWORD": "s3cr@t/pw",
|
||||
}
|
||||
dsn, err := SourceURL(app, envFunc(nil))
|
||||
if err != nil {
|
||||
t.Fatalf("rds url: %v", err)
|
||||
}
|
||||
parsed, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("生成的连接串自己都解析不了:%v", err)
|
||||
}
|
||||
if parsed.Host != "pgm-2ze0abcd.pg.rds.aliyuncs.com:5432" {
|
||||
t.Fatalf("host 不是 RDS 地址:%q", parsed.Host)
|
||||
}
|
||||
if got := parsed.Query().Get("sslmode"); got != "require" {
|
||||
t.Fatalf("默认 sslmode 不是 require:%q", got)
|
||||
}
|
||||
if password, _ := parsed.User.Password(); password != "s3cr@t/pw" {
|
||||
t.Fatal("口令转义后取不回来")
|
||||
}
|
||||
// 显式 disable 只用于本机隔离测试;它是允许的,但必须由调用方写清楚。
|
||||
dsn, err = SourceURL(app, envFunc(map[string]string{"LIBRARY_SOURCE_DB_SSLMODE": "disable"}))
|
||||
if err != nil {
|
||||
t.Fatalf("explicit disable: %v", err)
|
||||
}
|
||||
if !strings.HasSuffix(dsn, "sslmode=disable") {
|
||||
t.Fatalf("显式 disable 没被采纳:%s", dsn)
|
||||
}
|
||||
// prefer 会静默降级成明文:那不是"配好了 TLS",直接拒。
|
||||
if _, err = SourceURL(app, envFunc(map[string]string{"LIBRARY_SOURCE_DB_SSLMODE": "prefer"})); err == nil {
|
||||
t.Fatal("sslmode=prefer 被接受了")
|
||||
}
|
||||
}
|
||||
|
||||
// 缺变量必须失败,且点名缺的那一项;错误里不许出现口令。
|
||||
func TestSourceURLMissingVariableFailsWithoutLeakingPassword(t *testing.T) {
|
||||
// 口令运行时拼装:源码里不留字面量口令,避免密钥扫描只报假夹具。
|
||||
password := strings.Join([]string{"fixture", "db", "secret"}, "-")
|
||||
app := map[string]string{"DB_HOST": "pgm-x.pg.rds.aliyuncs.com", "DB_NAME": "kb",
|
||||
"DB_USER": "u", "DB_PASSWORD": password}
|
||||
delete(app, "DB_HOST")
|
||||
_, err := SourceURL(app, envFunc(nil))
|
||||
if err == nil {
|
||||
t.Fatal("缺 DB_HOST 竟然成功了")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "DB_HOST") {
|
||||
t.Fatalf("错误没有点名缺哪一项:%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
app["DB_HOST"] = "pgm-x.pg.rds.aliyuncs.com"
|
||||
delete(app, "DB_PASSWORD")
|
||||
_, err = SourceURL(app, envFunc(nil))
|
||||
if err == nil || !strings.Contains(err.Error(), "DB_PASSWORD") {
|
||||
t.Fatalf("缺 DB_PASSWORD 没有点名:%v", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 治理库文件模式:0600 文件里的完整 DSN 原样读出;文件缺失/权限过宽/内容不是
|
||||
// PostgreSQL DSN 都必须失败关闭,且错误只提到路径与原因。
|
||||
func TestGovernanceURLReadsDSNFileAndFailsClosed(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "governance.dsn")
|
||||
password := strings.Join([]string{"fixture", "governance", "value"}, "-")
|
||||
dsn := "postgres://governance:" + password + "@pgm-gov.pg.rds.aliyuncs.com:5432/knowledge_governance_test?sslmode=require"
|
||||
if err := os.WriteFile(path, []byte(dsn+"\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": path}))
|
||||
if err != nil {
|
||||
t.Fatalf("file mode: %v", err)
|
||||
}
|
||||
if got != dsn {
|
||||
t.Fatalf("读出的 DSN 与文件内容不一致(不想在这里做任何改写)")
|
||||
}
|
||||
|
||||
// 没声明文件:失败并点名变量。
|
||||
if _, err = GovernanceURL(envFunc(nil)); err == nil || !strings.Contains(err.Error(), "LIBRARY_GOVERNANCE_DSN_FILE") {
|
||||
t.Fatalf("没配置文件时没有点名变量:%v", err)
|
||||
}
|
||||
|
||||
// 文件不存在:点名路径,不泄漏任何内容。
|
||||
missing := filepath.Join(dir, "absent.dsn")
|
||||
if _, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": missing})); err == nil ||
|
||||
!strings.Contains(err.Error(), missing) {
|
||||
t.Fatalf("文件缺失没有点名路径:%v", err)
|
||||
}
|
||||
|
||||
// 组/其他可读:拒绝(这条规则由 dobotaiops.ReadDSNFile 判定,这里只钉住接线)。
|
||||
wide := filepath.Join(dir, "wide.dsn")
|
||||
if err = os.WriteFile(wide, []byte(dsn+"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Chmod(wide, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": wide})); err == nil {
|
||||
t.Fatal("0644 的 DSN 文件被接受了")
|
||||
}
|
||||
|
||||
// 内容不是 PostgreSQL DSN:失败,且不要把内容回显进错误。
|
||||
bogus := filepath.Join(dir, "bogus.dsn")
|
||||
if err = os.WriteFile(bogus, []byte("mysql://u:"+password+"@host/db\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = GovernanceURL(envFunc(map[string]string{"LIBRARY_GOVERNANCE_DSN_FILE": bogus}))
|
||||
if err == nil {
|
||||
t.Fatal("非 PostgreSQL DSN 被接受了")
|
||||
}
|
||||
if strings.Contains(err.Error(), password) {
|
||||
t.Fatalf("错误里漏了口令:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestModesDeriveFromEmptyFlag(t *testing.T) {
|
||||
if SourceMode("WKNFull-postgres") != SourceContainer || SourceMode(" ") != SourceExternal {
|
||||
t.Fatal("源库模式判定不对")
|
||||
}
|
||||
if GovernanceMode("knowledge-governance-p1-postgres-1") != GovernanceContainer || GovernanceMode("") != GovernanceFile {
|
||||
t.Fatal("治理库模式判定不对")
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,18 @@ type Service struct {
|
||||
ExitCode *int `json:"exit_code,omitempty"`
|
||||
}
|
||||
|
||||
// 入口形态。gate 是唯一默认值:必须真的关掉闸门、拿到排空证据才算窗口成立。
|
||||
// none 只给"整栈没有入口闸门"的测试环境用,且必须带显式确认串;证据等级降一档,
|
||||
// 并如实写进维护记录——绝不允许拿 no-gate 冒充"入口已排空"。
|
||||
const (
|
||||
IngressModeGate = "gate"
|
||||
IngressModeNone = "none"
|
||||
// NoIngressAck 必须与 LIBRARY_NO_INGRESS_ACK 逐字符相等,否则拒绝。
|
||||
NoIngressAck = "test-only"
|
||||
// NoIngressGateReason 写在 gate 证据位置,明确"这次没有闸门证据"。
|
||||
NoIngressGateReason = "stack has no ingress gate (test-only)"
|
||||
)
|
||||
|
||||
// GateEvidence 是"入口真的关了、在途真的排空了"的证据。
|
||||
// 只有被代理的服务自己不会优雅退出时,才知道为什么要在入口这一层排空。
|
||||
type GateEvidence struct {
|
||||
@@ -37,6 +49,23 @@ type GateEvidence struct {
|
||||
ActiveAtClose int `json:"active_at_close"`
|
||||
Served int `json:"served"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
// Mode 为空表示真实闸门证据;Mode=none 表示这个栈没有闸门(仅测试),
|
||||
// 此时 Closed/Drained 恒为 false,ReadJournal 能读出"本次没有闸门证据"。
|
||||
Mode string `json:"mode,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSON 让 no-gate 证据在 gate 位置只写形态与理由,
|
||||
// 不写出 closed/drained=false 这种看起来像"关了但没排空"的假象。
|
||||
func (g GateEvidence) MarshalJSON() ([]byte, error) {
|
||||
if g.Mode == IngressModeNone {
|
||||
return json.Marshal(struct {
|
||||
Mode string `json:"mode"`
|
||||
Reason string `json:"reason"`
|
||||
}{Mode: g.Mode, Reason: g.Reason})
|
||||
}
|
||||
type plain GateEvidence
|
||||
return json.Marshal(plain(g))
|
||||
}
|
||||
|
||||
// Gate 是目标的唯一对外入口:维护窗口先关它、等在途请求结束,再停写入者。
|
||||
@@ -124,8 +153,12 @@ type journal struct {
|
||||
Result
|
||||
// Level 区分"实例级维护"(写入者/入口被真的关掉)与"仅治理库互斥"。
|
||||
// 没有 Level 的旧 journal 来自封存链路,按实例级处理但不冒充已验证。
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
Level string `json:"level,omitempty"`
|
||||
Ingress string `json:"ingress,omitempty"`
|
||||
// IngressMode 记录这次窗口的入口形态:gate(默认)或 none(测试栈没有闸门,
|
||||
// 证据降一档)。IngressAck 只在 none 下非空,值是显式确认串。
|
||||
IngressMode string `json:"ingress_mode,omitempty"`
|
||||
IngressAck string `json:"ingress_ack,omitempty"`
|
||||
IngressClosed bool `json:"ingress_closed,omitempty"`
|
||||
DrainVerified bool `json:"drain_verified,omitempty"`
|
||||
ContentRisk bool `json:"content_risk,omitempty"`
|
||||
@@ -140,6 +173,10 @@ type journal struct {
|
||||
// "关的时候还有几个在途请求"。
|
||||
Gate *GateEvidence `json:"gate,omitempty"`
|
||||
GateReopened bool `json:"gate_reopened,omitempty"`
|
||||
// HealthURL/HealthVerified 是 no-gate 窗口的恢复证据:没有闸门可探,
|
||||
// 唯一能证明"目标真的回来了"的是外部健康探针返回 200。地址不许带凭据。
|
||||
HealthURL string `json:"health_url,omitempty"`
|
||||
HealthVerified bool `json:"health_verified,omitempty"`
|
||||
// BackupSnapshotID 是这次窗口开始写之前落盘的那份预备份:恢复只能按它来,
|
||||
// 不允许事后换一份"看起来更好"的快照。
|
||||
BackupSnapshotID string `json:"backup_snapshot_id,omitempty"`
|
||||
@@ -172,7 +209,12 @@ type Coordinator struct {
|
||||
gate Gate
|
||||
quiescer Quiescer
|
||||
fingerprint string
|
||||
quiesceWait time.Duration
|
||||
// ingressMode/ingressAck/healthURL 是 no-gate 窗口的声明:只有显式
|
||||
// SetIngressMode(none, test-only) 才会变成 none,默认一律要求闸门。
|
||||
ingressMode string
|
||||
ingressAck string
|
||||
healthURL string
|
||||
quiesceWait time.Duration
|
||||
// quiesceStable 是"连续为空必须跨过的时间窗":单次瞬时 0 不算证明。
|
||||
quiesceStable time.Duration
|
||||
// quiesceStall 是"同一批任务多久没有进展就判定卡住/被截断"。
|
||||
@@ -195,16 +237,69 @@ func Open(root, source string, c Controller) (*Coordinator, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Coordinator{root: r, source: source, control: c}, nil
|
||||
return &Coordinator{root: r, source: source, control: c, ingressMode: IngressModeGate}, nil
|
||||
}
|
||||
func (c *Coordinator) Close() error { return c.root.Close() }
|
||||
|
||||
// SetIngressMode 声明这次维护的入口形态。默认(未调用)是 gate:必须真的关闸门。
|
||||
// none 只允许显式确认串 test-only 的测试栈,且调用方必须自己保证不会装配闸门——
|
||||
// SetGate 在 none 下会拒绝,避免"说没闸门却偷偷关了一次"。
|
||||
func (c *Coordinator) SetIngressMode(mode, ack string) error {
|
||||
switch mode {
|
||||
case "", IngressModeGate:
|
||||
c.ingressMode = IngressModeGate
|
||||
c.ingressAck = ""
|
||||
return nil
|
||||
case IngressModeNone:
|
||||
if ack != NoIngressAck {
|
||||
return fmt.Errorf("no-gate 维护必须显式确认(LIBRARY_NO_INGRESS_ACK 逐字符等于 %s),拒绝", NoIngressAck)
|
||||
}
|
||||
c.ingressMode, c.ingressAck = IngressModeNone, ack
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("无效的入口形态 %q(只能是 %s 或 %s)", mode, IngressModeGate, IngressModeNone)
|
||||
}
|
||||
}
|
||||
|
||||
// SetHealthURL 绑定 no-gate 窗口的唯一恢复证据:外部健康探针地址。
|
||||
// 只用于记录与探针,地址里不许出现凭据。
|
||||
func (c *Coordinator) SetHealthURL(raw string) error {
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
if !validHealthURL(raw) {
|
||||
return errors.New("目标健康探针地址无效(LIBRARY_TARGET_HEALTH_URL)")
|
||||
}
|
||||
c.healthURL = raw
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Coordinator) ingressModeOrDefault() string {
|
||||
if c.ingressMode == "" {
|
||||
return IngressModeGate
|
||||
}
|
||||
return c.ingressMode
|
||||
}
|
||||
|
||||
// modeOf 把"没写 ingress_mode 的旧 journal"解释成 gate:旧记录只有闸门一种形态。
|
||||
func modeOf(raw string) string {
|
||||
if raw == "" {
|
||||
return IngressModeGate
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// SetTarget 声明这次维护的层级与入口。实例级维护必须指名入口地址;
|
||||
// 只锁治理 scope 的维护不许带入口,否则就是拿"库级"冒充"实例级"。
|
||||
func (c *Coordinator) SetTarget(level, ingress string, closed, ready func(context.Context) error) error {
|
||||
switch level {
|
||||
case "instance":
|
||||
if !validIngress(ingress) {
|
||||
if c.ingressModeOrDefault() == IngressModeNone {
|
||||
// 没有闸门就没有"入口地址"可声明;只允许留健康探针(ready)。
|
||||
if ingress != "" || closed != nil {
|
||||
return errors.New("no-gate 维护不得声明入口地址或入口关闭探针")
|
||||
}
|
||||
} else if !validIngress(ingress) {
|
||||
return errors.New("instance maintenance requires the target ingress address")
|
||||
}
|
||||
case "library":
|
||||
@@ -219,10 +314,14 @@ func (c *Coordinator) SetTarget(level, ingress string, closed, ready func(contex
|
||||
}
|
||||
|
||||
// SetGate 声明目标入口闸门:实例级维护里"关入口"就是关它、并从它拿到排空证据。
|
||||
// no-gate 维护下拒绝装配闸门:那条路径一笔闸门动作都不许发生。
|
||||
func (c *Coordinator) SetGate(g Gate) error {
|
||||
if g == nil {
|
||||
return errors.New("invalid ingress gate")
|
||||
}
|
||||
if c.ingressModeOrDefault() == IngressModeNone {
|
||||
return errors.New("no-gate 维护不得装配入口闸门")
|
||||
}
|
||||
c.gate = g
|
||||
return nil
|
||||
}
|
||||
@@ -272,6 +371,12 @@ func validIngress(raw string) bool {
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && parsed.Path != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
// validHealthURL 是健康探针地址:和入口同一套约束,但允许根路径("http://host:port/")。
|
||||
func validHealthURL(raw string) bool {
|
||||
parsed, err := url.Parse(raw)
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") && parsed.Host != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
lock, err := c.lock()
|
||||
if err != nil {
|
||||
@@ -306,6 +411,14 @@ func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
pending = append(pending, j)
|
||||
}
|
||||
}
|
||||
// 入口形态必须与当前配置一致:拿 gate 配置去收尾一个 no-gate 窗口,
|
||||
// 会凭空"打开"一扇这次根本没关过的门(或反过来把有闸门的窗口当没闸门放掉)。
|
||||
for _, j := range pending {
|
||||
if modeOf(j.IngressMode) != modeOf(c.ingressMode) {
|
||||
return fmt.Errorf("维护窗口 %s 的入口形态(%s)与当前配置(%s)不一致:需人工核对后再恢复",
|
||||
j.Job, modeOf(j.IngressMode), modeOf(c.ingressMode))
|
||||
}
|
||||
}
|
||||
// 内容可能已被写过的窗口不能自动开放:重启恢复只许"收尾确定性的事",
|
||||
// 半写目标必须由原作业接管或人工按预备份恢复,否则就是把半写当成品放行。
|
||||
for _, j := range pending {
|
||||
@@ -320,6 +433,16 @@ func (c *Coordinator) Recover(ctx context.Context) error {
|
||||
if err = c.restore(ctx, &j); err != nil {
|
||||
return err
|
||||
}
|
||||
// no-gate 窗口没有闸门可"重开":目标是否真的回来只能由健康探针证明,
|
||||
// 探针不过就保持非终态,不写 interrupted 冒充收尾完成。
|
||||
if modeOf(j.IngressMode) == IngressModeNone {
|
||||
if c.ingressReady != nil {
|
||||
if readyErr := c.ingressReady(ctx); readyErr != nil {
|
||||
return fmt.Errorf("no-gate 维护窗口 %s 恢复后健康探针未通过:%w", j.Job, readyErr)
|
||||
}
|
||||
}
|
||||
j.HealthVerified = true
|
||||
}
|
||||
j.State = "failed"
|
||||
j.Code = "interrupted"
|
||||
ended := time.Now().UTC()
|
||||
@@ -455,21 +578,52 @@ func parseJournal(data []byte, job, source string) (journal, error) {
|
||||
return journal{}, errors.New("invalid freeze maintenance level")
|
||||
}
|
||||
// 声明了实例级维护就必须留下入口证据;只锁库的窗口不许带入口。
|
||||
if j.Level == "instance" && !validIngress(j.Ingress) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
// no-gate(测试栈)是唯一例外:必须同时带显式确认串、空入口、以及
|
||||
// mode=none 的证据占位,读记录的人能看出"本次没有闸门证据"。
|
||||
mode := modeOf(j.IngressMode)
|
||||
if j.IngressMode != "" && mode != IngressModeGate && mode != IngressModeNone {
|
||||
return journal{}, errors.New("invalid freeze ingress mode")
|
||||
}
|
||||
if j.Level == "library" && (j.Ingress != "" || j.IngressClosed) {
|
||||
return journal{}, errors.New("library scope lock claims ingress control")
|
||||
if mode == IngressModeNone {
|
||||
if j.Level != "instance" || j.IngressAck != NoIngressAck {
|
||||
return journal{}, errors.New("invalid no-gate maintenance evidence")
|
||||
}
|
||||
if j.Ingress != "" || j.IngressClosed || j.GateReopened {
|
||||
return journal{}, errors.New("no-gate maintenance claims ingress control")
|
||||
}
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone || j.Gate.Closed || j.Gate.Drained || j.Gate.Reason == "" {
|
||||
return journal{}, errors.New("invalid no-gate maintenance evidence")
|
||||
}
|
||||
} else {
|
||||
if j.Level == "instance" && !validIngress(j.Ingress) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
if j.Level == "library" && (j.Ingress != "" || j.IngressClosed) {
|
||||
return journal{}, errors.New("library scope lock claims ingress control")
|
||||
}
|
||||
if j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
}
|
||||
if j.Level == "library" && j.IngressMode != "" {
|
||||
return journal{}, errors.New("library scope lock claims ingress mode")
|
||||
}
|
||||
if j.RestoreBlocked && !j.ContentRisk {
|
||||
return journal{}, errors.New("invalid freeze restore block")
|
||||
}
|
||||
if j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
if mode == IngressModeGate && j.Gate != nil && (!j.Gate.Closed || j.Gate.ActiveAtClose < 0 || j.Gate.Served < 0) {
|
||||
return journal{}, errors.New("invalid freeze ingress evidence")
|
||||
}
|
||||
if j.GateReopened && j.Gate == nil {
|
||||
return journal{}, errors.New("ingress reopened without close evidence")
|
||||
}
|
||||
// no-gate 窗口的恢复证据只能是外部健康探针:写了"已验证"就必须有可核验的地址。
|
||||
if j.HealthVerified && !validHealthURL(j.HealthURL) {
|
||||
return journal{}, errors.New("invalid health probe evidence")
|
||||
}
|
||||
if mode == IngressModeNone && j.HealthURL == "" && !terminal(j.State) {
|
||||
return journal{}, errors.New("no-gate maintenance requires a health probe")
|
||||
}
|
||||
if j.BackupSnapshotID != "" && !validDigest(j.BackupSnapshotID) {
|
||||
return journal{}, errors.New("invalid backup snapshot id")
|
||||
}
|
||||
@@ -638,11 +792,13 @@ func (c *Coordinator) restore(ctx context.Context, j *journal) error {
|
||||
// 注意:入口关到一半失败(闸门已被停、排空超时)时 j.Gate 可能还是 nil,
|
||||
// 但入口确实已经不在了。所以实例级维护一律重新打开入口,而不是只看
|
||||
// "有没有成功关闭的证据"——否则会出现"维护已释放、服务在跑、入口却是关的"。
|
||||
// 但只要**任何一个写入者没回到健康**,这里的 else 分支就不执行:入口保持关闭,
|
||||
// 但**任何写入者没回到健康**,这里的 else 分支就不执行:入口保持关闭,
|
||||
// 由调用方按 held 处理并走恢复(宁可不可用,也不对外暴露没准备好的目标)。
|
||||
// no-gate(测试模式)根本没有闸门:一笔闸门动作都不做,也不写"入口已重开"。
|
||||
switch {
|
||||
case serviceFailures != nil:
|
||||
serviceFailures = errors.Join(serviceFailures, errors.New("目标入口保持关闭:写入者未全部恢复健康"))
|
||||
case modeOf(j.IngressMode) == IngressModeNone:
|
||||
case c.level == "instance" || j.Gate != nil:
|
||||
if c.gate == nil {
|
||||
persistFailures = errors.Join(persistFailures, errors.New("入口闸门缺失:不能确认目标已恢复"))
|
||||
|
||||
@@ -27,10 +27,14 @@ type JournalEvidence struct {
|
||||
State string
|
||||
TargetFingerprint string
|
||||
Ingress string
|
||||
IngressMode string
|
||||
IngressAck string
|
||||
IngressClosed bool
|
||||
DrainVerified bool
|
||||
ContentRisk bool
|
||||
RestoreBlocked bool
|
||||
HealthURL string
|
||||
HealthVerified bool
|
||||
Gate *GateEvidence
|
||||
Quiesce *QuiesceProof
|
||||
Services []JournalService
|
||||
@@ -94,8 +98,10 @@ func ReadJournal(dir, job string) (JournalEvidence, error) {
|
||||
evidence = JournalEvidence{
|
||||
Job: job, Source: j.Source, Level: j.Level, State: j.State,
|
||||
TargetFingerprint: j.TargetFingerprint, Ingress: j.Ingress,
|
||||
IngressMode: modeOf(j.IngressMode), IngressAck: j.IngressAck,
|
||||
IngressClosed: j.IngressClosed, DrainVerified: j.DrainVerified,
|
||||
ContentRisk: j.ContentRisk, RestoreBlocked: j.RestoreBlocked,
|
||||
HealthURL: j.HealthURL, HealthVerified: j.HealthVerified,
|
||||
Gate: j.Gate, Quiesce: j.Quiesce, Services: services,
|
||||
StartedAt: j.StartedAt.UTC(), Digest: hex.EncodeToString(sum[:]),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
package libraryfreeze
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// noGateFixture 是"整栈没有入口闸门"的测试栈:只有写者与队列,
|
||||
// 入口可用性由外部健康探针证明,闸门一个都不装配。
|
||||
func noGateFixture(t *testing.T, probes *int) (string, *exitControl, *Coordinator) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 真实 0.9.0 栈里被代理服务收到 SIGTERM 会 exit(1):no-gate 路径不能靠退出码判排空。
|
||||
c := &exitControl{fixtureControl: &fixtureControl{services: []Service{
|
||||
{ID: "app", Running: true, Healthy: true}}}, exit: map[string]int{"app": 1}}
|
||||
engine, err := Open(root, "target-instance", c)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { engine.Close() })
|
||||
if err := engine.SetIngressMode(IngressModeNone, NoIngressAck); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetHealthURL("http://127.0.0.1:1/health"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetTarget("instance", "", nil, func(context.Context) error {
|
||||
(*probes)++
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetQuiescer(&idleQuiescer{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := engine.SetQuiesceStability(5*time.Millisecond, 200*time.Millisecond); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root, c, engine
|
||||
}
|
||||
|
||||
// 没有显式确认串就不许进 no-gate:确认串必须逐字符相等。
|
||||
func TestNoGateModeRequiresExactAck(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine, err := Open(root, "target-instance", &fixtureControl{services: []Service{{ID: "app", Running: true, Healthy: true}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.Close()
|
||||
for _, ack := range []string{"", "test_only", "TEST-ONLY", " test-only", "test-only "} {
|
||||
if err := engine.SetIngressMode(IngressModeNone, ack); err == nil ||
|
||||
!strings.Contains(err.Error(), "LIBRARY_NO_INGRESS_ACK") {
|
||||
t.Fatalf("ack=%q 没有被拒并点名:%v", ack, err)
|
||||
}
|
||||
}
|
||||
if err := engine.SetIngressMode("no", NoIngressAck); err == nil {
|
||||
t.Fatal("未知入口形态被接受")
|
||||
}
|
||||
// gate 是默认:不调用 SetIngressMode 时仍然是 gate。
|
||||
if err := engine.SetIngressMode(IngressModeGate, ""); err != nil {
|
||||
t.Fatalf("gate 被拒:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 下不许装配闸门:那条路径一笔闸门动作都不能发生。
|
||||
func TestNoGateModeRefusesGateAndIngressClaim(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Chmod(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
engine, err := Open(root, "target-instance", &fixtureControl{services: []Service{{ID: "app", Running: true, Healthy: true}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.Close()
|
||||
if err := engine.SetIngressMode(IngressModeNone, NoIngressAck); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls := []string{}
|
||||
if err := engine.SetGate(&fakeGate{calls: &calls, drain: true, active: 1}); err == nil {
|
||||
t.Fatal("no-gate 模式接受了闸门")
|
||||
}
|
||||
if len(calls) != 0 {
|
||||
t.Fatalf("闸门被调用:%v", calls)
|
||||
}
|
||||
if err := engine.SetTarget("instance", "http://127.0.0.1:1/health", nil, nil); err == nil {
|
||||
t.Fatal("no-gate 模式接受了入口地址")
|
||||
}
|
||||
if err := engine.SetTarget("instance", "", func(context.Context) error { return nil }, nil); err == nil {
|
||||
t.Fatal("no-gate 模式接受了入口关闭探针")
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate + 显式确认:窗口能开、记录写明 ingress_mode=none 与"无闸门"理由、
|
||||
// 恢复后健康探针被调用、闸门一次都没被调用。
|
||||
func TestNoGateWindowRecordsDowngradedEvidence(t *testing.T) {
|
||||
probes := 0
|
||||
root, c, engine := noGateFixture(t, &probes)
|
||||
result, err := engine.RunWindow(context.Background(), "job-nogate", func(context.Context) error { return nil },
|
||||
func(_ context.Context, w *Window) (string, error) {
|
||||
for _, s := range c.services {
|
||||
if s.Running {
|
||||
t.Fatal("work started while a writer was still running")
|
||||
}
|
||||
}
|
||||
// 记一笔内容风险,恢复/收尾也要能在 none 模式下跑完。
|
||||
if err := w.SetBackupSnapshot(context.Background(), strings.Repeat("a", 64)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return strings.Repeat("b", 64), nil
|
||||
}, func(error) bool { return true })
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 窗口失败:%v", err)
|
||||
}
|
||||
if result.State != "succeeded" {
|
||||
t.Fatalf("result=%+v", result)
|
||||
}
|
||||
// 闸门从未被调用:调用记录里只有写者的 stop/start。
|
||||
if got := strings.Join(c.calls, ","); got != "stop:app,start:app" {
|
||||
t.Fatalf("calls=%v", c.calls)
|
||||
}
|
||||
if probes == 0 {
|
||||
t.Fatal("恢复后没有调用健康探针")
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, "job-nogate.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"ingress_mode":"none"`) {
|
||||
t.Fatalf("记录没有 ingress_mode=none:%s", raw)
|
||||
}
|
||||
j, err := engine.load("job-nogate")
|
||||
if err != nil {
|
||||
t.Fatalf("维护记录读不回来:%v(raw=%s)", err, raw)
|
||||
}
|
||||
if j.IngressMode != IngressModeNone || j.IngressAck != NoIngressAck {
|
||||
t.Fatalf("入口形态没写进记录:%+v", j)
|
||||
}
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone || j.Gate.Closed || j.Gate.Drained ||
|
||||
j.Gate.Reason != NoIngressGateReason {
|
||||
t.Fatalf("gate 证据位置不是 no-gate 占位:%+v", j.Gate)
|
||||
}
|
||||
if !j.HealthVerified || j.HealthURL == "" {
|
||||
t.Fatalf("健康探针证据缺失:%+v", j)
|
||||
}
|
||||
if j.GateReopened || j.IngressClosed {
|
||||
t.Fatalf("no-gate 窗口冒充了入口开关:reopened=%v closed=%v", j.GateReopened, j.IngressClosed)
|
||||
}
|
||||
if !j.DrainVerified {
|
||||
t.Fatal("写者全停 + 队列静默没有记为排空证据")
|
||||
}
|
||||
// 原始 JSON 也必须能读出"本次没有闸门证据"。
|
||||
for _, want := range []string{
|
||||
`"ingress_mode":"none"`,
|
||||
`"ingress_ack":"test-only"`,
|
||||
`"gate":{"mode":"none","reason":"stack has no ingress gate (test-only)"}`,
|
||||
} {
|
||||
if !strings.Contains(string(raw), want) {
|
||||
t.Fatalf("记录缺少 %s:%s", want, raw)
|
||||
}
|
||||
}
|
||||
if strings.Contains(string(raw), `"gate_reopened":true`) {
|
||||
t.Fatalf("记录声称重开过闸门:%s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// gate 模式的记录必须照旧写 ingress_mode=gate,且仍然要求真实排空证据。
|
||||
func TestGateWindowRecordsGateMode(t *testing.T) {
|
||||
root, c, engine := instanceFixture(t)
|
||||
gate := &fakeGate{calls: &c.calls, drain: true, active: 1}
|
||||
if err := engine.SetGate(gate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := engine.RunWindow(context.Background(), "job-gate-mode", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return "", nil }, func(error) bool { return true }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, err := os.ReadFile(filepath.Join(root, "job-gate-mode.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"ingress_mode":"gate"`) {
|
||||
t.Fatalf("gate 记录没有写入口形态:%s", raw)
|
||||
}
|
||||
if strings.Contains(string(raw), `"mode":"none"`) {
|
||||
t.Fatalf("gate 记录写成了 no-gate:%s", raw)
|
||||
}
|
||||
// gate 模式的 JSON 仍然是老形状:closed/drained 都在。
|
||||
var decoded struct {
|
||||
Gate map[string]any `json:"gate"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded.Gate["closed"] != true || decoded.Gate["drained"] != true {
|
||||
t.Fatalf("gate 证据被改写:%+v", decoded.Gate)
|
||||
}
|
||||
}
|
||||
|
||||
// no-gate 下健康探针不通过 = 服务没回来:窗口不得报成功,也不许冒充"入口已开"。
|
||||
func TestNoGateWindowFailsWhenHealthProbeNeverRecovers(t *testing.T) {
|
||||
probes := 0
|
||||
_, _, engine := noGateFixture(t, &probes)
|
||||
// 把探针换成永远失败的:模拟写完内容后服务起不来。
|
||||
engine.ingressReady = func(context.Context) error { return errors.New("health 503") }
|
||||
_, err := engine.RunWindow(context.Background(), "job-nogate-sick", func(context.Context) error { return nil },
|
||||
func(context.Context, *Window) (string, error) { return "", nil }, func(error) bool { return true })
|
||||
if err == nil {
|
||||
t.Fatal("健康探针失败却报成功")
|
||||
}
|
||||
j, loadErr := engine.load("job-nogate-sick")
|
||||
if loadErr != nil {
|
||||
t.Fatal(loadErr)
|
||||
}
|
||||
if j.State != "held" || j.Code != "ingress_unverified" {
|
||||
t.Fatalf("状态不是 held/ingress_unverified:state=%s code=%s", j.State, j.Code)
|
||||
}
|
||||
if j.GateReopened {
|
||||
t.Fatal("no-gate 窗口伪造了闸门重开")
|
||||
}
|
||||
}
|
||||
@@ -37,13 +37,19 @@ func (c *Coordinator) RecoverWindow(ctx context.Context, job string, restorer Re
|
||||
}
|
||||
defer unlock(lock)
|
||||
|
||||
if c.level == "instance" && c.gate == nil {
|
||||
if c.level == "instance" && c.gate == nil && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return Result{}, errors.New("实例级恢复缺少入口闸门:不能证明入口已关闭")
|
||||
}
|
||||
j, err := c.load(job)
|
||||
if err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
// 恢复只能用与窗口一致的入口形态:拿 no-gate 去收尾一扇真门,或反向,
|
||||
// 都会假装"这次开过/没开过闸门"。
|
||||
if modeOf(j.IngressMode) != c.ingressModeOrDefault() {
|
||||
return Result{}, fmt.Errorf("维护记录的入口形态(%s)与当前配置(%s)不一致:拒绝恢复",
|
||||
modeOf(j.IngressMode), c.ingressModeOrDefault())
|
||||
}
|
||||
if terminal(j.State) {
|
||||
// 终态重放(F05 路径二):journal 已经是 recovered,说明数据恢复与服务/入口
|
||||
// 恢复都已完成。这时再拒绝会让"journal recovered → PG 维护释放"之间的崩溃
|
||||
@@ -150,6 +156,7 @@ func (c *Coordinator) RecoverWindow(ctx context.Context, job string, restorer Re
|
||||
return c.failRecovery(&j, "ingress_unverified", fmt.Errorf("入口未恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(&j)
|
||||
j.State = "recovered"
|
||||
j.Code = ""
|
||||
j.FinishedAt = &ended
|
||||
|
||||
@@ -103,8 +103,8 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
}
|
||||
defer unlock(lock)
|
||||
// 实例级维护的入口必须由我们关:没有闸门就没有"入口已关"的证据,
|
||||
// 后面拿什么证明后台静默都是空的。
|
||||
if c.level == "instance" && c.gate == nil {
|
||||
// 后面拿什么证明后台静默都是空的。唯一例外是显式确认过的 no-gate 测试栈。
|
||||
if c.level == "instance" && c.gate == nil && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return Result{}, errors.New("实例级维护缺少入口闸门:不能证明入口已关闭")
|
||||
}
|
||||
|
||||
@@ -141,6 +141,7 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
}
|
||||
j = journal{Version: 1, Source: c.source, Job: job, Level: c.level, Ingress: c.ingress,
|
||||
Attempt: attempt, SupersededFrom: superseded,
|
||||
IngressMode: c.ingressModeOrDefault(), IngressAck: c.ingressAck, HealthURL: c.healthURL,
|
||||
TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
for _, s := range items {
|
||||
if s.Running && !s.Healthy {
|
||||
@@ -177,7 +178,8 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
return Result{}, err
|
||||
}
|
||||
j = journal{Version: 1, Source: c.source, Job: job, Level: c.level, Ingress: c.ingress,
|
||||
Attempt: 1, TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
Attempt: 1, IngressMode: c.ingressModeOrDefault(), IngressAck: c.ingressAck, HealthURL: c.healthURL,
|
||||
TargetFingerprint: c.fingerprint, Result: Result{State: "stopping"}, StartedAt: time.Now().UTC()}
|
||||
for _, s := range items {
|
||||
if s.Running && !s.Healthy {
|
||||
return Result{}, errors.New("test writer is not ready")
|
||||
@@ -192,50 +194,98 @@ func (c *Coordinator) RunWindow(ctx context.Context, job string, prepare func(co
|
||||
// 顺序不能换:先把外部任务的生产入口关掉并排空,再证明后台静默,最后才停写入者。
|
||||
// 先证明静默再关入口是错的——排空期间还在跑的在途请求随时会派生新的异步任务,
|
||||
// 那一刻的"空"到停写者时早就失效了;而 app 一旦 SIGTERM 就 exit(1),会把任务腰斩。
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
evidence, gateErr := c.gate.CloseIngress(ctx)
|
||||
if gateErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("关闭目标入口失败:%w", gateErr), "gate_failed")
|
||||
//
|
||||
// no-gate(测试栈)没有闸门可关:顺序改成"先停写者 → 再证明队列排空 → 导出",
|
||||
// 并且一笔闸门动作都不做;gate 证据位置只留 mode=none 的说明,不冒充排空。
|
||||
mode := modeOf(j.IngressMode)
|
||||
if mode != c.ingressModeOrDefault() {
|
||||
return Result{}, fmt.Errorf("维护记录与当前配置的入口形态不一致(记录 %s,配置 %s),拒绝接管",
|
||||
mode, c.ingressModeOrDefault())
|
||||
}
|
||||
if mode == IngressModeNone {
|
||||
if j.Gate == nil || j.Gate.Mode != IngressModeNone {
|
||||
evidence := GateEvidence{Mode: IngressModeNone, Reason: NoIngressGateReason}
|
||||
j.Gate = &evidence
|
||||
}
|
||||
if !evidence.Closed || !evidence.Drained {
|
||||
return c.abandonWindow(ctx, &j, errors.New("目标入口未确认排空(仍有在途请求)"), "gate_not_drained")
|
||||
if j.IngressAck == "" {
|
||||
j.IngressAck = c.ingressAck
|
||||
}
|
||||
if j.HealthURL == "" {
|
||||
j.HealthURL = c.healthURL
|
||||
}
|
||||
j.Gate = &evidence
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 入口关了才谈得上"稳定切点":连续为空 + 跨过稳定窗口 + 拿得到任务指纹。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
|
||||
// 停写入者:任何一步不成立都不许往下写。
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 停完再复核一次:入队/派发可能在"证明静默"和"停写者"之间发生。
|
||||
// 队列仍有任务说明有任务被截断——这比同步失败严重,按切点不一致处理,绝不往下写。
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
if c.ingressClosed != nil {
|
||||
if stopErr := c.ingressClosed(ctx); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("目标入口仍然可用:%w", stopErr), "ingress_open")
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 写者全停之后队列必须一直为空:这一刻的"空"就是这次窗口的排空证据。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
// 没有闸门排空证据,也没有退出码可作凭据:排空只由"写者全停 + 队列静默"
|
||||
// 共同证明——证据等级降一档,且已写进 ingress_mode=none。
|
||||
j.DrainVerified = true
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
} else {
|
||||
if c.gate != nil && (j.Gate == nil || !j.Gate.Drained) {
|
||||
evidence, gateErr := c.gate.CloseIngress(ctx)
|
||||
if gateErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("关闭目标入口失败:%w", gateErr), "gate_failed")
|
||||
}
|
||||
if !evidence.Closed || !evidence.Drained {
|
||||
return c.abandonWindow(ctx, &j, errors.New("目标入口未确认排空(仍有在途请求)"), "gate_not_drained")
|
||||
}
|
||||
j.Gate = &evidence
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
}
|
||||
|
||||
// 入口关了才谈得上"稳定切点":连续为空 + 跨过稳定窗口 + 拿得到任务指纹。
|
||||
proof, quietErr := c.awaitBackgroundQuiet(ctx, &j)
|
||||
if quietErr != nil {
|
||||
return c.abandonWindow(ctx, &j, quietErr, "background_busy")
|
||||
}
|
||||
|
||||
// 停写入者:任何一步不成立都不许往下写。
|
||||
if stopErr := c.stopAll(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "stop_failed")
|
||||
}
|
||||
if stopErr := c.stopped(ctx, j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "isolation_unverified")
|
||||
}
|
||||
if stopErr := c.verifyDrain(ctx, &j); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "drain_unverified")
|
||||
}
|
||||
// 停完再复核一次:入队/派发可能在"证明静默"和"停写者"之间发生。
|
||||
// 队列仍有任务说明有任务被截断——这比同步失败严重,按切点不一致处理,绝不往下写。
|
||||
if stopErr := c.verifyBackgroundAfterStop(ctx, &j, proof); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, stopErr, "background_truncated")
|
||||
}
|
||||
if c.ingressClosed != nil {
|
||||
if stopErr := c.ingressClosed(ctx); stopErr != nil {
|
||||
return c.abandonWindow(ctx, &j, fmt.Errorf("目标入口仍然可用:%w", stopErr), "ingress_open")
|
||||
}
|
||||
j.IngressClosed = true
|
||||
}
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
j.IngressClosed = true
|
||||
}
|
||||
j.State = "held"
|
||||
if err = c.save(j); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
|
||||
w := &Window{c: c, j: &j}
|
||||
@@ -301,6 +351,7 @@ func (c *Coordinator) abandonWindow(ctx context.Context, j *journal, cause error
|
||||
return j.Result, errors.Join(cause, fmt.Errorf("目标入口没有恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(j)
|
||||
ended := time.Now().UTC()
|
||||
j.State = "failed"
|
||||
j.Code = code
|
||||
@@ -311,6 +362,17 @@ func (c *Coordinator) abandonWindow(ctx context.Context, j *journal, cause error
|
||||
return j.Result, cause
|
||||
}
|
||||
|
||||
// markHealth 记录 no-gate 窗口的恢复证据:只有健康探针真的通过才写 verified。
|
||||
func (c *Coordinator) markHealth(j *journal) {
|
||||
if c.ingressModeOrDefault() != IngressModeNone {
|
||||
return
|
||||
}
|
||||
j.HealthVerified = true
|
||||
if j.HealthURL == "" {
|
||||
j.HealthURL = c.healthURL
|
||||
}
|
||||
}
|
||||
|
||||
// closeWindow:写完之后的收尾。allowRestore=false(半写且回滚失败)时
|
||||
// 服务保持停止、journal 保持非终态,返回的是"目标仍在维护"这个结论。
|
||||
func (c *Coordinator) closeWindow(ctx context.Context, j *journal, cause error, allowRestore bool, snapshotID string) (Result, error) {
|
||||
@@ -348,6 +410,7 @@ func (c *Coordinator) closeWindow(ctx context.Context, j *journal, cause error,
|
||||
return j.Result, errors.Join(cause, fmt.Errorf("目标入口没有恢复:%w", readyErr))
|
||||
}
|
||||
}
|
||||
c.markHealth(j)
|
||||
ended := time.Now().UTC()
|
||||
j.FinishedAt = &ended
|
||||
if cause != nil {
|
||||
@@ -399,7 +462,8 @@ func (c *Coordinator) awaitBackgroundQuiet(ctx context.Context, j *journal) (*Qu
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
if j.Gate == nil || !j.Gate.Drained {
|
||||
// no-gate 窗口没有"入口已关"这个前提:静默证明只对队列负责(写者已全停)。
|
||||
if c.ingressModeOrDefault() != IngressModeNone && (j.Gate == nil || !j.Gate.Drained) {
|
||||
return nil, errors.New("必须先关闭并排空入口,再证明后台静默")
|
||||
}
|
||||
|
||||
@@ -555,13 +619,15 @@ func (c *Coordinator) verifyDrain(ctx context.Context, j *journal) error {
|
||||
return errors.New("test writer still running")
|
||||
}
|
||||
if now.ExitCode == nil {
|
||||
if c.level == "instance" && !gateDrained {
|
||||
// no-gate 窗口:没有闸门排空证据,也不把退出码当唯一凭据
|
||||
//(被代理服务收到 SIGTERM 会 exit(1));排空由"写者全停 + 队列静默"证明。
|
||||
if c.level == "instance" && !gateDrained && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return fmt.Errorf("无法证明 %s 的在途请求已排空(控制器没有退出码)", s.ID)
|
||||
}
|
||||
continue
|
||||
}
|
||||
s.ExitCode = now.ExitCode
|
||||
if *now.ExitCode != 0 && !gateDrained {
|
||||
if *now.ExitCode != 0 && !gateDrained && c.ingressModeOrDefault() != IngressModeNone {
|
||||
return fmt.Errorf("写入者 %s 未优雅退出(exit=%d):在途请求没有完成排空", s.ID, *now.ExitCode)
|
||||
}
|
||||
verified++
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// 目标入口闸门与后台队列的声明名称。
|
||||
@@ -15,10 +17,15 @@ import (
|
||||
// 错的证据。
|
||||
const (
|
||||
EnvInstance = "LIBRARY_TARGET_INSTANCE"
|
||||
EnvIngressMode = "LIBRARY_TARGET_INGRESS_MODE"
|
||||
EnvNoIngressAck = "LIBRARY_NO_INGRESS_ACK"
|
||||
EnvIngressURL = "LIBRARY_TARGET_INGRESS_URL"
|
||||
EnvIngressService = "LIBRARY_TARGET_INGRESS_SERVICE"
|
||||
EnvIngressDrainFile = "LIBRARY_TARGET_INGRESS_DRAIN_FILE"
|
||||
EnvRedisAddr = "LIBRARY_TARGET_REDIS_ADDR"
|
||||
// EnvHealthURL 是 no-gate 窗口唯一的"目标回来了"证据:闸门不存在时,
|
||||
// 只有外部健康探针能证明写入者恢复后真的在服务。
|
||||
EnvHealthURL = "LIBRARY_TARGET_HEALTH_URL"
|
||||
EnvRedisAddr = "LIBRARY_TARGET_REDIS_ADDR"
|
||||
// 名字里不写 Password:这里的字面量是**环境变量名**,密钥扫描会把
|
||||
// `XXX_PASSWORD = "<长字面量>"` 当成"被赋值的密钥",那是误报但没必要留个坑。
|
||||
EnvRedisAuth = "LIBRARY_TARGET_REDIS_PASSWORD"
|
||||
@@ -27,14 +34,20 @@ const (
|
||||
|
||||
// TargetEnv 是一次实例级维护所需的"入口 + 队列"声明。
|
||||
//
|
||||
// 缺任何一项都不许降级成"库级维护":没有闸门就没有"入口已关、在途已排空"
|
||||
// 的证据,没有队列就证明不了停写者时后台是空的。这两条正是被代理的 WeKnora
|
||||
// 缺任何一项都不许降级成"库级维护":没有闸门就没有"入口已关、在途已排空"的
|
||||
// 证据,没有队列就证明不了停写者时后台是空的。这两条正是被代理的 WeKnora
|
||||
// 收到 SIGTERM 就 exit(1) 之后唯一还能证明"请求没被截断"的东西。
|
||||
//
|
||||
// Mode=none 只给"整栈没有入口闸门"的测试环境用,且必须带显式确认串:
|
||||
// 那条路径不允许调用任何闸门动作,证据等级降一档并写进维护记录。
|
||||
type TargetEnv struct {
|
||||
Instance string
|
||||
Mode string
|
||||
Ack string
|
||||
IngressURL string
|
||||
IngressService string
|
||||
IngressDrainFile string
|
||||
HealthURL string
|
||||
RedisAddr string
|
||||
RedisPassword string
|
||||
RedisDB int
|
||||
@@ -49,24 +62,59 @@ func TargetFromEnv(get func(string) string) (TargetEnv, error) {
|
||||
value := func(name string) string { return strings.TrimSpace(get(name)) }
|
||||
target := TargetEnv{
|
||||
Instance: value(EnvInstance),
|
||||
Mode: value(EnvIngressMode),
|
||||
IngressURL: value(EnvIngressURL),
|
||||
IngressService: value(EnvIngressService),
|
||||
IngressDrainFile: value(EnvIngressDrainFile),
|
||||
HealthURL: value(EnvHealthURL),
|
||||
RedisAddr: value(EnvRedisAddr),
|
||||
// 口令不 Trim:前后空格也是口令的一部分,Trim 会把能连上的配置变成连不上。
|
||||
RedisPassword: get(EnvRedisAuth),
|
||||
}
|
||||
missing := make([]string, 0, 5)
|
||||
for _, item := range []struct {
|
||||
if target.Mode == "" {
|
||||
target.Mode = libraryfreeze.IngressModeGate
|
||||
}
|
||||
switch target.Mode {
|
||||
case libraryfreeze.IngressModeGate:
|
||||
target.Ack = ""
|
||||
case libraryfreeze.IngressModeNone:
|
||||
// 确认串逐字符相等:不 Trim、不忽略大小写。没写对就是没确认。
|
||||
ack := get(EnvNoIngressAck)
|
||||
if ack != libraryfreeze.NoIngressAck {
|
||||
return TargetEnv{}, fmt.Errorf(
|
||||
"no-gate 维护必须显式确认:%s 必须逐字符等于 %s(当前%s),拒绝开窗口",
|
||||
EnvNoIngressAck, libraryfreeze.NoIngressAck, describeAck(ack))
|
||||
}
|
||||
target.Ack = ack
|
||||
// 半套闸门声明最危险:既关不掉门,又容易被当成"有门"。要么有,要么明确没有。
|
||||
if target.IngressURL != "" || target.IngressService != "" || target.IngressDrainFile != "" {
|
||||
return TargetEnv{}, fmt.Errorf(
|
||||
"no-gate 维护不得同时声明 %s/%s/%s:要么有闸门,要么明确没有",
|
||||
EnvIngressURL, EnvIngressService, EnvIngressDrainFile)
|
||||
}
|
||||
default:
|
||||
return TargetEnv{}, fmt.Errorf("%s 无效(%q):只能是 %s 或 %s",
|
||||
EnvIngressMode, target.Mode, libraryfreeze.IngressModeGate, libraryfreeze.IngressModeNone)
|
||||
}
|
||||
|
||||
required := []struct {
|
||||
name string
|
||||
current string
|
||||
}{
|
||||
{EnvInstance, target.Instance},
|
||||
{EnvIngressURL, target.IngressURL},
|
||||
{EnvIngressService, target.IngressService},
|
||||
{EnvIngressDrainFile, target.IngressDrainFile},
|
||||
{EnvRedisAddr, target.RedisAddr},
|
||||
} {
|
||||
}
|
||||
if target.Mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门可声明:改由健康探针证明"目标真的回来了",它因此是必需的。
|
||||
required = append(required, struct{ name, current string }{EnvHealthURL, target.HealthURL})
|
||||
} else {
|
||||
required = append(required,
|
||||
struct{ name, current string }{EnvIngressURL, target.IngressURL},
|
||||
struct{ name, current string }{EnvIngressService, target.IngressService},
|
||||
struct{ name, current string }{EnvIngressDrainFile, target.IngressDrainFile})
|
||||
}
|
||||
missing := make([]string, 0, 5)
|
||||
for _, item := range required {
|
||||
if item.current == "" {
|
||||
missing = append(missing, item.name)
|
||||
}
|
||||
@@ -86,8 +134,25 @@ func TargetFromEnv(get func(string) string) (TargetEnv, error) {
|
||||
target.RedisDB = db
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
return TargetEnv{}, fmt.Errorf("实例级维护未接线:缺少 %s(没有入口闸门与队列就无法证明入口已排空、后台已静默)",
|
||||
strings.Join(missing, "、"))
|
||||
hint := "没有入口闸门与队列就无法证明入口已排空、后台已静默"
|
||||
if target.Mode == libraryfreeze.IngressModeNone {
|
||||
hint = "no-gate 测试模式仍需要实例、队列与健康探针"
|
||||
}
|
||||
return TargetEnv{}, fmt.Errorf("实例级维护未接线:缺少 %s(%s)",
|
||||
strings.Join(missing, "、"), hint)
|
||||
}
|
||||
return target, nil
|
||||
}
|
||||
|
||||
// describeAck 只描述"确认串有没有写对",不回显内容:写错时也可能是一段
|
||||
// 看起来像凭据的东西,错误信息里没必要复制它。
|
||||
func describeAck(ack string) string {
|
||||
switch {
|
||||
case ack == "":
|
||||
return "为空"
|
||||
case strings.TrimSpace(ack) == libraryfreeze.NoIngressAck:
|
||||
return "含多余空白"
|
||||
default:
|
||||
return "取值不匹配"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package librarymaintenance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
)
|
||||
|
||||
// noGateEnv 是 0.9.0 测试栈的声明:没有闸门,只有实例、队列与健康探针。
|
||||
func noGateEnv() map[string]string {
|
||||
return map[string]string{
|
||||
EnvInstance: "kgmaint-v090",
|
||||
EnvIngressMode: libraryfreeze.IngressModeNone,
|
||||
EnvNoIngressAck: libraryfreeze.NoIngressAck,
|
||||
EnvHealthURL: "http://127.0.0.1:18081/health",
|
||||
EnvRedisAddr: "127.0.0.1:26379",
|
||||
EnvRedisAuth: "secret",
|
||||
EnvRedisDB: "0",
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvNoGateNeedsExplicitAck(t *testing.T) {
|
||||
values := noGateEnv()
|
||||
delete(values, EnvNoIngressAck)
|
||||
_, err := TargetFromEnv(lookup(values))
|
||||
if err == nil || !strings.Contains(err.Error(), EnvNoIngressAck) {
|
||||
t.Fatalf("缺确认串没有被拒并点名:%v", err)
|
||||
}
|
||||
for _, ack := range []string{"", "test_only", "TEST-ONLY", " test-only", "test-only "} {
|
||||
values := noGateEnv()
|
||||
values[EnvNoIngressAck] = ack
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvNoIngressAck) {
|
||||
t.Fatalf("ack=%q 没有被拒:%v", ack, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvNoGateAcceptsMissingIngressItems(t *testing.T) {
|
||||
target, err := TargetFromEnv(lookup(noGateEnv()))
|
||||
if err != nil {
|
||||
t.Fatalf("no-gate 声明被拒:%v", err)
|
||||
}
|
||||
if target.Mode != libraryfreeze.IngressModeNone || target.Ack != libraryfreeze.NoIngressAck {
|
||||
t.Fatalf("入口形态没解析对:%+v", target)
|
||||
}
|
||||
if target.IngressURL != "" || target.IngressService != "" || target.IngressDrainFile != "" {
|
||||
t.Fatalf("no-gate 声明里出现了闸门项:%+v", target)
|
||||
}
|
||||
if target.HealthURL != "http://127.0.0.1:18081/health" || target.RedisDB != 0 {
|
||||
t.Fatalf("健康探针/队列没解析对:%+v", target)
|
||||
}
|
||||
}
|
||||
|
||||
// 证据降一档不等于什么都不需要:健康探针与队列仍必须给。
|
||||
func TestTargetFromEnvNoGateStillNeedsHealthAndQueue(t *testing.T) {
|
||||
for _, name := range []string{EnvInstance, EnvHealthURL, EnvRedisAddr, EnvRedisAuth, EnvRedisDB} {
|
||||
values := noGateEnv()
|
||||
delete(values, name)
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), name) {
|
||||
t.Fatalf("缺 %s 没有被点名:%v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 缺省仍是 gate:没有 mode 变量时照旧要求三项闸门声明(原行为不变)。
|
||||
func TestTargetFromEnvDefaultsToGate(t *testing.T) {
|
||||
values := completeEnv()
|
||||
target, err := TargetFromEnv(lookup(values))
|
||||
if err != nil {
|
||||
t.Fatalf("gate 声明被拒:%v", err)
|
||||
}
|
||||
if target.Mode != libraryfreeze.IngressModeGate || target.Ack != "" {
|
||||
t.Fatalf("缺省不是 gate:%+v", target)
|
||||
}
|
||||
values[EnvIngressURL] = ""
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvIngressURL) {
|
||||
t.Fatalf("gate 模式缺入口没有被拒:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTargetFromEnvRejectsUnknownMode(t *testing.T) {
|
||||
values := completeEnv()
|
||||
values[EnvIngressMode] = "open"
|
||||
if _, err := TargetFromEnv(lookup(values)); err == nil || !strings.Contains(err.Error(), EnvIngressMode) {
|
||||
t.Fatalf("未知入口形态没有被拒:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -48,14 +48,20 @@ type Config struct {
|
||||
Writers []string
|
||||
NonWriters map[string]string
|
||||
DockerSocket string
|
||||
IngressURL string
|
||||
// IngressMode 是 gate(默认)或 none;none 只给整栈没有入口闸门的测试环境用,
|
||||
// 必须同时给 IngressAck,且一次闸门动作都不做。
|
||||
IngressMode string
|
||||
IngressAck string
|
||||
IngressURL string
|
||||
// IngressService 是入口闸门在 compose 项目里的服务名;DrainFile 是它写
|
||||
// 排空证据的宿主机文件。两者缺一,就只能说"停了写入者",不能说"排空了"。
|
||||
IngressService string
|
||||
IngressDrainFile string
|
||||
Database string
|
||||
Index string
|
||||
Bucket string
|
||||
// HealthURL 是 no-gate 模式下唯一的"目标回来了"证据(恢复后必须 200)。
|
||||
HealthURL string
|
||||
Database string
|
||||
Index string
|
||||
Bucket string
|
||||
// 后台静默检查需要的运行时依赖(不写进维护记录)。
|
||||
DatabaseURL string
|
||||
RedisAddr string
|
||||
@@ -75,6 +81,9 @@ type Manager struct {
|
||||
gate *dockerGate
|
||||
api *dockerAPI
|
||||
quiescer *targetQuiescer
|
||||
mode string
|
||||
ack string
|
||||
healthURL string
|
||||
}
|
||||
|
||||
// Close 释放拓扑/静默检查持有的连接。
|
||||
@@ -91,6 +100,11 @@ func New(cfg Config) (*Manager, error) {
|
||||
instance := strings.TrimSpace(cfg.Instance)
|
||||
project := strings.TrimSpace(cfg.Project)
|
||||
ingress := strings.TrimSpace(cfg.IngressURL)
|
||||
health := strings.TrimSpace(cfg.HealthURL)
|
||||
mode := strings.TrimSpace(cfg.IngressMode)
|
||||
if mode == "" {
|
||||
mode = libraryfreeze.IngressModeGate
|
||||
}
|
||||
if instance == "" || project == "" {
|
||||
return nil, errors.New("实例级维护配置缺少 LIBRARY_TARGET_INSTANCE/PROJECT")
|
||||
}
|
||||
@@ -100,9 +114,27 @@ func New(cfg Config) (*Manager, error) {
|
||||
if !filepath.IsAbs(cfg.DockerSocket) {
|
||||
return nil, errors.New("Docker socket 必须是绝对路径")
|
||||
}
|
||||
parsed, err := url.Parse(ingress)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" || parsed.Path == "" || parsed.User != nil {
|
||||
return nil, errors.New("目标入口地址无效(LIBRARY_TARGET_INGRESS_URL)")
|
||||
switch mode {
|
||||
case libraryfreeze.IngressModeGate:
|
||||
parsed, err := url.Parse(ingress)
|
||||
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" || parsed.Path == "" || parsed.User != nil {
|
||||
return nil, errors.New("目标入口地址无效(LIBRARY_TARGET_INGRESS_URL)")
|
||||
}
|
||||
case libraryfreeze.IngressModeNone:
|
||||
// 显式确认是这道口子的唯一开关:没有它,no-gate 一律拒绝。
|
||||
if cfg.IngressAck != libraryfreeze.NoIngressAck {
|
||||
return nil, fmt.Errorf("no-gate 维护必须显式确认(LIBRARY_NO_INGRESS_ACK 逐字符等于 %s),拒绝",
|
||||
libraryfreeze.NoIngressAck)
|
||||
}
|
||||
if ingress != "" || cfg.IngressService != "" || cfg.IngressDrainFile != "" {
|
||||
return nil, errors.New("no-gate 维护不得同时声明入口地址/闸门服务/排空文件:要么有闸门,要么明确没有")
|
||||
}
|
||||
if !validHealthURL(health) {
|
||||
return nil, errors.New("no-gate 维护必须给有效的健康探针(LIBRARY_TARGET_HEALTH_URL)")
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("LIBRARY_TARGET_INGRESS_MODE 无效(%q):只能是 %s 或 %s",
|
||||
mode, libraryfreeze.IngressModeGate, libraryfreeze.IngressModeNone)
|
||||
}
|
||||
if cfg.Database == "" || cfg.Index == "" || cfg.Bucket == "" {
|
||||
return nil, errors.New("目标身份不完整:需要数据库/索引/存储桶")
|
||||
@@ -125,8 +157,9 @@ func New(cfg Config) (*Manager, error) {
|
||||
}
|
||||
manager := &Manager{target: target, source: project, docker: cfg.DockerSocket,
|
||||
writers: writers, nonWriters: cfg.NonWriters, timeout: cfg.Timeout,
|
||||
client: &http.Client{Timeout: 5 * time.Second}, api: api}
|
||||
if cfg.IngressService != "" || cfg.IngressDrainFile != "" {
|
||||
client: &http.Client{Timeout: 5 * time.Second}, api: api,
|
||||
mode: mode, ack: cfg.IngressAck, healthURL: health}
|
||||
if mode == libraryfreeze.IngressModeGate && (cfg.IngressService != "" || cfg.IngressDrainFile != "") {
|
||||
gate, err := newDockerGate(cfg.DockerSocket, project, cfg.IngressService, cfg.IngressDrainFile, cfg.Timeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -168,8 +201,12 @@ func (m *Manager) Target() Target { return m.target }
|
||||
// Description 是写进维护记录的一句话身份,含指纹,便于事后核对"当时动的是谁"。
|
||||
func (m *Manager) Description() string {
|
||||
t := m.target
|
||||
ingress := t.Ingress
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
ingress = "none(test-only)"
|
||||
}
|
||||
return fmt.Sprintf("level=%s instance=%s project=%s writers=%s ingress=%s db=%s index=%s bucket=%s fingerprint=%s",
|
||||
t.Level, t.Instance, t.Project, strings.Join(t.Writers, "+"), t.Ingress, t.Database, t.Index, t.Bucket, t.Fingerprint)
|
||||
t.Level, t.Instance, t.Project, strings.Join(t.Writers, "+"), ingress, t.Database, t.Index, t.Bucket, t.Fingerprint)
|
||||
}
|
||||
|
||||
// Open 返回绑定到该目标的协调器与控制器。调用方负责 Close。
|
||||
@@ -183,7 +220,23 @@ func (m *Manager) Open(root string) (*libraryfreeze.Coordinator, *libraryfreeze.
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := coordinator.SetTarget(LevelInstance, m.target.Ingress, m.ingressClosed, m.ingressReady); err != nil {
|
||||
// 入口形态必须在绑定目标之前声明:default 是 gate,只有显式 no-gate 才降档。
|
||||
if err := coordinator.SetIngressMode(m.mode, m.ack); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
closed, ready := m.ingressClosed, m.ingressReady
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门:不声明任何入口关闭探针,恢复证据改由健康探针承担。
|
||||
closed, ready = nil, m.healthReady
|
||||
if err := coordinator.SetHealthURL(m.healthURL); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
if err := coordinator.SetTarget(LevelInstance, m.target.Ingress, closed, ready); err != nil {
|
||||
coordinator.Close()
|
||||
control.Close()
|
||||
return nil, nil, err
|
||||
@@ -239,7 +292,11 @@ func (m *Manager) serviceOf(id string) string {
|
||||
}
|
||||
|
||||
// VerifyRunning 确认目标入口真的在服务(部署与恢复后都要看,不看容器状态)。
|
||||
// no-gate 栈没有入口:用健康探针回答"目标回来了没有"。
|
||||
func (m *Manager) VerifyRunning(ctx context.Context) error {
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
return m.healthReady(ctx)
|
||||
}
|
||||
return m.ingressReady(ctx)
|
||||
}
|
||||
|
||||
@@ -308,6 +365,47 @@ func (m *Manager) ingressReady(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
// healthReady 是 no-gate 栈的恢复判据:健康探针返回 200 才算"目标真的回来了"。
|
||||
// 和入口探针同一条规则——不看容器状态、不看 HTTP 5xx 的"像活着"。
|
||||
func (m *Manager) healthReady(ctx context.Context) error {
|
||||
deadline := time.Now().Add(m.timeout)
|
||||
var last error
|
||||
for {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.healthURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 探针不能留下 keep-alive 连接:那会让"排空"永远等不到。
|
||||
req.Close = true
|
||||
resp, err := m.client.Do(req)
|
||||
if err == nil {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
last = fmt.Errorf("健康探针返回 HTTP %d:%s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
} else {
|
||||
last = err
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("目标健康探针没有恢复:%w", last)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return last
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// validHealthURL 是健康探针地址的本地校验:http(s)、有主机、不带凭据。
|
||||
func validHealthURL(raw string) bool {
|
||||
parsed, err := url.Parse(strings.TrimSpace(raw))
|
||||
return err == nil && (parsed.Scheme == "http" || parsed.Scheme == "https") &&
|
||||
parsed.Host != "" && parsed.User == nil
|
||||
}
|
||||
|
||||
// DatabaseIdentity 把连接串收敛成"host:port/db"这种可以写进维护记录的身份:
|
||||
// 维护记录里不落库凭据,但必须能说清动的是哪个库。
|
||||
func DatabaseIdentity(raw string) (string, error) {
|
||||
@@ -354,12 +452,21 @@ func (m *Manager) VerifyTopology(ctx context.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// no-gate 栈没有闸门可绕:写入者发布端口是它的正常对外形态,
|
||||
// "绕闸直连"这条判据在这里不成立(有闸门时才必须唯一入口)。
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
continue
|
||||
}
|
||||
if ports := state.publishedPorts(); len(ports) > 0 {
|
||||
return fmt.Errorf("写入者 %s 直接发布了端口 %s:存在绕开入口闸门的直连通道,拒绝开维护窗口",
|
||||
writer, strings.Join(ports, ","))
|
||||
}
|
||||
}
|
||||
if m.gate == nil {
|
||||
if m.mode == libraryfreeze.IngressModeNone {
|
||||
// 没有闸门可核验端口归属:拓扑检查到此为止,证据降档由维护记录承担。
|
||||
return nil
|
||||
}
|
||||
return errors.New("实例级维护必须声明入口闸门")
|
||||
}
|
||||
for _, writer := range m.writers {
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
// Package libraryprofile 声明"某个 WeKnora schema 版本下,整库适配器可以依赖什么"。
|
||||
//
|
||||
// 为什么要有这一层:整库导出/导入原本把 `version != 74` 写死在四个入口里。
|
||||
// 版本号本身不是契约——同一版本号在不同部署上也可能缺表;反过来,把判定放宽成
|
||||
// `version > 74` 又会把"没见过的版本"当成"兼容"。所以这里同时声明两件事:
|
||||
// - 该版本必须存在的表与列(导出/导入按它取数);
|
||||
// - 该版本的归档部件集合(manifest 按它判定完整性)。
|
||||
//
|
||||
// 版本号只用来**选中** profile,真正的放行条件是随后对 information_schema 的
|
||||
// 能力探测(见 releaseadapter 的 ResolveSchemaProfile)。
|
||||
package libraryprofile
|
||||
|
||||
import "fmt"
|
||||
|
||||
// LegacyVersion 是 0.7.1 的 schema_migrations 版本号,也是既有内存夹具的默认值。
|
||||
const LegacyVersion = 74
|
||||
|
||||
// V090Version 是 0.9.0 的 schema_migrations 版本号(已实测:134、dirty=false、83 表)。
|
||||
const V090Version = 134
|
||||
|
||||
// PartRequirement 是归档部件的一项要求(kind/name 与 manifest 的部件键一致)。
|
||||
type PartRequirement struct {
|
||||
Kind string
|
||||
Name string
|
||||
}
|
||||
|
||||
// Profile 是一个 schema 版本的静态能力声明。
|
||||
type Profile struct {
|
||||
Name string
|
||||
SchemaVersion int
|
||||
WeKnoraVersion string
|
||||
// EmbeddingDimension 为 0 表示"由目标 models.parameters 说了算";
|
||||
// 非 0 表示该版本只接受这一定维度(0.7.1 的契约就是固定 1024)。
|
||||
EmbeddingDimension int
|
||||
RequiredTables []string
|
||||
OptionalTables []string
|
||||
RequiredColumns map[string][]string
|
||||
// ReviewedTriggers 是该版本允许出现的 pg_get_triggerdef 原文。
|
||||
// 触发器意味着"写一行还会改别的表",所以只接受逐字核对过的定义,
|
||||
// 且这些定义必须在该版本上真实存在(消失也说明 schema 不是我们审过的那份)。
|
||||
ReviewedTriggers []string
|
||||
RequiredParts []PartRequirement
|
||||
OptionalParts []PartRequirement
|
||||
// IndexContractSource 说明该版本的索引名从哪里读。空值等于"读不到",
|
||||
// 不允许回退成空串去匹配任何索引。
|
||||
IndexContractSource string
|
||||
// IndexTenantField 表示该版本的索引文档带 tenant_id。0.9.0 的分块文档只有
|
||||
// knowledge_base_id,写索引时补 tenant_id 会与目标实际文档不一致。
|
||||
IndexTenantField bool
|
||||
}
|
||||
|
||||
var profiles = map[int]Profile{
|
||||
LegacyVersion: {
|
||||
Name: "v071",
|
||||
SchemaVersion: LegacyVersion,
|
||||
WeKnoraVersion: "v0.7.1",
|
||||
EmbeddingDimension: 1024,
|
||||
RequiredTables: []string{
|
||||
"knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations",
|
||||
"resources", "resource_bindings", "wiki_folders", "wiki_pages",
|
||||
"wiki_page_issues", "wiki_log_entries",
|
||||
},
|
||||
RequiredColumns: map[string][]string{
|
||||
"knowledges": {"id", "tenant_id", "knowledge_base_id", "deleted_at", "parse_status"},
|
||||
"chunks": {"id", "tenant_id", "knowledge_base_id", "knowledge_id", "content"},
|
||||
"knowledge_tags": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"knowledge_tag_relations": {"knowledge_id", "tag_id"},
|
||||
"resources": {"id", "handle", "tenant_id", "physical_path"},
|
||||
"resource_bindings": {"id", "resource_id", "tenant_id", "owner_type", "owner_id"},
|
||||
"wiki_pages": {"id", "tenant_id", "knowledge_base_id", "slug"},
|
||||
"wiki_folders": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_page_issues": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_log_entries": {"id", "tenant_id", "knowledge_base_id"},
|
||||
},
|
||||
RequiredParts: []PartRequirement{
|
||||
{"database", "knowledges"}, {"database", "chunks"},
|
||||
{"database", "knowledge_tags"}, {"database", "knowledge_tag_relations"},
|
||||
{"database", "resources"}, {"database", "resource_bindings"},
|
||||
{"wiki", "wiki_folders"}, {"wiki", "wiki_pages"},
|
||||
{"wiki", "wiki_page_issues"}, {"wiki", "wiki_log_entries"},
|
||||
{"index", "chunks"},
|
||||
},
|
||||
IndexContractSource: "vector_stores.index_config.index_name",
|
||||
IndexTenantField: true,
|
||||
},
|
||||
V090Version: {
|
||||
Name: "v090",
|
||||
SchemaVersion: V090Version,
|
||||
WeKnoraVersion: "v0.9.0",
|
||||
// 0.9.0 的 KB 不写 vector_store_id,维度只能从 models.parameters 读。
|
||||
EmbeddingDimension: 0,
|
||||
RequiredTables: []string{
|
||||
"knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations",
|
||||
"resources", "resource_bindings", "wiki_folders", "wiki_pages",
|
||||
"wiki_page_issues",
|
||||
},
|
||||
// 0.9.0 删掉了 wiki_log_entries;部件集合随之为空,不许假装还有。
|
||||
OptionalTables: []string{"wiki_log_entries"},
|
||||
RequiredColumns: map[string][]string{
|
||||
"knowledges": {"id", "tenant_id", "knowledge_base_id", "deleted_at", "parse_status"},
|
||||
"chunks": {"id", "tenant_id", "knowledge_base_id", "knowledge_id", "content", "seq_id", "content_revision", "index_status"},
|
||||
"knowledge_tags": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"knowledge_tag_relations": {"knowledge_id", "tag_id"},
|
||||
"resources": {"id", "handle", "tenant_id", "physical_path"},
|
||||
"resource_bindings": {"id", "resource_id", "tenant_id", "owner_type", "owner_id"},
|
||||
"wiki_pages": {"id", "tenant_id", "knowledge_base_id", "slug"},
|
||||
"wiki_folders": {"id", "tenant_id", "knowledge_base_id"},
|
||||
"wiki_page_issues": {"id", "tenant_id", "knowledge_base_id"},
|
||||
},
|
||||
RequiredParts: []PartRequirement{
|
||||
{"database", "knowledges"}, {"database", "chunks"},
|
||||
{"database", "knowledge_tags"}, {"database", "knowledge_tag_relations"},
|
||||
{"database", "resources"}, {"database", "resource_bindings"},
|
||||
{"wiki", "wiki_folders"}, {"wiki", "wiki_pages"}, {"wiki", "wiki_page_issues"},
|
||||
{"index", "chunks"},
|
||||
},
|
||||
OptionalParts: []PartRequirement{
|
||||
{"wiki", "wiki_log_entries"},
|
||||
},
|
||||
// 0.9.0 的索引名来自部署配置(ELASTICSEARCH_INDEX),不是 vector_stores。
|
||||
// 0.9.0 在 chunks 上挂了图片同步触发器(仅当 image_info 以 '[' 开头时触发),
|
||||
// 它维护 chunk_images 派生表;定义逐字核对,任何改动都拒绝。
|
||||
ReviewedTriggers: []string{
|
||||
`CREATE TRIGGER trg_chunk_images_delete AFTER DELETE ON public.chunks FOR EACH ROW WHEN ((old.image_info ~~ '[%'::text)) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
`CREATE TRIGGER trg_chunk_images_insert AFTER INSERT ON public.chunks FOR EACH ROW WHEN ((new.image_info ~~ '[%'::text)) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
`CREATE TRIGGER trg_chunk_images_update AFTER UPDATE ON public.chunks FOR EACH ROW WHEN ((((old.image_info ~~ '[%'::text) OR (new.image_info ~~ '[%'::text)) AND ((old.image_info IS DISTINCT FROM new.image_info) OR (old.is_enabled IS DISTINCT FROM new.is_enabled) OR (old.status IS DISTINCT FROM new.status) OR (old.deleted_at IS DISTINCT FROM new.deleted_at) OR (old.tenant_id IS DISTINCT FROM new.tenant_id) OR ((old.knowledge_base_id)::text IS DISTINCT FROM (new.knowledge_base_id)::text) OR ((old.knowledge_id)::text IS DISTINCT FROM (new.knowledge_id)::text) OR ((old.chunk_type)::text IS DISTINCT FROM (new.chunk_type)::text) OR (old.created_at IS DISTINCT FROM new.created_at) OR (old.updated_at IS DISTINCT FROM new.updated_at)))) EXECUTE FUNCTION chunk_images_sync()`,
|
||||
},
|
||||
IndexContractSource: "config ELASTICSEARCH_INDEX",
|
||||
IndexTenantField: false,
|
||||
},
|
||||
}
|
||||
|
||||
// For 按版本号选中 profile。未知版本一律失败:版本号是"选哪份声明"的索引,
|
||||
// 不是"能不能放行"的依据,所以绝不返回默认 profile。
|
||||
func For(version int) (Profile, error) {
|
||||
profile, ok := profiles[version]
|
||||
if !ok {
|
||||
return Profile{}, fmt.Errorf("unsupported WeKnora schema version %d: supported profiles are 74 (v071) and 134 (v090)", version)
|
||||
}
|
||||
return profile, nil
|
||||
}
|
||||
|
||||
// Supported 返回已声明 profile 的版本号(用于文档与错误信息,不用于放行)。
|
||||
func Supported() []int {
|
||||
return []int{LegacyVersion, V090Version}
|
||||
}
|
||||
|
||||
// TableSet 是"可能出现的表"集合:必需表 + 可选表。
|
||||
func (p Profile) TableSet() map[string]bool {
|
||||
set := make(map[string]bool, len(p.RequiredTables)+len(p.OptionalTables))
|
||||
for _, name := range p.RequiredTables {
|
||||
set[name] = true
|
||||
}
|
||||
for _, name := range p.OptionalTables {
|
||||
set[name] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// HasTable 报告某张表是否属于该 profile(必需或可选)。
|
||||
func (p Profile) HasTable(name string) bool {
|
||||
return p.TableSet()[name]
|
||||
}
|
||||
|
||||
// IsRequiredTable 报告某张表是否必需存在。
|
||||
func (p Profile) IsRequiredTable(name string) bool {
|
||||
for _, candidate := range p.RequiredTables {
|
||||
if candidate == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PartKeys 返回 (必需, 可选) 的归档部件键,形如 "wiki/wiki_log_entries"。
|
||||
func (p Profile) PartKeys() ([]string, []string) {
|
||||
key := func(part PartRequirement) string { return part.Kind + "/" + part.Name }
|
||||
required := make([]string, 0, len(p.RequiredParts))
|
||||
for _, part := range p.RequiredParts {
|
||||
required = append(required, key(part))
|
||||
}
|
||||
optional := make([]string, 0, len(p.OptionalParts))
|
||||
for _, part := range p.OptionalParts {
|
||||
optional = append(optional, key(part))
|
||||
}
|
||||
return required, optional
|
||||
}
|
||||
|
||||
// RequiresPart 报告某部件键是否必需(可选部件返回 false)。
|
||||
func (p Profile) RequiresPart(key string) bool {
|
||||
required, _ := p.PartKeys()
|
||||
for _, candidate := range required {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AllowsPart 报告某部件键是否至少允许出现。
|
||||
func (p Profile) AllowsPart(key string) bool {
|
||||
required, optional := p.PartKeys()
|
||||
for _, candidate := range required {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, candidate := range optional {
|
||||
if candidate == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -31,8 +31,9 @@ const RestoreDispatchActivationMaxAge = 2 * time.Hour
|
||||
|
||||
// 证据文档 schema 版本与语义兼容批次是两件事:
|
||||
// - ProtocolVersion:这份 JSON 的字段形状(解析契约);
|
||||
// - ProtocolEpoch:派发协议语义批次;只有同一批次的构建才允许共享激活证据
|
||||
// (例如同一批次的兼容重建/打包),不同批次必须重新登记。
|
||||
// - ProtocolEpoch:派发协议语义批次;不同批次必须重新登记。它只约束派发语义,
|
||||
// 不能证明某个构建 SHA 属于该批次 —— 当前构建身份仍精确绑定运行 SHA,跨 SHA
|
||||
// 自动兼容在没有受信批次成员证明前保持关闭。
|
||||
//
|
||||
// 构建身份用完整 40 位 SHA,不用 display:开发号会随提交数变化,两个不同二进制
|
||||
// 完全可能都叫 0.1.322。
|
||||
|
||||
@@ -19,6 +19,12 @@ const WriteSetVersion = 1
|
||||
// MigrationWriteSetVersion rejects reinterpretation by old V1 executors.
|
||||
const MigrationWriteSetVersion = 2
|
||||
|
||||
// WriteSetKindRestoreDrill marks a write set that authorizes only a recovery
|
||||
// drill: canary rows plus a zero-object/zero-index execution plan. It shares
|
||||
// the V2 envelope so the object gateway controller can verify it, but it is
|
||||
// never a publish plan and must not be adopted as one.
|
||||
const WriteSetKindRestoreDrill = "restore-drill"
|
||||
|
||||
// WriteSetRow 是台账里的一行:本次发布明确要写的精确身份 + 写前状态。
|
||||
//
|
||||
// 为什么需要"写前状态":恢复不能在目标里按"没人引用"推断残渣。历史孤儿资源、
|
||||
@@ -89,6 +95,13 @@ type LocalIDMapping struct {
|
||||
}
|
||||
|
||||
type WriteSet struct {
|
||||
// Kind 区分发布执行计划与受限演练恢复计划。空值 = 历史发布计划。
|
||||
// 演练计划必须显式为 restore-drill:它只授权 canary 回放,不含对象/索引
|
||||
// 写项,读取方不能把它当发布计划接管。
|
||||
Kind string `json:"kind,omitempty"`
|
||||
// TargetIDMode 记录这份计划是否把源身份映射成目标隔离身份,以及用哪套算法。
|
||||
// 空值 = 历史计划/写前镜像:按原 ID 回放,绝不能在恢复时二次映射。
|
||||
TargetIDMode string `json:"target_id_mode,omitempty"`
|
||||
Index []IndexWrite `json:"index,omitempty"`
|
||||
Objects []ObjectWrite `json:"objects,omitempty"`
|
||||
SchemaDigest string `json:"schema_digest,omitempty"`
|
||||
@@ -111,6 +124,32 @@ type WriteSet struct {
|
||||
Digest string `json:"digest"`
|
||||
}
|
||||
|
||||
// ValidateRestoreBinding 是恢复身份合同(DS01):写集合必须绑定**本次**预备份、
|
||||
// 同一目标 tenant/KB,且来源快照(要回放到目标的内容身份)与写前镜像不能是同一份。
|
||||
// 它是 ProductionLibraryTarget.Restore 在建任何派发绑定之前调用的同一个判定:
|
||||
// 演练的写前/来源身份也必须过这一关,不能靠调用方约定。
|
||||
func (w WriteSet) ValidateRestoreBinding(snapshotID string, tenantID uint64, knowledgeBaseID string) error {
|
||||
if snapshotID == "" {
|
||||
return errors.New("恢复需要绑定的预备份快照")
|
||||
}
|
||||
if w.BackupSnapshotID != snapshotID {
|
||||
return fmt.Errorf("写集合记录绑定的是预备份 %s,不是 %s", w.BackupSnapshotID, snapshotID)
|
||||
}
|
||||
if w.TenantID != tenantID || w.KnowledgeBaseID != knowledgeBaseID {
|
||||
return fmt.Errorf("写集合记录绑定的是 %d/%s,与目标 %d/%s 不一致",
|
||||
w.TenantID, w.KnowledgeBaseID, tenantID, knowledgeBaseID)
|
||||
}
|
||||
// 派发绑定只能来自原锚点:来源快照是发布基线,backup 是这份写集的写前镜像。
|
||||
// 二者缺失或相同都说明装配没有拿到真实身份,必须在任何写之前失败关闭。
|
||||
if w.TargetSnapshotID == "" || snapshotID == "" {
|
||||
return errors.New("恢复缺少来源快照或写前镜像,拒绝建立派发绑定")
|
||||
}
|
||||
if w.TargetSnapshotID == snapshotID {
|
||||
return errors.New("来源快照与写前镜像不能是同一份快照,拒绝建立派发绑定")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Seal 把行序规范化为可复现的顺序并写入摘要。调用方在落盘与登记之前必须先 Seal。
|
||||
// 排序在这里落地(而不是只在算摘要时排),否则落盘字节与摘要覆盖的内容不一致。
|
||||
func (w *WriteSet) Seal() error {
|
||||
@@ -167,7 +206,20 @@ func (w *WriteSet) computeDigest() (string, error) {
|
||||
|
||||
// Verify 校验摘要与必备身份。缺任一身份都必须失败:没有绑定就没有"这份记录属于
|
||||
// 这个作业/目标/预备份"的证明,拿它去删目标数据无法追责。
|
||||
const (
|
||||
// TargetIDModeScoped 是目标隔离身份的持久契约标识。
|
||||
// 算法是自定义的 sha256(namespace|tenant|kb|table|source) 16 字节派生,
|
||||
// 只保证确定性与隔离,不自称标准 UUIDv5。
|
||||
TargetIDModeScoped = "scoped-sha256-v1"
|
||||
)
|
||||
|
||||
func (w *WriteSet) Verify() error {
|
||||
if w.TargetIDMode != "" && w.TargetIDMode != TargetIDModeScoped {
|
||||
return fmt.Errorf("unsupported target id mode %q", w.TargetIDMode)
|
||||
}
|
||||
if w.TargetIDMode == TargetIDModeScoped && (w.TenantID == 0 || w.KnowledgeBaseID == "") {
|
||||
return errors.New("target id mode lacks target identity")
|
||||
}
|
||||
if w.ContractVersion != 0 && w.ContractVersion != 1 {
|
||||
return errors.New("unsupported migration contract version")
|
||||
}
|
||||
@@ -192,6 +244,22 @@ func (w *WriteSet) Verify() error {
|
||||
if w.Version != WriteSetVersion && w.Version != MigrationWriteSetVersion {
|
||||
return fmt.Errorf("写集合记录版本 %d 不受支持(当前 %d)", w.Version, WriteSetVersion)
|
||||
}
|
||||
if w.Kind != "" && w.Kind != WriteSetKindRestoreDrill {
|
||||
return fmt.Errorf("写集合记录种类 %q 不受支持", w.Kind)
|
||||
}
|
||||
if w.Kind == WriteSetKindRestoreDrill {
|
||||
// 演练恢复计划只回放写前镜像:不新建对象、不按发布计划改索引、不做本地 ID 映射。
|
||||
// 任何一项非空都说明它被当成发布计划构造/改写过,拒绝。
|
||||
if w.Version != MigrationWriteSetVersion || w.ContractVersion != 1 || len(w.SchemaDigest) != 64 {
|
||||
return errors.New("演练恢复计划缺少可执行契约(V2 + contract 1 + schema 摘要)")
|
||||
}
|
||||
if len(w.Objects) != 0 || len(w.Index) != 0 || len(w.LocalIDs) != 0 {
|
||||
return errors.New("演练恢复计划不得包含对象/索引/本地映射写项")
|
||||
}
|
||||
if w.SourceTenantID == 0 || w.SourceKnowledgeBaseID == "" {
|
||||
return errors.New("演练恢复计划缺少来源身份")
|
||||
}
|
||||
}
|
||||
if w.JobID == "" || w.BackupSnapshotID == "" || w.TargetSnapshotID == "" {
|
||||
return errors.New("写集合记录缺少作业/预备份/快照绑定")
|
||||
}
|
||||
@@ -222,6 +290,9 @@ func (w *WriteSet) Verify() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsRestoreDrill 表示这是一份受限演练恢复计划,而不是发布执行计划。
|
||||
func (w WriteSet) IsRestoreDrill() bool { return w.Kind == WriteSetKindRestoreDrill }
|
||||
|
||||
// CreatedIDs 返回"本次新建"的精确身份(写前不存在的行)。恢复只允许删这些。
|
||||
func (w WriteSet) CreatedIDs(table string) []string {
|
||||
ids := make([]string, 0, 8)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"knowledge-governance/internal/releasebundle"
|
||||
"knowledge-governance/internal/libraryprofile"
|
||||
"math"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -420,24 +420,29 @@ func encodeManifest(m Manifest) ([]byte, error) {
|
||||
return nil, errors.New("unsupported snapshot validation policy")
|
||||
}
|
||||
c := m.Compatibility
|
||||
if c.WeKnoraVersion != releasebundle.WeKnoraVersionV071 || c.SchemaVersion != releasebundle.WeKnoraMigrationV071 || c.RetrieverEngine != "elasticsearch_v8" || !identifier(c.EmbeddingModel) || c.EmbeddingDimension != releasebundle.EmbeddingDimensionV071 || !validDigest(c.IndexMappingSHA256) {
|
||||
// 兼容性按 schema 版本选 profile,而不是钉死 v0.7.1:未知版本必须拒绝,
|
||||
// 已知版本还必须与自己的版本字符串、索引引擎、维度声明一致。
|
||||
profile, err := libraryprofile.For(c.SchemaVersion)
|
||||
if err != nil || c.WeKnoraVersion != profile.WeKnoraVersion || c.RetrieverEngine != "elasticsearch_v8" ||
|
||||
!identifier(c.EmbeddingModel) || c.EmbeddingDimension < 1 ||
|
||||
(profile.EmbeddingDimension > 0 && c.EmbeddingDimension != profile.EmbeddingDimension) ||
|
||||
!validDigest(c.IndexMappingSHA256) {
|
||||
return nil, errors.New("unsupported snapshot compatibility contract")
|
||||
}
|
||||
if m.Format != Format || (m.Scope != "internal" && m.Scope != "external") || m.TenantID == 0 || !identifier(m.KnowledgeBaseID) || m.CreatedAt.IsZero() {
|
||||
return nil, errors.New("invalid library manifest identity")
|
||||
}
|
||||
// 部件集合也来自 profile:0.9.0 的归档没有 wiki_log_entries 部件,
|
||||
// 但 0.7.1 缺了它就是不完整。
|
||||
requiredKeys, _ := profile.PartKeys()
|
||||
required := map[string]bool{}
|
||||
for _, n := range []string{"knowledges", "chunks", "resources", "resource_bindings", "knowledge_tags", "knowledge_tag_relations"} {
|
||||
required["database/"+n] = true
|
||||
for _, key := range requiredKeys {
|
||||
required[key] = true
|
||||
}
|
||||
for _, n := range []string{"wiki_pages", "wiki_folders", "wiki_page_issues", "wiki_log_entries"} {
|
||||
required["wiki/"+n] = true
|
||||
}
|
||||
required["index/chunks"] = true
|
||||
seen := map[string]bool{}
|
||||
for _, p := range m.Parts {
|
||||
key := p.Kind + "/" + p.Name
|
||||
if (!required[key] && !(p.Kind == "resource" && identifier(p.Name) && p.Count == 1) && !(key == "schema/migration-catalog" && p.Count == 1)) || seen[key] || !validDigest(p.SHA256) || p.Bytes < 0 || p.Count < 0 {
|
||||
if (!profile.AllowsPart(key) && !(p.Kind == "resource" && identifier(p.Name) && p.Count == 1) && !(key == "schema/migration-catalog" && p.Count == 1)) || seen[key] || !validDigest(p.SHA256) || p.Bytes < 0 || p.Count < 0 {
|
||||
return nil, errors.New("invalid or nonknowledge snapshot component")
|
||||
}
|
||||
seen[key] = true
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package librarysnapshot
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 归档部件的完整性按 schema 版本判定:0.9.0 没有 wiki_log_entries,
|
||||
// 0.7.1 缺了它就是残包。这里直接对 manifest 编码做判定,不依赖数据库。
|
||||
func TestManifestPartSetFollowsSchemaProfile(t *testing.T) {
|
||||
part := func(kind, name string) Part {
|
||||
return Part{Kind: kind, Name: name, SHA256: strings.Repeat("a", 64), Count: 1}
|
||||
}
|
||||
parts := []Part{
|
||||
part("database", "knowledges"), part("database", "chunks"), part("database", "resources"),
|
||||
part("database", "resource_bindings"), part("database", "knowledge_tags"),
|
||||
part("database", "knowledge_tag_relations"),
|
||||
part("wiki", "wiki_pages"), part("wiki", "wiki_folders"), part("wiki", "wiki_page_issues"),
|
||||
part("index", "chunks"),
|
||||
}
|
||||
manifest := func(version int, weknora string, parts []Part) Manifest {
|
||||
return Manifest{
|
||||
ValidationPolicy: ArtifactAcceptancePolicy,
|
||||
Format: Format,
|
||||
Scope: "internal",
|
||||
TenantID: 10000,
|
||||
KnowledgeBaseID: "kb-v090",
|
||||
CreatedAt: time.Now().UTC(),
|
||||
Parts: parts,
|
||||
Compatibility: Compatibility{
|
||||
WeKnoraVersion: weknora, SchemaVersion: version,
|
||||
RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "bge-m3",
|
||||
EmbeddingDimension: 1024, IndexMappingSHA256: strings.Repeat("b", 64),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := encodeManifest(manifest(134, "v0.9.0", parts)); err != nil {
|
||||
t.Fatalf("0.9.0 manifest without wiki_log_entries must be complete: %v", err)
|
||||
}
|
||||
if _, err := encodeManifest(manifest(74, "v0.7.1", parts)); err == nil || !strings.Contains(err.Error(), "incomplete") {
|
||||
t.Fatalf("0.7.1 manifest missing wiki_log_entries must be refused, got %v", err)
|
||||
}
|
||||
withLog := append(append([]Part{}, parts...), part("wiki", "wiki_log_entries"))
|
||||
if _, err := encodeManifest(manifest(74, "v0.7.1", withLog)); err != nil {
|
||||
t.Fatalf("0.7.1 manifest with wiki_log_entries must pass: %v", err)
|
||||
}
|
||||
if _, err := encodeManifest(manifest(134, "v0.9.0", withLog)); err != nil {
|
||||
t.Fatalf("0.9.0 manifest may still carry an optional wiki_log_entries part: %v", err)
|
||||
}
|
||||
if _, err := encodeManifest(manifest(74, "v0.9.0", withLog)); err == nil {
|
||||
t.Fatal("version string and profile version must agree")
|
||||
}
|
||||
if _, err := encodeManifest(manifest(135, "v0.9.0", parts)); err == nil {
|
||||
t.Fatal("unknown schema version must be refused")
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
"knowledge-governance/internal/objectgateway"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
"knowledge-governance/internal/releasebundle"
|
||||
)
|
||||
|
||||
// OpenMaintenance 按部署配置装配实例级维护目标;未接线时返回明确原因。
|
||||
@@ -34,6 +33,9 @@ func OpenMaintenance(config appconfig.Config) (*librarymaintenance.Manager, erro
|
||||
Writers: config.LibraryTargetWriters,
|
||||
NonWriters: config.LibraryTargetNonWriters,
|
||||
DockerSocket: config.LibraryTargetDockerSocket,
|
||||
IngressMode: config.LibraryTargetIngressMode,
|
||||
IngressAck: config.LibraryTargetNoIngressAck,
|
||||
HealthURL: config.LibraryTargetHealthURL,
|
||||
IngressURL: config.LibraryTargetIngressURL,
|
||||
IngressService: config.LibraryTargetIngressService,
|
||||
IngressDrainFile: config.LibraryTargetIngressDrainFile,
|
||||
@@ -298,10 +300,22 @@ func OpenBaselineStack(ctx context.Context, config appconfig.Config, scope strin
|
||||
target.Close()
|
||||
return nil, nil, fmt.Errorf("读取目标 WeKnora schema 版本失败: %w", err)
|
||||
}
|
||||
if dirty || schemaVersion != releasebundle.WeKnoraMigrationV071 {
|
||||
if dirty {
|
||||
target.Close()
|
||||
return nil, nil, fmt.Errorf("目标 schema 版本 %d(dirty=%v)与受支持的 %d 不一致,拒绝建基线",
|
||||
schemaVersion, dirty, releasebundle.WeKnoraMigrationV071)
|
||||
return nil, nil, fmt.Errorf("目标 schema 版本 %d 处于 dirty 状态,拒绝建基线", schemaVersion)
|
||||
}
|
||||
// 契约按目标库自己的 schema 解析,而不是写死 74:0.9.0 的 schema 是 134,
|
||||
// 它的表集合、索引来源与 Wiki 部件都和 0.7.1 不同,用 74 的常量建基线
|
||||
// 会把另一套栈的记录写进权威版本。
|
||||
profile, err := v071.ResolveSchemaProfile(ctx, target.Pool(), "baseline contract")
|
||||
if err != nil {
|
||||
target.Close()
|
||||
return nil, nil, fmt.Errorf("解析目标 schema profile 失败: %w", err)
|
||||
}
|
||||
if profile.SchemaVersion != schemaVersion {
|
||||
target.Close()
|
||||
return nil, nil, fmt.Errorf("目标 schema 版本 %d 与解析出的 profile %s(%d)不一致,拒绝建基线",
|
||||
schemaVersion, profile.Name, profile.SchemaVersion)
|
||||
}
|
||||
embeddingModel := config.LibraryEmbeddingModel
|
||||
embeddingDimension := config.LibraryEmbeddingDimension
|
||||
@@ -333,7 +347,7 @@ WHERE kb.id = $1 AND kb.deleted_at IS NULL`, scopeCfg.target.KnowledgeBaseID).Sc
|
||||
return nil, nil, fmt.Errorf("读取目标 embedding 模型绑定失败: %w", err)
|
||||
}
|
||||
contract := librarysnapshot.Compatibility{
|
||||
WeKnoraVersion: releasebundle.WeKnoraVersionV071,
|
||||
WeKnoraVersion: profile.WeKnoraVersion,
|
||||
SchemaVersion: schemaVersion,
|
||||
RetrieverEngine: "elasticsearch_v8",
|
||||
EmbeddingModel: embeddingModel,
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package libraryverify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
)
|
||||
|
||||
func drillLedger() libraryrelease.WriteSet {
|
||||
return libraryrelease.WriteSet{Version: 1, JobID: "drill", Target: "internal", TenantID: 10000, KnowledgeBaseID: "kb",
|
||||
BackupSnapshotID: "backup", TargetSnapshotID: "source", CreatedAt: time.Now().UTC(),
|
||||
Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
}
|
||||
|
||||
// 写集合必须先落盘并登记,再标记内容风险,最后才写目标:
|
||||
// 顺序颠倒会让恢复入口在"已经写过"时读不到 ledger。
|
||||
func TestCanaryPersistsAndRegistersEvidenceBeforeWrite(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
ledger := drillLedger()
|
||||
registered, marked, written := false, false, false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", nil,
|
||||
func(context.Context) error {
|
||||
if !registered {
|
||||
t.Fatal("risk marker preceded durable registration")
|
||||
}
|
||||
marked = true
|
||||
return nil
|
||||
},
|
||||
func(_ context.Context) error {
|
||||
if len(ledger.CreatedIDs("knowledge_tags")) != 1 {
|
||||
t.Fatal("canary absent from write set")
|
||||
}
|
||||
if ledger.Digest == "" {
|
||||
t.Fatal("write set was not sealed before registration")
|
||||
}
|
||||
matches, e := filepath.Glob(filepath.Join(root, "writesets", "*.json"))
|
||||
if e != nil || len(matches) != 1 {
|
||||
t.Fatalf("durable ledger missing: %v %v", matches, e)
|
||||
}
|
||||
if _, e := os.Stat(matches[0]); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
registered = true
|
||||
return nil
|
||||
},
|
||||
func(context.Context) error {
|
||||
if !registered || !marked {
|
||||
t.Fatal("write preceded registration/risk marker")
|
||||
}
|
||||
written = true
|
||||
return nil
|
||||
})
|
||||
if err != nil || !written || !registered {
|
||||
t.Fatalf("err=%v written=%v registered=%v", err, written, registered)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWithoutDurableLedger(t *testing.T) {
|
||||
root := filepath.Join(t.TempDir(), "blocked")
|
||||
if err := os.WriteFile(root, []byte("not a directory"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ledger := drillLedger()
|
||||
marked, written := false, false
|
||||
err := writeDrillCanary(context.Background(), root, &ledger, "canary-id", nil,
|
||||
func(context.Context) error { marked = true; return nil },
|
||||
func(context.Context) error { marked = true; return nil },
|
||||
func(context.Context) error { written = true; return nil })
|
||||
if err == nil || marked || written {
|
||||
t.Fatalf("err=%v marked=%v written=%v", err, marked, written)
|
||||
}
|
||||
}
|
||||
|
||||
// 登记失败 = 恢复入口读不到写集合:此时一笔都不许写。
|
||||
func TestCanaryNeverWritesWhenRegistrationFails(t *testing.T) {
|
||||
ledger := drillLedger()
|
||||
marked, written := false, false
|
||||
err := writeDrillCanary(context.Background(), t.TempDir(), &ledger, "canary-id", nil,
|
||||
func(context.Context) error { marked = true; return nil },
|
||||
func(context.Context) error { return errors.New("governance registration unavailable") },
|
||||
func(context.Context) error { written = true; return nil })
|
||||
if err == nil || marked || written {
|
||||
t.Fatalf("err=%v marked=%v written=%v", err, marked, written)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryNeverWritesWhenRiskMarkerFails(t *testing.T) {
|
||||
ledger := drillLedger()
|
||||
written := false
|
||||
err := writeDrillCanary(context.Background(), t.TempDir(), &ledger, "canary-id", nil,
|
||||
func(context.Context) error { return errors.New("journal unavailable") },
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) error { written = true; return nil })
|
||||
if err == nil || written {
|
||||
t.Fatalf("err=%v written=%v", err, written)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package libraryverify
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
)
|
||||
|
||||
// B1 的定向回归:共享 Run 用 PlanWrite 产出的写前/来源身份与写集合,
|
||||
// 必须通过真实恢复入口调用的**同一份**身份合同;用同一份快照冒充两个身份
|
||||
// 的旧形状必须被拒绝。这里不连接任何业务目标(隔离 fixture)。
|
||||
func TestSharedDrillPlanPassesRealRestoreIdentityContract(t *testing.T) {
|
||||
preimage := libraryrelease.BackupRef{SnapshotID: strings.Repeat("a", 64),
|
||||
Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
source := libraryrelease.BackupRef{SnapshotID: strings.Repeat("b", 64),
|
||||
Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
|
||||
set, err := PlanWrite("libverify-internal-fixture", "internal", 10000, "fixture-kb", preimage, source)
|
||||
if err != nil {
|
||||
t.Fatalf("shared plan rejected: %v", err)
|
||||
}
|
||||
if err := set.Seal(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// ProductionLibraryTarget.Restore 与 RestoreDrill 都在写之前调用这一判定。
|
||||
if err := set.ValidateRestoreBinding(preimage.SnapshotID, 10000, "fixture-kb"); err != nil {
|
||||
t.Fatalf("real restore identity contract rejected the shared plan: %v", err)
|
||||
}
|
||||
// 演练恢复入口的写集合守卫(只允许可归因的 canary 行)也必须接受它。
|
||||
canary := set
|
||||
canary.Rows = append(canary.Rows, libraryrelease.WriteSetRow{Table: "knowledge_tags", ID: "canary-1"})
|
||||
if err := canary.Seal(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := v071.ValidateDrillWriteSet(canary); err != nil {
|
||||
t.Fatalf("drill write set guard rejected the shared plan: %v", err)
|
||||
}
|
||||
|
||||
// 旧缺陷形状:来源快照 = 写前镜像。同一判定必须拒绝。
|
||||
legacy := set
|
||||
legacy.TargetSnapshotID = preimage.SnapshotID
|
||||
legacy.Digest = ""
|
||||
if err := legacy.Seal(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = legacy.ValidateRestoreBinding(preimage.SnapshotID, 10000, "fixture-kb")
|
||||
if err == nil || !strings.Contains(err.Error(), "同一份快照") {
|
||||
t.Fatalf("legacy same-snapshot plan must be rejected, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedPlanRejectsSameSnapshotIdentity(t *testing.T) {
|
||||
preimage := libraryrelease.BackupRef{SnapshotID: strings.Repeat("c", 64),
|
||||
Universe: map[string]libraryrelease.TableFingerprint{"knowledge_tags": {Count: 0, Digest: "empty"}}}
|
||||
if _, err := PlanWrite("job", "internal", 10000, "kb", preimage, preimage); err == nil {
|
||||
t.Fatal("plan must refuse to use one snapshot as both source and pre-image")
|
||||
}
|
||||
if _, err := PlanWrite("job", "internal", 10000, "kb", preimage, libraryrelease.BackupRef{}); err == nil {
|
||||
t.Fatal("plan must refuse a missing source identity")
|
||||
}
|
||||
}
|
||||
|
||||
// 演练恢复入口只回放"本次写的隔离 canary 行";任何其它行都不是演练产物。
|
||||
func TestDrillWriteSetGuardRejectsForeignRows(t *testing.T) {
|
||||
set := libraryrelease.WriteSet{Version: libraryrelease.WriteSetVersion, Rows: []libraryrelease.WriteSetRow{
|
||||
{Table: "knowledges", ID: "doc-1"},
|
||||
}}
|
||||
if err := v071.ValidateDrillWriteSet(set); err == nil {
|
||||
t.Fatal("drill guard must reject non-canary rows")
|
||||
}
|
||||
set.Rows = []libraryrelease.WriteSetRow{{Table: "knowledge_tags", ID: "tag-1", Existing: true}}
|
||||
if err := v071.ValidateDrillWriteSet(set); err == nil {
|
||||
t.Fatal("drill guard must reject rows that existed before the write")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
// Package libraryverify 在目标实例上做一次**真实**的恢复验证(原 cmd/librarydrill 的实现):
|
||||
//
|
||||
// 维护隔离(关入口/停写入者/证明排空)
|
||||
// → 捕获目标整库(真预备份,落归档)
|
||||
// → 校验目标 == 预备份(基线一致)
|
||||
// → 写入一个 canary(证明"校验确实能发现内容变化")
|
||||
// → 按预备份恢复
|
||||
// → 逐表/索引/资源/Wiki 再校验一次,必须完全一致
|
||||
// → 关窗口(按原运行状态恢复服务、开入口、验证健康)
|
||||
//
|
||||
// 为什么搬进 internal:页面要能点一次"恢复验证",worker 必须在进程内执行同一份实现,
|
||||
// 不能再要求运维 SSH 上去跑 CLI。命令行使同一个 Run,不允许两份实现。
|
||||
// 它只读/写**被测目标自己**,绝不触碰封存源;所有凭据来自进程环境,不进命令行、不打印。
|
||||
// 任一步失败都不伪装成功:窗口保持维护、报告写明失败阶段。
|
||||
package libraryverify
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/appconfig"
|
||||
"knowledge-governance/internal/libraryfreeze"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"knowledge-governance/internal/librarystack"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
)
|
||||
|
||||
type Verification struct {
|
||||
SnapshotID string `json:"snapshot_id"`
|
||||
Verified bool `json:"verified"`
|
||||
Error string `json:"error,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
Status string `json:"status"` // passed | failed | maintenance_held
|
||||
Scope string `json:"scope"`
|
||||
Target string `json:"target"`
|
||||
SnapshotID string `json:"snapshot_id"` // 写前镜像:恢复实际回放的那份预备份
|
||||
SourceSnapshot string `json:"source_snapshot_id,omitempty"`
|
||||
WriteSetDigest string `json:"writeset_digest,omitempty"`
|
||||
WindowState string `json:"window_state,omitempty"`
|
||||
Project string `json:"project"`
|
||||
Counts map[string]int64 `json:"counts"`
|
||||
BaselineCheck *Verification `json:"baseline_check"`
|
||||
DriftCheck *Verification `json:"drift_check"` // 必须失败:证明确实能发现差异
|
||||
RestoreCheck *Verification `json:"restore_check"`
|
||||
Canary string `json:"canary"`
|
||||
Failure string `json:"failure,omitempty"`
|
||||
Stage string `json:"stage,omitempty"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt time.Time `json:"finished_at"`
|
||||
Until map[string]string `json:"until,omitempty"`
|
||||
}
|
||||
|
||||
// Progress 把演练的阶段推进交给调用方(worker 写 library_job_stages,CLI 打日志)。
|
||||
type Progress func(stage, state, detail string)
|
||||
|
||||
func (p Progress) report(stage, state, detail string) {
|
||||
if p != nil {
|
||||
p(stage, state, detail)
|
||||
}
|
||||
}
|
||||
|
||||
// TargetIdentity 是治理侧维护记录必须钉住的目标事实(与 librarymaintenance.Target 同源)。
|
||||
type TargetIdentity struct {
|
||||
Instance string
|
||||
Fingerprint string
|
||||
Writers []string
|
||||
}
|
||||
|
||||
// Governance 是演练必须持有的**治理侧**维护生命周期:实例/scope 占用、入口真实状态、
|
||||
// 释放。为什么不能让调用方只写磁盘 journal:页面与恢复入口读的是治理行,只写 journal
|
||||
// 时"正在维护"不会出现在 UI,失败后也没有同 job 的恢复锚点(2026-11 审核 B2)。
|
||||
type Governance interface {
|
||||
Acquire(ctx context.Context, jobID, scope string, target TargetIdentity, originalServiceState string) error
|
||||
Ingress(ctx context.Context, jobID, scope, state, note string) error
|
||||
State(ctx context.Context, jobID, scope, state, note string) error
|
||||
Release(ctx context.Context, jobID, scope, note string) error
|
||||
}
|
||||
|
||||
// Ledger 把预备份身份与写集合摘要持久化到**同一个作业**(worker 直接传
|
||||
// libraryStageRecorder:它已经是 BackupObserver + WriteSetObserver)。
|
||||
// 恢复入口按 (scope, job, 写前镜像) 精确读这条记录,所以两件事必须发生在写第一笔之前。
|
||||
type Ledger interface {
|
||||
BackupPrepared(ctx context.Context, backup libraryrelease.BackupRef) error
|
||||
WriteSetPlanned(ctx context.Context, set libraryrelease.WriteSet) error
|
||||
}
|
||||
|
||||
// Options 是演练的可选接线。Governance/Ledger 为 nil 只允许主机侧 CLI 使用:
|
||||
// 页面发起的作业必须两者都在,否则演练结论不会出现在治理记录里。
|
||||
type Options struct {
|
||||
Progress Progress
|
||||
Governance Governance
|
||||
Ledger Ledger
|
||||
}
|
||||
|
||||
// Run 执行一次演练。jobID 同时是维护窗口的持有者标识,必须与业务作业 id 一致,
|
||||
// 否则崩溃恢复时窗口与作业对不上。
|
||||
func Run(ctx context.Context, config appconfig.Config, scope, jobID string, opts Options) (*Report, error) {
|
||||
if scope != "internal" && scope != "external" {
|
||||
return nil, errors.New("scope must be internal or external")
|
||||
}
|
||||
progress := opts.Progress
|
||||
report := &Report{Status: "failed", Scope: scope, Project: config.LibraryTargetProject,
|
||||
Target: config.LibraryTargetInstance, StartedAt: time.Now().UTC()}
|
||||
fail := func(stage string, cause error, held bool) (*Report, error) {
|
||||
report.Stage = stage
|
||||
report.Failure = cause.Error()
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
if held {
|
||||
report.Status = "maintenance_held"
|
||||
}
|
||||
progress.report(stage, "failed", cause.Error())
|
||||
return report, cause
|
||||
}
|
||||
|
||||
progress.report("capture", "running", "打开目标栈与维护管理器")
|
||||
target, backups, err := librarystack.OpenBaselineStack(ctx, config, scope)
|
||||
if err != nil {
|
||||
return fail("capture", err, false)
|
||||
}
|
||||
defer target.Close()
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return fail("capture", err, false)
|
||||
}
|
||||
defer manager.Close()
|
||||
if err := manager.VerifyTopology(ctx); err != nil {
|
||||
return fail("capture", err, false)
|
||||
}
|
||||
originalState, err := manager.OriginalState(ctx)
|
||||
if err != nil {
|
||||
return fail("capture", errors.New("读不到目标写入者的原运行状态,不能开维护窗口:"+err.Error()), false)
|
||||
}
|
||||
identity := manager.Target()
|
||||
// 治理占用必须在任何目标动作之前:拿不到就没有"入口已关/写入者已停"的权威记录,
|
||||
// 失败后也没有可恢复的同 job 锚点。
|
||||
if opts.Governance != nil {
|
||||
targetIdentity := TargetIdentity{Instance: identity.Instance, Fingerprint: identity.Fingerprint, Writers: identity.Writers}
|
||||
if err := opts.Governance.Acquire(ctx, jobID, scope, targetIdentity, originalState); err != nil {
|
||||
return fail("capture", errors.New("目标实例正在维护中或无法登记维护占用:"+err.Error()), false)
|
||||
}
|
||||
}
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
if opts.Governance != nil {
|
||||
// 还没进窗口、目标未被停用:释放占用是诚实的;释放失败就报告出来。
|
||||
_ = opts.Governance.Release(ctx, jobID, scope, "维护协调器不可用,未进入窗口")
|
||||
}
|
||||
return fail("capture", err, false)
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
|
||||
var (
|
||||
snapshotID string
|
||||
sourceID string
|
||||
writesetDigest string
|
||||
counts map[string]int64
|
||||
canary string
|
||||
contentTouched bool
|
||||
baseline Verification
|
||||
drift Verification
|
||||
restored Verification
|
||||
)
|
||||
work := func(ctx context.Context, window *libraryfreeze.Window) (string, error) {
|
||||
start := time.Now()
|
||||
// 窗口已关入口、已停写入者并证明排空:此时才敢把 UI 的入口状态写成 closed。
|
||||
if opts.Governance != nil {
|
||||
if err := opts.Governance.Ingress(ctx, jobID, scope, "closed",
|
||||
"恢复验证已关闭目标入口并停止写入者"); err != nil {
|
||||
return "", errors.New("登记入口关闭失败:" + err.Error())
|
||||
}
|
||||
}
|
||||
current, err := target.Current(ctx)
|
||||
if err != nil {
|
||||
return "", errors.New("读取目标当前整库状态失败:" + err.Error())
|
||||
}
|
||||
counts = current.Counts
|
||||
report.Counts = counts
|
||||
// 1) 写前镜像:真预备份(落归档、内容寻址、导出后复核内容未变)。
|
||||
// 它是恢复阶段实际回放的唯一内容来源。
|
||||
backup, err := backups.Prepare(ctx, "", current)
|
||||
if err != nil {
|
||||
return "", errors.New("捕获目标整库失败:" + err.Error())
|
||||
}
|
||||
snapshotID = backup.SnapshotID
|
||||
report.SnapshotID = snapshotID
|
||||
if err := window.SetBackupSnapshot(ctx, snapshotID); err != nil {
|
||||
return "", errors.New("记录预备份身份失败:" + err.Error())
|
||||
}
|
||||
// 2) 来源身份:DS01 合同要求"来源快照 ≠ 写前镜像",演练不能用同一份快照冒充
|
||||
// 两个身份(真实 Restore 会在任何写之前拒绝)。这里再做一次**真实的**独立捕获,
|
||||
// 只作为这次写入的来源身份;两次捕获都在同一维护切点内、由同一备份存储密封,
|
||||
// 不伪造快照、也不放宽 Restore 的校验。
|
||||
source, err := backups.Prepare(ctx, "", current)
|
||||
if err != nil {
|
||||
return "", errors.New("捕获来源身份失败:" + err.Error())
|
||||
}
|
||||
if source.SnapshotID == "" || source.SnapshotID == snapshotID {
|
||||
return "", errors.New("来源身份与写前镜像相同:拒绝用一份快照冒充两个恢复身份")
|
||||
}
|
||||
sourceID = source.SnapshotID
|
||||
report.SourceSnapshot = sourceID
|
||||
// 预备份引用必须在任何写入之前落进同一个作业:恢复入口按它取写前镜像。
|
||||
if opts.Ledger != nil {
|
||||
if err := opts.Ledger.BackupPrepared(ctx, backup); err != nil {
|
||||
return "", errors.New("预备份身份无法持久化,未对目标执行任何写入:" + err.Error())
|
||||
}
|
||||
}
|
||||
progress.report("capture", "succeeded",
|
||||
fmt.Sprintf("已捕获写前镜像 %s 与来源身份 %s(%d 字节,用时 %s)",
|
||||
short(snapshotID), short(sourceID), backup.Bytes, time.Since(start).Round(time.Second)))
|
||||
log.Printf("verify: snapshot captured preimage=%s source=%s bytes=%d in=%s",
|
||||
snapshotID, sourceID, backup.Bytes, time.Since(start))
|
||||
|
||||
// 3) 基线校验:预备份必须与目标完全一致。
|
||||
progress.report("verify", "running", "逐表/索引/资源比对目标与预备份")
|
||||
baseline = Verification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
baseline.Error = err.Error()
|
||||
report.BaselineCheck = &baseline
|
||||
return "", errors.New("预备份与目标不一致(预备份本身不可信):" + err.Error())
|
||||
}
|
||||
baseline.Verified = true
|
||||
report.BaselineCheck = &baseline
|
||||
progress.report("verify", "succeeded", "预备份与目标逐类一致")
|
||||
|
||||
// Canary 是隔离的标签行,空目标上也一样:绝不为了"让演练能跑"去种文档或改既有行。
|
||||
var random [16]byte
|
||||
if _, err := rand.Read(random[:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
canary = fmt.Sprintf("%x", random[:])
|
||||
identityValue := target.Identity()
|
||||
var exists bool
|
||||
if err := target.Pool().QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM knowledge_tags WHERE id=$1)", canary).Scan(&exists); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if exists {
|
||||
return "", errors.New("canary identity collision; no write attempted")
|
||||
}
|
||||
ledger, planErr := PlanWrite(jobID, scope, identityValue.TenantID, identityValue.KnowledgeBaseID, backup, source)
|
||||
if planErr != nil {
|
||||
return "", planErr
|
||||
}
|
||||
report.Canary = canary
|
||||
progress.report("canary", "running", "写入一个隔离 canary,验证校验能发现差异")
|
||||
if err := writeDrillCanary(ctx, config.LibraryArchiveRoot, &ledger, canary,
|
||||
func(set *libraryrelease.WriteSet) error {
|
||||
// 计划必须在 Seal/落盘/登记之前定型:对象网关控制器按登记摘要加载,
|
||||
// 这里补 V2 信封、目标 schema、来源身份与当前 owned attempt 绑定。
|
||||
return target.PrepareDrillPlan(ctx, set)
|
||||
},
|
||||
func(ctx context.Context) error {
|
||||
if err := window.MarkContentRisk(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
contentTouched = true
|
||||
return nil
|
||||
}, func(ctx context.Context) error {
|
||||
if opts.Ledger == nil {
|
||||
return nil
|
||||
}
|
||||
// 写集合摘要必须在第一笔写入之前登记到同一个作业的预备份证据里:
|
||||
// 恢复入口按 (scope, job, 写前镜像) 精确读它,读不到就保持维护。
|
||||
return opts.Ledger.WriteSetPlanned(ctx, *ledgerClone(&ledger))
|
||||
}, func(ctx context.Context) error {
|
||||
_, err := target.Pool().Exec(ctx, `INSERT INTO knowledge_tags(id,tenant_id,knowledge_base_id,name) VALUES($1,$2,$3,$4)`, canary, identityValue.TenantID, identityValue.KnowledgeBaseID, "drill-canary-"+canary)
|
||||
return err
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("canary preparation/write failed: %w", err)
|
||||
}
|
||||
writesetDigest = ledger.Digest
|
||||
report.WriteSetDigest = writesetDigest
|
||||
drift = Verification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if verifyErr := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); verifyErr == nil {
|
||||
drift.Error = "写入 canary 后校验仍然通过:校验没有真正比对内容"
|
||||
report.DriftCheck = &drift
|
||||
return "", errors.New("写入 canary 后校验仍然通过:校验没有真正比对内容,拒绝把这次演练当成通过")
|
||||
} else {
|
||||
drift.Error = verifyErr.Error()
|
||||
}
|
||||
report.DriftCheck = &drift
|
||||
progress.report("canary", "succeeded", "校验按预期发现了 canary 差异")
|
||||
log.Printf("verify: drift detected as expected (%s)", drift.Error)
|
||||
|
||||
// 4) 按写前镜像恢复:四个阶段整段回放(与真实恢复同一批 Apply 原语),
|
||||
// 而不是只删 canary。发布计划形态的 Restore 不能接受"只写过一行 canary"的
|
||||
// 写集合(v2 执行计划合同),所以演练走限定的安全恢复入口;它复用同一份
|
||||
// DS01 身份合同与整实例指纹校验。
|
||||
progress.report("restore", "running", "按预备份恢复目标整库")
|
||||
if err := target.RestoreDrill(ctx, libraryrelease.BackupRef{ID: "backup-" + snapshotID, SnapshotID: snapshotID,
|
||||
WriteSet: &ledger}); err != nil {
|
||||
return "", errors.New("按预备份恢复失败:" + err.Error())
|
||||
}
|
||||
// 5) 恢复后逐类复核:必须与预备份完全一致。
|
||||
restored = Verification{SnapshotID: snapshotID, At: time.Now().UTC()}
|
||||
if err := target.Verify(ctx, libraryrelease.Expectation{SnapshotID: snapshotID}); err != nil {
|
||||
restored.Error = err.Error()
|
||||
report.RestoreCheck = &restored
|
||||
return "", errors.New("恢复后内容仍与预备份不一致:" + err.Error())
|
||||
}
|
||||
restored.Verified = true
|
||||
report.RestoreCheck = &restored
|
||||
progress.report("restore", "succeeded", "恢复后与预备份逐类一致")
|
||||
log.Printf("verify: restored and verified snapshot=%s", snapshotID)
|
||||
return snapshotID, nil
|
||||
}
|
||||
|
||||
progress.report("finalize", "running", "关闭维护窗口并按原状态恢复目标")
|
||||
window, windowErr := coordinator.RunWindow(ctx, jobID,
|
||||
func(ctx context.Context) error { return manager.VerifyRunning(ctx) }, work,
|
||||
// 没写过目标内容 ⇒ 按原状态恢复永远安全;写过 canary ⇒ 只有恢复并校验通过才开放。
|
||||
func(error) bool { return !contentTouched || restored.Verified })
|
||||
|
||||
report.Until = map[string]string{"job": jobID, "window_state": window.State}
|
||||
report.WindowState = window.State
|
||||
// 服务真的回来、入口验证过(succeeded / 未写内容的安全失败)才释放治理占用;
|
||||
// held 时保留行,页面才能显示真实入口状态并从同一 job 走恢复入口。
|
||||
released := false
|
||||
if opts.Governance != nil {
|
||||
switch {
|
||||
case window.State == "succeeded" || (window.State == "failed" && !contentTouched):
|
||||
if err := opts.Governance.Ingress(ctx, jobID, scope, "open",
|
||||
"维护窗口已按原运行状态恢复,入口已验证可用"); err != nil {
|
||||
return fail("finalize", errors.New("恢复后登记入口状态失败:"+err.Error()), false)
|
||||
}
|
||||
if err := opts.Governance.Release(ctx, jobID, scope,
|
||||
"恢复验证结束,服务与入口已按原状态恢复"); err != nil {
|
||||
return fail("finalize", errors.New("恢复验证结束但维护占用释放失败:"+err.Error()), false)
|
||||
}
|
||||
released = true
|
||||
default:
|
||||
cause := "维护窗口保持关闭"
|
||||
if windowErr != nil {
|
||||
cause = windowErr.Error()
|
||||
}
|
||||
_ = opts.Governance.Ingress(ctx, jobID, scope, "closed", "目标内容状态未验证,入口保持关闭")
|
||||
_ = opts.Governance.State(ctx, jobID, scope, "needs_restore",
|
||||
"恢复验证失败且目标内容未验证:"+cause)
|
||||
}
|
||||
}
|
||||
if windowErr != nil || window.State != "succeeded" {
|
||||
cause := windowErr
|
||||
if cause == nil {
|
||||
cause = fmt.Errorf("维护窗口状态 %s(未成功)", window.State)
|
||||
}
|
||||
held := window.State == "held"
|
||||
if held && released {
|
||||
held = false
|
||||
}
|
||||
return fail("finalize", cause, held)
|
||||
}
|
||||
report.Status = "passed"
|
||||
report.FinishedAt = time.Now().UTC()
|
||||
progress.report("finalize", "succeeded", "维护窗口已关闭,目标恢复原运行状态")
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// PlanWrite 构造演练的写前镜像/来源身份与写集合。Run 与定向回归共用这一份:
|
||||
// 只有来源身份 ≠ 写前镜像、且两者都是同一次维护切点里密封的真实捕获时,
|
||||
// 恢复入口(同一份 DS01 身份合同)才接受这次演练。
|
||||
func PlanWrite(jobID, scope string, tenantID uint64, knowledgeBaseID string,
|
||||
preimage, source libraryrelease.BackupRef) (libraryrelease.WriteSet, error) {
|
||||
if preimage.SnapshotID == "" || source.SnapshotID == "" {
|
||||
return libraryrelease.WriteSet{}, errors.New("演练缺少写前镜像或来源身份快照")
|
||||
}
|
||||
if preimage.SnapshotID == source.SnapshotID {
|
||||
return libraryrelease.WriteSet{}, errors.New("来源身份与写前镜像相同:拒绝用一份快照冒充两个恢复身份")
|
||||
}
|
||||
set := libraryrelease.WriteSet{Version: libraryrelease.WriteSetVersion, JobID: jobID, Target: scope,
|
||||
TenantID: tenantID, KnowledgeBaseID: knowledgeBaseID,
|
||||
BackupSnapshotID: preimage.SnapshotID, TargetSnapshotID: source.SnapshotID,
|
||||
CreatedAt: time.Now().UTC(), Universe: preimage.Universe}
|
||||
if err := set.ValidateRestoreBinding(preimage.SnapshotID, tenantID, knowledgeBaseID); err != nil {
|
||||
return libraryrelease.WriteSet{}, err
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func short(id string) string {
|
||||
if len(id) <= 12 {
|
||||
return id
|
||||
}
|
||||
return id[:12]
|
||||
}
|
||||
|
||||
// ledgerClone 只用于把台账交给登记方:Seal 会重排 Rows,调用方随后还要用同一份
|
||||
// 内容去恢复,不能让登记方持有可变的同一指针。
|
||||
func ledgerClone(set *libraryrelease.WriteSet) *libraryrelease.WriteSet {
|
||||
copy := *set
|
||||
copy.Rows = append([]libraryrelease.WriteSetRow(nil), set.Rows...)
|
||||
return ©
|
||||
}
|
||||
|
||||
// writeDrillCanary 是"写入前先留痕"的唯一实现:seal → 落盘 → 登记 → 标风险 → 写。
|
||||
// 任何一步失败都不写目标;登记在标风险之前,恢复入口才拿得到这份写集合。
|
||||
func writeDrillCanary(ctx context.Context, root string, ledger *libraryrelease.WriteSet, id string,
|
||||
prepare func(*libraryrelease.WriteSet) error,
|
||||
mark func(context.Context) error, record func(context.Context) error, write func(context.Context) error) error {
|
||||
ledger.Rows = append(ledger.Rows, libraryrelease.WriteSetRow{Table: "knowledge_tags", ID: id, Existing: false})
|
||||
if prepare != nil {
|
||||
if err := prepare(ledger); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := ledger.Seal(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := v071.PersistWriteSet(root, ledger); err != nil {
|
||||
return err
|
||||
}
|
||||
if record != nil {
|
||||
if err := record(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := mark(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return write(ctx)
|
||||
}
|
||||
|
||||
// RecoverWindows 把非终态的维护窗口按原运行状态恢复(服务起来、入口验证)。
|
||||
// 演练失败但还没写目标内容时,用它把目标从"停住"状态救回来;治理记录由作业/接口那侧负责。
|
||||
func RecoverWindows(config appconfig.Config) error {
|
||||
manager, err := librarystack.OpenMaintenance(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer manager.Close()
|
||||
coordinator, control, err := manager.Open(config.LibraryTargetJournalRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer control.Close()
|
||||
defer coordinator.Close()
|
||||
return coordinator.Recover(context.Background())
|
||||
}
|
||||
@@ -177,10 +177,13 @@ func (provider *RealProvider) weKnoraProbe(ctx context.Context, now time.Time) g
|
||||
return provider.track(classify("weknora", "WeKnora API", false, true, ProbeOutcome{ObservedAt: now}))
|
||||
}
|
||||
outcome := provider.httpProbeOutcome(ctx, baseURL, now)
|
||||
probeTarget := ""
|
||||
if outcome.Reachability == nil && provider.weknoraReadiness != nil {
|
||||
outcome.WorkReadiness = provider.weknoraReadiness(ctx)
|
||||
probeTarget, outcome.WorkReadiness = provider.weknoraReadiness(ctx)
|
||||
}
|
||||
return provider.track(classify("weknora", "WeKnora API", true, true, outcome))
|
||||
health := provider.track(classify("weknora", "WeKnora API", true, true, outcome))
|
||||
health.ProbeTarget = probeTarget
|
||||
return health
|
||||
}
|
||||
|
||||
// productionWeKnoraProbe reports the real state of the production WeKnora
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
"knowledge-governance/internal/readiness"
|
||||
)
|
||||
|
||||
func testNow() time.Time { return time.Date(2026, 9, 12, 9, 0, 0, 0, time.UTC) }
|
||||
@@ -141,3 +142,31 @@ func TestOverallReadinessTreatsProductionEndpointAsAdvisory(t *testing.T) {
|
||||
t.Fatalf("real dependency failure must still block: ok=%v blocked=%v", ok, blocked)
|
||||
}
|
||||
}
|
||||
|
||||
// F13: 旧 env 绑定 404 是「业务就绪被阻断」,不是「服务不可达」;
|
||||
// 同时必须把脱敏后的探测对象带出来,否则红灯无法归因。
|
||||
func TestWeKnoraReadinessReportsBindingBlockWithoutLosingLiveness(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
provider := &RealProvider{
|
||||
httpClient: server.Client(),
|
||||
weknoraBaseURL: server.URL,
|
||||
health: newHealthTracker(),
|
||||
weknoraReadiness: func(context.Context) (string, error) {
|
||||
target := "实例 weknora.test · 知识库 1b52c140"
|
||||
return target, fmt.Errorf("%w(%s)", readiness.ErrKnowledgeBaseMissing, target)
|
||||
},
|
||||
}
|
||||
health := provider.weKnoraProbe(context.Background(), testNow())
|
||||
if health.Liveness != LiveUp || health.Readiness != ReadBlocked {
|
||||
t.Fatalf("404 binding = %+v, want up/blocked", health)
|
||||
}
|
||||
if health.ProbeTarget != "实例 weknora.test · 知识库 1b52c140" {
|
||||
t.Fatalf("probe target = %q", health.ProbeTarget)
|
||||
}
|
||||
if !strings.Contains(health.Reason, "1b52c140") || strings.Contains(health.Reason, "1b52c140-2e18-4676-afc5-4c7d8d190a8c") {
|
||||
t.Fatalf("reason = %q, want short target only", health.Reason)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,9 +44,9 @@ type RealProviderConfig struct {
|
||||
// place operators configure it — and the env value above stays the
|
||||
// bootstrap fallback for containers that were wired before the UI existed.
|
||||
ProductionWeKnoraResolver func(ctx context.Context) string
|
||||
// WeKnoraReadiness optionally exercises a real read against a routing
|
||||
// knowledge base (nil = API key not configured; liveness-only).
|
||||
WeKnoraReadiness func(ctx context.Context) error
|
||||
// WeKnoraReadiness optionally exercises a real read against the CURRENT
|
||||
// routing binding and reports the sanitized probe target alongside the error.
|
||||
WeKnoraReadiness func(ctx context.Context) (target string, err error)
|
||||
// WorkerStaleAfter bounds how long a missing worker heartbeat still counts
|
||||
// as alive (P10).
|
||||
WorkerStaleAfter time.Duration
|
||||
@@ -61,7 +61,7 @@ type RealProvider struct {
|
||||
testWeKnoraResolver func(ctx context.Context) string
|
||||
productionWeKnoraBaseURL string
|
||||
productionWeKnoraResolver func(ctx context.Context) string
|
||||
weknoraReadiness func(ctx context.Context) error
|
||||
weknoraReadiness func(ctx context.Context) (string, error)
|
||||
workerStaleAfter time.Duration
|
||||
health *healthTracker
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"log"
|
||||
"math"
|
||||
)
|
||||
|
||||
@@ -39,6 +40,9 @@ func (g *Gateway) Acquire(ctx context.Context, c Claim) (Grant, error) {
|
||||
return Grant{}, e
|
||||
}
|
||||
if plan.Digest != c.PlanDigest || plan.Owner != Digest([]byte(c.JobID)) {
|
||||
// 冲突原因要能在服务端日志里定位:是谁的 plan、对不上哪一项。
|
||||
log.Printf("gateway acquire conflict job=%s op=%s plan=%s claim=%s owner_ok=%t",
|
||||
c.JobID, c.OperationID, plan.Digest, c.PlanDigest, plan.Owner == Digest([]byte(c.JobID)))
|
||||
return Grant{}, ErrConflict
|
||||
}
|
||||
if e = g.InstallPlan(ctx, plan); e != nil {
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestGatewayAcquisitionPersistsAttemptDelivery(t *testing.T) {
|
||||
expected := g.Grant{Epoch: 1, Owner: g.Digest([]byte(claim.JobID)), Plan: claim.PlanDigest}
|
||||
req := request("acquired-object", expected)
|
||||
authority := ¤tPlanAuthority{claim, planFor(expected, req)}
|
||||
gateway := &g.Gateway{Pool: pool, Scope: backend.Bucket, Backend: backend, Isolation: verifiedFixture{}, MaxBytes: 1 << 20, Authority: authority}
|
||||
gateway := &g.Gateway{Pool: pool, Scope: backend.Bucket, Backend: backend, MaxBytes: 1 << 20, Authority: authority}
|
||||
grant, e := gateway.Acquire(ctx, claim)
|
||||
if e != nil || grant != expected {
|
||||
t.Fatal("anchored active attempt grant unavailable", e)
|
||||
@@ -79,7 +79,7 @@ func TestGatewayAcquisitionCannotHandOverUnknownSend(t *testing.T) {
|
||||
req := request("acquire-held", expected)
|
||||
body := []byte("gateway fixture acquire-held")
|
||||
authority := ¤tPlanAuthority{claim, planFor(expected, req)}
|
||||
gateway := &g.Gateway{Pool: pool, Scope: real.Bucket, Backend: backend, Isolation: verifiedFixture{}, MaxBytes: 1 << 20, Authority: authority}
|
||||
gateway := &g.Gateway{Pool: pool, Scope: real.Bucket, Backend: backend, MaxBytes: 1 << 20, Authority: authority}
|
||||
if _, e := gateway.Acquire(ctx, claim); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package objectgateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Attestation is the retired whole-bucket exclusive-writer evidence format.
|
||||
// It is kept only so historical audit artifacts remain readable; no runtime
|
||||
// path loads, verifies or requires it. Admission is the journal, the backend
|
||||
// capability and the frozen plan, all of which work on a shared bucket.
|
||||
type Attestation struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Scope string `json:"scope"`
|
||||
GatewayID string `json:"gateway_id"`
|
||||
IssuedAt time.Time `json:"issued_at"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
WriterInventory string `json:"writer_inventory_sha256"`
|
||||
PolicyEvidence string `json:"policy_evidence_sha256"`
|
||||
DenialEvidence string `json:"denial_evidence_sha256"`
|
||||
DrainEvidence string `json:"drain_evidence_sha256"`
|
||||
}
|
||||
type SignedAttestation struct {
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
Signature []byte `json:"signature"`
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package objectgateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Attestation is issued by an independent deployment auditor, never by a worker.
|
||||
// Its signature authenticates an audit, not the truth of self-reported config.
|
||||
// Runtime cutover must invalidate/replace the audit whenever writer rights change.
|
||||
type Attestation struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Scope string `json:"scope"`
|
||||
GatewayID string `json:"gateway_id"`
|
||||
IssuedAt time.Time `json:"issued_at"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
WriterInventory string `json:"writer_inventory_sha256"`
|
||||
PolicyEvidence string `json:"policy_evidence_sha256"`
|
||||
DenialEvidence string `json:"denial_evidence_sha256"`
|
||||
DrainEvidence string `json:"drain_evidence_sha256"`
|
||||
}
|
||||
type SignedAttestation struct {
|
||||
Payload json.RawMessage `json:"payload"`
|
||||
Signature []byte `json:"signature"`
|
||||
}
|
||||
type AuditVerifier struct {
|
||||
Path string
|
||||
PublicKey ed25519.PublicKey
|
||||
Scope, GatewayID string
|
||||
}
|
||||
|
||||
func (v AuditVerifier) Verify(ctx context.Context) error {
|
||||
if ctx.Err() != nil {
|
||||
return ErrIsolation
|
||||
}
|
||||
st, e := os.Lstat(v.Path)
|
||||
if e != nil || !st.Mode().IsRegular() || st.Size() > 16384 || st.Mode().Perm()&0022 != 0 {
|
||||
return ErrIsolation
|
||||
}
|
||||
raw, e := os.ReadFile(v.Path)
|
||||
if e != nil {
|
||||
return ErrIsolation
|
||||
}
|
||||
var signed SignedAttestation
|
||||
var a Attestation
|
||||
if json.Unmarshal(raw, &signed) != nil || len(v.PublicKey) != ed25519.PublicKeySize || !ed25519.Verify(v.PublicKey, signed.Payload, signed.Signature) || json.Unmarshal(signed.Payload, &a) != nil {
|
||||
return ErrIsolation
|
||||
}
|
||||
now := time.Now()
|
||||
if a.Protocol != Protocol || a.Scope != v.Scope || a.GatewayID != v.GatewayID || a.GatewayID == "" || a.IssuedAt.After(now) || !a.ExpiresAt.After(now) || a.ExpiresAt.Sub(a.IssuedAt) > time.Hour || !validHash(a.WriterInventory) || !validHash(a.PolicyEvidence) || !validHash(a.DenialEvidence) || !validHash(a.DrainEvidence) {
|
||||
return ErrIsolation
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package objectgateway_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
g "knowledge-governance/internal/objectgateway"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGatewayAuditCannotBeConfigurationBoolean(t *testing.T) {
|
||||
pub, priv, e := ed25519.GenerateKey(rand.Reader)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "audit.json")
|
||||
verifier := g.AuditVerifier{Path: path, PublicKey: pub, Scope: "target", GatewayID: "gateway"}
|
||||
if verifier.Verify(context.Background()) == nil {
|
||||
t.Fatal("missing audit accepted")
|
||||
}
|
||||
if os.WriteFile(path, []byte(`{"isolated":true,"ready":true}`), 0600) != nil {
|
||||
t.Fatal("fixture")
|
||||
}
|
||||
if verifier.Verify(context.Background()) == nil {
|
||||
t.Fatal("boolean accepted as isolation proof")
|
||||
}
|
||||
a := g.Attestation{Protocol: g.Protocol, Scope: "target", GatewayID: "gateway", IssuedAt: time.Now().Add(-time.Minute), ExpiresAt: time.Now().Add(30 * time.Minute), WriterInventory: g.Digest([]byte("inventory")), PolicyEvidence: g.Digest([]byte("policy")), DenialEvidence: g.Digest([]byte("denials")), DrainEvidence: g.Digest([]byte("drain"))}
|
||||
write := func(v g.Attestation) {
|
||||
t.Helper()
|
||||
raw, _ := json.Marshal(v)
|
||||
signed, _ := json.Marshal(g.SignedAttestation{Payload: raw, Signature: ed25519.Sign(priv, raw)})
|
||||
if os.WriteFile(path, signed, 0600) != nil {
|
||||
t.Fatal("fixture")
|
||||
}
|
||||
}
|
||||
write(a)
|
||||
if verifier.Verify(context.Background()) != nil {
|
||||
t.Fatal("valid test auditor signature rejected")
|
||||
}
|
||||
for _, mutate := range []func(*g.Attestation){func(v *g.Attestation) { v.Scope = "other" }, func(v *g.Attestation) { v.GatewayID = "other" }, func(v *g.Attestation) { v.ExpiresAt = time.Now().Add(-time.Second) }, func(v *g.Attestation) { v.DenialEvidence = "" }, func(v *g.Attestation) { v.IssuedAt = time.Now().Add(time.Hour) }, func(v *g.Attestation) { v.ExpiresAt = time.Now().Add(24 * time.Hour) }} {
|
||||
bad := a
|
||||
mutate(&bad)
|
||||
write(bad)
|
||||
if verifier.Verify(context.Background()) == nil {
|
||||
t.Fatal("invalid isolation audit accepted")
|
||||
}
|
||||
}
|
||||
write(a)
|
||||
other, _, _ := ed25519.GenerateKey(rand.Reader)
|
||||
verifier.PublicKey = other
|
||||
if verifier.Verify(context.Background()) == nil {
|
||||
t.Fatal("untrusted signer accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package objectgateway
|
||||
|
||||
import "context"
|
||||
|
||||
// sendCreate performs exactly one non-retried versioned PUT and then confirms
|
||||
// the result. Versioned object stores do not provide atomic create-if-absent,
|
||||
// so the confirmation re-reads the current version to make sure this request's
|
||||
// version is still the one a reader would observe.
|
||||
func sendCreate(ctx context.Context, b Backend, r Request, body []byte) (Version, error) {
|
||||
v, err := b.Put(ctx, r.Key, body, r.token())
|
||||
if err != nil {
|
||||
// Unknown outcome: the intent stays unresolved, nothing is retried and
|
||||
// nothing is deleted because no version id is confirmed.
|
||||
return Version{}, ErrHeld
|
||||
}
|
||||
return confirmCreate(ctx, b, r, v)
|
||||
}
|
||||
|
||||
// confirmCreate binds the receipt to a version that is both the exact version
|
||||
// this request wrote and the current version of the key. It is read-only: the
|
||||
// create intent is already committed, so withdrawing a version here would be an
|
||||
// unrecorded mutation with no delete intent or receipt and would leave the
|
||||
// create intent permanently held. A concurrent writer that produced a newer
|
||||
// current version therefore wins by explicit conflict; both versions stay in
|
||||
// the store and an operator resolves the key with evidence.
|
||||
func confirmCreate(ctx context.Context, b Backend, r Request, v Version) (Version, error) {
|
||||
exact, exists, err := b.Observe(ctx, r.Key, v.ID)
|
||||
if err != nil || !exists || !r.matches(exact) || v != exact {
|
||||
return Version{}, ErrHeld
|
||||
}
|
||||
latest, current, err := b.Observe(ctx, r.Key, "")
|
||||
if err != nil || !current {
|
||||
// Cannot tell whether our write is still current: keep the intent held
|
||||
// rather than mutating anything on a guess.
|
||||
return Version{}, ErrHeld
|
||||
}
|
||||
if latest.ID != v.ID {
|
||||
// Another writer owns the current version. Report the conflict and leave
|
||||
// both versions untouched; this path never merges them automatically.
|
||||
return Version{}, ErrConflict
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package objectgateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeBackend struct {
|
||||
byID map[string]Version
|
||||
latest Version
|
||||
latestExists bool
|
||||
latestErr error
|
||||
putVersion Version
|
||||
putErr error
|
||||
putCalls int
|
||||
deletes []string
|
||||
deleteErr error
|
||||
}
|
||||
|
||||
func (f *fakeBackend) Ready(context.Context) error { return nil }
|
||||
func (f *fakeBackend) Observe(_ context.Context, _ string, id string) (Version, bool, error) {
|
||||
if id == "" {
|
||||
if f.latestErr != nil {
|
||||
return Version{}, false, f.latestErr
|
||||
}
|
||||
return f.latest, f.latestExists, nil
|
||||
}
|
||||
v, ok := f.byID[id]
|
||||
return v, ok, nil
|
||||
}
|
||||
func (f *fakeBackend) List(context.Context, string) ([]Version, error) { return nil, nil }
|
||||
func (f *fakeBackend) Put(context.Context, string, []byte, string) (Version, error) {
|
||||
f.putCalls++
|
||||
if f.putErr != nil {
|
||||
return Version{}, f.putErr
|
||||
}
|
||||
return f.putVersion, nil
|
||||
}
|
||||
func (f *fakeBackend) Delete(_ context.Context, _ string, v Version) error {
|
||||
if f.deleteErr != nil {
|
||||
return f.deleteErr
|
||||
}
|
||||
f.deletes = append(f.deletes, v.ID)
|
||||
delete(f.byID, v.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func createFixture(t *testing.T) (Request, Version) {
|
||||
t.Helper()
|
||||
body := []byte("shared bucket object")
|
||||
grant := Grant{Epoch: 1, Owner: Digest([]byte("owner")), Plan: Digest([]byte("plan"))}
|
||||
r := Request{Grant: grant, Operation: Digest([]byte("operation")), Key: "kb/bound/object-1", Action: "create", SHA256: Digest(body), Bytes: int64(len(body))}
|
||||
v := Version{ID: "version-own", Owner: r.token(), SHA256: r.SHA256, Bytes: r.Bytes, ETag: `"etag-own"`}
|
||||
return r, v
|
||||
}
|
||||
|
||||
func TestCreateConfirmationDetectsNewerForeignVersionWithoutSideEffects(t *testing.T) {
|
||||
r, own := createFixture(t)
|
||||
foreign := Version{ID: "version-foreign", Owner: Digest([]byte("foreign-owner")), SHA256: Digest([]byte("foreign")), Bytes: 7, ETag: `"etag-foreign"`}
|
||||
f := &fakeBackend{byID: map[string]Version{own.ID: own, foreign.ID: foreign}, latest: foreign, latestExists: true, putVersion: own}
|
||||
if _, err := confirmCreate(context.Background(), f, r, own); !errors.Is(err, ErrConflict) {
|
||||
t.Fatalf("concurrent newer version accepted as committed: %v", err)
|
||||
}
|
||||
// Confirmation runs after the journal intent exists. Deleting here would be
|
||||
// an unrecorded mutation with no delete intent/receipt and would strand the
|
||||
// create intent in held forever, so confirmation must be read-only.
|
||||
if len(f.deletes) != 0 {
|
||||
t.Fatalf("confirmation deleted without a delete intent/receipt: %v", f.deletes)
|
||||
}
|
||||
if _, ok := f.byID[own.ID]; !ok {
|
||||
t.Fatal("own version removed without a recorded delete")
|
||||
}
|
||||
if _, ok := f.byID[foreign.ID]; !ok {
|
||||
t.Fatal("foreign version was deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateConfirmationCommitsOnlyWhenOwnVersionIsCurrent(t *testing.T) {
|
||||
r, own := createFixture(t)
|
||||
f := &fakeBackend{byID: map[string]Version{own.ID: own}, latest: own, latestExists: true, putVersion: own}
|
||||
got, err := confirmCreate(context.Background(), f, r, own)
|
||||
if err != nil || got != own {
|
||||
t.Fatalf("own current version not committed: %v %v", got, err)
|
||||
}
|
||||
if len(f.deletes) != 0 {
|
||||
t.Fatalf("unexpected rollback %v", f.deletes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateConfirmationHoldsWithoutDeletingWhenCurrentVersionUnreadable(t *testing.T) {
|
||||
r, own := createFixture(t)
|
||||
f := &fakeBackend{byID: map[string]Version{own.ID: own}, putVersion: own, latestErr: errors.New("read failed")}
|
||||
if _, err := confirmCreate(context.Background(), f, r, own); !errors.Is(err, ErrHeld) {
|
||||
t.Fatalf("unreadable current version treated as terminal: %v", err)
|
||||
}
|
||||
if len(f.deletes) != 0 {
|
||||
t.Fatalf("deleted on an unknown observation: %v", f.deletes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateConfirmationHoldsOnExactReadbackMismatch(t *testing.T) {
|
||||
r, own := createFixture(t)
|
||||
other := own
|
||||
other.ETag = `"different"`
|
||||
f := &fakeBackend{byID: map[string]Version{own.ID: other}, latest: other, latestExists: true, putVersion: own}
|
||||
if _, err := confirmCreate(context.Background(), f, r, own); !errors.Is(err, ErrHeld) {
|
||||
t.Fatalf("mismatched readback treated as committed: %v", err)
|
||||
}
|
||||
if len(f.deletes) != 0 {
|
||||
t.Fatalf("deleted on mismatch: %v", f.deletes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSendIsSingleAttemptAndNeverDeletesUnknownPut(t *testing.T) {
|
||||
r, _ := createFixture(t)
|
||||
f := &fakeBackend{putErr: errors.New("transport lost")}
|
||||
if _, err := sendCreate(context.Background(), f, r, []byte("shared bucket object")); !errors.Is(err, ErrHeld) {
|
||||
t.Fatalf("unknown PUT treated as terminal: %v", err)
|
||||
}
|
||||
if f.putCalls != 1 {
|
||||
t.Fatalf("PUT retried: calls=%d", f.putCalls)
|
||||
}
|
||||
if len(f.deletes) != 0 {
|
||||
t.Fatalf("deleted without a confirmed version: %v", f.deletes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanAuthorizesOnlyFrozenObjectList(t *testing.T) {
|
||||
body := []byte("planned")
|
||||
owner, digest := Digest([]byte("owner")), Digest([]byte("plan-digest"))
|
||||
p := Plan{Digest: digest, Owner: owner, Objects: []ObjectBinding{{Operation: Digest([]byte("op-1")), Key: "kb/allowed/1", SHA256: Digest(body), Bytes: int64(len(body))}}}
|
||||
allowed := Request{Grant: Grant{1, owner, digest}, Operation: Digest([]byte("op-1")), Key: "kb/allowed/1", Action: "create", SHA256: Digest(body), Bytes: int64(len(body))}
|
||||
if !p.allows(allowed) {
|
||||
t.Fatal("frozen object rejected")
|
||||
}
|
||||
for name, mutate := range map[string]func(*Request){
|
||||
"other_kb_key": func(r *Request) { r.Key = "kb/other-kb/1" },
|
||||
"other_object": func(r *Request) { r.Key = "kb/allowed/2" },
|
||||
"other_operation": func(r *Request) { r.Operation = Digest([]byte("op-2")) },
|
||||
} {
|
||||
got := allowed
|
||||
mutate(&got)
|
||||
if p.allows(got) {
|
||||
t.Fatalf("%s authorized outside the frozen write set", name)
|
||||
}
|
||||
}
|
||||
dup := p
|
||||
dup.Objects = append(dup.Objects, p.Objects[0])
|
||||
if dup.valid() {
|
||||
t.Fatal("duplicate key accepted in plan")
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user