From 09942c3d69c20f89667441f6de83b8d8fc8e4bb2 Mon Sep 17 00:00:00 2001
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
Date: Wed, 17 Mar 2021 15:35:04 -0300
Subject: [PATCH] Force plugins to execute using LANGUAGE='C'.

IPA translates exception messages and Ansible uses controller's
language to execute plugins on target hosts, and since ansible-freeipa
uses Exceptions messages to detect some errors and/or states, using any
language that has a translation for the required messages may cause the
plugin to misbehave.

This patch modifies ansible_freeipa_module in plugin/module_utils to
force the use of "C" as the language by setting the environment variable
LANGUAGE.

Tests were added to verify the correct behavior:

    tests/environment/test_locale.yml

The first test will fail, if ansible_freeipa_module is not patched, with
the message:

   host_show failed: nonexistent: host nicht gefunden

This issue is not present if the language selected does not provide
a translation for the eror message.

This patch does not fix encoding issues that might occur in certain
releases (e.g.: CentOS 8.3).

Fix #516
---
 .../module_utils/ansible_freeipa_module.py    |  3 ++
 tests/environment/test_locale.yml             | 32 +++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 tests/environment/test_locale.yml

diff --git a/plugins/module_utils/ansible_freeipa_module.py b/plugins/module_utils/ansible_freeipa_module.py
index 16e7343f..5ac52c73 100644
--- a/plugins/module_utils/ansible_freeipa_module.py
+++ b/plugins/module_utils/ansible_freeipa_module.py
@@ -93,6 +93,9 @@ except ImportError:
 if six.PY3:
     unicode = str
 
+# ansible-freeipa requires locale to be C, IPA requires utf-8.
+os.environ["LANGUAGE"] = "C"
+
 
 def valid_creds(module, principal):  # noqa
     """Get valid credentials matching the princial, try GSSAPI first."""
diff --git a/tests/environment/test_locale.yml b/tests/environment/test_locale.yml
new file mode 100644
index 00000000..27d03de7
--- /dev/null
+++ b/tests/environment/test_locale.yml
@@ -0,0 +1,32 @@
+---
+- name: Test language variations
+  hosts: ipaserver
+
+  tasks:
+  - name: Ensure a host is not present, with language set to "de_DE".
+    ipahost:
+      ipaadmin_password: SomeADMINpassword
+      name: nonexistent
+      state: absent
+    environment:
+      LANGUAGE: "de_DE"
+    register: result
+    failed_when: result.failed or result.changed
+
+  - name: Ensure a host is not present, with language set to "C".
+    ipahost:
+      ipaadmin_password: SomeADMINpassword
+      name: nonexistent
+      state: absent
+    environment:
+      LANGUAGE: "C"
+    register: result
+    failed_when: result.failed or result.changed
+
+  - name: Ensure a host is not present, using controller language.
+    ipahost:
+      ipaadmin_password: SomeADMINpassword
+      name: nonexistent
+      state: absent
+    register: result
+    failed_when: result.failed or result.changed
-- 
GitLab