Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
253 changes: 232 additions & 21 deletions authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading