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: added support for row level security to pg_policy #141991

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Dedej-Bergin
Copy link
Contributor

Previously the pg_policy table was unimplemented. With these code changes the table is now populated accordingly, like its postgres equivalent: https://www.postgresql.org/docs/17/catalog-pg-policy.html

Epic: CRDB-45205
Fixes: #136702
Release note: None

Copy link

blathers-crl bot commented Feb 25, 2025

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

@Dedej-Bergin Dedej-Bergin force-pushed the vtable-rls branch 5 times, most recently from ca3a717 to 1278591 Compare February 26, 2025 19:33
Previously the `pg_policy` table was unimplemented.  With these code changes
the table is now populated accordingly, like its postgres equivalent:
`https://www.postgresql.org/docs/17/catalog-pg-policy.html`

Epic: CRDB-45205
Fixes: cockroachdb#136702
Release note: None
@Dedej-Bergin Dedej-Bergin marked this pull request as ready for review February 26, 2025 21:46
@Dedej-Bergin Dedej-Bergin requested review from a team as code owners February 26, 2025 21:46
Copy link
Contributor

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @Dedej-Bergin)


pkg/sql/logictest/testdata/logic_test/row_level_security line 0 at r1 (raw file):
Can we have a tests that uses a filter with pg_policy to select the policies for one table when policies exists for multiple tables?


pkg/sql/logictest/testdata/logic_test/row_level_security line 119 at r1 (raw file):


statement ok
CREATE POLICY "policy1" ON multi_pol_tab1 AS PERMISSIVE

Were these policy names renamed so that they can be returned via pg_policy?


pkg/sql/logictest/testdata/logic_test/row_level_security line 162 at r1 (raw file):


query ITIBTTT colnames,rowsort
select oid::INT, polname, polrelid::INT, polpermissive, polroles::string, polqual, polwithcheck from pg_catalog.pg_policy

Can we query the polcmd column?


pkg/sql/pg_catalog.go line 4238 at r1 (raw file):

}

var pgCatalogPolicyTable = makeAllRelationsVirtualTableWithDescriptorIDIndex(

To utilize the index, we likely need to define one on the pg_policy table. I believe adding an INDEX(polrelid) to its definition will create the index, allowing us to filter pg_policy by the table OID and read only that table's descriptor.


pkg/sql/pg_catalog.go line 4249 at r1 (raw file):

			// get the policy command and convert it to postgres equivalent
			var cmd string
			switch policy.Command.String() {

nit: can we just compare on the enum type rather than checking for a hard coded string value?


pkg/sql/pg_catalog.go line 4261 at r1 (raw file):

				cmd = "*"
			default:
				panic(errors.AssertionFailedf("unexpected policy command: %s", policy.Command.String()))

nit: this function returns an error, so maybe just return the errors.AssertionFailedf instead of panic'ing.


pkg/sql/pg_catalog.go line 4287 at r1 (raw file):

			// get the using expression
			usingExpr := ""
			if len(policy.UsingExpr) != 0 {

In postgres, if the using expression isn't defined then polqual is null. Same with polwithcheck. So, I think we should fill in a null value if those expressions are missing.


pkg/sql/pg_catalog.go line 4310 at r1 (raw file):

			if err := addRow(
				tree.NewDOid(oid.Oid(policy.ID)),                           // oid

The policyID is not strictly an oid. We should leave a comment to clarify this.

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: Implement the pg_policy vtable
3 participants