fix(release-worker): 启动失败带端点与脱敏原因
正式 release-worker 崩溃重启时日志只有一句 connect release target database, 主机名、库名和底层原因都不在里面。现在错误里带 host:port/db、路由标识与原因。 端点去口令不等于整条日志安全:pgx 的解析错误可能原样回显连接串。新增 internal/pgdsn.Redact(按已知口令精确替换 + 按 URL/keyword 形状兜底), 错误保留 Unwrap 以便 errors.Is/As,技术原因不丢。cmd/worker 里只留库名的旧实现 换成同一个包,避免两份脱敏逻辑。libraryctl 用法补上 dead-letters。
This commit is contained in:
@@ -111,7 +111,7 @@ func main() {
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprint(os.Stderr, `libraryctl <status|preflight|capture|list|verify|recover> [flags]
|
||||
fmt.Fprint(os.Stderr, `libraryctl <status|preflight|capture|list|verify|recover|dead-letters> [flags]
|
||||
|
||||
status probe the real source and print what is configured / blocked
|
||||
preflight read the source, run settlement + candidate validation (no writes, no stop)
|
||||
@@ -119,6 +119,8 @@ func usage() {
|
||||
list list sealed candidates and stored bytes
|
||||
verify re-verify one sealed candidate (digests, coverage, identity)
|
||||
recover restore writers from an interrupted freeze journal
|
||||
dead-letters print the source's live dead letters (read-only) with a digest the
|
||||
governance platform binds a precise release to
|
||||
|
||||
Flags (defaults target the local WKNFull test stack):
|
||||
-source-app WKNFull-app container whose env describes the source
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 正式栈的 release-worker 启动失败时只打一句 "connect release target database",
|
||||
// 主机名、库名与底层原因都不在日志里。修好之后仍然要证明:可诊断 ≠ 泄露凭据,
|
||||
// 因为 pgx 的解析/连接错误会把连接串原样带回来。
|
||||
var diagnosisSecret = "rel" + "ease-s3cret-value"
|
||||
|
||||
func TestReleaseDatabaseErrorNamesTheEndpointWithoutCredentials(t *testing.T) {
|
||||
cause := errors.New("dial tcp 127.0.0.1:1: connect: connection refused")
|
||||
message := releaseDatabaseError("target",
|
||||
"postgres://postgres:ignored@prod-pg:25432/weknora_test?sslmode=disable", "kb-release-diagnosis", cause).Error()
|
||||
for _, want := range []string{"connect release target database", "prod-pg:25432/weknora_test",
|
||||
"kb-release-diagnosis", "connection refused"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Fatalf("error %q does not mention %q", message, want)
|
||||
}
|
||||
}
|
||||
source := releaseDatabaseError("source", "postgres://u:p@WKNFull-postgres:5432/weknora_full", "kb-1", cause).Error()
|
||||
if !strings.Contains(source, "WKNFull-postgres:5432/weknora_full") {
|
||||
t.Fatalf("source error = %q", source)
|
||||
}
|
||||
}
|
||||
|
||||
// cause 原样回显 DSN(pgx 在解析/连接失败时确实可能这样)是最危险的一种:
|
||||
// 端点脱敏救不了它,整条错误里都藏着口令。
|
||||
func TestReleaseDatabaseErrorRedactsACauseThatEchoesTheDSN(t *testing.T) {
|
||||
cases := []string{
|
||||
"postgres://postgres:" + diagnosisSecret + "@127.0.0.1:notaport/weknora_test",
|
||||
"host=127.0.0.1 port=5432 user=postgres password=" + diagnosisSecret + " dbname=weknora_test sslmode=bogus",
|
||||
}
|
||||
for _, dsn := range cases {
|
||||
cause := fmt.Errorf("cannot parse `%s`: invalid port after host", dsn)
|
||||
message := releaseDatabaseError("target", dsn, "kb-release-diagnosis", cause).Error()
|
||||
if strings.Contains(message, diagnosisSecret) {
|
||||
t.Fatalf("error leaked the password: %q", message)
|
||||
}
|
||||
if !strings.Contains(message, "invalid port after host") {
|
||||
t.Fatalf("error lost the underlying reason: %q", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保留 Unwrap:调用方仍然可以用 errors.Is/As 判断原因;只是文本被脱敏。
|
||||
func TestReleaseDatabaseErrorKeepsTheCauseChain(t *testing.T) {
|
||||
sentinel := errors.New("boom")
|
||||
err := releaseDatabaseError("target", "postgres://u:p@h:1/db", "kb-1", sentinel)
|
||||
if !errors.Is(err, sentinel) {
|
||||
t.Fatalf("cause chain broken: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
"knowledge-governance/internal/platform/weknora"
|
||||
v071 "knowledge-governance/internal/releaseadapter/v071"
|
||||
"knowledge-governance/internal/releasesnapshot"
|
||||
@@ -63,13 +65,13 @@ func buildReleaseRoute(ctx context.Context, archiveRoot string, snapshot release
|
||||
}
|
||||
sourcePool, err := openReleasePool(ctx, sourceDatabaseURL)
|
||||
if err != nil {
|
||||
return nil, nil, errors.New("connect release source database")
|
||||
return nil, nil, releaseDatabaseError("source", sourceDatabaseURL, route.KnowledgeBaseID, err)
|
||||
}
|
||||
pools := []*pgxpool.Pool{sourcePool}
|
||||
targetPool, err := openReleasePool(ctx, targetDatabaseURL)
|
||||
if err != nil {
|
||||
sourcePool.Close()
|
||||
return nil, nil, errors.New("connect release target database")
|
||||
return nil, nil, releaseDatabaseError("target", targetDatabaseURL, route.KnowledgeBaseID, err)
|
||||
}
|
||||
pools = append(pools, targetPool)
|
||||
cleanup := func(err error) (releaseworkflow.Publisher, []*pgxpool.Pool, error) {
|
||||
@@ -207,6 +209,29 @@ func readOptionalRestrictedValue(path string) (string, error) {
|
||||
return readRestrictedValue(path)
|
||||
}
|
||||
|
||||
// releaseDatabaseError 是"连不上某个库"的唯一措辞:端点 + 路由 + 底层原因。
|
||||
//
|
||||
// 为什么不用 %w 直接拼原因:pgx 的错误里可能原样带着连接串(含口令),
|
||||
// 只在端点脱敏证明不了整条日志安全。这里把原因文本交给 pgdsn.Redact 抹口令,
|
||||
// 同时保留 Unwrap,调用方仍能用 errors.Is/As 判断。技术原因(拒绝连接、
|
||||
// 认证失败、TLS 配置错误等)一个字都不丢。
|
||||
type databaseConnectError struct {
|
||||
message string
|
||||
cause error
|
||||
}
|
||||
|
||||
func (e *databaseConnectError) Error() string { return e.message }
|
||||
func (e *databaseConnectError) Unwrap() error { return e.cause }
|
||||
|
||||
func releaseDatabaseError(role, databaseURL, knowledgeBaseID string, cause error) error {
|
||||
reason := pgdsn.Redact(databaseURL, cause.Error())
|
||||
return &databaseConnectError{
|
||||
message: fmt.Sprintf("connect release %s database %s (route %s): %s",
|
||||
role, pgdsn.Endpoint(databaseURL), knowledgeBaseID, reason),
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
func openReleasePool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"knowledge-governance/internal/librarymaintenance"
|
||||
"knowledge-governance/internal/libraryrelease"
|
||||
"knowledge-governance/internal/librarysnapshot"
|
||||
"knowledge-governance/internal/pgdsn"
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
@@ -194,7 +195,7 @@ func runLibraryRelease(ctx context.Context, store *postgresstore.Store, config a
|
||||
"目标拓扑不满足唯一入口约束,拒绝开维护窗口:"+topoErr.Error(), "prebackup")
|
||||
}
|
||||
targetIdentity := fmt.Sprintf("scope=%s db=%s index=%s bucket=%s", payload.Scope,
|
||||
redactDSN(config.LibraryProductionDatabaseURL), config.LibraryProductionIndex, config.LibraryProductionS3Bucket)
|
||||
pgdsn.Endpoint(config.LibraryProductionDatabaseURL), config.LibraryProductionIndex, config.LibraryProductionS3Bucket)
|
||||
originalState, stateErr := manager.OriginalState(ctx)
|
||||
if stateErr != nil {
|
||||
return failLibraryJob(ctx, store, recorder, payload.LibraryJobID,
|
||||
|
||||
Reference in New Issue
Block a user