Files
knowledge-governance/internal/objectgateway/plan.go
T
姚章浩 5eef13b615 feat(object-gateway): 统一对象写网关与不可变治理日志
方案A:所有对象写入经 loopback 网关,持久计划/grant/intent/逐版本 receipt 与
attempt→grant delivery 落在治理 PG;scope 由 provider+endpoint+bucket 决定,
同一物理桶只允许一个写域。版本 append 不等于 CAS,未锚定计划在首笔业务写前拒绝。

本提交只含网关与其适配层接线、协议文档;生产独占、凭据切换与桶设置未改动。
2026-11-01 11:35:05 +08:00

79 lines
2.1 KiB
Go

package objectgateway
import (
"context"
"encoding/json"
)
// Plan is frozen by the authenticated controller, not by the data client. Digest
// is the upstream write-set digest; the controller must verify that write set.
// This allowlist prevents a valid epoch from authorizing arbitrary extra keys.
type Plan struct {
Digest string `json:"digest"`
Owner string `json:"owner"`
Objects []ObjectBinding `json:"objects"`
}
type ObjectBinding struct {
Operation string `json:"operation"`
Key string `json:"key"`
SHA256 string `json:"sha256"`
Bytes int64 `json:"bytes"`
}
func (p Plan) valid() bool {
if !validHash(p.Digest) || !validHash(p.Owner) {
return false
}
keys, ops := map[string]bool{}, map[string]bool{}
for _, o := range p.Objects {
r := Request{Grant: Grant{1, p.Owner, p.Digest}, Operation: o.Operation, Key: o.Key, Action: "create", SHA256: o.SHA256, Bytes: o.Bytes}
if r.validate() != nil || keys[o.Key] || ops[o.Operation] {
return false
}
keys[o.Key] = true
ops[o.Operation] = true
}
return true
}
func (p Plan) allows(r Request) bool {
op := r.Operation
if r.Action == "delete" {
op = r.CreatedBy
}
if p.Owner != r.Grant.Owner || p.Digest != r.Grant.Plan {
return false
}
for _, o := range p.Objects {
if o.Operation == op && o.Key == r.Key && o.SHA256 == r.SHA256 && o.Bytes == r.Bytes {
return true
}
}
return false
}
func (g *Gateway) InstallPlan(ctx context.Context, p Plan) error {
if !p.valid() {
return ErrConflict
}
for _, o := range p.Objects {
if o.Bytes > g.MaxBytes {
return ErrConflict
}
}
if e := g.Ready(ctx); e != nil {
return e
}
raw, _ := json.Marshal(p)
_, e := g.Pool.Exec(ctx, "INSERT INTO kg_object_gateway_plans(scope,plan,body) VALUES($1,$2,$3) ON CONFLICT DO NOTHING", g.Scope, p.Digest, raw)
if e != nil {
return ErrPersistence
}
var found []byte
if g.Pool.QueryRow(ctx, "SELECT body FROM kg_object_gateway_plans WHERE scope=$1 AND plan=$2", g.Scope, p.Digest).Scan(&found) != nil {
return ErrPersistence
}
if string(found) != string(raw) {
return ErrConflict
}
return nil
}