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

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