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: prevent dropping roles with synthetic privileges #86499

Conversation

RichardJCai
Copy link
Contributor

Followup work to be done to support DROP OWNED BY and
REASSIGN OWNED BY here.

Release justification: Bug fix to newly introduced feature
Release note: None

@RichardJCai RichardJCai requested review from rafiss and a team August 19, 2022 22:41
@cockroach-teamcity
Copy link
Member

This change is Reviewable

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 @rafiss and @RichardJCai)


pkg/sql/drop_role.go line 583 at r1 (raw file):

		names[i] = username.Normalized()
	}
	rows, err := p.QueryIteratorEx(ctx, `drop-role-get-system-privileges`, sessiondata.NodeUserSessionDataOverride,

should this go through the cache instead of querying the table?


pkg/sql/drop_role.go line 587 at r1 (raw file):

	if err != nil {
		return err
	}

need to add:

defer func() { retErr = errors.CombineErrors(retErr, rows.Close()) }()

(and make the return parameter be named)


pkg/sql/drop_role.go line 607 at r1 (raw file):

			privilegeObjectFormatter.FormatName(fmt.Sprintf("%s %s", obj.GetObjectType(), obj.GetName()))
		} else {
			privilegeObjectFormatter.FormatName(string(obj.GetObjectType()))

i don't think FormatName should be used for the ObjectType. maybe use:

privilegeObjectFormatter.WriteString(string(obj.GetObjectType()))
if obj.GetName() != "" {
  privilegeObjectFormatter.WriteString(" ")
  privilegeObjectFormatter.FormatNode(obj.GetName())
}

pkg/sql/logictest/testdata/logic_test/synthetic_privileges line 256 at r1 (raw file):

GRANT USAGE ON EXTERNAL CONNECTION foo TO testuser

statement error pq: cannot drop role/user testuser: grants still exist on "external_connection foo", global, "virtual_table crdb_internal.tables"

the "global" here in this error message is a little confusing


pkg/sql/logictest/testdata/logic_test/synthetic_privileges line 257 at r1 (raw file):


statement error pq: cannot drop role/user testuser: grants still exist on "external_connection foo", global, "virtual_table crdb_internal.tables"
DROP USER testuser

it looks like we should also test that if there are default privileges then the user can't be dropped

@RichardJCai RichardJCai force-pushed the prevent_dropping_roles_with_synthetic_privileges branch from 8eabad1 to 8fec20a Compare August 22, 2022 19:11
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 @rafiss)


pkg/sql/drop_role.go line 583 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

should this go through the cache instead of querying the table?

Yeah, don't have cache yet, I'll need to add an interface to get synthetic privileges.


pkg/sql/drop_role.go line 587 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

need to add:

defer func() { retErr = errors.CombineErrors(retErr, rows.Close()) }()

(and make the return parameter be named)

Done.


pkg/sql/drop_role.go line 607 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

i don't think FormatName should be used for the ObjectType. maybe use:

privilegeObjectFormatter.WriteString(string(obj.GetObjectType()))
if obj.GetName() != "" {
  privilegeObjectFormatter.WriteString(" ")
  privilegeObjectFormatter.FormatNode(obj.GetName())
}

obj.GetName returns a string so FormatNode won't work, updated to just use another writestring


pkg/sql/logictest/testdata/logic_test/synthetic_privileges line 256 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

the "global" here in this error message is a little confusing

I've updated it to be more explicit


pkg/sql/logictest/testdata/logic_test/synthetic_privileges line 257 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

it looks like we should also test that if there are default privileges then the user can't be dropped

Done.

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


pkg/sql/drop_role.go line 583 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Yeah, don't have cache yet, I'll need to add an interface to get synthetic privileges.

Wait sorry I did add the cache, still need to add an interface for cases like this though

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.

lgtm with one more nit if you agree with it

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


pkg/sql/drop_role.go line 607 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

obj.GetName returns a string so FormatNode won't work, updated to just use another writestring

what about this

privilegeObjectFormatter.WriteString(string(obj.GetObjectType()))
if obj.GetName() != "" {
  privilegeObjectFormatter.WriteString(" ")
  privilegeObjectFormatter.FormatName(obj.GetName())
}

@RichardJCai RichardJCai force-pushed the prevent_dropping_roles_with_synthetic_privileges branch from 8fec20a to 41c69c1 Compare August 24, 2022 22:29
@RichardJCai RichardJCai requested a review from a team August 24, 2022 22: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 @rafiss)


pkg/sql/drop_role.go line 607 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

what about this

privilegeObjectFormatter.WriteString(string(obj.GetObjectType()))
if obj.GetName() != "" {
  privilegeObjectFormatter.WriteString(" ")
  privilegeObjectFormatter.FormatName(obj.GetName())
}

Good call, should probably do this anyway, if for whatever reason we have to redact the error message (don't think this ever happens right now) then this would be correct.

@RichardJCai RichardJCai requested a review from msbutler August 24, 2022 22:29
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 @msbutler, @rafiss, and @RichardJCai)


pkg/sql/drop_role.go line 607 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…

Good call, should probably do this anyway, if for whatever reason we have to redact the error message (don't think this ever happens right now) then this would be correct.

we are redacting the error message to use in telemetry logs now: #83807

@RichardJCai
Copy link
Contributor Author

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

pkg/sql/drop_role.go line 607 at r1 (raw file):

Previously, RichardJCai (Richard Cai) wrote…
we are redacting the error message to use in telemetry logs now: #83807

Ah even more valid then

Also first commit is from: #86823
Gonna wait for that to merge.

@RichardJCai RichardJCai force-pushed the prevent_dropping_roles_with_synthetic_privileges branch from 41c69c1 to a9fb4b0 Compare August 29, 2022 18:18
Followup work to be done to support DROP OWNED BY and
REASSIGN OWNED BY here.

Release justification: Bug fix to newly introduced feature
Release note: None
@RichardJCai RichardJCai force-pushed the prevent_dropping_roles_with_synthetic_privileges branch from a9fb4b0 to 0880a46 Compare August 29, 2022 20:37
@RichardJCai
Copy link
Contributor Author

Thanks for reviewing!

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Aug 29, 2022

Build succeeded:

@craig craig bot merged commit f1b3a94 into cockroachdb:master Aug 29, 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.

3 participants