5eef13b615
方案A:所有对象写入经 loopback 网关,持久计划/grant/intent/逐版本 receipt 与 attempt→grant delivery 落在治理 PG;scope 由 provider+endpoint+bucket 决定, 同一物理桶只允许一个写域。版本 append 不等于 CAS,未锚定计划在首笔业务写前拒绝。 本提交只含网关与其适配层接线、协议文档;生产独占、凭据切换与桶设置未改动。
112 lines
4.4 KiB
Go
112 lines
4.4 KiB
Go
package objectgateway_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
g "knowledge-governance/internal/objectgateway"
|
|
)
|
|
|
|
func TestGatewayNewEpochPreservesOriginalSendIdentity(t *testing.T) {
|
|
ctx, pool, backend := fixture(t)
|
|
gateway := &g.Gateway{Pool: pool, Scope: backend.Bucket, Backend: backend, Isolation: verifiedFixture{}, MaxBytes: 1 << 20}
|
|
first := g.Grant{Epoch: 1, Owner: g.Digest([]byte(t.Name())), Plan: g.Digest([]byte("takeover-plan"))}
|
|
sent, unsent := request("takeover-sent", first), request("takeover-unsent", first)
|
|
if e := gateway.InstallPlan(ctx, planFor(first, sent, unsent)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := gateway.Advance(ctx, 0, first); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
old, e := gateway.Execute(ctx, sent, []byte("gateway fixture takeover-sent"))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
// Always clean only this test's exact original version, including on RED.
|
|
cleanupGrant := first
|
|
defer func() { copy := old; copy.Request.Grant = cleanupGrant; cleanup(t, ctx, gateway, copy) }()
|
|
var original []byte
|
|
if e = pool.QueryRow(ctx, "SELECT request FROM kg_object_gateway_intents WHERE scope=$1 AND operation=$2", gateway.Scope, sent.Operation).Scan(&original); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
next := first
|
|
next.Epoch++
|
|
if e = gateway.Advance(ctx, 1, next); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
cleanupGrant = next
|
|
data, control := g.Digest([]byte("takeover-data")), g.Digest([]byte("takeover-control"))
|
|
server := httptest.NewServer(g.HTTPServer{Gateway: gateway, DataToken: data, ControlToken: control})
|
|
defer server.Close()
|
|
post := func(path string, in any) (int, []byte) {
|
|
raw, _ := json.Marshal(in)
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", server.URL+path, bytes.NewReader(raw))
|
|
req.Header.Set("Authorization", "Bearer "+data)
|
|
req.Header.Set("X-KG-Gateway-Protocol", g.Protocol)
|
|
req.Header.Set("X-KG-Gateway-Scope", gateway.Scope)
|
|
resp, e := http.DefaultClient.Do(req)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer resp.Body.Close()
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return resp.StatusCode, body
|
|
}
|
|
code, body := post("/v1/execute", map[string]any{"request": sent, "authorization": next, "body": []byte("gateway fixture takeover-sent")})
|
|
if code != 200 {
|
|
t.Fatalf("new authorization cannot replay immutable original request: status=%d", code)
|
|
}
|
|
var replay g.Receipt
|
|
if json.Unmarshal(body, &replay) != nil || replay != old {
|
|
t.Fatal("replay rewrote original receipt")
|
|
}
|
|
desired := sent
|
|
desired.Grant = next
|
|
code, body = post("/v1/resolve", desired)
|
|
var resolved g.Request
|
|
if code != 200 || json.Unmarshal(body, &resolved) != nil || resolved != sent {
|
|
t.Fatal("original send identity not resolved", code)
|
|
}
|
|
changed := sent
|
|
changed.SHA256 = g.Digest([]byte("different"))
|
|
if code, _ = post("/v1/resolve", changed); code == 200 {
|
|
t.Fatal("operation rebound body")
|
|
}
|
|
if code, _ = post("/v1/execute", map[string]any{"request": unsent, "authorization": first, "body": []byte("gateway fixture takeover-unsent")}); code == 200 {
|
|
t.Fatal("stale epoch created unsent key")
|
|
}
|
|
unsent.Grant = next
|
|
code, body = post("/v1/execute", map[string]any{"request": unsent, "authorization": next, "body": []byte("gateway fixture takeover-unsent")})
|
|
var created g.Receipt
|
|
if code != 200 || json.Unmarshal(body, &created) != nil || created.State != "committed" {
|
|
t.Fatal("new epoch did not continue unsent key", code)
|
|
}
|
|
cleanup(t, ctx, gateway, created)
|
|
var after []byte
|
|
if e = pool.QueryRow(ctx, "SELECT request FROM kg_object_gateway_intents WHERE scope=$1 AND operation=$2", gateway.Scope, sent.Operation).Scan(&after); e != nil || !bytes.Equal(after, original) {
|
|
t.Fatal("immutable intent changed", e)
|
|
}
|
|
versions, e := backend.List(ctx, sent.Key)
|
|
if e != nil || len(versions) != 1 || versions[0] != old.Version {
|
|
t.Fatal("replay created extra version", e)
|
|
}
|
|
// Deletion is a new send in the current epoch, not a rewritten create intent.
|
|
deletion := sent
|
|
deletion.Grant = next
|
|
deletion.Operation = g.Digest([]byte(sent.Operation + "delete"))
|
|
deletion.Action = "delete"
|
|
deletion.CreatedBy = sent.Operation
|
|
deletion.Version = old.Version
|
|
code, _ = post("/v1/execute", map[string]any{"request": deletion, "authorization": next})
|
|
if code != 200 {
|
|
t.Fatal("new epoch exact cleanup refused", code)
|
|
}
|
|
// deferred cleanup replays the same already settled delete request below.
|
|
old.Request.Grant = next
|
|
t.Log("new epoch replay retained original request/receipt/claim; unsent key committed once; stale request rejected")
|
|
}
|