query:CREATE FUNCTION public.a() RETURNS integer LANGUAGE SQL AS 'SELECT 2';
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
# Test
- name:Grant execute to all functions
postgresql_privs:
type:function
state:present
privs:EXECUTE
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
# Checks
- assert:
that:result is changed
- name:Check that all functions have execute privileges
become:yes
become_user:"{{ pg_user }}"
shell:psql {{ db_name }} -c "SELECT proacl FROM pg_proc WHERE proname = 'a'" -t
register:result
- assert:
that:"'{{ db_user2 }}=X/{{ db_user3 }}' in '{{ result.stdout_lines[0] }}'"
# Test
- name:Grant execute to all functions again
postgresql_privs:
type:function
state:present
privs:EXECUTE
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
# Checks
- assert:
that:result is not changed
# Test
- name:Revoke execute to all functions
postgresql_privs:
type:function
state:absent
privs:EXECUTE
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
# Checks
- assert:
that:result is changed
# Test
- name:Revoke execute to all functions again
postgresql_privs:
type:function
state:absent
privs:EXECUTE
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
- assert:
that:result is not changed
# Function ALL_IN_SCHEMA cleanup
- name:Remove function for test
postgresql_query:
query:DROP FUNCTION public.a();
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
#################################################
# Test ALL_IN_SCHEMA for 'partioned tables type #
#################################################
# Partitioning tables is a feature introduced in Postgresql 10.
# (see https://www.postgresql.org/docs/10/ddl-partitioning.html )
# The test below check for this version
# Function ALL_IN_SCHEMA Setup
- name:Create partioned table for test purpose
postgresql_query:
query:CREATE TABLE public.testpt (id int not null, logdate date not null) PARTITION BY RANGE (logdate);
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Grant execute to all tables in check mode
postgresql_privs:
type:table
state:present
privs:SELECT
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
check_mode:yes
# Checks
- name:Check that all partitioned tables don't have select privileges after the check mode task
postgresql_query:
query:SELECT grantee, privilege_type FROM information_schema.role_table_grants WHERE table_name='testpt' and privilege_type='SELECT' and grantee = %(grantuser)s
db:"{{ db_name }}"
login_user:'{{ db_user2 }}'
login_password:password
named_args:
grantuser:'{{ db_user2 }}'
become:yes
become_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- result.rowcount == 0
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Grant execute to all tables
postgresql_privs:
type:table
state:present
privs:SELECT
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Check that all partitioned tables have select privileges
postgresql_query:
query:SELECT grantee, privilege_type FROM information_schema.role_table_grants WHERE table_name='testpt' and privilege_type='SELECT' and grantee = %(grantuser)s
db:"{{ db_name }}"
login_user:'{{ db_user2 }}'
login_password:password
named_args:
grantuser:'{{ db_user2 }}'
become:yes
become_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- result.rowcount == 1
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Grant execute to all tables again to see no changes are reported
postgresql_privs:
type:table
state:present
privs:SELECT
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:result is not changed
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Revoke SELECT to all tables
postgresql_privs:
type:table
state:absent
privs:SELECT
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Check that all partitioned tables don't have select privileges
postgresql_query:
query:SELECT grantee, privilege_type FROM information_schema.role_table_grants WHERE table_name='testpt' and privilege_type='SELECT' and grantee = %(grantuser)s
db:"{{ db_name }}"
login_user:'{{ db_user2 }}'
login_password:password
named_args:
grantuser:'{{ db_user2 }}'
become:yes
become_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- result.rowcount == 0
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Revoke SELECT to all tables and no changes are reported
postgresql_privs:
type:table
state:absent
privs:SELECT
roles:"{{ db_user2 }}"
objs:ALL_IN_SCHEMA
schema:public
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
register:result
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:result is not changed
when:postgres_version_resp.stdout is version('10', '>=')
# Table ALL_IN_SCHEMA cleanup
- name:Remove table for test
postgresql_query:
query:DROP TABLE public.testpt;
db:"{{ db_name }}"
login_user:"{{ db_user3 }}"
login_password:password
ignore_errors:yes
when:postgres_version_resp.stdout is version('10', '>=')
###########################################
# Test for 'type' value of type parameter #
###########################################
# Test
- name:Grant type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:present
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:numeric
schema:pg_catalog
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:SELECT typacl FROM pg_catalog.pg_type WHERE typname = 'numeric';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
- name:Grant type privileges again using check_mode
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:present
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:numeric
schema:pg_catalog
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
check_mode:yes
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is not changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:SELECT typacl FROM pg_catalog.pg_type WHERE typname = 'numeric';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
- name:Grant type privileges again
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:present
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:numeric
schema:pg_catalog
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is not changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:SELECT typacl FROM pg_catalog.pg_type WHERE typname = 'numeric';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
- name:Revoke type privileges in check_mode
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:absent
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:numeric
schema:pg_catalog
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
check_mode:yes
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:SELECT typacl FROM pg_catalog.pg_type WHERE typname = 'numeric';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
- name:Revoke type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:absent
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:numeric
schema:pg_catalog
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:SELECT typacl FROM pg_catalog.pg_type WHERE typname = 'numeric';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' not in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
# type with default schema (public):
- name:Create custom type in schema public
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:"CREATE TYPE compfoo AS (f1 int, f2 text)"
when:postgres_version_resp.stdout is version('10', '>=')
# Test
- name:Grant type privileges with default schema
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:present
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:compfoo
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
register:result
when:postgres_version_resp.stdout is version('10', '>=')
# Checks
- assert:
that:
- result is changed
when:postgres_version_resp.stdout is version('10', '>=')
- name:Get type privileges
become:yes
become_user:"{{ pg_user }}"
postgresql_query:
login_user:"{{ pg_user }}"
login_db:"{{ db_name }}"
query:>
SELECT t.typacl FROM pg_catalog.pg_type t JOIN pg_catalog.pg_namespace n
ONn.oid = t.typnamespace WHERE t.typname = 'compfoo' AND n.nspname = 'public';
register:typ_result
when:postgres_version_resp.stdout is version('10', '>=')
- assert:
that:
- "'{{ db_user2 }}' in typ_result.query_result[0].typacl"
when:postgres_version_resp.stdout is version('10', '>=')
# Cleanup
- name:Remove privs
become:yes
become_user:"{{ pg_user }}"
postgresql_privs:
state:absent
type:type
roles:"{{ db_user2 }}"
privs:ALL
objs:compfoo
db:"{{ db_name }}"
login_user:"{{ pg_user }}"
when:postgres_version_resp.stdout is version('10', '>=')