Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RBAC: Split non-empty scopes into kind, attribute and identifier fields for better search performance #71933

Merged
merged 12 commits into from
Jul 21, 2023

Conversation

IevaVasiljeva
Copy link
Contributor

Part 2 of RBAC search v1 permission filter improvements. First PR: #71913, ultimate PR: https://github.com/grafana/grafana-enterprise/pull/5460

What is this feature?

This PR lays the groundwork for the upcoming search permission filter improvements.

This PR:

  • adds a feature toggle splitScopes, the new permission filter logic will be behind this feature toggle;
  • adds fields for the scope parts - kind, attribute and identifier;
  • adds migrations that introduces new kind, attribute and identifier fields in the DB;
  • adds a migration that will be ran upon server startup and will set the new fields for any permissions with non-empty scopes that don't have the new fields set.

Why do we need this feature?

It breaks down work from #71844 into smaller chunks.

permissions[i].Attribute = attribute
permissions[i].Identifier = identifier

_, err := sess.Exec("UPDATE permission SET kind = ?, attribute = ?, identifier = ? WHERE id = ?", permissions[i].Kind, permissions[i].Attribute, permissions[i].Identifier, permissions[i].ID)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought: instead of doing individual permission updates, we could delete the existing permissions and do a batch insert. It is slightly riskier (as it deletes the existing entries), but it should be faster, especially for large instances with lots of permissions. Let me know if you think it's worth doing.

Copy link
Contributor

@gamab gamab Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea :) If you do the insert before the delete it's safer

Edit: disregard, we have a unique clause on roleID/action/scope

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can do batching or concurrent batching in a transaction 🤔

Copy link
Contributor

@gamab gamab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of comments, I guess the only important one is the missing unique constraint and maybe the feature toggle check not to run the migration until everything is ready.

pkg/services/store/testdata/public_testdata.golden.jsonc Outdated Show resolved Hide resolved
Name: "identifier", Type: migrator.DB_NVarchar, Length: 40, Default: "''",
}))

mg.AddMigration("add permission identifier index", migrator.NewAddIndexMigration(permissionV1, &migrator.Index{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also include the kind (and attribute), wdyt ? We might have resources of different kind with the same identifier.
It would make the migration lookup for untranslated permissions faster (unless my other comment is accepted 😅)
However, given the action already accounts for the kind in a certain way (ex:dashboards:read), this is not mandatory. This would just be double proofing in the eventuality that we remove the kind from the action.
Another drawback is that the index would be heavier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about it too, but decided to leave it out for now, and then we can test and see if additional indexes make a change to performance and add them later on. Does that sound ok?

pkg/services/accesscontrol/models.go Outdated Show resolved Hide resolved

tests := []testCase{
{desc: "all fields should be empty for empty scope", scope: "", kind: "", attribute: "", identifier: ""},
{desc: "only identifier should not be empty for wildcard", scope: "*", kind: "", attribute: "", identifier: "*"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot wrap my head around: should scope=* -> kind=*, attr=*, id=* 🤔
On one hand, it's simpler the way you did it to merge the scope back together.
On the other hand, if I want to search for permissions targeting a specific kind the where clause would be a bit weird:

SELECT * FROM permission
WHERE ( identifier="*" AND kind="" ) OR ( kind="dashboards" ) 

VS

SELECT * FROM permission
WHERE kind="*" OR kind="dashboards" 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how Kalle had done it, so I stuck with this. I think both approaches make sense. Your suggestion is probably easier to think about, so I'll go with that :)

pkg/services/accesscontrol/acimpl/service.go Show resolved Hide resolved
pkg/services/accesscontrol/acimpl/service.go Show resolved Hide resolved
pkg/services/accesscontrol/migrator/migrator.go Outdated Show resolved Hide resolved
@IevaVasiljeva IevaVasiljeva merged commit cfa1a2c into main Jul 21, 2023
17 checks passed
@IevaVasiljeva IevaVasiljeva deleted the rbac/search-v1-permission-filter-pt2 branch July 21, 2023 14:23
linoman pushed a commit that referenced this pull request Jul 24, 2023
…` fields for better search performance (#71933)

* add a feature toggle

* add the fields for attribute, kind and identifier to permission

Co-authored-by: Kalle Persson <kalle.persson@grafana.com>

* set the new fields when new permissions are stored

* add migrations

Co-authored-by: Kalle Persson <kalle.persson@grafana.com>

* remove comments

* Update pkg/services/accesscontrol/migrator/migrator.go

Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>

* feedback: put column migrations behind the feature toggle, added an index, changed how wildcard scopes are split

* PR feedback: add a comment and revert an accidentally changed file

* PR feedback: handle the case with : in resource identifier

* switch from checking feature toggle through cfg to checking it through featuremgmt

* don't put the column migrations behind a feature toggle after all - this breaks permission queries from db

---------

Co-authored-by: Kalle Persson <kalle.persson@grafana.com>
Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>
@ricky-undeadcoders ricky-undeadcoders modified the milestones: 10.1.x, 10.1.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants