167 lines
6.9 KiB
Go
167 lines
6.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
const MaxNavigationEntries = 20000
|
|
|
|
var ErrNavigationBudget = errors.New("navigation snapshot exceeds its bounded budget")
|
|
|
|
// A database-backed generation observes other processes and CLI writes too.
|
|
// It invalidates data caches, never caches a Principal's authorization result.
|
|
func (s *Store) ensureNavigationSchema() error {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
return s.ensurePostgreSQLPublication(context.Background())
|
|
}
|
|
if _, err := s.DB.Exec(`CREATE TABLE IF NOT EXISTS navigation_generation(id INTEGER PRIMARY KEY CHECK(id=1),version INTEGER NOT NULL); INSERT OR IGNORE INTO navigation_generation VALUES(1,1)`); err != nil {
|
|
return err
|
|
}
|
|
for _, table := range []string{"locality_auto_publication", "locality_publication_policies", "locality_access_policies", "locality_access_entries", "city_owner_bindings", "user_space_policies", "user_space_path_policies", "principals", "domain_memberships", "group_members", "groups", "group_permissions", "auth_roles", "auth_role_bindings", "auth_capability_grants", "campuses", "paths", "revisions", "domains", "config", "zones", "spots", "street_spots", "spot_pages", "spot_relations", "street_inbox_pages", "city_page_overrides", "domain_topology_versions", "city_governance_epochs", "locality_publication_entries", "locality_publication_manifests"} {
|
|
var exists bool
|
|
if err := s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name=?)`, table).Scan(&exists); err != nil {
|
|
return err
|
|
}
|
|
if !exists {
|
|
continue
|
|
}
|
|
for _, op := range []string{"INSERT", "UPDATE", "DELETE"} {
|
|
condition := ""
|
|
switch table {
|
|
case "principals", "domain_memberships", "group_members", "groups", "group_permissions", "auth_roles", "auth_role_bindings", "auth_capability_grants", "city_owner_bindings":
|
|
condition = " WHEN EXISTS(SELECT 1 FROM locality_auto_publication WHERE mode='automatic')"
|
|
}
|
|
q := fmt.Sprintf(`CREATE TRIGGER IF NOT EXISTS navigation_%s_%s AFTER %s ON %s%s BEGIN UPDATE navigation_generation SET version=version+1 WHERE id=1; END`, table, op, op, table, condition)
|
|
if _, err := s.DB.Exec(q); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) NavigationGeneration(ctx context.Context) (_ int64, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("NavigationGeneration", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
var version int64
|
|
err := s.DB.QueryRowContext(ctx, `SELECT version FROM navigation_generation WHERE id=1`).Scan(&version)
|
|
return version, err
|
|
}
|
|
|
|
func (s *Store) NavigationPaths(ctx context.Context, domain string) (_ []string, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("NavigationPaths", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT path FROM paths WHERE domain_uuid=`+s.sqlParam(1)+` ORDER BY path LIMIT `+s.sqlParam(2), domain, MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []string{}
|
|
bytes := 0
|
|
for rows.Next() {
|
|
var p string
|
|
if err = rows.Scan(&p); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, p)
|
|
bytes += len(p)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
type PublishedNavigationPage struct {
|
|
Path string
|
|
Revision Revision
|
|
}
|
|
|
|
func (s *Store) PublicNavigation(ctx context.Context, domain string) (_ []PublishedNavigationPage, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("PublicNavigation", resultErr) }()
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
// Same live binding/head/epoch checks as AnonymousRevision. No canonical
|
|
// bodies, historical revisions, unapproved names or private child counts.
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return nil, s.sqlError("public navigation", err)
|
|
}
|
|
defer tx.Rollback()
|
|
rows, err := tx.QueryContext(ctx, `SELECT DISTINCT e.path,r.uuid,r.object_uuid,r.document_hash,COALESCE(NULLIF(r.lang,''),'und'),COALESCE(NULLIF(r.content_type,''),'text/html; charset=utf-8'),COALESCE(r.filename,''),r.created_at_ms `+s.anonymousRevisionFromSQL()+`
|
|
ORDER BY e.path,r.created_at_ms DESC,r.uuid LIMIT `+s.sqlParam(2), domain, MaxNavigationEntries+1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []PublishedNavigationPage{}
|
|
bytes := 0
|
|
for rows.Next() {
|
|
var p PublishedNavigationPage
|
|
v := &p.Revision
|
|
if err = rows.Scan(&p.Path, &v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.CreatedAtMS); err != nil {
|
|
return nil, err
|
|
}
|
|
if !PublicSemanticPath(p.Path) {
|
|
continue
|
|
}
|
|
out = append(out, p)
|
|
bytes += len(p.Path) + len(v.Filename) + len(v.ContentType)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
rows.Close()
|
|
automatic, err := s.automaticPublicPagesTx(ctx, tx, domain, "")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, p := range out {
|
|
seen[p.Path+"\x00"+p.Revision.Language] = true
|
|
}
|
|
for _, p := range automatic {
|
|
key := p.Path + "\x00" + p.Revision.Language
|
|
if seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
out = append(out, p)
|
|
bytes += len(p.Path) + len(p.Revision.Filename) + len(p.Revision.ContentType)
|
|
if len(out) > MaxNavigationEntries || bytes > 4<<20 {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (s *Store) anonymousRevisionFromSQL() string {
|
|
return `FROM locality_publication_entries e JOIN locality_publication_manifests m ON m.domain_uuid=e.domain_uuid AND m.node_kind=e.node_kind AND m.node_uuid=e.node_uuid
|
|
JOIN paths p ON p.domain_uuid=e.domain_uuid AND p.path=e.path AND p.object_uuid=e.object_uuid
|
|
JOIN revisions r ON r.uuid=e.revision_uuid AND r.object_uuid=e.object_uuid AND r.document_hash=e.document_hash AND COALESCE(NULLIF(r.lang,''),'und')=e.lang
|
|
JOIN domains d ON d.uuid=e.domain_uuid AND d.enabled=1
|
|
JOIN campuses c ON c.uuid=d.campus_uuid AND c.enabled=1
|
|
WHERE e.domain_uuid=` + s.sqlParam(1) + ` AND m.epoch=COALESCE((SELECT version FROM city_governance_epochs WHERE domain_uuid=e.domain_uuid),0)
|
|
AND m.topology_version=COALESCE((SELECT version FROM domain_topology_versions WHERE domain_uuid=e.domain_uuid),1)
|
|
AND (SELECT value FROM config WHERE domain_uuid=e.domain_uuid AND key='authorization_mode')='enforcing'
|
|
AND r.uuid=(SELECT head.uuid FROM revisions head WHERE head.object_uuid=r.object_uuid AND COALESCE(NULLIF(head.lang,''),'und')=e.lang ORDER BY ` + s.revisionHeadOrder("head.") + ` LIMIT 1)`
|
|
}
|
|
|
|
// Match each backend's existing canonical head ordering. Legacy SQLite uses
|
|
// insertion rowid; native PostgreSQL metadata uses UUID as its stable tie-break.
|
|
func (s *Store) revisionHeadOrder(prefix string) string {
|
|
tie := "rowid"
|
|
if s.sqlBackend == storePostgreSQL {
|
|
tie = "uuid"
|
|
}
|
|
return prefix + "created_at_ms DESC," + prefix + tie + " DESC"
|
|
}
|