2014-01-22 01:45:58 +01:00
|
|
|
require 'spec_helper_acceptance'
|
2013-08-27 22:43:47 +02:00
|
|
|
|
|
|
|
describe 'postgresql::server::table_grant:' do
|
|
|
|
after :all do
|
|
|
|
# Cleanup after tests have ran
|
2014-01-22 01:45:58 +01:00
|
|
|
apply_manifest("class { 'postgresql::server': ensure => absent }", :catch_failures => true)
|
2013-08-27 22:43:47 +02:00
|
|
|
end
|
|
|
|
|
2013-11-12 14:23:05 +01:00
|
|
|
it 'should grant all accesses to a user' do
|
|
|
|
begin
|
|
|
|
pp = <<-EOS.unindent
|
|
|
|
$db = 'table_grant'
|
|
|
|
$user = 'psql_grant_tester'
|
|
|
|
$password = 'psql_table_pw'
|
|
|
|
|
|
|
|
class { 'postgresql::server': }
|
|
|
|
|
|
|
|
# Since we are not testing pg_hba or any of that, make a local user for ident auth
|
|
|
|
user { $user:
|
|
|
|
ensure => present,
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::role { $user:
|
|
|
|
password_hash => postgresql_password($user, $password),
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::database { $db: }
|
|
|
|
|
|
|
|
# Create a rule for the user
|
|
|
|
postgresql::server::pg_hba_rule { "allow ${user}":
|
|
|
|
type => 'local',
|
|
|
|
database => $db,
|
|
|
|
user => $user,
|
|
|
|
auth_method => 'ident',
|
|
|
|
order => 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql_psql { 'Create testing table':
|
|
|
|
command => 'CREATE TABLE "test_table" (field integer NOT NULL)',
|
|
|
|
db => $db,
|
|
|
|
unless => "SELECT * FROM pg_tables WHERE tablename = 'test_table'",
|
|
|
|
require => Postgresql::Server::Database[$db],
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::table_grant { 'grant insert test':
|
|
|
|
privilege => 'ALL',
|
|
|
|
table => 'test_table',
|
|
|
|
db => $db,
|
|
|
|
role => $user,
|
|
|
|
require => Postgresql_psql['Create testing table'],
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
2014-01-22 01:45:58 +01:00
|
|
|
apply_manifest(pp, :catch_failures => true)
|
|
|
|
apply_manifest(pp, :catch_changes => true)
|
2013-11-12 14:23:05 +01:00
|
|
|
|
|
|
|
## Check that the user can create a table in the database
|
|
|
|
psql('--command="create table foo (foo int)" postgres', 'psql_grant_tester') do |r|
|
2014-01-22 01:45:58 +01:00
|
|
|
expect(r.stdout).to match(/CREATE TABLE/)
|
|
|
|
expect(r.stderr).to eq('')
|
2013-11-12 14:23:05 +01:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
psql('--command="drop table foo" postgres', 'psql_grant_tester')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-27 22:43:47 +02:00
|
|
|
it 'should grant access so a user can insert in a table' do
|
|
|
|
begin
|
2013-09-18 23:25:51 +02:00
|
|
|
pp = <<-EOS.unindent
|
2013-08-27 22:43:47 +02:00
|
|
|
$db = 'table_grant'
|
2013-09-19 00:11:10 +02:00
|
|
|
$user = 'psql_grant_tester'
|
2013-08-27 22:43:47 +02:00
|
|
|
$password = 'psql_table_pw'
|
|
|
|
|
|
|
|
class { 'postgresql::server': }
|
|
|
|
|
|
|
|
# Since we are not testing pg_hba or any of that, make a local user for ident auth
|
|
|
|
user { $user:
|
|
|
|
ensure => present,
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::role { $user:
|
|
|
|
password_hash => postgresql_password($user, $password),
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::database { $db: }
|
|
|
|
|
2013-09-19 00:11:10 +02:00
|
|
|
# Create a rule for the user
|
|
|
|
postgresql::server::pg_hba_rule { "allow ${user}":
|
|
|
|
type => 'local',
|
|
|
|
database => $db,
|
|
|
|
user => $user,
|
|
|
|
auth_method => 'ident',
|
|
|
|
order => 1,
|
|
|
|
}
|
|
|
|
|
2013-08-27 22:43:47 +02:00
|
|
|
postgresql_psql { 'Create testing table':
|
|
|
|
command => 'CREATE TABLE "test_table" (field integer NOT NULL)',
|
|
|
|
db => $db,
|
|
|
|
unless => "SELECT * FROM pg_tables WHERE tablename = 'test_table'",
|
|
|
|
require => Postgresql::Server::Database[$db],
|
|
|
|
}
|
|
|
|
|
|
|
|
postgresql::server::table_grant { 'grant insert test':
|
|
|
|
privilege => 'INSERT',
|
|
|
|
table => 'test_table',
|
|
|
|
db => $db,
|
|
|
|
role => $user,
|
|
|
|
require => Postgresql_psql['Create testing table'],
|
|
|
|
}
|
|
|
|
EOS
|
|
|
|
|
2014-01-22 01:45:58 +01:00
|
|
|
apply_manifest(pp, :catch_failures => true)
|
|
|
|
apply_manifest(pp, :catch_changes => true)
|
2013-08-27 22:43:47 +02:00
|
|
|
|
|
|
|
## Check that the user can create a table in the database
|
2013-09-18 23:25:51 +02:00
|
|
|
psql('--command="create table foo (foo int)" postgres', 'psql_grant_tester') do |r|
|
2014-01-22 01:45:58 +01:00
|
|
|
expect(r.stdout).to match(/CREATE TABLE/)
|
|
|
|
expect(r.stderr).to eq('')
|
2013-09-18 23:25:51 +02:00
|
|
|
end
|
2013-08-27 22:43:47 +02:00
|
|
|
ensure
|
2013-09-18 23:25:51 +02:00
|
|
|
psql('--command="drop table foo" postgres', 'psql_grant_tester')
|
2013-08-27 22:43:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|