135 lines
4.4 KiB
Go
135 lines
4.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"pageswiki/internal/schutzaria"
|
|
)
|
|
|
|
// ReadPathPolicy is a request-scoped, read-only policy snapshot.
|
|
type ReadPathPolicy = schutzaria.ReadPathPolicy
|
|
|
|
// LoadReadPathPolicy uses one bounded query, independent of the number of
|
|
// directory entries. It deliberately excludes Object grants: an Object grant
|
|
// does not reveal all of that Object's semantic aliases.
|
|
func (s *Store) LoadReadPathPolicy(domain, principal string) (*ReadPathPolicy, error) {
|
|
return s.LoadReadPathPolicyContext(context.Background(), domain, principal)
|
|
}
|
|
|
|
func (s *Store) LoadReadPathPolicyContext(ctx context.Context, domain, principal string) (*ReadPathPolicy, error) {
|
|
var policy *ReadPathPolicy
|
|
err := s.policyRead(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
|
var err error
|
|
policy, err = s.loadReadPathPolicy(cityReadQuery{ctx, tx}, domain, principal)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return policy, nil
|
|
}
|
|
|
|
func (s *Store) policyRead(ctx context.Context, read func(context.Context, *sql.Tx) error) error {
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
// Grants and restrictions must come from the same database snapshot.
|
|
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
|
if err != nil {
|
|
return s.sqlError("read policy snapshot", err)
|
|
}
|
|
defer tx.Rollback()
|
|
if err = read(ctx, tx); err != nil {
|
|
return s.sqlError("read policy", err)
|
|
}
|
|
if err = tx.Commit(); err != nil {
|
|
return s.sqlError("read policy snapshot", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) policyReadOptions() *sql.TxOptions {
|
|
level := sql.LevelSerializable
|
|
if s.sqlBackend == storePostgreSQL {
|
|
level = sql.LevelRepeatableRead
|
|
}
|
|
return &sql.TxOptions{ReadOnly: true, Isolation: level}
|
|
}
|
|
|
|
func (s *Store) lockPolicyWrite(ctx context.Context, tx *sql.Tx) error {
|
|
if s.sqlBackend == storePostgreSQL {
|
|
_, err := tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653,1853058931)`)
|
|
return err
|
|
}
|
|
_, err := tx.ExecContext(ctx, `UPDATE groups SET uuid=uuid WHERE 0`)
|
|
return err
|
|
}
|
|
|
|
func loadReadPathPolicy(q pathQueryer, domain, principal string) (*ReadPathPolicy, error) {
|
|
return (&Store{}).loadReadPathPolicy(q, domain, principal)
|
|
}
|
|
|
|
func (s *Store) loadReadPathPolicy(q pathQueryer, domain, principal string) (*ReadPathPolicy, error) {
|
|
var all bool
|
|
var exact, subtrees []string
|
|
if domain == "" || principal == "" {
|
|
return &ReadPathPolicy{}, ErrAccessDenied
|
|
}
|
|
rows, err := q.Query(`
|
|
SELECT 'role',g.target_type,g.target_id FROM principals p
|
|
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active'
|
|
JOIN auth_role_bindings b ON b.membership_uuid=m.uuid
|
|
JOIN auth_roles r ON r.uuid=b.role_uuid AND r.domain_uuid=m.domain_uuid
|
|
JOIN auth_capability_grants g ON g.role_uuid=r.uuid AND g.capability='read'
|
|
WHERE p.uuid=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') AND g.target_type IN ('domain','path')
|
|
UNION ALL
|
|
SELECT 'group','path',gp.scope_path FROM principals p
|
|
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(3)+` AND m.state='active'
|
|
JOIN group_members gm ON gm.membership_uuid=m.uuid
|
|
JOIN groups g ON g.uuid=gm.group_uuid AND g.domain_uuid=m.domain_uuid AND g.state='active'
|
|
JOIN group_permissions gp ON gp.group_uuid=g.uuid AND gp.permission IN ('read','read_write','read_write_once')
|
|
WHERE p.uuid=`+s.sqlParam(4)+` AND p.state IN ('active','readonly')
|
|
LIMIT 4097`, domain, principal, domain, principal)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
count := 0
|
|
for rows.Next() {
|
|
count++
|
|
if count > 4096 {
|
|
return nil, errors.New("too many read grants for bounded projection")
|
|
}
|
|
var source, kind, target string
|
|
if err := rows.Scan(&source, &kind, &target); err != nil {
|
|
return nil, err
|
|
}
|
|
if source == "role" {
|
|
if kind == "domain" && target == domain || kind == "path" && target == "*" {
|
|
all = true
|
|
} else if kind == "path" {
|
|
exact = append(exact, target)
|
|
}
|
|
} else if target == "/" {
|
|
all = true
|
|
} else if strings.HasPrefix(target, "/") {
|
|
exact = append(exact, target)
|
|
subtrees = append(subtrees, strings.TrimRight(target, "/"))
|
|
}
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
if err = rows.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
access, err := s.loadLocalityAccess(q, domain, principal)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return schutzaria.NewReadPathPolicy(all, exact, subtrees, access), nil
|
|
}
|