Skip to content

Properly quote role name in default_privileges type - #1666

Merged
SugatD merged 1 commit into
puppetlabs:mainfrom
antaflos:fix_default_privileges_quoting
Sep 24, 2026
Merged

SugatD merged 1 commit into
puppetlabs:mainfrom
antaflos:fix_default_privileges_quoting

Conversation

@antaflos

Copy link
Copy Markdown
Contributor

Summary

Like in many other places in this module we need to ensure that user-supplied identifiers, such as role names, database names, etc. are quoted in SQL queries, because they may contain characters such as the hyphen that the SQL standard considers illegal unless the identifier is surrounded by double quotes.

The unless clause in the default_privileges defined type constructs an SQL query to check if the ALTER DEFAULT PRIVILEGES grant is already applied, but this SQL query does not quote the role name. This leads to server-side errors when trying to run the query if the role name contains a character like the hyphen. In this example the role name is monitoring-user:

Could not evaluate: Error evaluating 'unless' clause, returned pid 891642 exit 1: 'ERROR:  unrecognized key word: "monitoring"
LINE 1: ...amespace AS n ON da.defaclnamespace = n.oid WHERE 'monitori...
                                                             ^
HINT:  ACL key word must be "group" or "user".
'

This change fixes the SQL query for the unless clause and contains updated spec tests.

Fixes #1665

Additional Context

  • Root cause and the steps to reproduce. (If applicable)
  • Thought process behind the implementation.

Related Issues (if any)

#1665

Checklist

  • 🟢 Spec tests.
  • 🟢 Acceptance tests.
  • Manually verified. (For example puppet apply)

@SugatD

SugatD commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Please rebase the branch

@antaflos
antaflos force-pushed the fix_default_privileges_quoting branch from 70edc1b to bdecf93 Compare June 2, 2026 08:48
@antaflos

antaflos commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebase done

@SugatD

SugatD commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Created a PR to execute CI tests for this change: #1678

@jst-cyr

jst-cyr commented Jul 27, 2026

Copy link
Copy Markdown
Member

@SugatD : Any update on progress for the acceptance testing here?

@jst-cyr
jst-cyr force-pushed the fix_default_privileges_quoting branch from bdecf93 to bfcb8ab Compare September 16, 2026 19:25

@SugatD SugatD Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like the CI tests are failing due these leftover expectations. Please try the suggested changes, and we can check the CI again. Thank you

Suggested change
.with_unless("SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da LEFT JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '\"test\"=arwdDxtm' = ANY (defaclacl) AND nspname = 'public' and defaclobjtype = 'r')")

@SugatD SugatD Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like the CI tests are failing due these leftover expectations. Please try the suggested changes, and we can check the CI again. Thank you

Suggested change
.with_unless("SELECT 1 WHERE EXISTS (SELECT * FROM pg_default_acl AS da LEFT JOIN pg_namespace AS n ON da.defaclnamespace = n.oid WHERE '\"test\"=arwdDxt' = ANY (defaclacl) AND nspname = 'public' and defaclobjtype = 'r')")

@SugatD SugatD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please update the code for the suggested changes, so we can run CI again to verify.

Like in many other places in this module we need to ensure that
user-supplied identifiers, such as role names, database names, etc. are
quoted in SQL queries, because they may contain characters such as the
hyphen that the SQL standard considers illegal unless the identifier is
surrounded by double quotes.

The `unless` clause in the default_privileges defined type constructs an
SQL query to check if the ALTER DEFAULT PRIVILEGES grant is already
applied, but this SQL query does not quote the role name. This leads to
server-side errors when trying to run the query if the role name
contains a character like the hyphen. In this example the role name is
`monitoring-user`:

    Could not evaluate: Error evaluating 'unless' clause, returned pid 891642 exit 1: 'ERROR:  unrecognized key word: "monitoring"
    LINE 1: ...amespace AS n ON da.defaclnamespace = n.oid WHERE 'monitori...
                                                                 ^
    HINT:  ACL key word must be "group" or "user".
    '

This change fixes the SQL query for the `unless` clause and contains
updated spec tests.

Fixes puppetlabs#1665
@antaflos
antaflos force-pushed the fix_default_privileges_quoting branch from bfcb8ab to d271d63 Compare September 23, 2026 06:59
@antaflos

Copy link
Copy Markdown
Contributor Author

I updated the spec tests as requested, rebased and pushed.

@SugatD

SugatD commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Acceptance tests are passing correctly -> https://github.com/puppetlabs/puppetlabs-postgresql/pull/1678/checks
only Debian-11 (x2) and AlmaLinux-8/9 (x4) fail, which are unrelated to this PR.

Approving PR.

@SugatD SugatD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@SugatD
SugatD merged commit c7185dc into puppetlabs:main Sep 24, 2026
6 of 7 checks passed
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.

postgresql::server::default_privileges does not quote role name correctly, leading to faulty SQL query in the "unless" clause

3 participants