Blame SOURCES/ansible-freeipa-1.8.3-ipaconfig-Do-not-require-enable_sid-for-add_sids-or-_RHBZ#2135775.patch

fc3955
Adapted version of
fc3955
fc3955
From c808ad6e3408c2145ba660025c75531920f05d73 Mon Sep 17 00:00:00 2001
fc3955
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
fc3955
Date: Tue, 18 Oct 2022 10:26:01 -0300
fc3955
Subject: [PATCH] ipaconfig: Do not require enable_sid for add_sids or
fc3955
 netbios_name
fc3955
fc3955
Current behavior of ipaconfig mimics FreeIPA CLI and requires that
fc3955
'enable_sid' is set to True every time add_sids or netbios_name are
fc3955
used. It is sufficient that SID generation is enabled to use add_sids
fc3955
and netbios_name, but the IPA API requires 'enable_sid' so that the
fc3955
operations are executed.
fc3955
fc3955
This patch allows ansible-freeipa plugin ipaconfig to run 'add_sids' or
fc3955
set 'netbios_name without requiring 'enable_sid' to be set on the
fc3955
playbook.
fc3955
fc3955
If SID generation is enabled, 'add_sids' and 'netbios_name' can be used
fc3955
without 'enable_sid: yes'. If SID generation is not enabled, an error
fc3955
message will be raised if 'enable_sid: yes' is not used.
fc3955
---
fc3955
 README-config.md                 |  4 +--
fc3955
 plugins/modules/ipaconfig.py     | 53 +++++++++++++++++---------------
fc3955
 tests/config/test_config_sid.yml | 48 +++++++++++++++++++++++++++--
fc3955
 3 files changed, 76 insertions(+), 29 deletions(-)
fc3955
fc3955
diff --git a/README-config.md b/README-config.md
fc3955
index d6fe40a..a1d6117 100644
fc3955
--- a/README-config.md
fc3955
+++ b/README-config.md
fc3955
@@ -149,8 +149,8 @@ Variable | Description | Required
fc3955
 `domain_resolution_order` \| `ipadomainresolutionorder` | Set list of domains used for short name qualification | no
fc3955
 `ca_renewal_master_server` \| `ipacarenewalmasterserver`| Renewal master for IPA certificate authority. | no
fc3955
 `enable_sid` | New users and groups automatically get a SID assigned. Cannot be deactivated once activated. Requires IPA 4.9.8+. (bool) | no
fc3955
-`netbios_name` | NetBIOS name of the IPA domain. Requires IPA 4.9.8+ and 'enable_sid: yes'. | no
fc3955
-`add_sids` | Add SIDs for existing users and groups. Requires IPA 4.9.8+ and 'enable_sid: yes'. (bool) | no
fc3955
+`netbios_name` | NetBIOS name of the IPA domain. Requires IPA 4.9.8+ and SID generation to be activated. | no
fc3955
+`add_sids` | Add SIDs for existing users and groups. Requires IPA 4.9.8+ and SID generation to be activated. (bool) | no
fc3955
 
fc3955
 
fc3955
 Return Values
fc3955
diff --git a/plugins/modules/ipaconfig.py b/plugins/modules/ipaconfig.py
fc3955
index 9c19afb..7e78492 100644
fc3955
--- a/plugins/modules/ipaconfig.py
fc3955
+++ b/plugins/modules/ipaconfig.py
fc3955
@@ -180,14 +180,14 @@ options:
fc3955
         type: bool
fc3955
     netbios_name:
fc3955
         description: >
fc3955
-          NetBIOS name of the IPA domain.
fc3955
-          Requires IPA 4.9.8+ and 'enable_sid: yes'.
fc3955
+          NetBIOS name of the IPA domain. Requires IPA 4.9.8+
fc3955
+          and SID generation to be activated.
fc3955
         required: false
fc3955
         type: string
fc3955
     add_sids:
fc3955
         description: >
fc3955
-          Add SIDs for existing users and groups.
fc3955
-          Requires IPA 4.9.8+ and 'enable_sid: yes'.
fc3955
+          Add SIDs for existing users and groups. Requires IPA 4.9.8+
fc3955
+          and SID generation to be activated.
fc3955
         required: false
fc3955
         type: bool
fc3955
 '''
fc3955
@@ -362,7 +362,7 @@ def get_netbios_name(module):
fc3955
 
fc3955
 
fc3955
 def is_enable_sid(module):
fc3955
-    """When 'enable-sid' is true admin user and admins group have SID set."""
fc3955
+    """When 'enable_sid' is true admin user and admins group have SID set."""
fc3955
     _result = module.ipa_command("user_show", "admin", {"all": True})
fc3955
     sid = _result["result"].get("ipantsecurityidentifier", [""])
fc3955
     if not sid[0].endswith("-500"):
fc3955
@@ -517,7 +517,7 @@ def main():
fc3955
     changed = False
fc3955
     exit_args = {}
fc3955
 
fc3955
-    # Connect to IPA API (enable-sid requires context == 'client')
fc3955
+    # Connect to IPA API (enable_sid requires context == 'client')
fc3955
     with ansible_module.ipa_connect(context="client"):
fc3955
         has_enable_sid = ansible_module.ipa_command_param_exists(
fc3955
             "config_mod", "enable_sid")
fc3955
@@ -532,20 +532,8 @@ def main():
fc3955
                 ansible_module.fail_json(msg="SID cannot be disabled.")
fc3955
 
fc3955
             netbios_name = params.get("netbios_name")
fc3955
-            if netbios_name:
fc3955
-                netbios_name = netbios_name.upper()
fc3955
             add_sids = params.get("add_sids")
fc3955
-            required_sid = any([netbios_name, add_sids])
fc3955
-            if required_sid and not enable_sid:
fc3955
-                ansible_module.fail_json(
fc3955
-                    msg="'enable-sid: yes' required for 'netbios_name' "
fc3955
-                        "and 'add-sids'."
fc3955
-                )
fc3955
-            if enable_sid:
fc3955
-                if not has_enable_sid:
fc3955
-                    ansible_module.fail_json(
fc3955
-                        msg="This version of IPA does not support enable-sid."
fc3955
-                    )
fc3955
+            if has_enable_sid:
fc3955
                 if (
fc3955
                     netbios_name
fc3955
                     and netbios_name == get_netbios_name(ansible_module)
fc3955
@@ -554,12 +542,27 @@ def main():
fc3955
                     netbios_name = None
fc3955
                 if not add_sids and "add_sids" in params:
fc3955
                     del params["add_sids"]
fc3955
-                if (
fc3955
-                    not any([netbios_name, add_sids])
fc3955
-                    and sid_is_enabled
fc3955
-                ):
fc3955
-                    del params["enable_sid"]
fc3955
-
fc3955
+                if any([netbios_name, add_sids]):
fc3955
+                    if sid_is_enabled:
fc3955
+                        params["enable_sid"] = True
fc3955
+                    else:
fc3955
+                        if not enable_sid:
fc3955
+                            ansible_module.fail_json(
fc3955
+                                msg="SID generation must be enabled for "
fc3955
+                                    "'netbios_name' and 'add_sids'. Use "
fc3955
+                                    "'enable_sid: yes'."
fc3955
+                            )
fc3955
+                else:
fc3955
+                    if sid_is_enabled and "enable_sid" in params:
fc3955
+                        del params["enable_sid"]
fc3955
+
fc3955
+            else:
fc3955
+                if any([enable_sid, netbios_name, add_sids is not None]):
fc3955
+                    ansible_module.fail_json(
fc3955
+                        msg="This version of IPA does not support enable_sid, "
fc3955
+                            "add_sids or netbios_name setting through the "
fc3955
+                            "config module"
fc3955
+                    )
fc3955
             params = {
fc3955
                 k: v for k, v in params.items()
fc3955
                 if k not in result or result[k] != v
fc3955
diff --git a/tests/config/test_config_sid.yml b/tests/config/test_config_sid.yml
fc3955
index bd550a5..d8d78f1 100644
fc3955
--- a/tests/config/test_config_sid.yml
fc3955
+++ b/tests/config/test_config_sid.yml
fc3955
@@ -19,6 +19,32 @@
fc3955
 
fc3955
   # TESTS
fc3955
   - block:
fc3955
+    - name: Check if SID is enabled.
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        enable_sid: yes
fc3955
+      check_mode: yes
fc3955
+      register: sid_disabled
fc3955
+
fc3955
+    - name: Ensure netbios_name can't be changed without SID enabled.
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        netbios_name: IPATESTPLAY
fc3955
+      register: result
fc3955
+      failed_when: not result.failed and "SID generation must be enabled" in result.msg
fc3955
+      when: sid_disabled.changed
fc3955
+
fc3955
+    - name: Ensure SIDs can't be changed without SID enabled.
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        add_sids: yes
fc3955
+      register: result
fc3955
+      failed_when: not result.failed and "SID generation must be enabled" in result.msg
fc3955
+      when: sid_disabled.changed
fc3955
+
fc3955
     - name: Ensure SID is enabled.
fc3955
       ipaconfig:
fc3955
         ipaadmin_password: SomeADMINpassword
fc3955
@@ -56,18 +82,36 @@
fc3955
       ipaconfig:
fc3955
         ipaadmin_password: SomeADMINpassword
fc3955
         ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
-        enable_sid: yes
fc3955
         netbios_name: IPATESTPLAY
fc3955
       register: result
fc3955
       failed_when: result.failed or result.changed
fc3955
 
fc3955
+    - name: Ensure netbios_name cannot be set with lowercase characters
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        netbios_name: IPATESTplay
fc3955
+      register: result
fc3955
+      failed_when:
fc3955
+        (not result.failed
fc3955
+         and "Up to 15 characters and only uppercase ASCII letters, digits and dashes are allowed" not in result.message)
fc3955
+
fc3955
+    - name: Ensure netbios_name cannot be set different lowercase characters
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        netbios_name: otherPLAY
fc3955
+      register: result
fc3955
+      failed_when:
fc3955
+        (not result.failed
fc3955
+         and "Up to 15 characters and only uppercase ASCII letters, digits and dashes are allowed" not in result.message)
fc3955
+
fc3955
     # add_sids is not idempotent as it always tries to generate the missing
fc3955
     # SIDs for users and groups.
fc3955
     - name: Add SIDs to users and groups.
fc3955
       ipaconfig:
fc3955
         ipaadmin_password: SomeADMINpassword
fc3955
         ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
-        enable_sid: yes
fc3955
         add_sids: yes
fc3955
 
fc3955
     # only run tests if version supports enable-sid
fc3955
-- 
fc3955
2.37.3
fc3955