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

postgresql_lang: add trust_input parameter #272

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- postgresql_lang - add the ``trust_input`` parameter (https://github.com/ansible-collections/community.general/pull/272).
15 changes: 14 additions & 1 deletion plugins/modules/database/postgresql/postgresql_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
- Set an owner for the language.
- Ignored when I(state=absent).
type: str
trust_input:
description:
- If C(no), check whether values of some parameters are potentially dangerous.
type: bool
default: yes
seealso:
- name: PostgreSQL languages
description: General information about PostgreSQL languages.
Expand Down Expand Up @@ -176,6 +181,7 @@
'''

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.community.general.plugins.module_utils.database import check_input
from ansible_collections.community.general.plugins.module_utils.postgres import (
connect_to_db,
get_conn_params,
Expand Down Expand Up @@ -258,7 +264,7 @@ def set_lang_owner(cursor, lang, owner):
lang (str): language name.
owner (str): name of new owner.
"""
query = "ALTER LANGUAGE \"%s\" OWNER TO %s" % (lang, owner)
query = "ALTER LANGUAGE \"%s\" OWNER TO \"%s\"" % (lang, owner)
executed_queries.append(query)
cursor.execute(query)
return True
Expand All @@ -276,6 +282,7 @@ def main():
fail_on_drop=dict(type="bool", default="yes"),
session_role=dict(type="str"),
owner=dict(type="str"),
trust_input=dict(type="bool", default="yes")
)

module = AnsibleModule(
Expand All @@ -291,6 +298,12 @@ def main():
cascade = module.params["cascade"]
fail_on_drop = module.params["fail_on_drop"]
owner = module.params["owner"]
session_role = module.params["session_role"]
trust_input = module.params["trust_input"]

if not trust_input:
# Check input for potentially dangerous elements:
check_input(module, lang, session_role, owner)

conn_params = get_conn_params(module, module.params)
db_connection = connect_to_db(module, conn_params, autocommit=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<<: *pg_parameters
name: '{{ test_lang }}'
owner: '{{ test_user1 }}'
trust_input: no
check_mode: yes

- assert:
Expand Down Expand Up @@ -57,11 +58,12 @@
<<: *pg_parameters
name: '{{ test_lang }}'
owner: '{{ test_user1 }}'
trust_input: no

- assert:
that:
- result is changed
- result.queries == ['CREATE LANGUAGE "{{ test_lang }}"', 'ALTER LANGUAGE "{{ test_lang }}" OWNER TO {{ test_user1 }}']
- result.queries == ['CREATE LANGUAGE "{{ test_lang }}"', 'ALTER LANGUAGE "{{ test_lang }}" OWNER TO "{{ test_user1 }}"']

- name: Check
<<: *task_parameters
Expand All @@ -83,12 +85,13 @@
<<: *pg_parameters
name: '{{ test_lang }}'
owner: '{{ test_user2 }}'
trust_input: yes
check_mode: yes

- assert:
that:
- result is changed
- result.queries == ['ALTER LANGUAGE "{{ test_lang }}" OWNER TO {{ test_user2 }}']
- result.queries == ['ALTER LANGUAGE "{{ test_lang }}" OWNER TO "{{ test_user2 }}"']

- name: Check that nothing was actually changed
<<: *task_parameters
Expand Down Expand Up @@ -116,7 +119,7 @@
- result is changed
# TODO: the first elem of the returned list below
# looks like a bug, not related with the option owner, needs to be checked
- result.queries == ["UPDATE pg_language SET lanpltrusted = false WHERE lanname = '{{ test_lang }}'", 'ALTER LANGUAGE "{{ test_lang }}" OWNER TO {{ test_user2 }}']
- result.queries == ["UPDATE pg_language SET lanpltrusted = false WHERE lanname = '{{ test_lang }}'", 'ALTER LANGUAGE "{{ test_lang }}" OWNER TO "{{ test_user2 }}"']

- name: Check
<<: *task_parameters
Expand Down