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

Fix incorrectly quoted GRANT cmd on functions #1150

Merged
merged 7 commits into from
Mar 16, 2020

Conversation

olifre
Copy link
Contributor

@olifre olifre commented Mar 2, 2020

The existing code failed miserably to grant permissions on functions.

Using the following code:

postgresql::server::grant { "grant_func_privs":
  role                    => 'rewind',
  db                      => 'postgres',
  privilege               => 'EXECUTE',
  object_type             => 'FUNCTION',
  object_name             => 'pg_catalog.pg_ls_dir',
  object_arguments        => ['text', 'boolean', 'boolean'],
  require                 => Postgresql::Server::Role[$rewind_user],
}

The created queries were:

GRANT EXECUTE ON FUNCTION "pg_catalog.pg_ls_dir"("text","boolean","boolean") TO "rewind";

and for the "unless" case:

SELECT COUNT(*) FROM (SELECT 1 WHERE has_function_privilege('rewind', 'pg_catalog.pg_ls_dir("text","boolean","boolean")', 'EXECUTE') = true);"

The patches in this series fix that to be:

GRANT EXECUTE ON FUNCTION pg_catalog.pg_ls_dir(text,boolean,boolean) TO "rewind";
SELECT COUNT(*) FROM (SELECT 1 WHERE has_function_privilege('rewind', 'pg_catalog.pg_ls_dir(text,boolean,boolean)', 'EXECUTE') = true);"

since both the types and the complete function expression must not be quoted.

olifre added 3 commits March 2, 2020 13:45
The "arguments" in this case are the actual types,
and they must not be quoted.
Function and type arguments must stay together:
"pg_catalog.pg_ls_dir"(text,boolean,boolean)
=>
"pg_catalog.pg_ls_dir(text,boolean,boolean)"
GRANT EXECUTE ON FUNCTION "func()"
fails, we need to unquote in that case.
@olifre olifre requested a review from a team as a code owner March 2, 2020 14:34
@puppet-community-rangefinder
Copy link

postgresql::server::grant is a type

The enclosing module is declared in 50 of 578 indexed public Puppetfiles.

Breaking changes to this file WILL impact these modules (exact match):


These results were generated with Rangefinder, a tool that helps predict the downstream impact of breaking changes to elements used in Puppet modules. You can run this on the command line to get a full report.

Exact matches are those that we can positively identify via namespace and the declaring modules' metadata. Non-namespaced items, such as Puppet 3.x functions, will always be reported as near matches only.

Copy link
Contributor

@sanfrancrisko sanfrancrisko left a comment

Choose a reason for hiding this comment

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

Thanks for the fix @olifre ! I noticed there's a typo causing syntax check failures. Also, after checking out and running tests, there are going to be some test failures due to the change in syntax of the sql command:

bundle exec rake spec

Failed examples:

rspec ./spec/unit/defines/server/grant_spec.rb:263 # postgresql::server::grant function is expected to contain Postgresql_psql[grant:test] with command =~ /GRANT EXECUTE ON FUNCTION "test"\("text","text"\) TO\s* "test"/m and unless =~ /SELECT 1 WHERE has_function_privilege\('test',\s* 'test\("text","text"\)', 'EXECUTE'\)/m
rspec ./spec/unit/defines/server/grant_spec.rb:288 # postgresql::server::grant function with schema is expected to contain Postgresql_psql[grant:test] with command =~ /GRANT EXECUTE ON FUNCTION "myschema"."test"\("text","text"\) TO\s* "test"/m and unless =~ /SELECT 1 WHERE has_function_privilege\('test',\s* 'myschema.test\("text","text"\)', 'EXECUTE'\)/m

Could you have a look at the tests within grant_spec.rb? It would be good if we could add additional coverage to handle scenarios where we want to quote the command vs scenarios where we do not.

Please feel free to reach out if you need any help in that regard.

Thanks again for the PR!

manifests/server/grant.pp Outdated Show resolved Hide resolved
@olifre
Copy link
Contributor Author

olifre commented Mar 13, 2020

Thanks for the friendly feedback!
I was sadly unable to run PDK on my platform with this module. It seems something is very broken my platform (Gentoo), since even after running:

pdk bundle install --path vendor/bundle

(which succeeds fine and installs the dependencies), trying to validate or unit test will make PDK attempt to reinstall all dependencies with sudo, completely ignoring they are already there in vendor/bundle.

Unless you have an idea what is wrong (or can RTFM me), I could only develop the tests "blindly" (using travis-feedback). I can certainly do that, but it may take a bit.

@olifre olifre force-pushed the grant-perms-on-functions branch from 47cd447 to f691318 Compare March 13, 2020 21:11
olifre and others added 3 commits March 13, 2020 22:16
Co-Authored-By: sanfrancrisko <55992494+sanfrancrisko@users.noreply.github.com>
Types must not be quoted neither for granting not checking,
and the overall expression must not be quoted for granting.
…ven.

This was missed before and found by the test.
@olifre olifre force-pushed the grant-perms-on-functions branch from f691318 to 215b350 Compare March 13, 2020 21:16
@olifre
Copy link
Contributor Author

olifre commented Mar 13, 2020

@sanfrancrisko That should have done the trick to get the Static and Spec parts green.

I can also look at the acceptance tests, they should run through fine now, but they only ever worked since they never tested granting permissions to a function taking parameters, so it would be better to add something like this to accepteance tests (I can try tomorrow, hopefully won't take too many iterations).

@olifre olifre force-pushed the grant-perms-on-functions branch from cc29d2b to 681fba1 Compare March 13, 2020 22:20
@codecov-io
Copy link

codecov-io commented Mar 13, 2020

Codecov Report

❗ No coverage uploaded for pull request base (master@508571a). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master    #1150   +/-   ##
=========================================
  Coverage          ?   50.43%           
=========================================
  Files             ?       15           
  Lines             ?      458           
  Branches          ?        0           
=========================================
  Hits              ?      231           
  Misses            ?      227           
  Partials          ?        0

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 508571a...681fba1. Read the comment docs.

@olifre
Copy link
Contributor Author

olifre commented Mar 13, 2020

I did now also add an acceptance test for granting EXECUTE to functions with arguments.
Must have been beginner's luck, it seems to have played out well on my second attempt (and that was my first acceptance test ever) ;-).

@olifre olifre force-pushed the grant-perms-on-functions branch from 681fba1 to ec600a7 Compare March 13, 2020 22:40
@sanfrancrisko
Copy link
Contributor

Thanks for updating the current tests and adding the new coverage too @olifre. Appreciate you doing this as your first acceptance test! 😄

Change LGTM!

Regarding the issues you were seeing with the PDK - I asked some of my more PDK-savvy colleagues what they thought might be the issue. A few suggestions:

  • Drop the --path parameter from the bundle install command and let the PDK handle this
  • If you're using Bundler >= 2 you can set the path (seeing as using --path is now deprecated) globally: pdk bundle config set path <PATH> (FWIW, I have it set it to .bundle)

Before running pdk bundle install and pdk test unit, I would recommend removing the Gemfile.lock and vendor directory from your repo. Let me know how it works out.

@sanfrancrisko sanfrancrisko changed the title Fixup granting of permissions on functions Fix granting of permissions on functions Mar 16, 2020
@sanfrancrisko sanfrancrisko changed the title Fix granting of permissions on functions Fix incorrectly quoted GRANT cmd on functions Mar 16, 2020
@sanfrancrisko sanfrancrisko merged commit 34af4e0 into puppetlabs:master Mar 16, 2020
@olifre
Copy link
Contributor Author

olifre commented Mar 16, 2020

@sanfrancrisko Thanks for the friendly feedback and the PDK help!
Sadly, it fails for me — I have PDK 1.8.0.0 (latest packaged version for Gentoo) and Bunder 1.17.2 (newer versions are available up to 2.1.4, but I should probably not mix them with an old PDK).

After cleaning up (removing {{Gemfile.lock}} and {{vendor}}) and then running:

pdk bundle install

it tries to run sudo and wants to overwrite my system's ruby installation :-(.
For the config (even with bundler 2) I would expect it won't work since PDK sets BUNDLE_IGNORE_CONFIG: 1 (as I see in the debug output) so any config would be ignored — no? Potentially, that's also why the path setting in the older bundler version fails.

cegeka-jenkins pushed a commit to cegeka/puppet-postgresql that referenced this pull request Feb 3, 2022
Fix incorrectly quoted GRANT cmd on functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants