在既有 kbbinding(迁移 000081)上补齐任务要求,不另建表、不另起路由: - 测试连接区分 401/403(密钥无效 / 无权限)与不可达、成功四态; - 视图回显密钥尾 4 位(服务端不存、不回读明文); - key_action=clear 作为独立动作,清除后解析失败关闭、不回落文件旧 Key; - 内外库留空 = 取消绑定,回报回退来源(测试侧部署默认库 / 生产侧同 ID 语义); - 生产绑定回报在飞执行的冻结/仍可改指向条数,已冻结目标不被改写; - 新增 WEKNORA_ALLOW_PRIVATE_HTTP:客户端/探活/连接页共用 HTTPAllowed, 只放行 RFC1918/回环/链路本地 http,生产 18093 同网段反代因此可用。
This commit is contained in:
@@ -48,11 +48,14 @@ var (
|
||||
// View is the public projection. It never carries a key, ciphertext or any
|
||||
// value derived from the key beyond "configured or not".
|
||||
type View struct {
|
||||
Environment string `json:"environment"`
|
||||
Revision int64 `json:"revision"`
|
||||
BaseURL string `json:"base_url"`
|
||||
KeyConfigured bool `json:"key_configured"`
|
||||
KeySource string `json:"key_source"`
|
||||
Environment string `json:"environment"`
|
||||
Revision int64 `json:"revision"`
|
||||
BaseURL string `json:"base_url"`
|
||||
KeyConfigured bool `json:"key_configured"`
|
||||
KeySource string `json:"key_source"`
|
||||
// KeyTail is the only part of the key the page may show. It is derived at
|
||||
// read time and never stored, so a rotated master key cannot resurrect it.
|
||||
KeyTail string `json:"key_tail"`
|
||||
InternalKBID string `json:"internal_kb_id"`
|
||||
InternalKBName string `json:"internal_kb_name"`
|
||||
ExternalKBID string `json:"external_kb_id"`
|
||||
@@ -69,6 +72,13 @@ type View struct {
|
||||
// rows that had never been uploaded; a plain read reports zeros.
|
||||
Replanned int `json:"replanned"`
|
||||
RetiredDuplicates int `json:"retired_duplicates"`
|
||||
// 留空绑定 = 取消绑定,页面必须说清回退到了哪里:测试侧回退到部署默认库,
|
||||
// 生产侧回退到「同 ID 语义」。空串表示该方向仍有显式绑定。
|
||||
InternalFallback string `json:"internal_fallback"`
|
||||
ExternalFallback string `json:"external_fallback"`
|
||||
// 生产侧冻结计数:执行已开始的发布项目标已钉死,保存绑定不会改写它们。
|
||||
Frozen int `json:"frozen"`
|
||||
Retargetable int `json:"retargetable"`
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
@@ -119,6 +129,9 @@ type Options struct {
|
||||
// AllowDockerHTTP mirrors the per-environment client policy (a single-label
|
||||
// docker host may be reached over plain HTTP in the internal network).
|
||||
AllowDockerHTTP map[string]bool
|
||||
// AllowPrivateHTTP relaxes the plain-http rule for same-LAN addresses
|
||||
// (production 18093 reverse proxy). Public http stays refused.
|
||||
AllowPrivateHTTP map[string]bool
|
||||
// LegacyBaseURL resolves the pre-existing endpoint (web setting first, then
|
||||
// the deployment env value). It is only consulted when no row exists.
|
||||
LegacyBaseURL func(context.Context, string) string
|
||||
@@ -253,6 +266,13 @@ func (s *Store) Get(ctx context.Context, env string) (View, error) {
|
||||
view.UpdatedBy = saved.UpdatedBy
|
||||
view.UpdatedAt = saved.UpdatedAt
|
||||
view.Source = "web"
|
||||
if s.options.MasterKeyFile != "" {
|
||||
if tail, ok := s.keyTail(saved, env); ok {
|
||||
view.KeyTail = tail
|
||||
}
|
||||
}
|
||||
s.fillFallbacks(ctx, env, &view)
|
||||
s.fillExecutionCounts(ctx, env, &view)
|
||||
return view, nil
|
||||
}
|
||||
// Legacy fallback: exactly what the consumers use before any page save.
|
||||
@@ -270,10 +290,81 @@ func (s *Store) Get(ctx context.Context, env string) (View, error) {
|
||||
view.ExternalKBID = external
|
||||
}
|
||||
view.Verification = "legacy"
|
||||
s.fillFallbacks(ctx, env, &view)
|
||||
s.fillExecutionCounts(ctx, env, &view)
|
||||
s.fillNames(ctx, &view)
|
||||
return view, nil
|
||||
}
|
||||
|
||||
// keyTail decrypts only to keep the last four characters. It never returns the
|
||||
// key and never writes the tail anywhere.
|
||||
func (s *Store) keyTail(saved stored, env string) (string, bool) {
|
||||
box := s.loadBox()
|
||||
if box == nil || len(saved.KeyCiphertext) == 0 {
|
||||
return "", false
|
||||
}
|
||||
raw, err := box.Open(saved.KeyCiphertext, keyAAD(env, saved.Revision))
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
runes := []rune(string(raw))
|
||||
if len(runes) <= 4 {
|
||||
return string(runes), true
|
||||
}
|
||||
return string(runes[len(runes)-4:]), true
|
||||
}
|
||||
|
||||
// fillFallbacks explains where an unbound direction routes to. Without this the
|
||||
// page could only say 「留空」without saying what 「空」means.
|
||||
func (s *Store) fillFallbacks(ctx context.Context, env string, view *View) {
|
||||
if view.InternalKBID != "" && view.ExternalKBID != "" {
|
||||
return
|
||||
}
|
||||
if env == EnvProduction {
|
||||
if view.InternalKBID == "" {
|
||||
view.InternalFallback = "same-id"
|
||||
}
|
||||
if view.ExternalKBID == "" {
|
||||
view.ExternalFallback = "same-id"
|
||||
}
|
||||
return
|
||||
}
|
||||
if s.options.LegacyBinding == nil {
|
||||
return
|
||||
}
|
||||
internal, external, err := s.options.LegacyBinding(ctx, env)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if view.InternalKBID == "" && strings.TrimSpace(internal) != "" {
|
||||
view.InternalFallback = strings.TrimSpace(internal)
|
||||
}
|
||||
if view.ExternalKBID == "" && strings.TrimSpace(external) != "" {
|
||||
view.ExternalFallback = strings.TrimSpace(external)
|
||||
}
|
||||
}
|
||||
|
||||
// fillExecutionCounts reports how many in-flight release items already have a
|
||||
// frozen production target. Completed executions are history, not something a
|
||||
// binding change could still move.
|
||||
func (s *Store) fillExecutionCounts(ctx context.Context, env string, view *View) {
|
||||
if env != EnvProduction {
|
||||
return
|
||||
}
|
||||
var frozen, retargetable int
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT
|
||||
COUNT(*) FILTER (WHERE item.target_knowledge_base_id <> ''),
|
||||
COUNT(*) FILTER (WHERE item.target_knowledge_base_id = '')
|
||||
FROM release_item_executions item
|
||||
JOIN release_executions execution ON execution.id = item.release_execution_id
|
||||
WHERE execution.state IN ('pending','running')`).Scan(&frozen, &retargetable)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
view.Frozen, view.Retargetable = frozen, retargetable
|
||||
}
|
||||
|
||||
func keySource(configured bool) string {
|
||||
if configured {
|
||||
return "sealed"
|
||||
@@ -395,6 +486,13 @@ func (s *Store) keyFor(ctx context.Context, env string, action, baseURL, apiKey
|
||||
return "", ErrInvalid
|
||||
}
|
||||
return apiKey, nil
|
||||
case "clear":
|
||||
// 清除密钥是独立动作:留下地址与绑定,只是这一侧不再有可用凭据,
|
||||
// 消费者解析会失败关闭(不会悄悄回落到部署文件里的旧 Key)。
|
||||
if apiKey != "" {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
return "", nil
|
||||
default:
|
||||
return "", ErrInvalid
|
||||
}
|
||||
@@ -406,10 +504,11 @@ func validAPIKey(value string) bool {
|
||||
|
||||
func (s *Store) client(baseURL, apiKey, env string) (*weknora.Client, error) {
|
||||
return weknora.NewClient(weknora.ClientConfig{
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Timeout: s.options.Timeout,
|
||||
AllowDockerHTTP: s.options.AllowDockerHTTP[env],
|
||||
BaseURL: baseURL,
|
||||
APIKey: apiKey,
|
||||
Timeout: s.options.Timeout,
|
||||
AllowDockerHTTP: s.options.AllowDockerHTTP[env],
|
||||
AllowPrivateHTTP: s.options.AllowPrivateHTTP[env],
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user