diff --git a/README.md b/README.md index ab6cd39..22eb9b4 100644 --- a/README.md +++ b/README.md @@ -1650,11 +1650,18 @@ the package's uses foreign data, so none of the roles needs any. once it is applied. Their checks, searches and changes run beside each other, applied or not. `0034_giving_role_seeks` adds `rbac_grants_principal_role`, which leads to a grant's principal, tenant and role before its scope, so where one of their subject's keys, or its groups', the - tenant's members' or the public's, holds more than 64 grants on scopes of the listed type, `Filter` - and `Reachable` seek those of each role giving the permission alone: they read every grant of - those keys on scopes of the listed type, of any role, so beside a viewer's - grants to the tenant's members on 20,000 clusters a member's `Reachable` read 4,700 pages and a - query of `Filter`'s condition 14,000, against 41 for `Authorize`. Building it keeps every write to + tenant's members' or the public's, holds more than 64 grants on scopes of the listed type, and 4 + or more for each built-in role giving the permission there, `Filter` and `Reachable` seek those of + each such role alone, and of its grants of custom roles there, past 64 naming 16 roles or fewer, + those of each custom role giving the permission that the key holds, where it holds 16 or fewer + custom roles, found a seek each: they read every grant + of those keys on scopes of the listed type, of any role, so beside a viewer's grants to the + tenant's members on 20,000 clusters a member's `Reachable` read 4,700 pages and a query of + `Filter`'s condition 14,000, against 41 for `Authorize`; beside a custom viewer's, 8,900 and + 17,900, against 160 and 380 now. A key holding fewer they read by the scope, as before, so beside + a catalog of 1,000 roles giving the permission, a subject in 20 groups holding 100 grants each + reads 700 pages, not the 60,000 a seek for each role cost. As of a time they read by the scope, + every grant of the keys on the type, of any role, as before it. Building it keeps every write to `rbac_grants` waiting until migrating commits, though checks and searches go on. Code from before it reads as it did, by the scope, so replicas of either release run side by side. Code from before advisory locks were keyed by SHA-256 must not run beside code after it, so a diff --git a/authority_test.go b/authority_test.go index 15d5146..020e2f1 100644 --- a/authority_test.go +++ b/authority_test.go @@ -4485,6 +4485,18 @@ func unvacuumed(t *testing.T, h *harness, tables ...string) { } } +// filled puts a grant, in a tenant of its own, in each of rbac_grants' partial indexes: PostgreSQL +// keeps no height of an empty btree, so planning a statement reads the metapage of each empty one for +// each reference to the table, pages a count of what a search reads takes as its own, again wherever +// a statement is planned again, as a session does after another changes a schema. +func filled(t *testing.T, h *harness) { + t.Helper() + if _, err := h.pool.Exec(t.Context(), `INSERT INTO rbac_grants (tenant, scope_type, scope_id, principal_kind, principal_id, role, source, expires_at, condition) + VALUES ('filled', 'cluster', 'f', 'user', 'f', 'custom.f', 'activation', now() + interval '1 year', 'true')`); err != nil { + t.Fatal(err) + } +} + // crowdedTables are the tables crowd writes, history last, as it records the others. var crowdedTables = []string{ "rbac_grants", "rbac_roles", "rbac_groups", "rbac_memberships", "rbac_members", "rbac_eligibilities", "rbac_denies", "rbac_history", @@ -5406,6 +5418,8 @@ func TestSearchesReadOnlyTheGrantsOfRolesGivingThePermission(t *testing.T) { } { t.Run(plan+"/"+other.what, func(t *testing.T) { h := crowded(t, pgtest.Open(t, "plan_cache_mode="+plan)) + unvacuumed(t, h, "rbac_grants") + filled(t, h) ctx := t.Context() c0 := hostTable(t, h)[0] m := h.as(t, "m", "g") @@ -5442,29 +5456,15 @@ func TestSearchesReadOnlyTheGrantsOfRolesGivingThePermission(t *testing.T) { t.Fatal(err) } } - read := func() map[string]int64 { - t.Helper() - pages := map[string]int64{} - for what, f := range searches { - func() { - tx, err := h.pool.Begin(ctx) - if err != nil { - t.Fatal(err) - } - defer func() { _ = tx.Rollback(ctx) }() - start := pagesRead(ctx, t, tx, "rbac_grants") - if err := f(tx, h.authz.WithTx(tx)); err != nil { - t.Fatalf("%s: %v", what, err) - } - pages[what] = pagesRead(ctx, t, tx, "rbac_grants") - start - }() - } - return pages - } + // Under a generic plan, Filter's statements are planned on whichever search runs them first + // after ANALYZE, Filter or a query of its condition, in the map's order, which read 80 pages + // more: so each is counted on its second run (searchPages), with autovacuum kept off the + // grants, whose vacuum between the two had them planned again, and the grants' partial + // indexes filled, so a statement planned again all the same reads nothing for it. more(500) - few := read() + few := searchPages(ctx, t, h, plan, searches) more(5000) - for what, many := range read() { + for what, many := range searchPages(ctx, t, h, plan, searches) { if many > few[what]*3/2+deeper { t.Errorf("%s read %d pages beside 500 grants that are %s, and %d beside 5000", what, few[what], other.what, many) } @@ -5550,6 +5550,217 @@ func TestSearchesSeekFewGrantsOnceWhateverTheRolesGivingThePermission(t *testing } } +// searchPages are the pages of rbac_grants each of searches reads on tx's own transaction, counted +// under a generic plan on the run after the one that builds it: building it read some two dozen +// pages of the indexes on whichever run built it. +func searchPages(ctx context.Context, t *testing.T, h *harness, plan string, searches map[string]func(pgx.Tx, *rbac.Authorizer) error) map[string]int64 { + t.Helper() + pages := map[string]int64{} + for what, f := range searches { + func() { + tx, err := h.pool.Begin(ctx) + if err != nil { + t.Fatal(err) + } + defer func() { _ = tx.Rollback(ctx) }() + if plan == "force_generic_plan" { + if err := f(tx, h.authz.WithTx(tx)); err != nil { + t.Fatalf("%s: %v", what, err) + } + } + start := pagesRead(ctx, t, tx, "rbac_grants") + if err := f(tx, h.authz.WithTx(tx)); err != nil { + t.Fatalf("%s: %v", what, err) + } + pages[what] = pagesRead(ctx, t, tx, "rbac_grants") - start + }() + } + return pages +} + +// loginSearches are Reachable, Filter and a host's query of Filter's condition, for m's login to +// clusters, which keeps c0 alone. +func loginSearches(ctx context.Context, m rbac.Subject) map[string]func(pgx.Tx, *rbac.Authorizer) error { + cols := rbac.Columns{Tenant: "tenant", Path: "path"} + return map[string]func(pgx.Tx, *rbac.Authorizer) error{ + "Reachable": func(_ pgx.Tx, a *rbac.Authorizer) error { return second(a.Reachable(ctx, m, login, "t1", "cluster")) }, + "Filter": func(_ pgx.Tx, a *rbac.Authorizer) error { + return second(a.Filter(ctx, m, login, "t1", "cluster", cols, 1)) + }, + "a query of Filter's condition": func(tx pgx.Tx, a *rbac.Authorizer) error { + pred, err := a.Filter(ctx, m, login, "t1", "cluster", cols, 1) + if err != nil { + return err + } + var n int + if err := tx.QueryRow(ctx, `SELECT count(*) FROM clusters WHERE `+pred.SQL, pred.Args...).Scan(&n); err != nil { + return err + } + if n != 1 { + return fmt.Errorf("kept %d clusters, not c0 alone", n) + } + return nil + }, + } +} + +// Filter and Reachable read, of the grants of custom roles to their subject, its groups and the +// tenant's members on scopes of the listed type, only those of roles giving the permission there, +// under a generic plan and one for their arguments: past 64 on the type, they read those of built-in +// roles a role at a time but every grant of a custom role, so beside a tenant's viewer granted on +// thousands of clusters, a member's Reachable read 8,900 pages and a host's query of Filter's +// condition 17,900, where Authorize read 14. +func TestSearchesReadOnlyTheCustomGrantsOfRolesGivingThePermission(t *testing.T) { + for _, plan := range []string{"force_custom_plan", "force_generic_plan"} { + for _, other := range []struct{ what, kind, id string }{ + {"the members'", "members", "t1"}, + {"its own", "user", "m"}, + {"its group's", "group", "g"}, + } { + t.Run(plan+"/"+other.what, func(t *testing.T) { + h := crowded(t, pgtest.Open(t, "plan_cache_mode="+plan)) + unvacuumed(t, h, "rbac_grants") + filled(t, h) + ctx := t.Context() + if _, err := h.pool.Exec(ctx, `INSERT INTO rbac_roles (tenant, id, permissions, resource_types) VALUES ('t1', 'custom.viewer', '{cluster:view}', '{cluster}')`); err != nil { + t.Fatal(err) + } + c0 := hostTable(t, h)[0] + m := h.as(t, "m", "g") + h.seed(t, grant(c0.Scope(), "cluster.login", rbac.User, "m")) + searches := loginSearches(ctx, m) + granted := 0 + more := func(n int) { + t.Helper() + if _, err := h.pool.Exec(ctx, `INSERT INTO rbac_grants (tenant, scope_type, scope_id, principal_kind, principal_id, role, condition) + SELECT 't1', 'cluster', 'k' || i, $3, $4, 'custom.viewer', '' FROM generate_series($1::int + 1, $2::int) i`, granted, n, other.kind, other.id); err != nil { + t.Fatal(err) + } + granted = n + if _, err := h.pool.Exec(ctx, `ANALYZE rbac_grants`); err != nil { + t.Fatal(err) + } + } + more(500) + few := searchPages(ctx, t, h, plan, searches) + more(5000) + for what, many := range searchPages(ctx, t, h, plan, searches) { + if many > few[what]*3/2+deeper { + t.Errorf("%s read %d pages beside 500 grants of a custom role giving nothing that are %s, and %d beside 5000", what, few[what], other.what, many) + } + } + }) + } + } +} + +// Filter and Reachable step through a few of the custom roles a key holds at most, to find those +// giving the permission, and only where its grants of custom roles on a type name few, under a +// generic plan and one for their arguments: stepping through each, a seek each, whatever it gave and +// wherever it was granted, the members' grants of 300 custom roles giving nothing, one on each of 300 +// clusters, cost a member's Reachable 1,900 pages where by the scope it read 210, and beside their +// 100 grants of a custom viewer on clusters, each of 1,000 roles granted on another type cost a seek. +func TestSearchesStepThroughFewOfTheCustomRolesAKeyHolds(t *testing.T) { + for _, plan := range []string{"force_custom_plan", "force_generic_plan"} { + for _, other := range []struct{ what, kind, id string }{ + {"the members'", "members", "t1"}, + {"its own", "user", "m"}, + } { + t.Run(plan+"/"+other.what, func(t *testing.T) { + // read is the pages each search reads beside grants of roles custom roles, one on each of as + // many clusters, or with elsewhere, on as many buckets, beside 100 of a viewer on clusters. + read := func(roles int, elsewhere bool) map[string]int64 { + t.Helper() + h := crowded(t, pgtest.Open(t, "plan_cache_mode="+plan)) + unvacuumed(t, h, "rbac_grants") + filled(t, h) + ctx := t.Context() + c0 := hostTable(t, h)[0] + m := h.as(t, "m", "g") + h.seed(t, grant(c0.Scope(), "cluster.login", rbac.User, "m")) + grants := `WITH r AS (INSERT INTO rbac_roles (tenant, id, permissions, resource_types) + SELECT 't1', 'custom.r' || i, '{cluster:view}', '{cluster}' FROM generate_series(1, $1::int) i RETURNING id) + INSERT INTO rbac_grants (tenant, scope_type, scope_id, principal_kind, principal_id, role, condition) + SELECT 't1', 'cluster', 'k' || i, $2, $3, 'custom.r' || i, '' FROM generate_series(1, $1::int) i` + if elsewhere { + grants = `WITH r AS (INSERT INTO rbac_roles (tenant, id, permissions, resource_types) + VALUES ('t1', 'custom.viewer', '{cluster:view}', '{cluster}') RETURNING id) + INSERT INTO rbac_grants (tenant, scope_type, scope_id, principal_kind, principal_id, role, condition) + SELECT 't1', 'bucket', 'b' || i, $2, $3, 'custom.r' || i, '' FROM generate_series(1, $1::int) i + UNION ALL SELECT 't1', 'cluster', 'k' || i, $2, $3, 'custom.viewer', '' FROM generate_series(1, 100) i` + } + if _, err := h.pool.Exec(ctx, grants, roles, other.kind, other.id); err != nil { + t.Fatal(err) + } + if _, err := h.pool.Exec(ctx, `ANALYZE rbac_grants`); err != nil { + t.Fatal(err) + } + return searchPages(ctx, t, h, plan, loginSearches(ctx, m)) + } + few := read(100, true) + for what, many := range read(1000, true) { + if many > few[what]*3/2+deeper { + t.Errorf("%s read %d pages beside grants of 100 custom roles elsewhere that are %s, and %d beside 1000", what, few[what], other.what, many) + } + } + stepped := read(300, false) + rbac.SetHeldByScope(t, 1<<30) + for what, scoped := range read(300, false) { + if stepped[what] > scoped*3/2+deeper { + t.Errorf("%s read %d pages beside grants of 300 custom roles that are %s, and %d by the scope", what, stepped[what], other.what, scoped) + } + } + }) + } + } +} + +// Filter and Reachable read a subject's groups' grants of a type by the scope while each holds +// fewer than a few for each role giving the permission, under a generic plan and one for their +// arguments: past 64 they sought each role giving it, so beside a catalog of 1,000 such roles, 20 +// groups holding 100 grants each read 60,000 pages, where by the scope they read 700. +func TestSearchesReadByTheScopeBesideManyRolesGivingThePermission(t *testing.T) { + for _, plan := range []string{"force_custom_plan", "force_generic_plan"} { + t.Run(plan, func(t *testing.T) { + read := func(roles int) map[string]int64 { + t.Helper() + h := crowded(t, pgtest.Open(t, "plan_cache_mode="+plan), func(c *rbac.Config) { + for i := range roles { + c.Catalog = append(c.Catalog, rbac.RoleDefinition{ + ID: rbac.Role(fmt.Sprint("cluster.operator", i)), + Permissions: []rbac.Permission{login}, ResourceTypes: []string{"cluster"}, + }) + } + }) + unvacuumed(t, h, "rbac_grants") + filled(t, h) + ctx := t.Context() + c0 := hostTable(t, h)[0] + var groups []string + for i := range 20 { + groups = append(groups, fmt.Sprint("g", i)) + } + m := h.as(t, "m", groups...) + h.seed(t, grant(c0.Scope(), "cluster.login", rbac.User, "m")) + if _, err := h.pool.Exec(ctx, `INSERT INTO rbac_grants (tenant, scope_type, scope_id, principal_kind, principal_id, role, condition) + SELECT 't1', 'cluster', 'z' || i, 'group', 'g' || j, 'cluster.viewer', '' FROM generate_series(1, 100) i, generate_series(0, 19) j`); err != nil { + t.Fatal(err) + } + if _, err := h.pool.Exec(ctx, `ANALYZE rbac_grants`); err != nil { + t.Fatal(err) + } + return searchPages(ctx, t, h, plan, loginSearches(ctx, m)) + } + few := read(0) + for what, many := range read(200) { + if many > few[what]*3/2+deeper { + t.Errorf("%s read %d pages of grants beside the catalog's roles giving the permission, and %d beside 200 more", what, few[what], many) + } + } + }) + } +} + // Filter, Reachable and Holders read, of the grants to the tenant's members and the public, only // those of custom roles that their subject counts, under a generic plan and one for their arguments, // and as of a time: which custom roles those grants name was read from each of them on scopes of the diff --git a/authorize.go b/authorize.go index 031b19e..4a7252b 100644 --- a/authorize.go +++ b/authorize.go @@ -5,6 +5,7 @@ import ( "context" "fmt" "maps" + "math" "slices" "strings" "time" @@ -1348,7 +1349,7 @@ func (a *Authorizer) reachable(ctx context.Context, s Subject, p Permission, ten // they name (namedCustom). alone := a.labelPermission(p) rows, err := a.queryIndexed(ctx, a.db, ` - WITH held AS MATERIALIZED (`+a.heldByType("$5", "$2", "$3", custom, "g.scope, g.conditional, g.role, g.principal", ` + WITH held AS MATERIALIZED (`+a.heldByType("$5", "$2", "$3", givingCustomSQL(custom, "$1", "$8", "$9", "keyed.k"), "g.scope, g.conditional, g.role, g.principal", ` g.tenant = $1 AND (g.expires_at IS NULL OR g.expires_at > $4)`)+`) SELECT scope, bool_and(conditional) FROM held WHERE role = ANY(`+givingSQL("$3", "$1", "$8", "$9", custom, namedCustom("SELECT role FROM held"), false, alone)+`) @@ -1666,31 +1667,77 @@ func (a *Authorizer) byType(types, cols, where string) string { // key's few grants of a type cost a search a seek for each role giving the permission, of each key // and type: beside 40 roles giving it, a subject in 200 groups read 72,000 pages where by the scope // it read 3,300. So it reads each key's grants on a type by the scope, as before, up to -// heldByScope of them, and only a key holding that many there a role at a time. -func (a *Authorizer) heldByType(types, principals, roles string, custom bool, cols, where string) string { +// heldByScope of them, or heldPerRole for each built-in role giving the permission where that is +// more, and only a key holding that many there a role at a time: beside a catalog of 1,000 roles +// giving it, a role at a time, 20 groups holding 100 grants each read 60,000 pages, where by the +// scope they read 700. Such a key's grants of custom roles there it reads apart (byScopeOrRole): +// by the scope, by rbac_grants_custom_principal, up to 64, and past that a role at a time for those +// of the custom roles giving the permission on the type that it holds, custom, SQL for an array of +// them read by key (givingCustomSQL), NULL where it holds too many to step through, empty for none: +// every grant of a custom role, as a tenant's viewer's to the members on 20,000 clusters, read +// whatever the role gave, cost a member's Reachable 8,900 pages. Those roles are read only for a key +// holding 64 there, so a list whose keys hold few reads none. +func (a *Authorizer) heldByType(types, principals, roles, custom, cols, where string) string { held := fmt.Sprintf(`SELECT %[1]s FROM rbac_grants g WHERE %[2]s AND g.principal = keyed.k AND %[3]s`, cols, where, a.ofType()) if a.history != "" { held += " OFFSET 0" } else { - given := held - byRole := fmt.Sprintf(`SELECT %[1]s FROM unnest(%[2]s::text[]) AS r(r) CROSS JOIN LATERAL (%[3]s AND g.role = r.r OFFSET 0) g`, cols, roles, given) - if custom { - byRole = "(" + byRole + ") UNION ALL (" + given + a.customOnly("g.role") + " OFFSET 0)" + byRole := func(givers string) string { + return fmt.Sprintf(`SELECT %[1]s FROM unnest(%[2]s::text[]) AS r(r) CROSS JOIN LATERAL (%[3]s AND g.role = r.r OFFSET 0) g`, cols, givers, held) + } + given := byRole(roles) + if custom != "" { + given = "(" + given + ") UNION ALL (" + byScopeOrRole(held+a.customOnly("g.role")+" OFFSET 0", byRole, fmt.Sprint(heldByScope), custom) + ")" } // Each arm runs only where its count holds, which reads few once, so a key's grants are read // one way, once. - held = fmt.Sprintf(`WITH few AS MATERIALIZED (%[1]s LIMIT %[3]d) SELECT * FROM few WHERE (SELECT count(*) FROM few) < %[3]d - UNION ALL SELECT * FROM (%[2]s) g WHERE (SELECT count(*) FROM few) = %[3]d`, given, byRole, heldByScope) + first := scopeBound(roles + "::text[]") + held = fmt.Sprintf(`WITH few AS MATERIALIZED (%[1]s LIMIT %[3]s) SELECT * FROM few WHERE (SELECT count(*) FROM few) < %[3]s + UNION ALL SELECT * FROM (%[2]s) g WHERE (SELECT count(*) FROM few) = %[3]s`, held, given, first) } return fmt.Sprintf(`SELECT %[2]s FROM unnest(%[1]s::text[]) AS t(t) CROSS JOIN unnest(%[3]s::text[]) AS keyed(k) CROSS JOIN LATERAL ( %[4]s) g`, types, cols, principals, held) } +// byScopeOrRole is SQL selecting the rows given selects, a key's grants of custom roles on a type, +// by the scope where it holds fewer than first, or past that than the bound for the roles giving the +// permission it holds, givers, SQL for an array of them (scopeBound), and as byRole selects them for +// an array of those roles, a role at a time, where it holds the bound or more. givers, and so the +// bound, are read once, and only past first, and only where the first it holds there name +// scannedRoles roles at most: a key holding one grant each of hundreds of custom roles there, stepped +// through to find them, cost a member's Reachable 340 pages where by the scope it read 200. Where +// givers is NULL, or isn't read, it reads them all by the scope, as for a key holding few. Where the +// bound is first, a key's grants aren't read by the scope again. Each arm runs only where its counts +// hold, read in turn in a CASE, which the planner doesn't reorder, so a key's grants are read one +// way, once, and by the scope at most twice, up to the bound. +func byScopeOrRole(given string, byRole func(givers string) string, first, givers string) string { + return fmt.Sprintf(`WITH few AS MATERIALIZED (%[1]s LIMIT %[3]s), giving(r) AS MATERIALIZED (SELECT CASE WHEN (SELECT count(DISTINCT role) FROM few) <= %[7]d THEN %[4]s END), + bound(n) AS MATERIALIZED (SELECT CASE WHEN r IS NULL THEN %[6]d ELSE %[5]s END FROM giving), more AS MATERIALIZED (%[1]s LIMIT (SELECT n FROM bound)) + SELECT * FROM few WHERE (SELECT count(*) FROM few) < %[3]s + UNION ALL SELECT * FROM more WHERE CASE WHEN (SELECT count(*) FROM few) = %[3]s THEN CASE WHEN (SELECT n FROM bound) > %[3]s + THEN (SELECT count(*) FROM more) < (SELECT n FROM bound) END END + UNION ALL SELECT * FROM (%[2]s) g WHERE CASE WHEN (SELECT count(*) FROM few) = %[3]s THEN CASE WHEN (SELECT n FROM bound) > %[3]s + THEN (SELECT count(*) FROM more) = (SELECT n FROM bound) ELSE true END END`, + given, byRole("(SELECT r FROM giving)"), first, givers, scopeBound("r"), math.MaxInt64, scannedRoles()) +} + +// scopeBound is SQL for how many grants of a key on a type heldByType and byScopeOrRole read by the +// scope before they read them a role at a time, for the roles in the array roles: heldByScope, or +// heldPerRole for each role where that is more. +func scopeBound(roles string) string { + return fmt.Sprintf("greatest(%d, %d * coalesce(cardinality(%s), 0))", heldByScope, heldPerRole, roles) +} + // heldByScope is how many grants of a key on scopes of a type heldByType reads by the scope, of any // role, before it reads them a role at a time instead: about where reading them costs as many pages // as seeking a few dozen roles. A test lowers it. var heldByScope = 64 +// heldPerRole is how many grants of a key on a type, for each role giving the permission, it takes +// for heldByType to read them a role at a time: each seek is a descent of the index, a few pages, +// about what reading a few grants by the scope costs. A test lowers it. +var heldPerRole = 4 + // ofType is SQL for whether a grant g's scope is of the type t.t, as byType tests it. func (a *Authorizer) ofType() string { if a.history != "" { @@ -1705,9 +1752,23 @@ func (a *Authorizer) ofType() string { // to the principal, the tenant and the scope (customOnly). As of a time it selects the grants of // every role, the prefix tested apart (namedCustom): history's index of a grant's role leads to the // tenant and the role, so a plan for the prefix could read every version of every grant of a custom -// role in the tenant. +// role in the tenant. Past heldByScope of them on a type, it tests instead, a role at a time, which +// of the tenant's custom roles giving the permission $2 on the type $3 the principal holds a grant of +// there (byScopeOrRole): read by the scope, a tenant's viewer granted to the members on thousands of +// resources cost a list a page or more for each few of them, though it gave nothing. func (a *Authorizer) broadOn(principal string) string { - return a.byType("$4", "g.role", "g.tenant = $1 AND g.principal = "+principal+a.customOnly("g.role")) + where := "g.tenant = $1 AND g.principal = " + principal + if a.history != "" { + return a.byType("$4", "g.role", where) + } + custom := givingCustomSQL(true, "$1", "$2", "$3", principal) + given := fmt.Sprintf(`SELECT g.role FROM rbac_grants g WHERE %s%s AND %s`, where, a.customOnly("g.role"), a.ofType()) + byRole := func(givers string) string { + return fmt.Sprintf(`SELECT r.r AS role FROM unnest(%s) AS r(r) WHERE EXISTS (SELECT FROM rbac_grants g WHERE %s AND g.role = r.r AND %s)`, + givers, where, a.ofType()) + } + return fmt.Sprintf(`SELECT g.role FROM unnest($4::text[]) AS t(t) CROSS JOIN LATERAL (%s) g`, + byScopeOrRole(given, byRole, fmt.Sprint(heldByScope), custom)) } // broadAt is broadOn for the scopes in the array $4, a scope at a time. @@ -1900,7 +1961,7 @@ func (a *Authorizer) filterGrants(arg func(int) string, custom, alone bool, type // filterHeld is SQL selecting the subject's grants filterGrants reads, as held: with custom, those // of custom roles too. func (a *Authorizer) filterHeld(arg func(int) string, custom bool, types string) string { - return a.heldByType(types, a.filterKeys(arg, countedKeys(arg(13), arg(17))), arg(3), custom, "g.scope, g.role, g.principal", fmt.Sprintf(`g.tenant = %s::text + return a.heldByType(types, a.filterKeys(arg, countedKeys(arg(13), arg(17))), arg(3), givingCustomSQL(custom, arg(1), arg(8), arg(9), "keyed.k"), "g.scope, g.role, g.principal", fmt.Sprintf(`g.tenant = %s::text AND (g.expires_at IS NULL OR g.expires_at > %s) AND NOT g.conditional`, arg(1), a.filterAt(arg))) } diff --git a/docs/architecture.md b/docs/architecture.md index d54b683..97e57db 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -286,16 +286,32 @@ erDiagram principal and scope, and reads only those its subject counts: the anonymous and a guest read none of the members', and a search of a permission no public role gives none of the public's, so their grants of built-in roles on thousands of resources cost it nothing. It reads a key's grants on a - type's scopes by the scope, of any role, up to 64 of them (`heldByScope`), and a key holding that - many there, those of each role giving the permission alone, by `rbac_grants_principal_role` + type's scopes by the scope, of any role, up to 64 of them (`heldByScope`), or 4 for each built-in + role giving the permission (`heldPerRole`) where that is more, and a key holding that many there, + those of each built-in role giving the permission alone, by `rbac_grants_principal_role` (0034), which leads to the role before the scope: sought by the scope and tested for the role after, every grant of a key on the type's scopes, of a viewer on thousands of resources to the members, the public, the subject or its group, cost a page or more, and tested as `= ANY` of the roles, a generic plan still read them by `rbac_grants_principal`. A role at a time for every key, each seek a descent of the index, cost a key holding a grant or two a seek for each role giving the permission: beside 40 such roles, a subject in 200 groups read 72,000 pages where by the - scope it read 3,300. Each key is read one way, once: both ways count what the first read, held - apart (`few`). A key's conditional grants of a + scope it read 3,300, and the bound alone, 64, left a catalog of 1,000 roles giving it costing 20 + groups holding 100 grants each 60,000 pages where by the scope they read 700: so the bound grows + with the built-in roles giving the permission. Such a key's grants of custom roles on the type it + reads apart: by the scope up to 64 of them, and past that, where those 64 name 16 roles or fewer, + those of each custom role giving the permission there that the key holds, found by stepping from + one custom role of the key to the next in `rbac_grants_principal_role`, a seek each, and looked up + by key, while it holds 4 grants there for each, and by the scope below that. A key holding more than 16 custom roles, or whose 64 name + more, it reads by the scope, as before: each step costs about what reading a few grants does, + whatever the role gives or wherever it is granted, so stepping through the members' grants of 300 + custom roles, one on each of 300 clusters, cost a member's `Reachable` 1,900 pages where by the + scope it read 210, and read by the tenant, a plan made while the tables were small read every + custom role of it. A key's every grant of a custom role, as a tenant's viewer's to the members on + 20,000 clusters, read whatever the role gave, cost a member's `Reachable` 8,900 pages, as did + deciding which custom roles the members' grants name (`broadOn`), which tests each giving custom + role alike. Each key is read one way, once: both ways count what the first read, held + apart (`few`, and `more` up to the grown bound). As of a time it reads every grant of a key on the + type by the scope, of any role, as before 0034. A key's conditional grants of a role giving the permission it still reads, from that index alone, though `Filter` counts none: holding whether a grant is conditional before the scope, the index was costed as the scope's by a generic plan, which then read every grant of the key there. It names a custom role as that diff --git a/export_test.go b/export_test.go index 77c7948..2dd451d 100644 --- a/export_test.go +++ b/export_test.go @@ -475,12 +475,12 @@ func SetMaxContainerOverlap(t *testing.T, n int) { } // SetHeldByScope makes searches read a key's grants on a type a role at a time once it holds n -// there, until t ends. +// there, however many roles give the permission, until t ends. func SetHeldByScope(t *testing.T, n int) { t.Helper() - old := heldByScope - heldByScope = n - t.Cleanup(func() { heldByScope = old }) + old, perRole := heldByScope, heldPerRole + heldByScope, heldPerRole = n, 0 + t.Cleanup(func() { heldByScope, heldPerRole = old, perRole }) } // SetReadingRoles runs f once a read has read grants and the custom roles with them, until t diff --git a/roles.go b/roles.go index f897de9..0ece0b9 100644 --- a/roles.go +++ b/roles.go @@ -193,6 +193,55 @@ func givingSQL(builtin, tenant, perm, resourceType string, custom bool, ids stri return fmt.Sprintf(`(%s::text[] || ARRAY(%s))`, builtin, byKey(ids, "rbac_roles cr", "cr.id", "cr.id", where)) } +// givingCustomSQL is SQL for an array of the custom roles of a tenant giving perm on a resource type +// that the principal whose key is principal holds a grant of there, as givingSQL tests a custom role, +// of which it keeps those it is given: so a search reading grants a role at a time (heldByType, +// broadOn) reads every grant givingSQL can keep, in the same statement, which sees each role as it +// does. It finds the roles the principal holds by stepping from one to the next in +// rbac_grants_principal_role, a seek each, and looks each up by its key: read by the tenant, a plan +// made while the tables were small read every custom role of it, of which a tenant can hold +// MaxCustomRoles. It steps through scannedRoles of them at most, and is NULL for a principal holding +// more, whose grants a search reads by the scope: each step costs about what reading a few grants +// does, whatever the role gives or wherever it is granted, so beside the members' 100 grants of a +// custom viewer on clusters, their grants of 1,000 roles on another type cost a member's Reachable +// 8,200 pages where those of 100 cost 720. Empty without custom, for none. +func givingCustomSQL(custom bool, tenant, perm, resourceType, principal string) string { + if !custom { + return "" + } + // Each step is the next entry of rbac_grants_principal_role after the last, a row comparison in + // its order, which only an index leading to the principal, the tenant and the role reads in order, + // and ended past the principal's custom roles after it. Bounded by the principal, the tenant and + // the role apart, as each a plan counts a key holding one row or so, a plan read and sorted every + // grant of the principal by an index leading to the scope after them, its 10,000 of a built-in + // role, or bounded by the prefix too, every grant of a custom role it held, by + // rbac_grants_custom_principal, whose predicate that implies. + next := func(after string) string { + return fmt.Sprintf(`SELECT g.principal, g.tenant, g.role FROM rbac_grants g WHERE (g.principal, g.tenant, g.role) %s + ORDER BY g.principal, g.tenant, g.role LIMIT 1`, after) + } + mine := fmt.Sprintf(`h.principal = %s AND h.tenant = %s::text AND h.role < 'custom/'`, principal, tenant) + // One step past the last it keeps tells a principal holding more from one holding that many. + return fmt.Sprintf(`(WITH RECURSIVE held(principal, tenant, role, n) AS ((SELECT f.*, 1 FROM (%[1]s) f) + UNION ALL SELECT x.*, h.n + 1 FROM held h CROSS JOIN LATERAL (%[2]s) x WHERE %[3]s AND h.n <= %[7]d) + SELECT CASE WHEN count(*) <= %[7]d THEN coalesce(array_agg(cr.id) FILTER (WHERE cr.id IS NOT NULL), '{}') END + FROM held h LEFT JOIN LATERAL (SELECT cr.id FROM rbac_roles cr WHERE cr.tenant = %[4]s::text AND cr.id = h.role + AND %[5]s::text = ANY(cr.permissions) AND %[6]s::text = ANY(cr.resource_types) OFFSET 0) cr ON true WHERE %[3]s)`, + next(fmt.Sprintf(">= (%s, %s::text, 'custom.')", principal, tenant)), next("> (h.principal, h.tenant, h.role)"), mine, tenant, perm, resourceType, + scannedRoles()) +} + +// scannedRoles is how many custom roles of a key givingCustomSQL steps through, and how many its +// first grants on a type may name for byScopeOrRole to step through them: as many as the grants +// heldByType reads by the scope before it seeks roles, heldPerRole for each, or, with heldPerRole 0, +// as a test sets it, every one a tenant can hold. +func scannedRoles() int { + if heldPerRole == 0 { + return MaxCustomRoles + } + return heldByScope / heldPerRole +} + // broadArgs are a search's arguments for the roles giving p on a resource type that give it // through grants to the public and the tenant's members: the custom ones that are public, NULL // where no grant to the public gives p, as Filter and Reachable then read none and Holders drops