Blame SOURCES/ansible-freeipa-1.8.3-ipaconfig-Add-support-for-SID-related-attributes_3c8d6c7_RHBZ#2132994.patch

fc3955
From 3c8d6c7c7aec408b3b68440982929e30e7d69130 Mon Sep 17 00:00:00 2001
fc3955
From: Rafael Guterres Jeffman <rjeffman@redhat.com>
fc3955
Date: Mon, 5 Sep 2022 17:18:00 -0300
fc3955
Subject: [PATCH] ipaconfig: Add support for SID related attributes.
fc3955
fc3955
Since FreeIPA 4.9.8 the 'config_mod' command has parameters to enable
fc3955
and configure SIDs, and set the Netbios name.
fc3955
fc3955
This patch adds the following parameters to ipaconfig plugin:
fc3955
    enable_sids: New users and groups automatically get a SID assigned
fc3955
    add_sids: Add SIDs for existing users and groups
fc3955
    netbios_name: NetBIOS name of the IPA domain
fc3955
fc3955
Both add_sids and netbios_name requires 'enable_sid: yes'.
fc3955
fc3955
'enable_sid' and 'netbios_name' are returned when querying IPA
fc3955
configuration.
fc3955
fc3955
'add_sids' always generate SIDs for users and groups, so, muiltiple
fc3955
executions of the playbook with 'add_sids: yes' will return 'changed',
fc3955
even if users and groups SIDs are not modified.
fc3955
fc3955
A new test playbook is available:
fc3955
fc3955
    tests/config/test_config_sid.yml
fc3955
fc3955
New examples playbooks are available:
fc3955
fc3955
    playbooks/config/change-ipa-domain-netbios-name.yml
fc3955
    playbooks/config/generate-users-groups-sids.yml
fc3955
fc3955
Fixes: #781
fc3955
Related: https://bugzilla.redhat.com/show_bug.cgi?id=2069174
fc3955
Related: https://bugzilla.redhat.com/show_bug.cgi?id=2069184
fc3955
---
fc3955
 README-config.md                              |  42 ++++++
fc3955
 .../config/change-ipa-domain-netbios-name.yml |  12 ++
fc3955
 .../config/generate-users-groups-sids.yml     |  12 ++
fc3955
 plugins/modules/ipaconfig.py                  | 120 +++++++++++++++++-
fc3955
 tests/azure/templates/variables_centos-7.yaml |   9 +-
fc3955
 tests/config/test_config_sid.yml              |  70 ++++++++++
fc3955
 6 files changed, 256 insertions(+), 9 deletions(-)
fc3955
 create mode 100644 playbooks/config/change-ipa-domain-netbios-name.yml
fc3955
 create mode 100644 playbooks/config/generate-users-groups-sids.yml
fc3955
 create mode 100644 tests/config/test_config_sid.yml
fc3955
fc3955
diff --git a/README-config.md b/README-config.md
fc3955
index 17c85f1..13023ea 100644
fc3955
--- a/README-config.md
fc3955
+++ b/README-config.md
fc3955
@@ -65,6 +65,9 @@ Example playbook to read config options:
fc3955
         maxusername: 64
fc3955
 ```
fc3955
 
fc3955
+
fc3955
+Example playbook to set global configuration options:
fc3955
+
fc3955
 ```yaml
fc3955
 ---
fc3955
 - name: Playbook to ensure some config options are set
fc3955
@@ -79,6 +82,40 @@ Example playbook to read config options:
fc3955
 ```
fc3955
 
fc3955
 
fc3955
+Example playbook to enable SID and generate users and groups SIDs:
fc3955
+
fc3955
+```yaml
fc3955
+---
fc3955
+- name: Playbook to ensure SIDs are enabled and users and groups have SIDs
fc3955
+  hosts: ipaserver
fc3955
+  become: no
fc3955
+  gather_facts: no
fc3955
+
fc3955
+  tasks:
fc3955
+    - name: Enable SID and generate users and groups SIDS
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        add_sids: yes
fc3955
+```
fc3955
+
fc3955
+Example playbook to change IPA domain NetBIOS name:
fc3955
+
fc3955
+```yaml
fc3955
+---
fc3955
+- name: Playbook to change IPA domain netbios name
fc3955
+  hosts: ipaserver
fc3955
+  become: no
fc3955
+  gather_facts: no
fc3955
+
fc3955
+  tasks:
fc3955
+    - name: Set IPA domain netbios name
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        netbios_name: IPADOM
fc3955
+```
fc3955
+
fc3955
 Variables
fc3955
 =========
fc3955
 
fc3955
@@ -111,6 +148,9 @@ Variable | Description | Required
fc3955
 `user_auth_type` \| `ipauserauthtype` |  set default types of supported user authentication (choices: `password`, `radius`, `otp`, `disabled`). Use `""` to clear this variable. | no
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. 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
 
fc3955
 
fc3955
 Return Values
fc3955
@@ -140,6 +180,8 @@ Variable | Description | Returned When
fc3955
   | `user_auth_type` |  
fc3955
   | `domain_resolution_order` |  
fc3955
   | `ca_renewal_master_server` |  
fc3955
+  | `enable_sid` |  
fc3955
+  | `netbios_name` |  
fc3955
 
fc3955
 All returned fields take the same form as their namesake input parameters
fc3955
 
fc3955
diff --git a/playbooks/config/change-ipa-domain-netbios-name.yml b/playbooks/config/change-ipa-domain-netbios-name.yml
fc3955
new file mode 100644
fc3955
index 0000000..04e56b3
fc3955
--- /dev/null
fc3955
+++ b/playbooks/config/change-ipa-domain-netbios-name.yml
fc3955
@@ -0,0 +1,12 @@
fc3955
+---
fc3955
+- name: Playbook to change IPA domain netbios name
fc3955
+  hosts: ipaserver
fc3955
+  become: no
fc3955
+  gather_facts: no
fc3955
+
fc3955
+  tasks:
fc3955
+    - name: Set IPA domain netbios name
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        netbios_name: IPADOM
fc3955
diff --git a/playbooks/config/generate-users-groups-sids.yml b/playbooks/config/generate-users-groups-sids.yml
fc3955
new file mode 100644
fc3955
index 0000000..9df85ba
fc3955
--- /dev/null
fc3955
+++ b/playbooks/config/generate-users-groups-sids.yml
fc3955
@@ -0,0 +1,12 @@
fc3955
+---
fc3955
+- name: Playbook to ensure SIDs are enabled and users and groups have SIDs
fc3955
+  hosts: ipaserver
fc3955
+  become: no
fc3955
+  gather_facts: no
fc3955
+
fc3955
+  tasks:
fc3955
+    - name: Enable SID and generate users and groups SIDS
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        add_sids: yes
fc3955
diff --git a/plugins/modules/ipaconfig.py b/plugins/modules/ipaconfig.py
fc3955
index 6731e37..d1f1398 100644
fc3955
--- a/plugins/modules/ipaconfig.py
fc3955
+++ b/plugins/modules/ipaconfig.py
fc3955
@@ -148,6 +148,24 @@ options:
fc3955
         required: false
fc3955
         type: list
fc3955
         aliases: ["ipadomainresolutionorder"]
fc3955
+    enable_sid:
fc3955
+        description: >
fc3955
+          New users and groups automatically get a SID assigned.
fc3955
+          Requires IPA 4.9.8+.
fc3955
+        required: false
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
+        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
+        required: false
fc3955
+        type: bool
fc3955
 '''
fc3955
 
fc3955
 EXAMPLES = '''
fc3955
@@ -169,6 +187,24 @@ EXAMPLES = '''
fc3955
         ipaadmin_password: SomeADMINpassword
fc3955
         defaultshell: /bin/bash
fc3955
         maxusername: 64
fc3955
+
fc3955
+- name: Playbook to enable SID and generate users and groups SIDs
fc3955
+  hosts: ipaserver
fc3955
+  tasks:
fc3955
+    - name: Enable SID and generate users and groups SIDS
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        add_sids: yes
fc3955
+
fc3955
+- name: Playbook to change IPA domain netbios name
fc3955
+  hosts: ipaserver
fc3955
+  tasks:
fc3955
+    - name: Enable SID and generate users and groups SIDS
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        enable_sid: yes
fc3955
+        netbios_name: IPADOM
fc3955
 '''
fc3955
 
fc3955
 RETURN = '''
fc3955
@@ -247,6 +283,14 @@ config:
fc3955
     domain_resolution_order:
fc3955
         description: list of domains used for short name qualification
fc3955
         returned: always
fc3955
+    enable_sid:
fc3955
+        description: >
fc3955
+          new users and groups automatically get a SID assigned.
fc3955
+          Requires IPA 4.9.8+.
fc3955
+        returned: always
fc3955
+    netbios_name:
fc3955
+        description: NetBIOS name of the IPA domain. Requires IPA 4.9.8+.
fc3955
+        returned: if enable_sid is True
fc3955
 '''
fc3955
 
fc3955
 
fc3955
@@ -260,6 +304,28 @@ def config_show(module):
fc3955
     return _result["result"]
fc3955
 
fc3955
 
fc3955
+def get_netbios_name(module):
fc3955
+    try:
fc3955
+        _result = module.ipa_command_no_name("trustconfig_show", {"all": True})
fc3955
+    except Exception:  # pylint: disable=broad-except
fc3955
+        return None
fc3955
+    else:
fc3955
+        return _result["result"]["ipantflatname"][0]
fc3955
+
fc3955
+
fc3955
+def is_enable_sid(module):
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
+        return False
fc3955
+    _result = module.ipa_command("group_show", "admins", {"all": True})
fc3955
+    sid = _result["result"].get("ipantsecurityidentifier", [""])
fc3955
+    if not sid[0].endswith("-512"):
fc3955
+        return False
fc3955
+    return True
fc3955
+
fc3955
+
fc3955
 def main():
fc3955
     ansible_module = IPAAnsibleModule(
fc3955
         argument_spec=dict(
fc3955
@@ -313,7 +379,10 @@ def main():
fc3955
                                 aliases=["ipauserauthtype"]),
fc3955
             ca_renewal_master_server=dict(type="str", required=False),
fc3955
             domain_resolution_order=dict(type="list", required=False,
fc3955
-                                         aliases=["ipadomainresolutionorder"])
fc3955
+                                         aliases=["ipadomainresolutionorder"]),
fc3955
+            enable_sid=dict(type="bool", required=False),
fc3955
+            add_sids=dict(type="bool", required=False),
fc3955
+            netbios_name=dict(type="str", required=False),
fc3955
         ),
fc3955
         supports_check_mode=True,
fc3955
     )
fc3955
@@ -344,7 +413,10 @@ def main():
fc3955
         "pac_type": "ipakrbauthzdata",
fc3955
         "user_auth_type": "ipauserauthtype",
fc3955
         "ca_renewal_master_server": "ca_renewal_master_server",
fc3955
-        "domain_resolution_order": "ipadomainresolutionorder"
fc3955
+        "domain_resolution_order": "ipadomainresolutionorder",
fc3955
+        "enable_sid": "enable_sid",
fc3955
+        "netbios_name": "netbios_name",
fc3955
+        "add_sids": "add_sids",
fc3955
     }
fc3955
     allow_empty_string = ["pac_type", "user_auth_type", "configstring"]
fc3955
     reverse_field_map = {v: k for k, v in field_map.items()}
fc3955
@@ -394,11 +466,47 @@ def main():
fc3955
     changed = False
fc3955
     exit_args = {}
fc3955
 
fc3955
-    # Connect to IPA API
fc3955
-    with ansible_module.ipa_connect():
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
 
fc3955
         result = config_show(ansible_module)
fc3955
+
fc3955
         if params:
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
+            enable_sid = params.get("enable_sid")
fc3955
+            required_sid = any([netbios_name, add_sids])
fc3955
+            if required_sid and not enable_sid:
fc3955
+                ansible_module.fail_json(
fc3955
+                    "'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
+                        "This version of IPA does not support 'enable-sid'.")
fc3955
+                if (
fc3955
+                    netbios_name
fc3955
+                    and netbios_name == get_netbios_name(ansible_module)
fc3955
+                ):
fc3955
+                    del params["netbios_name"]
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 is_enable_sid(ansible_module)
fc3955
+                ):
fc3955
+                    del params["enable_sid"]
fc3955
+            else:
fc3955
+                for param in ["enable_sid", "netbios_name", "add_sids"]:
fc3955
+                    if param in params:
fc3955
+                        del params[params]
fc3955
+
fc3955
             params = {
fc3955
                 k: v for k, v in params.items()
fc3955
                 if k not in result or result[k] != v
fc3955
@@ -458,6 +566,10 @@ def main():
fc3955
             # Add empty domain_resolution_order if it is not set
fc3955
             if "domain_resolution_order" not in exit_args:
fc3955
                 exit_args["domain_resolution_order"] = []
fc3955
+            # Set enable_sid
fc3955
+            if has_enable_sid:
fc3955
+                exit_args["enable_sid"] = is_enable_sid(ansible_module)
fc3955
+                exit_args["netbios_name"] = get_netbios_name(ansible_module)
fc3955
 
fc3955
     # Done
fc3955
     ansible_module.exit_json(changed=changed, config=exit_args)
fc3955
#diff --git a/tests/azure/templates/variables_centos-7.yaml b/tests/azure/templates/variables_centos-7.yaml
fc3955
#index 586d5ec..8628af2 100644
fc3955
#--- a/tests/azure/templates/variables_centos-7.yaml
fc3955
#+++ b/tests/azure/templates/variables_centos-7.yaml
fc3955
#@@ -12,8 +12,7 @@
fc3955
# #
fc3955
# ---
fc3955
# variables:
fc3955
#-  empty: true
fc3955
#-#   ipa_enabled_modules: >-
fc3955
#-#   ipa_enabled_tests: >-
fc3955
#-#   ipa_disabled_modules: >-
fc3955
#-#   ipa_disabled_tests: >-
fc3955
#+  # ipa_enabled_modules: >-
fc3955
#+  # ipa_enabled_tests: >-
fc3955
#+  # ipa_disabled_modules: >-
fc3955
#+  ipa_disabled_tests: test_config_sid
fc3955
diff --git a/tests/config/test_config_sid.yml b/tests/config/test_config_sid.yml
fc3955
new file mode 100644
fc3955
index 0000000..1761795
fc3955
--- /dev/null
fc3955
+++ b/tests/config/test_config_sid.yml
fc3955
@@ -0,0 +1,70 @@
fc3955
+---
fc3955
+- name: Test config
fc3955
+  hosts: "{{ ipa_test_host | default('ipaserver') }}"
fc3955
+  become: no
fc3955
+  gather_facts: no
fc3955
+
fc3955
+  tasks:
fc3955
+
fc3955
+  # GET CURRENT CONFIG
fc3955
+
fc3955
+  - name: Return current values of the global configuration options
fc3955
+    ipaconfig:
fc3955
+      ipaadmin_password: SomeADMINpassword
fc3955
+      ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+    register: previous
fc3955
+
fc3955
+  # TESTS
fc3955
+  - block:
fc3955
+    - name: Ensure SID is enabled.
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        enable_sid: yes
fc3955
+      register: result
fc3955
+      failed_when: result.failed or previous.config.enable_sid == result.changed
fc3955
+
fc3955
+    - name: Ensure SID is enabled, again.
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        enable_sid: yes
fc3955
+      register: result
fc3955
+      failed_when: result.failed or result.changed
fc3955
+
fc3955
+    - name: Ensure netbios_name is "IPATESTPLAY"
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 not result.changed
fc3955
+
fc3955
+    - name: Ensure netbios_name is "IPATESTPLAY", again
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
+    # 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
+    # REVERT TO PREVIOUS CONFIG
fc3955
+    always:
fc3955
+    # Once SID is enabled, it cannot be reverted.
fc3955
+    - name: Revert netbios_name to original configuration
fc3955
+      ipaconfig:
fc3955
+        ipaadmin_password: SomeADMINpassword
fc3955
+        ipaapi_context: "{{ ipa_context | default(omit) }}"
fc3955
+        netbios_name: "{{ previous.config.netbios_name | default(omit) }}"
fc3955
+        enable_sid: yes
fc3955
-- 
fc3955
2.37.3
fc3955