feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s
Build and publish container images / images (push) Failing after 12m29s
This commit is contained in:
1 parent
96065d1e89
commit
0ba1dcfef0
243 files changed
+21441
-3090
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAdminAuthorizationBackendContract(t *testing.T) {
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
||||
s := testNamespaceStore(t, backend)
|
||||
ctx := context.Background()
|
||||
if backend == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLAuthFoundation(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
campus, err := s.GetDefaultCampus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := seedNamespaceDomain(t, s, campus.UUID, "other-admin-domain", "other-admin-domain")
|
||||
exec := func(query string, args ...any) {
|
||||
t.Helper()
|
||||
if _, err := s.DB.ExecContext(ctx, query, args...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
exec(`INSERT INTO principals(uuid,display_name,created_at_ms,updated_at_ms) VALUES('admin-test-principal','管理者',1,1)`)
|
||||
exec(`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,created_at_ms) VALUES('admin-test-membership',`+s.sqlParam(1)+`,'admin-test-principal',1)`, domain.UUID)
|
||||
exec(`INSERT INTO groups(uuid,domain_uuid,name,builtin_key,created_at_ms,updated_at_ms) VALUES('admin-test-group',`+s.sqlParam(1)+`,'admin-test-group','campus_admin',1,1)`, domain.UUID)
|
||||
exec(`INSERT INTO group_members(group_uuid,membership_uuid,member_role,created_at_ms) VALUES('admin-test-group','admin-test-membership','owner',1)`)
|
||||
check := func(want bool) {
|
||||
t.Helper()
|
||||
got, err := s.IsBuiltinGroupAdminContext(ctx, domain.UUID, "admin-test-principal", "campus_admin")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("group allowed=%v want=%v error=%v", got, want, err)
|
||||
}
|
||||
got, err = s.IsCampusAdminContext(ctx, campus.UUID, "admin-test-principal")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("campus allowed=%v want=%v error=%v", got, want, err)
|
||||
}
|
||||
}
|
||||
check(true)
|
||||
for _, field := range []string{"principals", "domain_memberships", "groups"} {
|
||||
exec(`UPDATE ` + field + ` SET state='suspended'`)
|
||||
check(false)
|
||||
exec(`UPDATE ` + field + ` SET state='active'`)
|
||||
}
|
||||
exec(`UPDATE domains SET enabled=0 WHERE uuid=`+s.sqlParam(1), domain.UUID)
|
||||
check(false)
|
||||
exec(`UPDATE domains SET enabled=1 WHERE uuid=`+s.sqlParam(1), domain.UUID)
|
||||
for _, role := range []string{"member", "manager", "owner"} {
|
||||
exec(`UPDATE group_members SET member_role=`+s.sqlParam(1)+` WHERE group_uuid='admin-test-group'`, role)
|
||||
check(role != "member")
|
||||
}
|
||||
// A forged cross-domain group membership must not confer authority.
|
||||
exec(`UPDATE groups SET domain_uuid=`+s.sqlParam(1)+` WHERE uuid='admin-test-group'`, other.UUID)
|
||||
check(false)
|
||||
exec(`UPDATE groups SET domain_uuid=`+s.sqlParam(1)+` WHERE uuid='admin-test-group'`, domain.UUID)
|
||||
check(true)
|
||||
for _, key := range []string{"domain_admin", "made-up", "campus_admin' OR 1=1--"} {
|
||||
if s.IsBuiltinGroupAdmin(domain.UUID, "admin-test-principal", key) {
|
||||
t.Fatal("unexpected administrative grant")
|
||||
}
|
||||
}
|
||||
if s.IsBuiltinGroupAdmin(other.UUID, "admin-test-principal", "campus_admin") || s.IsCampusAdmin("other-campus", "admin-test-principal") {
|
||||
t.Fatal("cross namespace grant")
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if allowed, err := s.IsCampusAdminContext(cancelled, campus.UUID, "admin-test-principal"); allowed || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancellation: allowed=%v error=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := s.IsBuiltinGroupAdminContext(cancelled, domain.UUID, "admin-test-principal", "campus_admin"); allowed || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancellation: allowed=%v error=%v", allowed, err)
|
||||
}
|
||||
exec(`ALTER TABLE group_members RENAME COLUMN member_role TO unavailable_role`)
|
||||
if s.IsCampusAdmin(campus.UUID, "admin-test-principal") || s.IsBuiltinGroupAdmin(domain.UUID, "admin-test-principal", "campus_admin") {
|
||||
t.Fatal("database error must fail closed")
|
||||
}
|
||||
if allowed, err := s.IsCampusAdminContext(ctx, campus.UUID, "admin-test-principal"); allowed || err == nil {
|
||||
t.Fatal("context API must report database error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLAuthFoundationMigration(t *testing.T) {
|
||||
s := testNamespaceStore(t, storePostgreSQL)
|
||||
ctx := context.Background()
|
||||
if err := s.EnsureAuthSchema(); err != nil {
|
||||
t.Fatalf("native authentication schema: %v", err)
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
errorsCh := make(chan error, 4)
|
||||
for i := 0; i < 4; i++ {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); errorsCh <- s.ensurePostgreSQLAuthFoundation(ctx) }()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errorsCh)
|
||||
for err := range errorsCh {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.DB.Exec(`INSERT INTO principals(uuid,created_at_ms,updated_at_ms) VALUES('retained',1,1)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var avatar []byte
|
||||
if err := s.DB.QueryRow(`SELECT avatar FROM principals WHERE uuid='retained'`).Scan(&avatar); err != nil || len(avatar) != 0 {
|
||||
t.Fatalf("avatar default: %v", err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`ALTER TABLE principals RENAME COLUMN avatar TO missing_avatar`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.ensurePostgreSQLAuthFoundation(ctx); err == nil {
|
||||
t.Fatal("schema drift accepted")
|
||||
}
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM principals WHERE uuid='retained'`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("existing identity lost")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user