Blame SOURCES/ansible-freeipa-0.1.8-ipapwpolicy-Use-global_policy-if-name-is-not-set_rhbz#1797532.patch

d9912c
From 4dd1d25eacd1481be0a881a017144ff4d3396ccd Mon Sep 17 00:00:00 2001
d9912c
From: Thomas Woerner <twoerner@redhat.com>
d9912c
Date: Thu, 6 Feb 2020 15:38:00 +0100
d9912c
Subject: [PATCH] ipapwpolicy: Use global_policy if name is not set
d9912c
d9912c
If the name is not set, the policy global_policy is now used. It was needed
d9912c
before to explicitly name the global_policy. Also a check has been added
d9912c
to fail early if global_policy is used with state absent.
d9912c
d9912c
The README for pwpolicy has been extended with an example for global_policy
d9912c
and also the description of the name variable.
d9912c
d9912c
The test has also been extended to check a change of maxlife for
d9912c
global_policy and that global_policy can not be used with state: absent
d9912c
d9912c
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1797532
d9912c
---
d9912c
 README-pwpolicy.md               | 19 +++++++++++--
d9912c
 plugins/modules/ipapwpolicy.py   |  9 ++++--
d9912c
 tests/pwpolicy/test_pwpolicy.yml | 49 ++++++++++++++++++++++++++++++++
d9912c
 3 files changed, 73 insertions(+), 4 deletions(-)
d9912c
d9912c
diff --git a/README-pwpolicy.md b/README-pwpolicy.md
d9912c
index 16306b7..847b32d 100644
d9912c
--- a/README-pwpolicy.md
d9912c
+++ b/README-pwpolicy.md
d9912c
@@ -56,7 +56,7 @@ Example playbook to ensure presence of pwpolicies for exisiting group ops:
d9912c
       maxfail: 3
d9912c
 ```
d9912c
 
d9912c
-Example playbook to ensure absence of pwpolicies for group ops
d9912c
+Example playbook to ensure absence of pwpolicies for group ops:
d9912c
 
d9912c
 ```yaml
d9912c
 ---
d9912c
@@ -72,6 +72,21 @@ Example playbook to ensure absence of pwpolicies for group ops
d9912c
       state: absent
d9912c
 ```
d9912c
 
d9912c
+Example playbook to ensure maxlife is set to 49 in global policy:
d9912c
+
d9912c
+```yaml
d9912c
+---
d9912c
+- name: Playbook to handle pwpolicies
d9912c
+  hosts: ipaserver
d9912c
+  become: true
d9912c
+
d9912c
+  tasks:
d9912c
+  # Ensure absence of pwpolicies for group ops
d9912c
+  - ipapwpolicy:
d9912c
+      ipaadmin_password: MyPassword123
d9912c
+      maxlife: 49
d9912c
+```
d9912c
+
d9912c
 
d9912c
 Variables
d9912c
 =========
d9912c
@@ -83,7 +98,7 @@ Variable | Description | Required
d9912c
 -------- | ----------- | --------
d9912c
 `ipaadmin_principal` | The admin principal is a string and defaults to `admin` | no
d9912c
 `ipaadmin_password` | The admin password is a string and is required if there is no admin ticket available on the node | no
d9912c
-`name` \| `cn` | The list of pwpolicy name strings. | no
d9912c
+`name` \| `cn` | The list of pwpolicy name strings. If name is not given, `global_policy` will be used automatically. | no
d9912c
 `maxlife` \| `krbmaxpwdlife` | Maximum password lifetime in days. (int) | no
d9912c
 `minlife` \| `krbminpwdlife` | Minimum password lifetime in hours. (int) | no
d9912c
 `history` \| `krbpwdhistorylength` | Password history size. (int) | no
d9912c
diff --git a/plugins/modules/ipapwpolicy.py b/plugins/modules/ipapwpolicy.py
d9912c
index 9437b59..f168703 100644
d9912c
--- a/plugins/modules/ipapwpolicy.py
d9912c
+++ b/plugins/modules/ipapwpolicy.py
d9912c
@@ -167,7 +167,7 @@ def main():
d9912c
             ipaadmin_password=dict(type="str", required=False, no_log=True),
d9912c
 
d9912c
             name=dict(type="list", aliases=["cn"], default=None,
d9912c
-                      required=True),
d9912c
+                      required=False),
d9912c
             # present
d9912c
 
d9912c
             maxlife=dict(type="int", aliases=["krbmaxpwdlife"], default=None),
d9912c
@@ -218,6 +218,9 @@ def main():
d9912c
 
d9912c
     # Check parameters
d9912c
 
d9912c
+    if names is None:
d9912c
+        names = ["global_policy"]
d9912c
+
d9912c
     if state == "present":
d9912c
         if len(names) != 1:
d9912c
             ansible_module.fail_json(
d9912c
@@ -225,8 +228,10 @@ def main():
d9912c
 
d9912c
     if state == "absent":
d9912c
         if len(names) < 1:
d9912c
+            ansible_module.fail_json(msg="No name given.")
d9912c
+        if "global_policy" in names:
d9912c
             ansible_module.fail_json(
d9912c
-                msg="No name given.")
d9912c
+                msg="'global_policy' can not be made absent.")
d9912c
         invalid = ["maxlife", "minlife", "history", "minclasses",
d9912c
                    "minlength", "priority", "maxfail", "failinterval",
d9912c
                    "lockouttime"]
d9912c
diff --git a/tests/pwpolicy/test_pwpolicy.yml b/tests/pwpolicy/test_pwpolicy.yml
d9912c
index 5c69345..f93f275 100644
d9912c
--- a/tests/pwpolicy/test_pwpolicy.yml
d9912c
+++ b/tests/pwpolicy/test_pwpolicy.yml
d9912c
@@ -5,10 +5,30 @@
d9912c
   gather_facts: false
d9912c
 
d9912c
   tasks:
d9912c
+  - name: Ensure maxlife of 90 for global_policy
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      maxlife: 90
d9912c
+
d9912c
+  - name: Ensure absence of group ops
d9912c
+    ipagroup:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      name: ops
d9912c
+      state: absent
d9912c
+
d9912c
+  - name: Ensure absence of pwpolicies for group ops
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      name: ops
d9912c
+      state: absent
d9912c
+
d9912c
   - name: Ensure presence of group ops
d9912c
     ipagroup:
d9912c
       ipaadmin_password: SomeADMINpassword
d9912c
       name: ops
d9912c
+      state: present
d9912c
+    register: result
d9912c
+    failed_when: not result.changed
d9912c
 
d9912c
   - name: Ensure presence of pwpolicies for group ops
d9912c
     ipapwpolicy:
d9912c
@@ -42,6 +62,28 @@
d9912c
     register: result
d9912c
     failed_when: result.changed
d9912c
 
d9912c
+  - name: Ensure maxlife of 49 for global_policy
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      maxlife: 49
d9912c
+    register: result
d9912c
+    failed_when: not result.changed
d9912c
+
d9912c
+  - name: Ensure maxlife of 49 for global_policy again
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      maxlife: 49
d9912c
+    register: result
d9912c
+    failed_when: result.changed
d9912c
+
d9912c
+  - name: Ensure absence of pwpoliciy global_policy will fail
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: SomeADMINpassword
d9912c
+      state: absent
d9912c
+    register: result
d9912c
+    ignore_errors: True
d9912c
+    failed_when: result is defined and result
d9912c
+
d9912c
   - name: Ensure absence of pwpolicies for group ops
d9912c
     ipapwpolicy:
d9912c
       ipaadmin_password: SomeADMINpassword
d9912c
@@ -50,6 +92,13 @@
d9912c
     register: result
d9912c
     failed_when: not result.changed
d9912c
 
d9912c
+  - name: Ensure maxlife of 90 for global_policy
d9912c
+    ipapwpolicy:
d9912c
+      ipaadmin_password: MyPassword123
d9912c
+      maxlife: 90
d9912c
+    register: result
d9912c
+    failed_when: not result.changed
d9912c
+
d9912c
   - name: Ensure absence of pwpolicies for group ops
d9912c
     ipapwpolicy:
d9912c
       ipaadmin_password: SomeADMINpassword