Files
knowledge-governance/internal/httpapi/kb_bindings.go
T
狐友 GXXP & Miku 8c84c9c601
build / build (push) Successful in 2m26s
fix(kb-bindings): allow key entry on private HTTP consoles
- Accept the private-console transport with an exact Origin/Host match; the http console is not a secure context, so Chrome sends no Sec-Fetch-Site and that requirement made every real write 400.
- Reject bracketed non-IPv6 host spellings, including ported forms, instead of normalising them to loopback.
- Show the plaintext-warning copy or the disabled state from a tri-state capability field; old APIs keep the legacy message.
- Document WEKNORA_ALLOW_PRIVATE_HTTP in .env.example and the operations runbook.
2026-11-24 11:22:06 +08:00

128 lines
5.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package httpapi
import (
"context"
"errors"
"net/http"
"strings"
"time"
"knowledge-governance/internal/kbbinding"
)
// KBBindingStore is the environment-scoped 「知识库绑定」capability. Save does
// the real candidate read itself, so the HTTP layer decides no policy beyond
// status/role.
type KBBindingStore interface {
List(context.Context) ([]kbbinding.View, error)
Catalog(context.Context, string, kbbinding.CatalogInput) (kbbinding.Catalog, error)
Save(context.Context, string, kbbinding.SaveInput, string, time.Time) (kbbinding.View, error)
ResolveBaseURL(context.Context, string) string
}
func (config Config) handleKBBindings(w http.ResponseWriter, r *http.Request) {
if config.KBBindings == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "knowledge base binding storage unavailable"})
return
}
views, err := config.KBBindings.List(r.Context())
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not load knowledge base bindings"})
return
}
storageReady := false
for _, view := range views {
storageReady = storageReady || view.StorageReady
}
writeJSON(w, http.StatusOK, map[string]any{"data": map[string]any{
"environments": views,
"storage_ready": storageReady,
// The page cannot test the server's transport rule from the browser URL
// alone (a TLS proxy and an untrusted proxy look identical there).
"secret_entry_allowed": consoleSecretEntryAllowed(r, config.QAAITrustTLSProxy, config.WeKnoraAllowPrivateHTTP),
}})
}
func (config Config) handleKBBindingCatalog(w http.ResponseWriter, r *http.Request) {
if config.KBBindings == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "knowledge base binding storage unavailable"})
return
}
if r.Header.Get("X-Governance-Action") != "kb-bindings" {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "knowledge base binding write intent is required"})
return
}
env := r.PathValue("environment")
if !kbbinding.ValidEnvironment(env) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unknown environment"})
return
}
var input kbbinding.CatalogInput
if err := decodeJSONStrict(r, &input); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid knowledge base catalog request"})
return
}
if input.APIKey != "" && !secretTransportAllowedForPrivateConsole(r, config.QAAITrustTLSProxy, config.WeKnoraAllowPrivateHTTP) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "密钥仅可通过有效HTTPS或本机回环地址录入;未保存、未发送密钥"})
return
}
catalog, err := config.KBBindings.Catalog(r.Context(), env, input)
if err != nil {
writeKBBindingError(w, err)
return
}
writeJSON(w, http.StatusOK, map[string]any{"data": catalog})
}
func (config Config) handleKBBindingSave(w http.ResponseWriter, r *http.Request) {
if config.KBBindings == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "knowledge base binding storage unavailable"})
return
}
if r.Header.Get("X-Governance-Action") != "kb-bindings" {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "knowledge base binding write intent is required"})
return
}
env := r.PathValue("environment")
if !kbbinding.ValidEnvironment(env) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unknown environment"})
return
}
var input kbbinding.SaveInput
if err := decodeJSONStrict(r, &input); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid knowledge base binding request"})
return
}
if input.APIKey != "" && !secretTransportAllowedForPrivateConsole(r, config.QAAITrustTLSProxy, config.WeKnoraAllowPrivateHTTP) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "密钥仅可通过有效HTTPS或本机回环地址录入;未保存、未发送密钥"})
return
}
view, err := config.KBBindings.Save(r.Context(), env, input, actorFor(r), config.Now().UTC())
if err != nil {
writeKBBindingError(w, err)
return
}
writeJSON(w, http.StatusOK, map[string]any{"data": view, "operation": "saved"})
}
func writeKBBindingError(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, kbbinding.ErrInvalid):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "绑定参数不合法:请检查地址、密钥与内外库选择"})
case errors.Is(err, kbbinding.ErrConflict):
writeJSON(w, http.StatusConflict, map[string]string{"error": "该环境的绑定已被其他保存更新;草稿未写入,旧绑定保持不变,请重新读取后再试"})
case errors.Is(err, kbbinding.ErrKeyForwarding):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "已更换服务地址:必须为新地址重新录入 API Key,旧密钥不会发往新地址"})
case errors.Is(err, kbbinding.ErrNotConfigured):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "这个地址还没有可用 API Key:请输入密钥或先选择已配置的连接"})
case errors.Is(err, kbbinding.ErrStorage):
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "独立安全存储未就绪(WEKNORA_BINDING_MASTER_KEY_FILE);未写入任何绑定"})
case errors.Is(err, kbbinding.ErrRejected):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": strings.TrimPrefix(err.Error(), kbbinding.ErrRejected.Error()+":")})
case errors.Is(err, kbbinding.ErrUnreachable):
writeJSON(w, http.StatusBadGateway, map[string]string{"error": strings.TrimPrefix(err.Error(), kbbinding.ErrUnreachable.Error()+":")})
default:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "knowledge base binding operation failed"})
}
}