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: Introduce the concept of system cluster privileges #82166

Merged
merged 5 commits into from
Jun 25, 2022

Conversation

RichardJCai
Copy link
Contributor

@RichardJCai RichardJCai commented May 31, 2022

System Cluster Privileges (global) privileges are the first
type of privileges that use the system.privileges table.

Release note (sql change): Introduce SYSTEM CLUSTER PRIVILEGES.
These are "global" privileges that live above the database level.

Example: GRANT SYSTEM MODIFYCLUSTERSETTING TO foo

Currently MODIFYCLUSTERSETTING is the only system cluster privilege,
it allows users to query the crdb_internal.cluster_settings table.

More functionality will be gated behind having MODIFYCLUSTERSETTING privilege
in the future. This is not the same as the MODIFYCLUSTERSETTING role option.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch 5 times, most recently from da1f404 to b0af30b Compare June 2, 2022 22:31
@RichardJCai RichardJCai changed the title System privileges 05242022 sql: Introduce the concept of system cluster privileges Jun 2, 2022
@RichardJCai RichardJCai requested review from rafiss and ajwerner June 2, 2022 22:32
@RichardJCai RichardJCai marked this pull request as ready for review June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team as a code owner June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team as a code owner June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team June 2, 2022 22:32
@RichardJCai RichardJCai requested a review from a team as a code owner June 2, 2022 22:32
@RichardJCai RichardJCai requested review from msbutler and removed request for a team June 2, 2022 22:32
@RichardJCai
Copy link
Contributor Author

Need to add some more tests / a bit more cleanup but I think this is ready for a first pass of reviews right now.

@rafiss rafiss requested a review from ecwall June 6, 2022 20:12
@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from b0af30b to d96b81a Compare June 7, 2022 18:53
Copy link
Contributor

@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.

Reviewed 40 of 49 files at r1, 8 of 50 files at r3, 45 of 45 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @msbutler, @rafiss, and @RichardJCai)


pkg/sql/grant_revoke.go line 154 at r4 (raw file):

}

type changeNonDescriptorBackedPrivilegesNode struct {

Can you use composition here with a struct that contains the shared fields between this and changePrivilegesNode and embed it in both?

preChangePrivilegesValidation could be a method with changePrivilegesNode as a receiver.


pkg/sql/grant_revoke_system.go line 118 at r4 (raw file):

			systemPrivilegeObject.ToString(),
			bitarray.MakeBitArrayFromInt64(64, int64(userPrivs.Privileges), 64),
			bitarray.MakeZeroBitArray(64),

Are grant options part of the table but unused? (same question for grant above)


pkg/sql/catalog/catprivilege/validate.go line 68 at r4 (raw file):

	}

	if !reflect.DeepEqual(out, systemPrivilegeObject) {

Does != not work here?


pkg/sql/privilege/privilege.go line 46 at r4 (raw file):

	CONNECT              Kind = 11
	RULE                 Kind = 12
	MODIFYCLUSTERSETTING Kind = 13 // SYSTEM CLUSTER PRIVILEGE.

Is there a concern about adding non-standard privilege kinds here? It looks like we are limited to 32 possible values type Kind uint32.

I haven't thought about this much, but is it possible to reuse the UPDATE privilege and add it to a "cluster setting" (does something like this exist?) database object similar to table, schema, etc?


pkg/util/bitarray/bitarray.go line 622 at r4 (raw file):

// AsUInt64 returns the uint64 constituted from the rightmost bits in the
// bit array.
func (d BitArray) AsUInt64() uint64 {

Does having a value receiver end up copying the array?

@knz
Copy link
Contributor

knz commented Jun 9, 2022

More functionality will be gated behind having MODIFYCLUSTERSETTING privilege
in the future. This is not the same as the MODIFYCLUSTERSETTING role option.

Sorry I missed the memo; what's the difference between the two? (Also I'm pretty sure that difference needs to be explained in a release note.)

@RichardJCai
Copy link
Contributor Author

More functionality will be gated behind having MODIFYCLUSTERSETTING privilege
in the future. This is not the same as the MODIFYCLUSTERSETTING role option.

Sorry I missed the memo; what's the difference between the two? (Also I'm pretty sure that difference needs to be explained in a release note.)

Functionally they're going to be the same thing, the goal is to emphasize using the privilege and not the role option and in general move away from and eventually deprecate these non granular role options (the ones we added somewhat adhoc and are not in pg)

@knz
Copy link
Contributor

knz commented Jun 9, 2022

Functionally they're going to be the same thing

if that part of the sentence is true, then the sentence "More functionality will be gated behind ..." should be moved from the release note back up higher in the commit message, outside of the release note. Because that means that change is not user-facing.

@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from d96b81a to 307e3ae Compare June 15, 2022 16:08
@RichardJCai RichardJCai requested a review from a team as a code owner June 15, 2022 16:08
@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch 2 times, most recently from 2b1406c to 9db37d8 Compare June 15, 2022 18:46
@RichardJCai RichardJCai requested a review from a team June 23, 2022 20:03
First step in upcoming privileges work.
Explained in RFC: cockroachdb#80580

Release note: None
@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from 5f7a22b to 62aa13f Compare June 23, 2022 20:46
Copy link
Contributor Author

@RichardJCai RichardJCai 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 @ajwerner, @ecwall, @msbutler, and @rafiss)


pkg/sql/crdb_internal.go line 1564 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

would it make sense to use a singleton var for this empty catalog.SystemClusterPrivilege -- then we can name it in and comment it to include the explanation you just gave.

DOne, created catalog.SystemClusterPrivilegeObject


pkg/sql/catalog/privilege_object.go line 24 at r6 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

yeah constants could make sense then, i think

Added a constant for system.


pkg/sql/catalog/privilege_object.go line 22 at r7 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

hm could you walk me though again why we need this interface? why can't we just call the "synthesize system privilege descriptor" function before the point where we need to start passing around these new PrivilegeObjects?

I think it simplifies a lot of with the GetObjectType for privilege validation.
It also simplifies some of the owner code.

This is mostly so we can identify objects and unify the code paths for descriptor backed and non-descriptor backed objects. I don't think it adds much overhead here.


pkg/sql/catalog/systemschema/system.go line 687 at r7 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

can the descriptor validation logic we already have be updated to make sure that there are only valid privlieges in this table (and validate each time we read from it)

ideally we could use an enum or CHECK constraint, but that would make adding a new privilege way too much of a pain i think, since each one would need a schema migration on a system table.

Added a validate into the synthesizePrivilegeDescriptorFromSystemPrivilegesTable method

@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from 62aa13f to 5df8cbd Compare June 24, 2022 00:23
@RichardJCai RichardJCai requested a review from rafiss June 24, 2022 15:05
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.

as discussed offline: let's have catalog.Descriptor implement catalog.PrivilegeObject so that it's easier to use as a parameter for existing functions

also hopefully minor nits, then lgtm

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @ecwall, @msbutler, @rafiss, and @RichardJCai)


pkg/sql/catalog/system_privilege.go line 36 at r9 (raw file):

// SystemClusterPrivilege represents a SystemClusterPrivilege.
type SystemClusterPrivilege struct {

nit: should this use a type assertion instead of embedding?

type SystemClusterPrivilege struct {}

var _ SystemPrivilegeObject = &SystemClusterPrivilege{}

(actually, i guess we wouldn't even need this because of the singleton defined below)

also i'm finding it hard to keep track of the names "SystemPrivilegeObject" versus "SystemClusterPrivilege". is there a way i should be thinking about this? why do we need both?


pkg/sql/privilege/privilege.go line 48 at r9 (raw file):

	CONNECT              Kind = 11
	RULE                 Kind = 12
	MODIFYCLUSTERSETTING Kind = 13 // SYSTEM CLUSTER PRIVILEGE.

is this comment correct? i believe the comment will be used for the string representation

Copy link
Contributor Author

@RichardJCai RichardJCai left a comment

Choose a reason for hiding this comment

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

as discussed offline: let's have catalog.Descriptor implement catalog.PrivilegeObject so that it's easier to use as a parameter for existing functions

This was the case but forgot to update TestState

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner, @ecwall, @msbutler, and @rafiss)


pkg/sql/catalog/system_privilege.go line 36 at r9 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: should this use a type assertion instead of embedding?

type SystemClusterPrivilege struct {}

var _ SystemPrivilegeObject = &SystemClusterPrivilege{}

(actually, i guess we wouldn't even need this because of the singleton defined below)

also i'm finding it hard to keep track of the names "SystemPrivilegeObject" versus "SystemClusterPrivilege". is there a way i should be thinking about this? why do we need both?

The mental model is SystemPrivilegeObject is a privilege object where the privileges are stored in the system table.

SystemClusterPrivilege is a type of SystemPrivilegeObject, there will be more types in the future (jobs, etc).

I can see that the naming is definitely confusing though, SystemClusterPrivilege is a "global" privilege.

Let me know if you can think of better naming solutions for these.

Also I removed the embedding since we do already have the type assertion.


pkg/sql/privilege/privilege.go line 48 at r9 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

is this comment correct? i believe the comment will be used for the string representation

I checked kind_string.go and that didn't seem to be the case but I'm happy to just remove this comment, it isn't adding anything.

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 @ajwerner, @ecwall, @msbutler, and @rafiss)


pkg/sql/privilege/privilege.go line 48 at r9 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

I checked kind_string.go and that didn't seem to be the case but I'm happy to just remove this comment, it isn't adding anything.

that's a bug because the comment from #81310 was not completely addressed

https://reviewable.io/reviews/cockroachdb/cockroach/81310#-N3R9piu3KukUO8hHuJr:-N3R9piu3KukUO8hHuJs:bd9h7vx

would you mind adding linecomment to //go:generate stringer -type=Kind above? cc @ecwall

@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from d73aabe to 5409c8b Compare June 24, 2022 20:29
Copy link
Contributor Author

@RichardJCai RichardJCai 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 @ajwerner, @ecwall, @msbutler, and @rafiss)


pkg/sql/privilege/privilege.go line 48 at r9 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

that's a bug because the comment from #81310 was not completely addressed

https://reviewable.io/reviews/cockroachdb/cockroach/81310#-N3R9piu3KukUO8hHuJr:-N3R9piu3KukUO8hHuJs:bd9h7vx

would you mind adding linecomment to //go:generate stringer -type=Kind above? cc @ecwall

Added it and updated the generated file.

@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch 2 times, most recently from 8f0dcc5 to 0747973 Compare June 24, 2022 20:37
Global privileges are the first
type of privileges that use the system.privileges table.

More functionality will be gated behind having `MODIFYCLUSTERSETTING` privilege
in the future. This is not the same as the `MODIFYCLUSTERSETTING` role option.

Release note (sql change): Introduce GLOBAL privileges.
These are privileges that live above the database level.

Example: `GRANT SYSTEM MODIFYCLUSTERSETTING TO foo`

Currently `MODIFYCLUSTERSETTING` is the only global privilege,
it allows users to query the `crdb_internal.cluster_settings` table.
misc lint changes and updating TestState to use the correct
method signature for CheckPrivilege and HasOwnership.

Splitting this into a separate commit due to be able to
track bazel build succeeding despite TestState not
implementing AuthorizationAccessor correctly.

Release note: None
This makes DEPRECATEDGRANT resolve to just "GRANT" as a string.

Release note: None
@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch 4 times, most recently from 54745f1 to 48dc0db Compare June 24, 2022 22:51
…ClusterPrivileges

Change internal representation of names.

Release note: None
@RichardJCai RichardJCai force-pushed the system_privileges_05242022 branch from 48dc0db to 18f3bce Compare June 25, 2022 01:58
@RichardJCai
Copy link
Contributor Author

Failure is flake, thanks for reviewing!

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 25, 2022

Build failed:

@RichardJCai
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 25, 2022

Build succeeded:

@craig craig bot merged commit 8fef727 into cockroachdb:master Jun 25, 2022
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.

5 participants