feat(storage): add native PostgreSQL runtime and verified offline application import
Build and publish container images / images (push) Failing after 12m29s

This commit is contained in:
Anya Tokugawa committed 2026-11-15 05:47:14 +09:00
1 parent 96065d1e89
commit 0ba1dcfef0
243 files changed
+21441 -3090

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
package store
import (
"context"
"database/sql"
"errors"
"pageswiki/internal/db"
)
// Requires namespace, core metadata and authentication migrations. This is a
// component foundation, not the complete City/publication pipeline.
func (s *Store) ensurePostgreSQLLocalityFoundation(ctx context.Context) error {
if err := s.ensurePostgreSQLCityFoundation(ctx); err != nil {
return err
}
scope := s.pathScopeSQL
statements := []string{
`CREATE TABLE locality_access_policies(domain_uuid TEXT COLLATE "C" NOT NULL,node_kind TEXT COLLATE "C" NOT NULL,node_uuid TEXT COLLATE "C" NOT NULL,scope_path TEXT COLLATE "C" NOT NULL,grants_access INTEGER NOT NULL CHECK(grants_access IN (0,1)),actor_uuid TEXT COLLATE "C" NOT NULL,authority_rank INTEGER NOT NULL CHECK(authority_rank BETWEEN 1 AND 3),PRIMARY KEY(domain_uuid,node_kind,node_uuid))`,
`CREATE INDEX idx_locality_access_scope ON locality_access_policies(domain_uuid,scope_path)`,
`CREATE TABLE locality_access_entries(domain_uuid TEXT COLLATE "C" NOT NULL,node_kind TEXT COLLATE "C" NOT NULL,node_uuid TEXT COLLATE "C" NOT NULL,subject_kind TEXT NOT NULL CHECK(subject_kind IN ('user','group')),subject_uuid TEXT COLLATE "C" NOT NULL,subject_name TEXT NOT NULL,permission TEXT NOT NULL CHECK(permission IN ('read','read_write')),PRIMARY KEY(domain_uuid,node_kind,node_uuid,subject_kind,subject_uuid))`,
`CREATE FUNCTION pages_locality_config_guard() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_advisory_xact_lock(1885431653,1853058931);
IF TG_OP='INSERT' THEN
IF NEW.key='authorization_mode' AND NEW.value!='enforcing' AND EXISTS(SELECT 1 FROM locality_access_policies WHERE domain_uuid=NEW.domain_uuid) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_requires_enforcing';
END IF;
ELSE
IF OLD.key='authorization_mode' AND EXISTS(SELECT 1 FROM locality_access_policies WHERE domain_uuid=OLD.domain_uuid) THEN
IF TG_OP='DELETE' THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_requires_enforcing';
ELSIF NEW.key!=OLD.key OR NEW.domain_uuid!=OLD.domain_uuid OR NEW.value!='enforcing' THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_requires_enforcing';
END IF;
END IF;
END IF;
IF TG_OP='DELETE' THEN RETURN OLD; END IF;
RETURN NEW;
END $$`,
`CREATE TRIGGER locality_access_config_guard BEFORE INSERT OR UPDATE OR DELETE ON config FOR EACH ROW EXECUTE FUNCTION pages_locality_config_guard()`,
`CREATE FUNCTION pages_locality_path_guard() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_advisory_xact_lock(1885431653,1853058931);
IF TG_OP='DELETE' THEN
IF EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=OLD.domain_uuid AND ` + scope("a.scope_path", "OLD.path") + `) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
RETURN OLD;
ELSIF TG_OP='UPDATE' THEN
IF OLD.path=NEW.path AND OLD.object_uuid=NEW.object_uuid AND OLD.domain_uuid=NEW.domain_uuid THEN RETURN NEW; END IF;
IF EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=OLD.domain_uuid AND ` + scope("a.scope_path", "OLD.path") + `) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
ELSIF NOT EXISTS(SELECT 1 FROM revisions WHERE object_uuid=NEW.object_uuid) AND NOT EXISTS(SELECT 1 FROM paths WHERE object_uuid=NEW.object_uuid) THEN
RETURN NEW;
END IF;
IF EXISTS(SELECT 1 FROM locality_access_policies a WHERE a.domain_uuid=NEW.domain_uuid AND ` + scope("a.scope_path", "NEW.path") + `)
OR EXISTS(SELECT 1 FROM paths p JOIN locality_access_policies a ON a.domain_uuid=p.domain_uuid WHERE p.object_uuid=NEW.object_uuid AND ` + scope("a.scope_path", "p.path") + `) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
RETURN NEW;
END $$`,
`CREATE TRIGGER locality_access_path_guard BEFORE INSERT OR DELETE OR UPDATE OF path,object_uuid,domain_uuid ON paths FOR EACH ROW EXECUTE FUNCTION pages_locality_path_guard()`,
}
steps := []db.PostgreSQLMigration{{Version: 1, Name: "locality access and core namespace guards", Statements: statements, Assertions: []string{
`SELECT domain_uuid,node_kind,node_uuid,scope_path,grants_access,actor_uuid,authority_rank FROM locality_access_policies WHERE 1=0`,
`SELECT domain_uuid,node_kind,node_uuid,subject_kind,subject_uuid,subject_name,permission FROM locality_access_entries WHERE 1=0`,
`SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='paths'::regclass AND tgname='locality_access_path_guard' AND tgenabled='O' AND NOT tgisinternal)`,
`SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='config'::regclass AND tgname='locality_access_config_guard' AND tgenabled='O' AND NOT tgisinternal)`,
}}}
cityGuards := []string{}
assertions := append([]string{}, steps[0].Assertions...)
for _, table := range []string{"zones", "spots"} {
columns := "path_prefix,state,domain_uuid"
if table == "zones" {
columns += ",parent_uuid"
}
name := "locality_access_" + table + "_structure"
cityGuards = append(cityGuards, `CREATE FUNCTION pages_`+name+`() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_advisory_xact_lock(1885431653,1853058931);
IF EXISTS(SELECT 1 FROM locality_access_policies WHERE domain_uuid=OLD.domain_uuid) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
IF TG_OP='UPDATE' AND EXISTS(SELECT 1 FROM locality_access_policies WHERE domain_uuid=NEW.domain_uuid) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
IF TG_OP='DELETE' THEN RETURN OLD; END IF;
RETURN NEW;
END $$`, `CREATE TRIGGER `+name+` BEFORE DELETE OR UPDATE OF `+columns+` ON `+table+` FOR EACH ROW EXECUTE FUNCTION pages_`+name+`()`)
assertions = append(assertions, `SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='`+table+`'::regclass AND tgname='`+name+`' AND tgenabled='O' AND NOT tgisinternal)`)
}
cityGuards = append(cityGuards, `CREATE FUNCTION pages_locality_street_spots_guard() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM pg_advisory_xact_lock(1885431653,1853058931);
IF TG_OP!='INSERT' THEN
IF EXISTS(SELECT 1 FROM spots s JOIN locality_access_policies a ON a.domain_uuid=s.domain_uuid WHERE s.uuid=OLD.spot_uuid) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
END IF;
IF TG_OP!='DELETE' THEN
IF EXISTS(SELECT 1 FROM spots s JOIN locality_access_policies a ON a.domain_uuid=s.domain_uuid WHERE s.uuid=NEW.spot_uuid) THEN
RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='locality_access_protected_namespace';
END IF;
END IF;
IF TG_OP='DELETE' THEN RETURN OLD; END IF;
RETURN NEW;
END $$`, `CREATE TRIGGER locality_access_street_spots_guard BEFORE INSERT OR UPDATE OR DELETE ON street_spots FOR EACH ROW EXECUTE FUNCTION pages_locality_street_spots_guard()`)
assertions = append(assertions, `SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='street_spots'::regclass AND tgname='locality_access_street_spots_guard' AND tgenabled='O' AND NOT tgisinternal)`)
steps = append(steps, db.PostgreSQLMigration{Version: 2, Name: "protected City structure", Statements: cityGuards, Assertions: assertions})
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-locality-foundation", steps)
}
func (s *Store) installLocalityAccessGuards(ctx context.Context, tx *sql.Tx) error {
if s.sqlBackend == storeSQLite {
return installLocalityAccessGuards(ctx, tx)
}
// DDL belongs to initialization, never to an authorization transaction.
for _, target := range []struct{ table, trigger string }{
{"paths", "locality_access_path_guard"}, {"config", "locality_access_config_guard"},
{"zones", "locality_access_zones_structure"}, {"spots", "locality_access_spots_structure"},
{"street_spots", "locality_access_street_spots_guard"},
} {
var enabled bool
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid=to_regclass($1) AND tgname=$2 AND tgenabled='O' AND NOT tgisinternal)`, target.table, target.trigger).Scan(&enabled); err != nil {
return err
}
if !enabled {
return errors.New("locality namespace protection unavailable")
}
}
return nil
}