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

sql: refactor checkCancelPrivilege to avoid unessessary hasAdminRole call #96341

Merged
merged 1 commit into from
Feb 2, 2023
Merged

sql: refactor checkCancelPrivilege to avoid unessessary hasAdminRole call #96341

merged 1 commit into from
Feb 2, 2023

Conversation

ecwall
Copy link
Contributor

@ecwall ecwall commented Feb 1, 2023

Fixes #95993

Reorders the logic in checkCancelPrivilege to only call hasAdminRole when necessary.

Release note: None

@ecwall ecwall requested review from rafiss, yuzefovich and a team February 1, 2023 14:12
@ecwall ecwall requested a review from a team as a code owner February 1, 2023 14:13
@blathers-crl
Copy link

blathers-crl bot commented Feb 1, 2023

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

// checked inside getUserAndRole above.
isReqAdmin := isCtxAdmin
if reqUsername != ctxUsername {
isReqAdmin, err = b.privilegeChecker.hasAdminRole(ctx, reqUsername)
Copy link
Collaborator

Choose a reason for hiding this comment

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

out of curiosity, if we use this diff instead:

diff --git a/pkg/sql/syntheticprivilegecache/accumulator.go b/pkg/sql/syntheticprivilegecache/accumulator.go
index 53fdd9533b..680dcb3bb7 100644
--- a/pkg/sql/syntheticprivilegecache/accumulator.go
+++ b/pkg/sql/syntheticprivilegecache/accumulator.go
@@ -29,7 +29,7 @@ type accumulator struct {
 // newAccumulator initializes a Accumulator.
 func newAccumulator(objectType privilege.ObjectType, path string) *accumulator {
     return &accumulator{
-        desc:       &catpb.PrivilegeDescriptor{},
+        desc:       catpb.NewBasePrivilegeDescriptor(username.NodeUserName()),
         objectType: objectType,
         path:       path,
     }

would that let us remove this call to hasAdminRole entirely. this diff should cause the hasGlobalPrivilege function called on line 309 to always return true if the user is an admin

@dhartunian dhartunian removed the request for review from a team February 1, 2023 15:48
Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

LGTM, but I'll defer to Rafi on this one.

Reviewed 1 of 2 files at r1, 2 of 2 files at r2, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ecwall)

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ecwall)


pkg/server/status.go line 295 at r2 (raw file):

	// Check if the user has permission to see the session.
	// If reqUsername == ctxUsername && isCtxAdmin then reqUsername is admin.
	if sessionUsername != reqUsername && !(reqUsername == ctxUsername && isCtxAdmin) {

nit: i feel like this would be easier to read with short-circuits. it also allows you to comment each short-circuit condition more clearly.

// A user can always cancel their own sessions.
if sessionUsername == reqUsername {
  return nil
}

// If reqUsername == ctxUsername && isCtxAdmin then reqUsername is admin.
if reqUsername == ctxUsername && isCtxAdmin {
  return nil
}

// rest of the code

Copy link
Contributor Author

@ecwall ecwall left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/server/status.go line 300 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

out of curiosity, if we use this diff instead:

diff --git a/pkg/sql/syntheticprivilegecache/accumulator.go b/pkg/sql/syntheticprivilegecache/accumulator.go
index 53fdd9533b..680dcb3bb7 100644
--- a/pkg/sql/syntheticprivilegecache/accumulator.go
+++ b/pkg/sql/syntheticprivilegecache/accumulator.go
@@ -29,7 +29,7 @@ type accumulator struct {
 // newAccumulator initializes a Accumulator.
 func newAccumulator(objectType privilege.ObjectType, path string) *accumulator {
     return &accumulator{
-        desc:       &catpb.PrivilegeDescriptor{},
+        desc:       catpb.NewBasePrivilegeDescriptor(username.NodeUserName()),
         objectType: objectType,
         path:       path,
     }

would that let us remove this call to hasAdminRole entirely. this diff should cause the hasGlobalPrivilege function called on line 309 to always return true if the user is an admin

I tried it, but it caused some tests to fail https://teamcity.cockroachdb.com/viewLog.html?buildId=8544383&buildTypeId=Cockroach_BazelEssentialCi.

…call

Fixes #95993

Reorders the logic in `checkCancelPrivilege` to only call `hasAdminRole` when
necessary.

Release note: None
Copy link
Contributor Author

@ecwall ecwall left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss and @yuzefovich)


pkg/server/status.go line 295 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: i feel like this would be easier to read with short-circuits. it also allows you to comment each short-circuit condition more clearly.

// A user can always cancel their own sessions.
if sessionUsername == reqUsername {
  return nil
}

// If reqUsername == ctxUsername && isCtxAdmin then reqUsername is admin.
if reqUsername == ctxUsername && isCtxAdmin {
  return nil
}

// rest of the code

Updated.

@ecwall ecwall requested a review from rafiss February 2, 2023 01:37
@ecwall
Copy link
Contributor Author

ecwall commented Feb 2, 2023

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Feb 2, 2023

Build succeeded:

@craig craig bot merged commit 15f5190 into cockroachdb:master Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: refactor checkCancelPrivilege to avoid unessessary hasAdminRole call
4 participants