Blame SOURCES/ansible-freeipa-0.1.12-ipa-user,host-Fail-on-duplucate-names-in-the-users-and-hosts-lists_rhbz#1822683.patch

fb9e9a
From 1d7fb31b8bfa00babd7c753b354d7344b531cd77 Mon Sep 17 00:00:00 2001
fb9e9a
From: Thomas Woerner <twoerner@redhat.com>
fb9e9a
Date: Mon, 29 Jun 2020 14:50:56 +0200
fb9e9a
Subject: [PATCH] ipa[user,host]: Fail on duplucate names in the users and
fb9e9a
 hosts lists
fb9e9a
fb9e9a
It was possible to have several entries for names with the hosts and users
fb9e9a
lists. This resulted sometimes in errors but also unexpected changes. A new
fb9e9a
check has been added to make sure that the names in the users and hosts
fb9e9a
lists are unique.
fb9e9a
fb9e9a
New tests have been added to verify this in the existing files:
fb9e9a
- tests/host/test_hosts.yml
fb9e9a
- tests/user/test_users.yml
fb9e9a
---
fb9e9a
 plugins/modules/ipahost.py |  7 +++++++
fb9e9a
 plugins/modules/ipauser.py |  7 +++++++
fb9e9a
 tests/host/test_hosts.yml  | 15 +++++++++++++++
fb9e9a
 tests/user/test_users.yml  | 19 +++++++++++++++++++
fb9e9a
 4 files changed, 48 insertions(+)
fb9e9a
fb9e9a
diff --git a/plugins/modules/ipahost.py b/plugins/modules/ipahost.py
fb9e9a
index 7a981f16..1fe11dc5 100644
fb9e9a
--- a/plugins/modules/ipahost.py
fb9e9a
+++ b/plugins/modules/ipahost.py
fb9e9a
@@ -799,10 +799,15 @@ def main():
fb9e9a
         server_realm = api_get_realm()
fb9e9a
 
fb9e9a
         commands = []
fb9e9a
+        host_set = set()
fb9e9a
 
fb9e9a
         for host in names:
fb9e9a
             if isinstance(host, dict):
fb9e9a
                 name = host.get("name")
fb9e9a
+                if name in host_set:
fb9e9a
+                    ansible_module.fail_json(
fb9e9a
+                        msg="host '%s' is used more than once" % name)
fb9e9a
+                host_set.add(name)
fb9e9a
                 description = host.get("description")
fb9e9a
                 locality = host.get("locality")
fb9e9a
                 location = host.get("location")
fb9e9a
@@ -1337,6 +1342,8 @@ def main():
fb9e9a
             else:
fb9e9a
                 ansible_module.fail_json(msg="Unkown state '%s'" % state)
fb9e9a
 
fb9e9a
+        del host_set
fb9e9a
+
fb9e9a
         # Execute commands
fb9e9a
 
fb9e9a
         errors = []
fb9e9a
diff --git a/plugins/modules/ipauser.py b/plugins/modules/ipauser.py
fb9e9a
index b8152ee4..03713a41 100644
fb9e9a
--- a/plugins/modules/ipauser.py
fb9e9a
+++ b/plugins/modules/ipauser.py
fb9e9a
@@ -958,10 +958,15 @@ def main():
fb9e9a
         # commands
fb9e9a
 
fb9e9a
         commands = []
fb9e9a
+        user_set = set()
fb9e9a
 
fb9e9a
         for user in names:
fb9e9a
             if isinstance(user, dict):
fb9e9a
                 name = user.get("name")
fb9e9a
+                if name in user_set:
fb9e9a
+                    ansible_module.fail_json(
fb9e9a
+                        msg="user '%s' is used more than once" % name)
fb9e9a
+                user_set.add(name)
fb9e9a
                 # present
fb9e9a
                 first = user.get("first")
fb9e9a
                 last = user.get("last")
fb9e9a
@@ -1370,6 +1375,8 @@ def main():
fb9e9a
             else:
fb9e9a
                 ansible_module.fail_json(msg="Unkown state '%s'" % state)
fb9e9a
 
fb9e9a
+        del user_set
fb9e9a
+
fb9e9a
         # Execute commands
fb9e9a
 
fb9e9a
         errors = []
fb9e9a
diff --git a/tests/host/test_hosts.yml b/tests/host/test_hosts.yml
fb9e9a
index 30fd6538..f82cc612 100644
fb9e9a
--- a/tests/host/test_hosts.yml
fb9e9a
+++ b/tests/host/test_hosts.yml
fb9e9a
@@ -96,3 +96,18 @@
fb9e9a
       state: absent
fb9e9a
     register: result
fb9e9a
     failed_when: result.changed
fb9e9a
+
fb9e9a
+  - name: Duplicate names in hosts failure test
fb9e9a
+    ipahost:
fb9e9a
+      ipaadmin_password: SomeADMINpassword
fb9e9a
+      hosts:
fb9e9a
+      - name: "{{ host1_fqdn }}"
fb9e9a
+        force: yes
fb9e9a
+      - name: "{{ host2_fqdn }}"
fb9e9a
+        force: yes
fb9e9a
+      - name: "{{ host3_fqdn }}"
fb9e9a
+        force: yes
fb9e9a
+      - name: "{{ host3_fqdn }}"
fb9e9a
+        force: yes
fb9e9a
+    register: result
fb9e9a
+    failed_when: result.changed or "is used more than once" not in result.msg
fb9e9a
diff --git a/tests/user/test_users.yml b/tests/user/test_users.yml
fb9e9a
index 5b5d4538..81c7b608 100644
fb9e9a
--- a/tests/user/test_users.yml
fb9e9a
+++ b/tests/user/test_users.yml
fb9e9a
@@ -85,6 +85,25 @@
fb9e9a
     register: result
fb9e9a
     failed_when: result.changed
fb9e9a
 
fb9e9a
+  - name: Duplicate names in users failure test
fb9e9a
+    ipauser:
fb9e9a
+      ipaadmin_password: SomeADMINpassword
fb9e9a
+      users:
fb9e9a
+      - name: user1
fb9e9a
+        givenname: user1
fb9e9a
+        last: Last
fb9e9a
+      - name: user2
fb9e9a
+        first: user2
fb9e9a
+        last: Last
fb9e9a
+      - name: user3
fb9e9a
+        first: user3
fb9e9a
+        last: Last
fb9e9a
+      - name: user3
fb9e9a
+        first: user3
fb9e9a
+        last: Last
fb9e9a
+    register: result
fb9e9a
+    failed_when: result.changed or "is used more than once" not in result.msg
fb9e9a
+
fb9e9a
   - name: Remove test users
fb9e9a
     ipauser:
fb9e9a
       ipaadmin_password: SomeADMINpassword