fix(library): 按目标身份隔离版本授权与执行历史
build / build (push) Successful in 1m47s

This commit is contained in:
姚章浩
2026-10-31 02:15:15 +08:00
parent 323f9ae502
commit 7ab2eb1ccf
15 changed files with 473 additions and 72 deletions
+41
View File
@@ -0,0 +1,41 @@
package appconfig
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"github.com/jackc/pgx/v5/pgxpool"
)
// LibraryTargetIdentity binds control-plane evidence to the logical deployment
// and data scope, not credentials or the caller's transport. API may use an SSH
// forward while the executor uses the private DB address. Instance/project are
// deployment identities, verified independently by executor admission; reusing
// them for another physical deployment is not a supported target switch.
func (c Config) LibraryTargetIdentity() string {
database := "unconfigured"
if c.LibraryProductionDatabaseURL != "" {
if parsed, err := pgxpool.ParseConfig(c.LibraryProductionDatabaseURL); err == nil {
database = parsed.ConnConfig.Database
} else {
database = "invalid"
}
}
raw, _ := json.Marshal(struct {
Instance string
Project string
Database string
Tenant uint64
InternalKB string
ExternalKB string
Index string
Provider string
ObjectEndpoint string
Bucket string
}{c.LibraryTargetInstance, c.LibraryTargetProject, database, c.LibraryProductionTenantID,
c.LibraryProductionInternalKB, c.LibraryProductionExternalKB, c.LibraryProductionIndex,
c.LibraryProductionS3Provider, c.LibraryProductionS3Endpoint, c.LibraryProductionS3Bucket})
sum := sha256.Sum256(raw)
return hex.EncodeToString(sum[:])
}
+48
View File
@@ -0,0 +1,48 @@
package appconfig
import "testing"
func identityForTest(c Config) string {
return c.LibraryTargetIdentity()
}
func TestLibraryTargetIdentityIgnoresTransportButBindsData(t *testing.T) {
a := Config{LibraryTargetInstance: "cloud-test", LibraryTargetProject: "weknora-test",
LibraryProductionDatabaseURL: "postgres://local@127.0.0.1:5433/target_db", LibraryProductionTenantID: 10000,
LibraryProductionInternalKB: "internal-a", LibraryProductionExternalKB: "external-a",
LibraryProductionIndex: "index-a", LibraryProductionS3Bucket: "bucket-a", LibraryProductionS3Provider: "s3", LibraryProductionS3Endpoint: "https://objects.example"}
want := identityForTest(a)
if want == "" {
t.Fatal("live services must never bind the empty legacy namespace")
}
transport := a
transport.LibraryProductionDatabaseURL = "postgres://remote@database:5432/target_db"
transport.LibraryTargetDockerSocket = "/other/docker.sock"
transport.LibraryArchiveRoot = "/other/archive"
if got := identityForTest(transport); got != want {
t.Fatal("SSH routing must not change logical identity")
}
cases := map[string]func(*Config){
"instance": func(c *Config) { c.LibraryTargetInstance = "other" },
"project": func(c *Config) { c.LibraryTargetProject = "other" },
"database": func(c *Config) { c.LibraryProductionDatabaseURL = "postgres://local@127.0.0.1:5433/other_db" },
"tenant": func(c *Config) { c.LibraryProductionTenantID = 20000 },
"internal": func(c *Config) { c.LibraryProductionInternalKB = "other" },
"external": func(c *Config) { c.LibraryProductionExternalKB = "other" },
"index": func(c *Config) { c.LibraryProductionIndex = "other" },
"bucket": func(c *Config) { c.LibraryProductionS3Bucket = "other" },
"object_endpoint": func(c *Config) { c.LibraryProductionS3Endpoint = "https://other.example" },
}
for name, change := range cases {
t.Run(name, func(t *testing.T) {
c := a
change(&c)
if identityForTest(c) == want {
t.Fatal("changed data target reused old identity")
}
})
}
if identityForTest(Config{}) == "" {
t.Fatal("unconfigured service must not adopt unbound historical evidence")
}
}