fix(postgresql): make schema optional and conditionally required in DefaultPrivileges#379
Conversation
|
Removing schema from the enum would prevent users from setting up automatic schema-level privilege grants, which is a legitimate multi-tenant/team usecase. Without becoming a breaking change, you can fix it with (reconciler.go, both cluster+namespace): - if params.Schema != nil {
+ // PostgreSQL does not allow IN SCHEMA with ON SCHEMAS.
+ if params.Schema != nil && (params.ObjectType == nil || *params.ObjectType != "schema") {And ideally validate it with a test 🙏 |
|
i.e. |
|
My original use case was exactly what you describe. However, since ALTER DEFAULT PRIVILEGES FOR ROLE foo IN SCHEMA public GRANT USAGE ON SCHEMAS TO bar;This is invalid SQL. Your suggested guard would only help if schema were optional, but since it's required, I think the correct fix is to make
I'm happy to rework the PR along those lines, with tests, if that approach makes sense. |
|
@AGejr yes, it makes absolute sense to me |
Signed-off-by: AGejr <albert.gejr@gmail.com>
…ype enum" This reverts commit 1a66e40. Signed-off-by: AGejr <albert.gejr@gmail.com>
Signed-off-by: AGejr <albert.gejr@gmail.com>
…DefaultPrivileges Signed-off-by: AGejr <albert.gejr@gmail.com>
…eges Signed-off-by: AGejr <albert.gejr@gmail.com>
…ectTypes Signed-off-by: AGejr <albert.gejr@gmail.com>
f52674f to
1296212
Compare
|
Reworked the PR along the lines we discussed.
|
Signed-off-by: AGejr <albert.gejr@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes PostgreSQL DefaultPrivileges reconciliation for objectType: schema by ensuring the controller does not generate an IN SCHEMA ... clause for schema default privileges, and it updates the API/docs/tests/examples to treat schema as conditionally applicable.
Changes:
- Adjust SQL generation so
objectType: schemaproduces... GRANT ... ON SCHEMAS ...withoutIN SCHEMA. - Make
schemaoptional in the CRD/API types and add controller-side validation thatschemais required for non-schemaobjectTypes and must be omitted forobjectType: schema. - Expand unit tests and examples to cover
objectType: schemaand alignobjectTypecasing with the CRD enum.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/controller/namespaced/postgresql/default_privileges/reconciler.go | Omits IN SCHEMA for objectType: schema, uppercases SQL object type, and adds schema/objectType validation in Observe. |
| pkg/controller/namespaced/postgresql/default_privileges/reconciler_test.go | Updates tests for lowercase objectType, adds schema where required, and adds coverage for objectType: schema. |
| pkg/controller/cluster/postgresql/default_privileges/reconciler.go | Same as namespaced controller changes for cluster-scoped resources. |
| pkg/controller/cluster/postgresql/default_privileges/reconciler_test.go | Same as namespaced test updates for cluster-scoped resources. |
| package/crds/postgresql.sql.m.crossplane.io_defaultprivileges.yaml | Makes schema optional in the CRD and clarifies schema applicability in description. |
| package/crds/postgresql.sql.crossplane.io_defaultprivileges.yaml | Makes schema optional in the CRD and clarifies schema applicability in description. |
| examples/namespaced/postgresql/defaultprivileges.yaml | Adds example manifests covering additional object types including schema without schema: field. |
| examples/cluster/postgresql/defaultprivileges.yaml | Adds cluster-scoped example manifests covering additional object types including schema without schema: field. |
| apis/namespaced/postgresql/v1alpha1/default_privileges_types.go | Updates API type docs and makes schema optional. |
| apis/cluster/postgresql/v1alpha1/default_privileges_types.go | Updates API type docs and makes schema optional. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…in DefaultPrivileges Signed-off-by: AGejr <albert.gejr@gmail.com>
Signed-off-by: AGejr <albert.gejr@gmail.com>
Description of your changes
Fixes
objectType: schemainDefaultPrivileges, which has never produced valid SQL. Previously, the controller unconditionally appendedIN SCHEMA <name>to the generated query, but PostgreSQL does not permitIN SCHEMAwhen the object type isSCHEMAS. Any resource usingobjectType: schemawould immediately and permanently fail with:This PR fixes the issue without removing schema from the enum (so the valid use case - granting privileges on future schemas remains supported.
spec.forProvider.schemaoptional in both the cluster-scoped and namespaced CRDs (previously it was required, makingobjectType: schemaimpossible to use correctly)objectTypeistable,sequence,function, ortype->schemais requiredobjectTypeisschema->schemamust not be setinSchemato never emitIN SCHEMAwhenobjectTypeisschemaobjectTypevariants, with theschemavariant correctly omitting theschemafieldFixes #378
I have:
make reviewableto ensure this PR is ready for review.How has this code been tested
Unit tests cover both new validation paths:
schemarequired whenobjectTypeis notschema, andschemarejected whenobjectTypeisschema. Full suite passes withgo test ./...Original description
Removes
schemafrom the validobjectTypeenum in theDefaultPrivilegesCRD spec (bothpostgresql.sql.crossplane.ioandpostgresql.sql.m.crossplane.iovariants), asobjectType: schemahas never worked - the provider unconditionally includes anIN SCHEMAclause in the generated SQL, but PostgreSQL does not allowIN SCHEMAwhen the object type isSCHEMAS, causing any resource using this value to immediately and permanently fail withcannot create default privileges: pq: cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS. This is technically a breaking change to the CRD, but sinceobjectType: schemahas never produced valid SQL, no working resources can exist that rely on it (as far as I can tell).Fixes #378
I have:
make reviewableto ensure this PR is ready for review.How has this code been tested
The change removes an enum value - no new code paths are introduced. Existing unit tests pass with
go test ./...