7d91343e5f
build / build (push) Successful in 1m45s
在既有 kbbinding(迁移 000081)上补齐任务要求,不另建表、不另起路由: - 测试连接区分 401/403(密钥无效 / 无权限)与不可达、成功四态; - 视图回显密钥尾 4 位(服务端不存、不回读明文); - key_action=clear 作为独立动作,清除后解析失败关闭、不回落文件旧 Key; - 内外库留空 = 取消绑定,回报回退来源(测试侧部署默认库 / 生产侧同 ID 语义); - 生产绑定回报在飞执行的冻结/仍可改指向条数,已冻结目标不被改写; - 新增 WEKNORA_ALLOW_PRIVATE_HTTP:客户端/探活/连接页共用 HTTPAllowed, 只放行 RFC1918/回环/链路本地 http,生产 18093 同网段反代因此可用。
163 lines
5.2 KiB
Go
163 lines
5.2 KiB
Go
package readiness
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
"strings"
|
||
"time"
|
||
|
||
"knowledge-governance/internal/platform/weknora"
|
||
)
|
||
|
||
// WeKnora 的 readiness 探测必须与业务执行链看同一个目标:网页保存的端点、
|
||
// 当前生效的知识库绑定、同一份 API Key。任何一项在启动时被捕获成常量,
|
||
// 都会在管理员改绑之后继续探测旧库(F13:旧库 404 被当成整个服务不可用)。
|
||
|
||
var (
|
||
ErrNotConfigured = errors.New("WeKnora 就绪探测未配置")
|
||
ErrKnowledgeBaseMissing = errors.New("WeKnora 探测目标知识库不存在")
|
||
ErrNoPermission = errors.New("WeKnora 探测目标知识库无读取权限")
|
||
)
|
||
|
||
type Target struct {
|
||
BaseURL string
|
||
KnowledgeBaseID string
|
||
APIKey string
|
||
}
|
||
|
||
type Result struct {
|
||
// Target 是脱敏后的探测对象(实例 + 短 ID),用于界面说明“探的是谁”。
|
||
Target string
|
||
Err error
|
||
}
|
||
|
||
// Prober 每次 Check 都重新解析目标并新建客户端:端点、凭据、绑定都允许
|
||
// 在进程运行期间变化,不能缓存成启动快照。
|
||
// Resolver is the single source for the effective WeKnora target: endpoint
|
||
// saved in the console (env fallback), current persisted binding (env fallback)
|
||
// and the same key file. API probes and worker clients both use it, so a
|
||
// runtime settings/binding change cannot leave one side on the old value.
|
||
type Resolver struct {
|
||
Endpoint func(context.Context) string
|
||
LoadBinding func(context.Context) (string, error)
|
||
ReadKey func() (string, error)
|
||
DefaultKB string
|
||
// ResolveConnection 可选:一次解析端点与凭据(例如来自加密存储)。
|
||
// 提供时优先于 Endpoint + ReadKey,因为密钥解密需要请求上下文。
|
||
ResolveConnection func(context.Context) (baseURL, apiKey string, err error)
|
||
}
|
||
|
||
func (resolver Resolver) Resolve(ctx context.Context) (Target, error) {
|
||
knowledgeBaseID := strings.TrimSpace(resolver.DefaultKB)
|
||
if resolver.LoadBinding != nil {
|
||
stored, err := resolver.LoadBinding(ctx)
|
||
if err != nil {
|
||
return Target{}, fmt.Errorf("读取当前知识库绑定失败")
|
||
}
|
||
if stored = strings.TrimSpace(stored); stored != "" {
|
||
knowledgeBaseID = stored
|
||
}
|
||
}
|
||
baseURL, apiKey := "", ""
|
||
if resolver.ResolveConnection != nil {
|
||
var err error
|
||
baseURL, apiKey, err = resolver.ResolveConnection(ctx)
|
||
if err != nil {
|
||
return Target{}, fmt.Errorf("读取 WeKnora 有效连接失败:%w", err)
|
||
}
|
||
} else {
|
||
if resolver.ReadKey != nil {
|
||
value, err := resolver.ReadKey()
|
||
if err != nil {
|
||
return Target{}, fmt.Errorf("读取 WeKnora API 凭据失败")
|
||
}
|
||
apiKey = strings.TrimSpace(value)
|
||
}
|
||
if resolver.Endpoint != nil {
|
||
baseURL = strings.TrimSpace(resolver.Endpoint(ctx))
|
||
}
|
||
}
|
||
return Target{BaseURL: strings.TrimSpace(baseURL), KnowledgeBaseID: knowledgeBaseID, APIKey: strings.TrimSpace(apiKey)}, nil
|
||
}
|
||
|
||
type Prober struct {
|
||
Resolve func(context.Context) (Target, error)
|
||
Timeout time.Duration
|
||
AllowDockerHTTP bool
|
||
// AllowPrivateHTTP relaxes the plain-http rule for same-LAN addresses, the
|
||
// same judgement the binding store and the client use.
|
||
AllowPrivateHTTP bool
|
||
}
|
||
|
||
func (prober Prober) Check(ctx context.Context) Result {
|
||
if prober.Resolve == nil {
|
||
return Result{Err: ErrNotConfigured}
|
||
}
|
||
target, err := prober.Resolve(ctx)
|
||
if err != nil {
|
||
return Result{Err: err}
|
||
}
|
||
described := describe(target)
|
||
if strings.TrimSpace(target.BaseURL) == "" || strings.TrimSpace(target.KnowledgeBaseID) == "" || strings.TrimSpace(target.APIKey) == "" {
|
||
return Result{Target: described, Err: ErrNotConfigured}
|
||
}
|
||
timeout := prober.Timeout
|
||
if timeout <= 0 || timeout > 10*time.Second {
|
||
timeout = 10 * time.Second
|
||
}
|
||
client, err := weknora.NewClient(weknora.ClientConfig{
|
||
BaseURL: target.BaseURL,
|
||
APIKey: target.APIKey,
|
||
Timeout: timeout,
|
||
AllowDockerHTTP: prober.AllowDockerHTTP,
|
||
AllowPrivateHTTP: prober.AllowPrivateHTTP,
|
||
})
|
||
if err != nil {
|
||
return Result{Target: described, Err: err}
|
||
}
|
||
if _, _, err := client.ListKnowledge(ctx, target.KnowledgeBaseID, 1, 1); err != nil {
|
||
return Result{Target: described, Err: classify(target, described, err)}
|
||
}
|
||
return Result{Target: described}
|
||
}
|
||
|
||
func classify(target Target, described string, err error) error {
|
||
var httpErr *weknora.HTTPError
|
||
if errors.As(err, &httpErr) {
|
||
switch httpErr.StatusCode {
|
||
case http.StatusNotFound:
|
||
return fmt.Errorf("%w(%s)", ErrKnowledgeBaseMissing, described)
|
||
case http.StatusUnauthorized, http.StatusForbidden:
|
||
return fmt.Errorf("%w(%s)", ErrNoPermission, described)
|
||
case http.StatusTooManyRequests:
|
||
return fmt.Errorf("目标知识库读取被限流(HTTP 429,%s)", described)
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
// describe 只保留实例主机与知识库短 ID:诊断够用,不输出完整 UUID、路径或凭据。
|
||
func describe(target Target) string {
|
||
host := ""
|
||
if parsed, err := url.Parse(strings.TrimSpace(target.BaseURL)); err == nil && parsed.Host != "" {
|
||
host = parsed.Host
|
||
}
|
||
id := strings.TrimSpace(target.KnowledgeBaseID)
|
||
if len(id) > 8 {
|
||
id = id[:8]
|
||
}
|
||
switch {
|
||
case host == "" && id == "":
|
||
return ""
|
||
case host == "":
|
||
return "知识库 " + id
|
||
case id == "":
|
||
return "实例 " + host
|
||
default:
|
||
return "实例 " + host + " · 知识库 " + id
|
||
}
|
||
}
|