feat(binding): 连接四态验证、密钥尾号与私网 http 放行
build / build (push) Successful in 1m45s

在既有 kbbinding(迁移 000081)上补齐任务要求,不另建表、不另起路由:
- 测试连接区分 401/403(密钥无效 / 无权限)与不可达、成功四态;
- 视图回显密钥尾 4 位(服务端不存、不回读明文);
- key_action=clear 作为独立动作,清除后解析失败关闭、不回落文件旧 Key;
- 内外库留空 = 取消绑定,回报回退来源(测试侧部署默认库 / 生产侧同 ID 语义);
- 生产绑定回报在飞执行的冻结/仍可改指向条数,已冻结目标不被改写;
- 新增 WEKNORA_ALLOW_PRIVATE_HTTP:客户端/探活/连接页共用 HTTPAllowed,
  只放行 RFC1918/回环/链路本地 http,生产 18093 同网段反代因此可用。
This commit is contained in:
狐友 GXXP & Miku
2026-11-22 15:46:13 +08:00
committed by 姚章浩
parent cb026e8dd1
commit 7d91343e5f
21 changed files with 528 additions and 73 deletions
+83 -2
View File
@@ -1,16 +1,22 @@
package kbbinding
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/jackc/pgx/v5"
"knowledge-governance/internal/platform/weknora"
"knowledge-governance/internal/secretbox"
)
type emptyDB struct{}
@@ -74,7 +80,7 @@ func TestCatalogDistinguishesEmptyFromPermissionFromUnreachable(t *testing.T) {
catalog, err = store.Catalog(ctx, EnvTest, CatalogInput{
BaseURL: denied.URL, KeyAction: "replace", APIKey: "key-1", Page: 1, PageSize: 20,
})
if err != nil || catalog.Status != statusUnauthorized {
if err != nil || catalog.Status != statusKeyInvalid {
t.Fatalf("denied catalog = %#v, %v", catalog, err)
}
@@ -119,7 +125,6 @@ func TestSaveInputValidationRejectsDuplicateAndBadIDs(t *testing.T) {
store := New(emptyDB{}, Options{Now: func() time.Time { return now }})
for _, input := range []SaveInput{
{BaseURL: "https://x.example", KeyAction: "replace", APIKey: "k", InternalKBID: "kb", ExternalKBID: "kb"},
{BaseURL: "https://x.example", KeyAction: "replace", APIKey: "k", InternalKBID: "kb-in"},
{BaseURL: "https://x.example", KeyAction: "replace", APIKey: "k", InternalKBID: "kb/1"},
{BaseURL: "https://x.example?token=1", KeyAction: "replace", APIKey: "k"},
} {
@@ -128,3 +133,79 @@ func TestSaveInputValidationRejectsDuplicateAndBadIDs(t *testing.T) {
}
}
}
func TestCatalogDistinguishesInvalidKeyFromForbiddenKey(t *testing.T) {
invalid := catalogFailure(&weknora.HTTPError{StatusCode: http.StatusUnauthorized})
if invalid.Status != "key_invalid" || !strings.Contains(invalid.Message, "密钥无效") {
t.Fatalf("401 -> %+v", invalid)
}
forbidden := catalogFailure(&weknora.HTTPError{StatusCode: http.StatusForbidden})
if forbidden.Status != "key_forbidden" || !strings.Contains(forbidden.Message, "无权限") {
t.Fatalf("403 -> %+v", forbidden)
}
dead := catalogFailure(context.DeadlineExceeded)
if dead.Status != "unreachable" || !(strings.Contains(dead.Message, "超时") || strings.Contains(dead.Message, "不可达")) {
t.Fatalf("timeout -> %+v", dead)
}
if dead.Status == invalid.Status || invalid.Status == forbidden.Status {
t.Fatal("the three outcomes must stay distinguishable")
}
}
func TestClearKeyIsASeparateAction(t *testing.T) {
store := New(emptyDB{}, Options{})
saved := stored{Environment: EnvTest, Revision: 2, BaseURL: "https://old.example", KeyCiphertext: []byte("sealed")}
key, err := store.keyFor(context.Background(), EnvTest, "clear", "https://old.example", "", saved, true)
if err != nil || key != "" {
t.Fatalf("clear = %q, %v", key, err)
}
if _, err := store.keyFor(context.Background(), EnvTest, "clear", "https://old.example", "typed", saved, true); !errors.Is(err, ErrInvalid) {
t.Fatalf("clear with a typed key = %v, want ErrInvalid", err)
}
}
func TestKeyTailShowsOnlyFourCharacters(t *testing.T) {
path := filepath.Join(t.TempDir(), "master.key")
if err := os.WriteFile(path, bytes.Repeat([]byte{7}, 32), 0o600); err != nil {
t.Fatal(err)
}
box, err := secretbox.Load(path)
if err != nil {
t.Fatalf("secretbox.Load: %v", err)
}
sealed, err := box.Seal([]byte("sk-live-abcd"), keyAAD(EnvTest, 3))
if err != nil {
t.Fatal(err)
}
store := New(emptyDB{}, Options{MasterKeyFile: path})
tail, ok := store.keyTail(stored{KeyCiphertext: sealed, Revision: 3}, EnvTest)
if !ok || tail != "abcd" {
t.Fatalf("tail = %q %v", tail, ok)
}
// A different revision must not open — the tail must never leak from a
// ciphertext that no longer matches the row.
if _, ok := store.keyTail(stored{KeyCiphertext: sealed, Revision: 4}, EnvTest); ok {
t.Fatal("tail opened with the wrong revision")
}
}
func TestUnboundSideReportsWhereItFallsBackTo(t *testing.T) {
store := New(emptyDB{}, Options{LegacyBinding: func(context.Context, string) (string, string, error) {
return "legacy-in-0001", "legacy-out-0001", nil
}})
view := View{Environment: EnvTest}
store.fillFallbacks(context.Background(), EnvTest, &view)
if view.InternalFallback != "legacy-in-0001" || view.ExternalFallback != "legacy-out-0001" {
t.Fatalf("test fallbacks = %+v", view)
}
production := View{Environment: EnvProduction}
store.fillFallbacks(context.Background(), EnvProduction, &production)
if production.InternalFallback != "same-id" || production.ExternalFallback != "same-id" {
t.Fatalf("production fallbacks = %+v", production)
}
bound := View{Environment: EnvTest, InternalKBID: "a", ExternalKBID: "b"}
store.fillFallbacks(context.Background(), EnvTest, &bound)
if bound.InternalFallback != "" || bound.ExternalFallback != "" {
t.Fatalf("bound view must not claim a fallback: %+v", bound)
}
}