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,318 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
func fixtureGeneralGroup(t *testing.T, f *adminBackendFixture, name, visibility string) *Group {
|
||||
t.Helper()
|
||||
g, err := f.s.CreateGroup(f.domains["a"], "alice", name, "Same title", "説明", visibility)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
func TestGeneralGroupBackendVisibilityAndMembers(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
alice := f.local("a", "alice")
|
||||
f.local("a", "bob")
|
||||
carol := f.local("outside", "carol")
|
||||
public := fixtureGeneralGroup(t, f, "aa-public", "public")
|
||||
internal := fixtureGeneralGroup(t, f, "bb-domain", "domain")
|
||||
private := fixtureGeneralGroup(t, f, "cc-members", "members")
|
||||
check := func(user string, want []string) {
|
||||
t.Helper()
|
||||
groups, err := f.s.ListGroups(f.domains["a"], user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got []string
|
||||
for _, g := range groups {
|
||||
got = append(got, g.UUID)
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("visible groups for %q=%v want %v", user, got, want)
|
||||
}
|
||||
}
|
||||
check("", []string{public.UUID})
|
||||
check("carol", []string{public.UUID})
|
||||
check("alice", []string{public.UUID, internal.UUID, private.UUID})
|
||||
check("bob", []string{public.UUID, internal.UUID})
|
||||
if err := f.s.AddGroupMemberByUsername(f.domains["a"], private.UUID, "bob", "member"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
check("bob", []string{public.UUID, internal.UUID, private.UUID})
|
||||
// Corrupt legacy data cannot expose members-only groups across Domains.
|
||||
f.exec(`INSERT INTO group_members(group_uuid,membership_uuid,member_role,created_at_ms) VALUES(%s,%s,'owner',1)`, private.UUID, carol)
|
||||
check("carol", []string{public.UUID})
|
||||
if f.s.GroupMemberRole(private.UUID, "carol") != "" || f.s.GroupMemberRoleByMembership(private.UUID, carol) != "" {
|
||||
t.Fatal("foreign membership has a local role")
|
||||
}
|
||||
if err := f.s.RemoveGroupMember(private.UUID, alice); err == nil {
|
||||
t.Fatal("foreign owner satisfied last local owner guard")
|
||||
}
|
||||
members, err := f.s.ListGroupMembers(private.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(members) != 2 || members[0].PrincipalUUID != "alice" || members[0].MembershipUUID != alice || members[1].PrincipalUUID != "bob" {
|
||||
t.Fatalf("member list mismatch: %+v", members)
|
||||
}
|
||||
f.exec(`UPDATE principals SET state='readonly' WHERE uuid='bob'`)
|
||||
check("bob", []string{public.UUID, internal.UUID, private.UUID})
|
||||
f.exec(`UPDATE principals SET state='suspended' WHERE uuid='bob'`)
|
||||
check("bob", []string{public.UUID})
|
||||
f.exec(`UPDATE principals SET state='active' WHERE uuid='bob'`)
|
||||
f.exec(`UPDATE domain_memberships SET state='suspended' WHERE domain_uuid=%s AND principal_uuid='bob'`, f.domains["a"])
|
||||
check("bob", []string{public.UUID})
|
||||
f.exec(`UPDATE groups SET state='archived' WHERE uuid=%s`, public.UUID)
|
||||
check("", nil)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGeneralGroupBackendCreationAndRollback(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
f.local("a", "alice")
|
||||
f.local("outside", "carol")
|
||||
g, err := f.s.CreateGroup(f.domains["a"], "alice", " TEAM ", "", "日本語説明", "unknown")
|
||||
if err != nil || g.Name != "team" || g.DisplayName != "team" || g.Visibility != "members" || g.CreatedAtMS < 1_000_000_000_000 {
|
||||
t.Fatalf("group creation: %+v %v", g, err)
|
||||
}
|
||||
if _, err := f.s.CreateGroup(f.domains["a"], "alice", "team", "duplicate", "", "public"); err == nil {
|
||||
t.Fatal("duplicate group accepted")
|
||||
}
|
||||
if _, err := f.s.CreateGroup(f.domains["a"], "carol", "foreign-owner", "", "", "members"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("foreign owner: %v", err)
|
||||
}
|
||||
for _, state := range []string{"readonly", "suspended", "quarantined"} {
|
||||
f.exec(`UPDATE principals SET state=%s WHERE uuid='alice'`, state)
|
||||
if _, err := f.s.CreateGroup(f.domains["a"], "alice", "inactive-owner", "", "", "members"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("inactive owner: %v", err)
|
||||
}
|
||||
}
|
||||
f.exec(`UPDATE principals SET state='active' WHERE uuid='alice'`)
|
||||
f.exec(`ALTER TABLE group_members RENAME COLUMN member_role TO unavailable_role`)
|
||||
if _, err := f.s.CreateGroup(f.domains["a"], "alice", "must-rollback", "", "", "public"); err == nil {
|
||||
t.Fatal("partial creation accepted")
|
||||
}
|
||||
if _, err := f.s.GroupByName(f.domains["a"], "must-rollback"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("group creation did not roll back", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGeneralGroupBackendAdoptsDerivedMembership(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
f.local("a", "alice")
|
||||
f.add("a", "alice", "owner")
|
||||
g, err := f.s.CreateGroup(f.domains["b"], "alice", "independent-team", "", "", "members")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.s.SetCampusAdminInheritance(f.groups["a"], false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.allowed("b", "alice", false)
|
||||
f.membership("b", "alice", true)
|
||||
if f.s.GroupMemberRole(g.UUID, "alice") != "owner" {
|
||||
t.Fatal("independent ownership lost")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGroupPermissionBackendContract(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
f.local("a", "alice")
|
||||
f.local("a", "bob")
|
||||
foreign := f.local("outside", "carol")
|
||||
g := fixtureGeneralGroup(t, f, "permissions", "members")
|
||||
if err := f.s.AddGroupMemberByUsername(f.domains["a"], g.UUID, "bob", "member"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
profiles := map[string]map[string]bool{
|
||||
"read": {"read": true},
|
||||
"read_write_once": {"read": true, "create": true, "upload": true},
|
||||
"read_write": {"read": true, "create": true, "write": true, "upload": true, "rename": true, "link": true},
|
||||
"upload_only": {"upload": true},
|
||||
}
|
||||
for profile, allows := range profiles {
|
||||
if err := f.s.SetGroupPermission(g.UUID, " /literal%_ ", profile); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, capability := range []string{"read", "write", "create", "upload", "rename", "link", "delete", "manage"} {
|
||||
err := f.s.authorizeGrant(AuthorizationRequest{DomainUUID: f.domains["a"], PrincipalUUID: "bob", Capability: capability, TargetType: "path", TargetID: "/literal%_/page"}, false)
|
||||
if allows[capability] && err != nil {
|
||||
t.Fatalf("%s/%s denied: %v", profile, capability, err)
|
||||
}
|
||||
if !allows[capability] && !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatalf("%s/%s must be denied: %v", profile, capability, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := f.s.SetGroupPermission(g.UUID, "/literal%_", "read_write"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
permissions, err := f.s.ListGroupPermissions(g.UUID)
|
||||
if err != nil || len(permissions) != 1 || permissions[0].ScopePath != "/literal%_" {
|
||||
t.Fatalf("permission upsert: %+v %v", permissions, err)
|
||||
}
|
||||
for _, path := range []string{"/literalXX/page", "/literal%_suffix/page", "/other"} {
|
||||
if allowed, err := f.s.groupPermissionAllows(f.domains["a"], "bob", "read", path); err != nil || allowed {
|
||||
t.Fatalf("scope leaked: %s %v", path, err)
|
||||
}
|
||||
}
|
||||
f.exec(`INSERT INTO group_members(group_uuid,membership_uuid,member_role,created_at_ms) VALUES(%s,%s,'owner',1)`, g.UUID, foreign)
|
||||
if allowed, err := f.s.groupPermissionAllows(f.domains["a"], "carol", "read", "/literal%_/page"); err != nil || allowed {
|
||||
t.Fatal("foreign membership granted access", err)
|
||||
}
|
||||
f.exec(`UPDATE principals SET state='readonly' WHERE uuid='bob'`)
|
||||
for _, capability := range []string{"read", "write", "create", "upload"} {
|
||||
allowed, err := f.s.groupPermissionAllows(f.domains["a"], "bob", capability, "/literal%_/page")
|
||||
if err != nil || allowed != (capability == "read") {
|
||||
t.Fatal("readonly restriction violated", err)
|
||||
}
|
||||
}
|
||||
if err := f.s.SetGroupPermission("missing-group", "/", "read"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("orphan permission: %v", err)
|
||||
}
|
||||
if err := f.s.SetGroupPermission(g.UUID, "/", "invalid"); err == nil {
|
||||
t.Fatal("invalid permission accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRoleGrantBackendContract(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
alice := f.local("a", "alice")
|
||||
f.local("b", "alice")
|
||||
f.exec(`INSERT INTO auth_roles(uuid,domain_uuid,name,created_at_ms) VALUES('writer',%s,'writer',1)`, f.domains["a"])
|
||||
f.exec(`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(%s,'writer',1)`, alice)
|
||||
f.exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('write','writer','write','spot','portal',1)`)
|
||||
for _, capability := range []string{"write", "create", "upload", "delete"} {
|
||||
request := AuthorizationRequest{DomainUUID: f.domains["a"], PrincipalUUID: "alice", Capability: capability, TargetType: "spot", TargetID: "portal"}
|
||||
err := f.s.authorizeGrant(request, false)
|
||||
if capability == "delete" {
|
||||
if !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("write implies delete", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
foreign := f.local("b", "alice")
|
||||
f.exec(`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(%s,'writer',1)`, foreign)
|
||||
if err := f.s.authorizeGrant(AuthorizationRequest{DomainUUID: f.domains["b"], PrincipalUUID: "alice", Capability: "write", TargetType: "spot", TargetID: "portal"}, false); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("foreign Role binding granted access", err)
|
||||
}
|
||||
f.exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('broad','writer','read','domain',%s,1)`, f.domains["a"])
|
||||
request := AuthorizationRequest{DomainUUID: f.domains["a"], PrincipalUUID: "alice", Capability: "read", TargetType: "object", TargetID: "object-1"}
|
||||
if err := f.s.authorizeGrant(request, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.s.authorizeGrant(request, true); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("broad grant bypassed exact-object mode", err)
|
||||
}
|
||||
f.exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES('exact','writer','read','object','object-1',1)`)
|
||||
if err := f.s.authorizeGrant(request, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.exec(`UPDATE principals SET state='suspended' WHERE uuid='alice'`)
|
||||
if err := f.s.authorizeGrant(request, true); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("suspended principal authorized", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestGeneralGroupBackendCancellationAndErrorBoundary(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
f.local("a", "alice")
|
||||
g := fixtureGeneralGroup(t, f, "cancel-test", "public")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, err := f.s.CreateGroupContext(ctx, f.domains["a"], "alice", "cancelled", "", "", "public"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.s.ListGroupsContext(ctx, f.domains["a"], "alice"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.s.ListGroupMembersContext(ctx, g.UUID); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.s.SetGroupPermissionContext(ctx, g.UUID, "/", "read"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.s.ListGroupPermissionsContext(ctx, g.UUID); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := AuthorizationRequest{DomainUUID: f.domains["a"], PrincipalUUID: "alice", Capability: "read", TargetType: "path", TargetID: "/"}
|
||||
if err := f.s.authorizeGrantContext(ctx, f.s.DB, request, false); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.s.sqlBackend == storePostgreSQL {
|
||||
// Synthetic constraint details must not expose an entered group title.
|
||||
f.exec(`ALTER TABLE groups ADD CONSTRAINT synthetic_title_check CHECK(display_name!='private-test-title')`)
|
||||
_, err := f.s.CreateGroup(f.domains["a"], "alice", "rejected", "private-test-title", "", "public")
|
||||
var safe *db.PostgreSQLError
|
||||
if !errors.As(err, &safe) || safe.SQLState != "23514" || strings.Contains(err.Error(), "private-test-title") {
|
||||
t.Fatalf("unsafe PostgreSQL error: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrincipalStateBackendAtomicRevocation(t *testing.T) {
|
||||
forAdminBackends(t, func(t *testing.T, f *adminBackendFixture) {
|
||||
f.local("a", "alice")
|
||||
f.add("a", "alice", "owner")
|
||||
for _, domain := range []string{"a", "b"} {
|
||||
f.exec(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms) VALUES(%s,%s,'alice',%s,1,9999999999999,1)`, domain, domain+"-synthetic-hash", f.domains[domain])
|
||||
}
|
||||
if err := f.s.SetPrincipalState(f.domains["outside"], "alice", "suspended"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("unrelated Domain state update: %v", err)
|
||||
}
|
||||
var firstRevocation int64
|
||||
for _, state := range []string{"readonly", "suspended", "quarantined"} {
|
||||
if err := f.s.SetPrincipalState(f.domains["a"], "alice", state); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.membership("b", "alice", false)
|
||||
f.allowed("b", "alice", false)
|
||||
var revoked int64
|
||||
if err := f.row(`SELECT min(revoked_at_ms) FROM auth_sessions WHERE principal_uuid='alice'`).Scan(&revoked); err != nil || revoked <= 0 {
|
||||
t.Fatal("session revocation missing", err)
|
||||
}
|
||||
if firstRevocation == 0 {
|
||||
firstRevocation = revoked
|
||||
} else if firstRevocation != revoked {
|
||||
t.Fatal("revocation history overwritten")
|
||||
}
|
||||
if err := f.s.SetPrincipalState(f.domains["a"], "alice", "active"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.allowed("b", "alice", true)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := f.s.SetPrincipalStateContext(ctx, f.domains["a"], "alice", "suspended"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.exec(`ALTER TABLE auth_sessions RENAME COLUMN revoked_at_ms TO missing_revocation`)
|
||||
if err := f.s.SetPrincipalState(f.domains["a"], "alice", "suspended"); err == nil {
|
||||
t.Fatal("partial principal suspension accepted")
|
||||
}
|
||||
var state string
|
||||
if err := f.row(`SELECT state FROM principals WHERE uuid='alice'`).Scan(&state); err != nil || state != "active" {
|
||||
t.Fatal("principal suspension did not roll back", err)
|
||||
}
|
||||
f.allowed("b", "alice", true)
|
||||
f.membership("b", "alice", true)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user