From a0c8a3034af56ed3c41fb10199289f6d209da88a Mon Sep 17 00:00:00 2001 From: VMax <48584368+vosmax@users.noreply.github.com> Date: Thu, 16 Jul 2020 17:44:19 +0300 Subject: [PATCH] modules: fix names with hyphens (#656) (#659) * modules: fix names with hyphens (#656) * modules: fix names with hyphens (#656) * Fix param name for postgresql_schema * Add double quotes for schema name * Add delete created DB objects * Fix module code * Set correct test tasks order Co-authored-by: Maxim Voskresenskiy --- changelogs/fragments/656-name-with-hyphen.yml | 3 + .../database/postgresql/postgresql_privs.py | 3 + .../postgresql_privs/defaults/main.yml | 3 + .../tasks/postgresql_privs_general.yml | 119 ++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 changelogs/fragments/656-name-with-hyphen.yml diff --git a/changelogs/fragments/656-name-with-hyphen.yml b/changelogs/fragments/656-name-with-hyphen.yml new file mode 100644 index 0000000000..ad326f73ec --- /dev/null +++ b/changelogs/fragments/656-name-with-hyphen.yml @@ -0,0 +1,3 @@ +--- +bugfixes: +- postgresql_privs - fix crash when set privileges on schema with hyphen in the name (https://github.com/ansible-collections/community.general/issues/656). diff --git a/plugins/modules/database/postgresql/postgresql_privs.py b/plugins/modules/database/postgresql/postgresql_privs.py index 0ad9812a39..6325c86656 100644 --- a/plugins/modules/database/postgresql/postgresql_privs.py +++ b/plugins/modules/database/postgresql/postgresql_privs.py @@ -766,6 +766,9 @@ class Connection(object): if target_roles: as_who = ','.join('"%s"' % r for r in target_roles) + if schema_qualifier: + schema_qualifier = '"%s"' % schema_qualifier + status_before = get_status(objs) query = QueryBuilder(state) \ diff --git a/tests/integration/targets/postgresql_privs/defaults/main.yml b/tests/integration/targets/postgresql_privs/defaults/main.yml index 3ef770e020..e03dd494f3 100644 --- a/tests/integration/targets/postgresql_privs/defaults/main.yml +++ b/tests/integration/targets/postgresql_privs/defaults/main.yml @@ -4,6 +4,9 @@ db_user2: ansible_db_user2 db_user3: ansible_db_user3 db_user_with_dots1: role.with.dots1 db_user_with_dots2: role.with.dots2 +db_name_with_hyphens: ansible-db +db_user_with_hyphens: ansible-db-user +db_schema_with_hyphens: ansible-db-schema db_session_role1: session_role1 db_session_role2: session_role2 dangerous_name: 'curious.anonymous"; SELECT * FROM information_schema.tables; --' diff --git a/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml b/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml index 98dab517d8..431c78aa17 100644 --- a/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml +++ b/tests/integration/targets/postgresql_privs/tasks/postgresql_privs_general.yml @@ -27,6 +27,125 @@ db: "{{ db_name }}" login_user: "{{ pg_user }}" +############################# +# Test of solving bug 656 # +############################# +- name: Create DB with hyphen in the name + become_user: "{{ pg_user }}" + become: yes + postgresql_db: + state: present + name: "{{ db_name_with_hyphens }}" + login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + +- name: Create a user with hyphen in the name + postgresql_user: + name: "{{ db_user_with_hyphens }}" + state: present + encrypted: yes + password: password + role_attr_flags: CREATEDB,LOGIN + db: "{{ db_name_with_hyphens }}" + login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + +- name: Create schema with hyphen in the name + postgresql_schema: + login_user: "{{ pg_user }}" + login_password: password + db: "{{ db_name_with_hyphens }}" + name: "{{ db_schema_with_hyphens }}" + state: present + register: result + +- assert: + that: + - result is changed + +- name: Set table default privs on the schema with hyphen in the name + postgresql_privs: + login_user: "{{ pg_user }}" + password: password + db: "{{ db_name_with_hyphens }}" + schema: "{{ db_schema_with_hyphens }}" + role: "{{ db_user_with_hyphens }}" + type: default_privs + obj: TABLES + privs: all + state: present + register: result + +- assert: + that: + - result is changed + +- name: Delete table default privs on the schema with hyphen in the name + postgresql_privs: + login_user: "{{ pg_user }}" + password: password + db: "{{ db_name_with_hyphens }}" + schema: "{{ db_schema_with_hyphens }}" + role: "{{ db_user_with_hyphens }}" + type: default_privs + obj: TABLES + privs: all + state: absent + register: result + +- assert: + that: + - result is changed + +- name: Delete schema with hyphen in the name + postgresql_schema: + login_user: "{{ pg_user }}" + login_password: password + db: "{{ db_name_with_hyphens }}" + name: "{{ db_schema_with_hyphens }}" + state: absent + register: result + +- assert: + that: + - result is changed + +- name: Delete a user with hyphen in the name + postgresql_user: + name: "{{ db_user_with_hyphens }}" + state: absent + encrypted: yes + password: password + role_attr_flags: CREATEDB,LOGIN + db: "{{ db_name_with_hyphens }}" + login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + +- name: Delete DB with hyphen in the name + become_user: "{{ pg_user }}" + become: yes + postgresql_db: + state: absent + name: "{{ db_name_with_hyphens }}" + login_user: "{{ pg_user }}" + register: result + +- assert: + that: + - result is changed + ############################# # Test of solving bug 27327 # #############################