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 table grant with schema #1315

Merged
merged 5 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions manifests/server/grant.pp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@
}
# Never put double quotes into has_*_privilege function
$_granted_object = join($_object_name, '.')
# pg_* views does not contain schema name as part of the object name
$_togrant_object_only = $_object_name[1]
}
default: {
$_granted_object = $_object_name
Expand All @@ -445,10 +447,10 @@
}

$_onlyif = $onlyif_function ? {
'table_exists' => "SELECT true FROM pg_tables WHERE tablename = '${_togrant_object}'",
'language_exists' => "SELECT true from pg_language WHERE lanname = '${_togrant_object}'",
'table_exists' => "SELECT true FROM pg_tables WHERE tablename = '${_togrant_object_only}'",
'language_exists' => "SELECT true from pg_language WHERE lanname = '${_togrant_object_only}}'",
'role_exists' => "SELECT 1 FROM pg_roles WHERE rolname = '${role}' or '${role}' = 'PUBLIC'",
'function_exists' => "SELECT true FROM pg_proc WHERE (oid::regprocedure)::text = '${_togrant_object}${arguments}'",
'function_exists' => "SELECT true FROM pg_proc WHERE (oid::regprocedure)::text = '${_togrant_object_only}}${arguments}'",
default => undef,
}

Expand Down
26 changes: 26 additions & 0 deletions spec/defines/server/grant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,30 @@ class {'postgresql::server':}
it { is_expected.to compile.and_raise_error(%r{parameter 'object_name' variant 0 expects size to be 2, got 3}) }
end
end

context 'with specific schema name only if object exists' do
let :params do
{
db: 'test',
role: 'test',
privilege: 'all',
object_name: ['myschema', 'mytable'],
object_type: 'table',
onlyif_exists: true,
}
end

let :pre_condition do
"class {'postgresql::server':}"
end

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_postgresql__server__grant('test') }
it do
is_expected.to contain_postgresql_psql('grant:test')
.with_command(%r{GRANT ALL ON TABLE "myschema"."mytable" TO\s* "test"}m)
.with_unless(%r{SELECT 1 WHERE has_table_privilege\('test',\s*'myschema.mytable', 'INSERT'\)}m)
.with_onlyif(%r{SELECT true FROM pg_tables WHERE tablename = 'mytable'}m)
end
end
end