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.

@IevaVasiljeva IevaVasiljeva added this to the 10.1.x milestone Jul 19, 2023
@IevaVasiljeva IevaVasiljeva requested review from grafanabot and a team as code owners July 19, 2023 11:20
@IevaVasiljeva IevaVasiljeva requested a review from a team July 19, 2023 11:20
@IevaVasiljeva IevaVasiljeva requested review from a team as code owners July 19, 2023 11:20
@grafanabot grafanabot added area/backend/db/migration area/frontend type/docs Flags the technical writing team for documentation support; auto adds to org-wide docs project labels Jul 19, 2023
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.

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?


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 :)

@IevaVasiljeva IevaVasiljeva merged commit cfa1a2c into main Jul 21, 2023
@IevaVasiljeva IevaVasiljeva deleted the rbac/search-v1-permission-filter-pt2 branch July 21, 2023 14:23
@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
Labels
add to changelog area/backend/db/migration area/backend area/frontend no-backport Skip backport of PR postmortem type/docs Flags the technical writing team for documentation support; auto adds to org-wide docs project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants