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 global privilege and legacy role option checking #109652

Merged
merged 1 commit into from
Aug 31, 2023

Conversation

andyyang890
Copy link
Collaborator

@andyyang890 andyyang890 commented Aug 29, 2023

This patch abstracts the logic for checking whether a user has a
global (system) privilege or the corresponding legacy role option
into a new function (HasGlobalPrivilegeOrRoleOption).

It also adds a wrapper function that returns an insufficient
privileges error when the user has neither the privilege nor
the legacy role option (CheckGlobalPrivilegeOrRoleOption).

Informs #103237

Release note: None

@blathers-crl
Copy link

blathers-crl bot commented Aug 29, 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

@andyyang890 andyyang890 marked this pull request as ready for review August 29, 2023 05:50
@andyyang890 andyyang890 requested a review from a team as a code owner August 29, 2023 05:50
@andyyang890 andyyang890 requested a review from rafiss August 29, 2023 05:50
@andyyang890 andyyang890 force-pushed the priv_fallback_func branch 2 times, most recently from 24c2a6e to 33157d6 Compare August 29, 2023 19:25
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.

this looks good! just had some small nits, feel free to merge after

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


pkg/sql/authorization.go line 815 at r1 (raw file):

}

func privilegeToLegacyRoleOption(priv privilege.Kind) roleoption.Option {

can we make this get populated automatically, so we don't have to remember this in the future? i'm thinking about an initialization like this:

var privilegeToLegacyRoleOption map[privilege.Kind]roleoption.Option

func init() {
	privilegeToLegacyRoleOption = make(map[privilege.Kind]roleoption.Option)
	for _, priv := range privilege.GlobalPrivileges {
		if roleOpt, ok := roleoption.ByName[priv.String()]; ok {
			privilegeToLegacyRoleOption[priv] = roleOpt
		}
	}
}

pkg/sql/set_cluster_setting.go line 74 at r1 (raw file):

		return err
	}
	hasSqlModify, err := p.HasGlobalPrivilegeOrRoleOption(ctx, privilege.MODIFYSQLCLUSTERSETTING)

can we still keep the previous logic that short-circuits, so we avoid checking them all?

Copy link
Collaborator Author

@andyyang890 andyyang890 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/sql/authorization.go line 815 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can we make this get populated automatically, so we don't have to remember this in the future? i'm thinking about an initialization like this:

var privilegeToLegacyRoleOption map[privilege.Kind]roleoption.Option

func init() {
	privilegeToLegacyRoleOption = make(map[privilege.Kind]roleoption.Option)
	for _, priv := range privilege.GlobalPrivileges {
		if roleOpt, ok := roleoption.ByName[priv.String()]; ok {
			privilegeToLegacyRoleOption[priv] = roleOpt
		}
	}
}

I considered this but it feels a little brittle since we're not guaranteed that the names are the same. e.g. CHANGEFEED maps to CONTROLCHANGEFEED.

I was thinking that a forgotten case should easily lead to a test failure since it would result in a failed privilege check when the user does not have the global privilege but does have the role option (i.e. is more restrictive). WDYT?

Copy link
Collaborator Author

@andyyang890 andyyang890 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/sql/set_cluster_setting.go line 74 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can we still keep the previous logic that short-circuits, so we avoid checking them all?

Done! I also took the liberty of reorganizing the code so that the privilege checks are closer to their actual usages.

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 @andyyang890)


pkg/sql/authorization.go line 815 at r1 (raw file):

Previously, andyyang890 (Andy Yang) wrote…

I considered this but it feels a little brittle since we're not guaranteed that the names are the same. e.g. CHANGEFEED maps to CONTROLCHANGEFEED.

I was thinking that a forgotten case should easily lead to a test failure since it would result in a failed privilege check when the user does not have the global privilege but does have the role option (i.e. is more restrictive). WDYT?

ah that's unfortunate.

i think we should do both then: do the automatic population with the loop, then for all the ones that don't have a matching name we add it explicitly to the map, with a comment explaining that it's because the names differ

that way, people only need to add boilerplate if the names differ, and not all the time.


pkg/sql/crdb_internal.go line 2352 at r3 (raw file):

			return err
		}
		hasView, err := p.HasGlobalPrivilegeOrRoleOption(ctx, privilege.VIEWCLUSTERSETTING)

nit: i see it was like this before already, but if it's easy enough can this be changed to avoid doing an unecessary privilege check?

Copy link
Collaborator Author

@andyyang890 andyyang890 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/sql/authorization.go line 815 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

ah that's unfortunate.

i think we should do both then: do the automatic population with the loop, then for all the ones that don't have a matching name we add it explicitly to the map, with a comment explaining that it's because the names differ

that way, people only need to add boilerplate if the names differ, and not all the time.

I realized that that CHANGEFEED is actually a table privilege, not a global privilege so we should be fine actually. I got rid of the map and moved the roleoption.ByName lookup directly into the function.


pkg/sql/crdb_internal.go line 2352 at r3 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: i see it was like this before already, but if it's easy enough can this be changed to avoid doing an unecessary privilege check?

Done.

This patch abstracts the logic for checking whether a user has a
global (system) privilege or the corresponding legacy role option
into a new function (`HasGlobalPrivilegeOrRoleOption`).

It also adds a wrapper function that returns an insufficient
privileges error when the user has neither the privilege nor
the legacy role option (`CheckGlobalPrivilegeOrRoleOption`).

Release note: None
@andyyang890
Copy link
Collaborator Author

tftr!

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Aug 31, 2023

Build succeeded:

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.

3 participants