1db21fb899
- 生产/测试 WeKnora 地址、租户 ID、知识库 ID 由静态 env 收敛为版本化 UI 设置,保留修订号与操作人审计 - 新增 serviceverify:保存前真调目标端点探测,端点无效才拒绝;网络抖动或无凭据仅告警 - 迁移 000042(生产 WeKnora 端点)/000043(服务目标);补齐设置候选与校验契约用例
293 lines
12 KiB
Go
293 lines
12 KiB
Go
package appconfig
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoadUsesSafeDefaults(t *testing.T) {
|
|
values := map[string]string{"DATABASE_URL": "postgres://example"}
|
|
config, err := Load(func(key string) string { return values[key] })
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if config.AppAddr != ":8080" || config.WorkerID != "worker-1" {
|
|
t.Fatalf("address/worker defaults = %#v", config)
|
|
}
|
|
if config.PollInterval != time.Second || config.LeaseDuration != 30*time.Second || config.RetryDelay != 5*time.Second {
|
|
t.Fatalf("duration defaults = %#v", config)
|
|
}
|
|
if config.ReleaseWorkerEnabled {
|
|
t.Fatal("release worker must be disabled by default")
|
|
}
|
|
if config.ReleaseConfigFile != "" {
|
|
t.Fatalf("release config default = %q", config.ReleaseConfigFile)
|
|
}
|
|
if config.ProductionReleaseRequestsEnabled {
|
|
t.Fatal("production release requests must be disabled by default")
|
|
}
|
|
if config.TestWeKnoraEnabled {
|
|
t.Fatal("test WeKnora must be disabled by default")
|
|
}
|
|
if config.PPScanEnabled {
|
|
t.Fatal("PP source scan must be disabled by default")
|
|
}
|
|
if config.PPScanAllowedRoot != "" || config.PPScanLedgerPath != "" || config.PPScanManifestPath != "" {
|
|
t.Fatalf("PP scan paths must be empty by default: %#v", config)
|
|
}
|
|
if config.TestWeKnoraAllowDockerHTTP {
|
|
t.Fatal("test WeKnora Docker HTTP must be disabled by default")
|
|
}
|
|
if config.TestWeKnoraAPIKeyFile != "/run/secrets/test_weknora_api_key" {
|
|
t.Fatalf("test API key file = %q", config.TestWeKnoraAPIKeyFile)
|
|
}
|
|
if config.GovernanceSpoolRoot != "/var/lib/governance/spool" {
|
|
t.Fatalf("spool root = %q", config.GovernanceSpoolRoot)
|
|
}
|
|
if config.TestWeKnoraHTTPTimeout != 60*time.Second || config.TestWeKnoraPollInitial != 10*time.Second || config.TestWeKnoraPollMax != time.Minute || config.TestWeKnoraStalledAfter != 30*time.Minute {
|
|
t.Fatalf("test WeKnora timing defaults = %#v", config)
|
|
}
|
|
if config.TestWeKnoraMaxUploadBytes != 50*1024*1024 || config.TestAcceptancePolicyVersion != "structural-v1" {
|
|
t.Fatalf("test WeKnora safety defaults = %#v", config)
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresDatabaseURL(t *testing.T) {
|
|
if _, err := Load(func(string) string { return "" }); err == nil {
|
|
t.Fatal("Load() error = nil, want missing DATABASE_URL error")
|
|
}
|
|
}
|
|
|
|
func TestLoadParsesProductionReleaseRequestGate(t *testing.T) {
|
|
values := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"PRODUCTION_RELEASE_REQUESTS_ENABLED": "true",
|
|
"PRODUCTION_BACKUP_RESTORE_VERIFIED": "true",
|
|
"PRODUCTION_RELEASE_AUTHORIZER_REF": "release-owner",
|
|
"PRODUCTION_RELEASE_AUTH_TOKEN_FILE": "/run/secrets/production_release_auth_token",
|
|
}
|
|
config, err := Load(func(key string) string { return values[key] })
|
|
if err != nil || !config.ProductionReleaseRequestsEnabled || !config.ProductionBackupRestoreVerified || config.ProductionReleaseAuthorizerRef != "release-owner" || config.ProductionReleaseAuthTokenFile != "/run/secrets/production_release_auth_token" {
|
|
t.Fatalf("config=%#v error=%v", config, err)
|
|
}
|
|
values["PRODUCTION_RELEASE_REQUESTS_ENABLED"] = "not-a-bool"
|
|
if _, err := Load(func(key string) string { return values[key] }); err == nil {
|
|
t.Fatal("invalid production release request gate must be rejected")
|
|
}
|
|
values["PRODUCTION_RELEASE_REQUESTS_ENABLED"] = "true"
|
|
values["PRODUCTION_BACKUP_RESTORE_VERIFIED"] = "not-a-bool"
|
|
if _, err := Load(func(key string) string { return values[key] }); err == nil {
|
|
t.Fatal("invalid production backup restore gate must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresProductionReleaseAuthorizationConfigWhenGateEnabled(t *testing.T) {
|
|
base := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"PRODUCTION_RELEASE_REQUESTS_ENABLED": "true",
|
|
"PRODUCTION_RELEASE_AUTHORIZER_REF": "release-owner",
|
|
"PRODUCTION_RELEASE_AUTH_TOKEN_FILE": "/run/secrets/production_release_auth_token",
|
|
}
|
|
for _, missing := range []string{"PRODUCTION_RELEASE_AUTHORIZER_REF", "PRODUCTION_RELEASE_AUTH_TOKEN_FILE"} {
|
|
values := make(map[string]string, len(base))
|
|
for key, value := range base {
|
|
values[key] = value
|
|
}
|
|
delete(values, missing)
|
|
if _, err := Load(func(key string) string { return values[key] }); err == nil {
|
|
t.Fatalf("missing %s accepted", missing)
|
|
}
|
|
}
|
|
base["PRODUCTION_RELEASE_AUTH_TOKEN_FILE"] = "relative/token"
|
|
if _, err := Load(func(key string) string { return base[key] }); err == nil {
|
|
t.Fatal("relative production release token file accepted")
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresReleaseConfigFileWhenReleaseWorkerEnabled(t *testing.T) {
|
|
if _, err := Load(func(key string) string {
|
|
values := map[string]string{"DATABASE_URL": "postgres://example", "RELEASE_WORKER_ENABLED": "true"}
|
|
return values[key]
|
|
}); err == nil {
|
|
t.Fatal("enabled release worker without config file accepted")
|
|
}
|
|
config, err := Load(func(key string) string {
|
|
values := map[string]string{"DATABASE_URL": "postgres://example", "RELEASE_WORKER_ENABLED": "true", "RELEASE_CONFIG_FILE": "/run/secrets/release-config"}
|
|
return values[key]
|
|
})
|
|
if err != nil || config.ReleaseConfigFile != "/run/secrets/release-config" {
|
|
t.Fatalf("config=%#v err=%v", config, err)
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresAllPPScanPathsWhenEnabled(t *testing.T) {
|
|
base := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"PP_SCAN_ENABLED": "true",
|
|
"PP_SCAN_ALLOWED_ROOT": "/source",
|
|
"PP_SCAN_LEDGER_PATH": "/source/state.jsonl",
|
|
"PP_SCAN_MANIFEST_PATH": "/source/manifest.json",
|
|
}
|
|
for _, missing := range []string{"PP_SCAN_ALLOWED_ROOT", "PP_SCAN_LEDGER_PATH", "PP_SCAN_MANIFEST_PATH"} {
|
|
values := make(map[string]string, len(base))
|
|
for key, value := range base {
|
|
values[key] = value
|
|
}
|
|
delete(values, missing)
|
|
if _, err := Load(func(key string) string { return values[key] }); err == nil {
|
|
t.Fatalf("Load() error = nil, want missing %s rejected", missing)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresTestWeKnoraEndpointAndKBWhenEnabled(t *testing.T) {
|
|
base := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"TEST_WEKNORA_ENABLED": "true",
|
|
}
|
|
for _, key := range []string{"TEST_WEKNORA_BASE_URL", "TEST_WEKNORA_KB_ID"} {
|
|
values := make(map[string]string, len(base)+1)
|
|
for name, value := range base {
|
|
values[name] = value
|
|
}
|
|
if key == "TEST_WEKNORA_BASE_URL" {
|
|
values["TEST_WEKNORA_KB_ID"] = "kb-test"
|
|
} else {
|
|
values["TEST_WEKNORA_BASE_URL"] = "https://weknora.test.example"
|
|
}
|
|
if _, err := Load(func(name string) string { return values[name] }); err == nil {
|
|
t.Fatalf("Load() error = nil, want missing %s error", key)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoadParsesTestWeKnoraSettings(t *testing.T) {
|
|
values := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"TEST_WEKNORA_ENABLED": "true",
|
|
"TEST_WEKNORA_ALLOW_DOCKER_HTTP": "true",
|
|
"TEST_WEKNORA_BASE_URL": "https://weknora.test.example",
|
|
"TEST_WEKNORA_KB_ID": "kb-test",
|
|
"TEST_WEKNORA_API_KEY_FILE": "/run/secrets/custom_key",
|
|
"GOVERNANCE_SPOOL_ROOT": "/srv/spool",
|
|
"TEST_WEKNORA_HTTP_TIMEOUT": "45s",
|
|
"TEST_WEKNORA_POLL_INITIAL": "5s",
|
|
"TEST_WEKNORA_POLL_MAX": "20s",
|
|
"TEST_WEKNORA_STALLED_AFTER": "15m",
|
|
"TEST_WEKNORA_MAX_UPLOAD_BYTES": "1048576",
|
|
"TEST_ACCEPTANCE_POLICY_VERSION": "structural-v2",
|
|
}
|
|
config, err := Load(func(name string) string { return values[name] })
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if !config.TestWeKnoraEnabled || !config.TestWeKnoraAllowDockerHTTP || config.TestWeKnoraBaseURL != values["TEST_WEKNORA_BASE_URL"] || config.TestWeKnoraKBID != "kb-test" {
|
|
t.Fatalf("test WeKnora identity = %#v", config)
|
|
}
|
|
if config.TestWeKnoraAPIKeyFile != "/run/secrets/custom_key" || config.GovernanceSpoolRoot != "/srv/spool" {
|
|
t.Fatalf("test WeKnora file settings = %#v", config)
|
|
}
|
|
if config.TestWeKnoraHTTPTimeout != 45*time.Second || config.TestWeKnoraPollInitial != 5*time.Second || config.TestWeKnoraPollMax != 20*time.Second || config.TestWeKnoraStalledAfter != 15*time.Minute {
|
|
t.Fatalf("test WeKnora timing settings = %#v", config)
|
|
}
|
|
if config.TestWeKnoraMaxUploadBytes != 1048576 || config.TestAcceptancePolicyVersion != "structural-v2" {
|
|
t.Fatalf("test WeKnora safety settings = %#v", config)
|
|
}
|
|
}
|
|
|
|
func TestLoadRejectsUnsafeTestWeKnoraSettings(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
key string
|
|
value string
|
|
}{
|
|
{name: "poll max before initial", key: "TEST_WEKNORA_POLL_MAX", value: "5s"},
|
|
{name: "stalled before poll max", key: "TEST_WEKNORA_STALLED_AFTER", value: "30s"},
|
|
{name: "upload limit above 50 MiB", key: "TEST_WEKNORA_MAX_UPLOAD_BYTES", value: "52428801"},
|
|
{name: "empty policy", key: "TEST_ACCEPTANCE_POLICY_VERSION", value: " "},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
values := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"TEST_WEKNORA_ENABLED": "true",
|
|
"TEST_WEKNORA_BASE_URL": "https://weknora.test.example",
|
|
"TEST_WEKNORA_KB_ID": "kb-test",
|
|
"TEST_WEKNORA_POLL_INITIAL": "10s",
|
|
"TEST_WEKNORA_POLL_MAX": "60s",
|
|
"TEST_WEKNORA_STALLED_AFTER": "30m",
|
|
"TEST_ACCEPTANCE_POLICY_VERSION": "structural-v1",
|
|
}
|
|
values[tt.key] = tt.value
|
|
if _, err := Load(func(name string) string { return values[name] }); err == nil {
|
|
t.Fatalf("Load() error = nil, want unsafe %s rejected", tt.key)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadRequiresBothCatalogRoutingKnowledgeBases(t *testing.T) {
|
|
values := map[string]string{
|
|
"DATABASE_URL": "postgres://example",
|
|
"TEST_INTERNAL_WEKNORA_KB_ID": "kb-internal",
|
|
"TEST_EXTERNAL_WEKNORA_KB_ID": "kb-external",
|
|
}
|
|
config, err := Load(func(key string) string { return values[key] })
|
|
if err != nil || config.InternalRoutingKnowledgeBaseID != "kb-internal" || config.ExternalRoutingKnowledgeBaseID != "kb-external" {
|
|
t.Fatalf("config=%#v err=%v", config, err)
|
|
}
|
|
delete(values, "TEST_EXTERNAL_WEKNORA_KB_ID")
|
|
if _, err = Load(func(key string) string { return values[key] }); err == nil {
|
|
t.Fatal("one-sided routing target must fail closed")
|
|
}
|
|
}
|
|
|
|
// TestProductionWeKnoraEndpointIsConfigurableAndFailsClosed freezes the
|
|
// production WeKnora configuration contract: the address is operator-supplied
|
|
// (never hardcoded), validation rejects anything that could smuggle secrets, and
|
|
// an unset address is an explicit "not configured" state instead of a guess.
|
|
func TestProductionWeKnoraEndpointIsConfigurableAndFailsClosed(t *testing.T) {
|
|
load := func(extra map[string]string) (Config, error) {
|
|
values := map[string]string{"DATABASE_URL": "postgres://example"}
|
|
for key, value := range extra {
|
|
values[key] = value
|
|
}
|
|
return Load(func(key string) string { return values[key] })
|
|
}
|
|
config, err := load(map[string]string{
|
|
"PRODUCTION_WEKNORA_BASE_URL": "https://weknora.prod.example/",
|
|
"PRODUCTION_WEKNORA_API_KEY_FILE": "/run/secrets/production_weknora_api_key",
|
|
"PRODUCTION_WEKNORA_ALLOW_DOCKER_HTTP": "true",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("valid production endpoint rejected: %v", err)
|
|
}
|
|
if config.ProductionWeKnoraBaseURL != "https://weknora.prod.example/" || !config.ProductionWeKnoraAllowDockerHTTP {
|
|
t.Fatalf("production endpoint = %#v", config)
|
|
}
|
|
if config.ProductionWeKnoraAPIKeyFile != "/run/secrets/production_weknora_api_key" {
|
|
t.Fatalf("production key file = %q", config.ProductionWeKnoraAPIKeyFile)
|
|
}
|
|
for _, invalid := range []map[string]string{
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "ftp://weknora.example"},
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "weknora.example"},
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "https://user:pass@weknora.example"},
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "https://weknora.example/?token=secret"},
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "https://weknora.example/#frag"},
|
|
{"PRODUCTION_WEKNORA_API_KEY_FILE": "/run/secrets/production_weknora_api_key"},
|
|
{"PRODUCTION_WEKNORA_BASE_URL": "https://weknora.example", "PRODUCTION_WEKNORA_API_KEY_FILE": "relative/key"},
|
|
{"PRODUCTION_WEKNORA_ALLOW_DOCKER_HTTP": "yes"},
|
|
} {
|
|
if _, err := load(invalid); err == nil {
|
|
t.Fatalf("invalid production endpoint accepted: %#v", invalid)
|
|
}
|
|
}
|
|
unset, err := load(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if unset.ProductionWeKnoraBaseURL != "" || unset.ProductionWeKnoraAPIKeyFile != "" || unset.ProductionWeKnoraAllowDockerHTTP {
|
|
t.Fatalf("production endpoint must default to unconfigured: %#v", unset)
|
|
}
|
|
}
|