Blame plugins/modules/ipasudorule.py

Packit Service 0a38ef
# -*- coding: utf-8 -*-
Packit Service 0a38ef
Packit Service 0a38ef
# Authors:
Packit Service 0a38ef
#   Rafael Guterres Jeffman <rjeffman@redhat.com>
Packit Service 0a38ef
#
Packit Service 0a38ef
# Copyright (C) 2019 Red Hat
Packit Service 0a38ef
# see file 'COPYING' for use and warranty information
Packit Service 0a38ef
#
Packit Service 0a38ef
# This program is free software; you can redistribute it and/or modify
Packit Service 0a38ef
# it under the terms of the GNU General Public License as published by
Packit Service 0a38ef
# the Free Software Foundation, either version 3 of the License, or
Packit Service 0a38ef
# (at your option) any later version.
Packit Service 0a38ef
#
Packit Service 0a38ef
# This program is distributed in the hope that it will be useful,
Packit Service 0a38ef
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 0a38ef
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 0a38ef
# GNU General Public License for more details.
Packit Service 0a38ef
#
Packit Service 0a38ef
# You should have received a copy of the GNU General Public License
Packit Service 0a38ef
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 0a38ef
Packit Service 0a38ef
ANSIBLE_METADATA = {
Packit Service 0a38ef
    "metadata_version": "1.0",
Packit Service 0a38ef
    "supported_by": "community",
Packit Service 0a38ef
    "status": ["preview"],
Packit Service 0a38ef
}
Packit Service 0a38ef
Packit Service 0a38ef
DOCUMENTATION = """
Packit Service 0a38ef
---
Packit Service 0a38ef
module: ipasudorule
Packit Service 0a38ef
short description: Manage FreeIPA sudo rules
Packit Service 0a38ef
description: Manage FreeIPA sudo rules
Packit Service 0a38ef
options:
Packit Service 0a38ef
  ipaadmin_principal:
Packit Service 0a38ef
    description: The admin principal
Packit Service 0a38ef
    default: admin
Packit Service 0a38ef
  ipaadmin_password:
Packit Service 0a38ef
    description: The admin password
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
  name:
Packit Service 0a38ef
    description: The sudorule name
Packit Service 0a38ef
    required: true
Packit Service 0a38ef
    aliases: ["cn"]
Packit Service 0a38ef
  description:
Packit Service 0a38ef
    description: The sudorule description
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
  user:
Packit Service 0a38ef
    description: List of users assigned to the sudo rule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
  usercategory:
Packit Service 0a38ef
    description: User category the sudo rule applies to
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    choices: ["all", ""]
Packit Service 0a38ef
    aliases: ["usercat"]
Packit Service 0a38ef
  usergroup:
Packit Service 0a38ef
    description: List of user groups assigned to the sudo rule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
  runasgroupcategory:
Packit Service 0a38ef
    description: RunAs Group category applied to the sudo rule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    choices: ["all", ""]
Packit Service 0a38ef
    aliases: ["runasgroupcat"]
Packit Service 0a38ef
  runasusercategory:
Packit Service 0a38ef
    description: RunAs User category applied to the sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    choices: ["all", ""]
Packit Service 0a38ef
    aliases: ["runasusercat"]
Packit Service 0a38ef
  nomembers:
Packit Service 0a38ef
    description: Suppress processing of membership attributes
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: bool
Packit Service 0a38ef
  host:
Packit Service 0a38ef
    description: List of host names assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  hostgroup:
Packit Service 0a38ef
    description: List of host groups assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  hostcategory:
Packit Service 0a38ef
    description: Host category the sudo rule applies to.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    choices: ["all", ""]
Packit Service 0a38ef
    aliases: ["hostcat"]
Packit Service 0a38ef
  allow_sudocmd:
Packit Service 0a38ef
    description: List of allowed sudocmds assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  allow_sudocmdgroup:
Packit Service 0a38ef
    description: List of allowed sudocmd groups assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  deny_sudocmd:
Packit Service 0a38ef
    description: List of denied sudocmds assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  deny_sudocmdgroup:
Packit Service 0a38ef
    description: List of denied sudocmd groups assigned to this sudorule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  cmdcategory:
Packit Service 0a38ef
    description: Command category the sudo rule applies to
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    choices: ["all", ""]
Packit Service 0a38ef
    aliases: ["cmdcat"]
Packit Service 0a38ef
  order:
Packit Service 0a38ef
    description: Order to apply this rule.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: int
Packit Service 0a38ef
  sudooption:
Packit Service a166ed
    description: List of sudo options.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
    aliases: ["options"]
Packit Service 0a38ef
  runasuser:
Packit Service 0a38ef
    description: List of users for Sudo to execute as.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  runasgroup:
Packit Service 0a38ef
    description: List of groups for Sudo to execute as.
Packit Service 0a38ef
    required: false
Packit Service 0a38ef
    type: list
Packit Service 0a38ef
  action:
Packit Service 0a38ef
    description: Work on sudorule or member level
Packit Service 0a38ef
    default: sudorule
Packit Service 0a38ef
    choices: ["member", "sudorule"]
Packit Service 0a38ef
  state:
Packit Service 0a38ef
    description: State to ensure
Packit Service 0a38ef
    default: present
Packit Service 0a38ef
    choices: ["present", "absent", "enabled", "disabled"]
Packit Service 0a38ef
author:
Packit Service 0a38ef
    - Rafael Jeffman
Packit Service 0a38ef
"""
Packit Service 0a38ef
Packit Service 0a38ef
EXAMPLES = """
Packit Service 0a38ef
# Ensure Sudo Rule tesrule1 is present
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: testrule1
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure sudocmd is present in Sudo Rule
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: pass1234
Packit Service 0a38ef
    name: testrule1
Packit Service 0a38ef
    allow_sudocmd:
Packit Service 0a38ef
      - /sbin/ifconfig
Packit Service 0a38ef
      - /usr/bin/vim
Packit Service 0a38ef
    action: member
Packit Service 0a38ef
    state: absent
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure host server is present in Sudo Rule
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: testrule1
Packit Service 0a38ef
    host: server
Packit Service 0a38ef
    action: member
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure hostgroup cluster is present in Sudo Rule
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: testrule1
Packit Service 0a38ef
    hostgroup: cluster
Packit Service 0a38ef
    action: member
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure sudo rule for usercategory "all"
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: allusers
Packit Service 0a38ef
    usercategory: all
Packit Service 0a38ef
    action: enabled
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure sudo rule for hostcategory "all"
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: allhosts
Packit Service 0a38ef
    hostcategory: all
Packit Service 0a38ef
    action: enabled
Packit Service 0a38ef
Packit Service 0a38ef
# Ensure Sudo Rule tesrule1 is absent
Packit Service 0a38ef
- ipasudorule:
Packit Service 0a38ef
    ipaadmin_password: SomeADMINpassword
Packit Service 0a38ef
    name: testrule1
Packit Service 0a38ef
    state: absent
Packit Service 0a38ef
"""
Packit Service 0a38ef
Packit Service 0a38ef
RETURN = """
Packit Service 0a38ef
"""
Packit Service 0a38ef
Packit Service 0a38ef
from ansible.module_utils.basic import AnsibleModule
Packit Service 0a38ef
from ansible.module_utils.ansible_freeipa_module import temp_kinit, \
Packit Service 0a38ef
    temp_kdestroy, valid_creds, api_connect, api_command, compare_args_ipa, \
Packit Service 0a38ef
    module_params_get, gen_add_del_lists
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
def find_sudorule(module, name):
Packit Service 0a38ef
    _args = {
Packit Service 0a38ef
        "all": True,
Packit Service 0a38ef
        "cn": name,
Packit Service 0a38ef
    }
Packit Service 0a38ef
Packit Service 0a38ef
    _result = api_command(module, "sudorule_find", name, _args)
Packit Service 0a38ef
Packit Service 0a38ef
    if len(_result["result"]) > 1:
Packit Service 0a38ef
        module.fail_json(
Packit Service 0a38ef
            msg="There is more than one sudorule '%s'" % (name))
Packit Service 0a38ef
    elif len(_result["result"]) == 1:
Packit Service 0a38ef
        return _result["result"][0]
Packit Service 0a38ef
    else:
Packit Service 0a38ef
        return None
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
def gen_args(description, usercat, hostcat, cmdcat, runasusercat,
Packit Service 0a38ef
             runasgroupcat, order, nomembers):
Packit Service 0a38ef
    _args = {}
Packit Service 0a38ef
Packit Service 0a38ef
    if description is not None:
Packit Service 0a38ef
        _args['description'] = description
Packit Service 0a38ef
    if usercat is not None:
Packit Service 0a38ef
        _args['usercategory'] = usercat
Packit Service 0a38ef
    if hostcat is not None:
Packit Service 0a38ef
        _args['hostcategory'] = hostcat
Packit Service 0a38ef
    if cmdcat is not None:
Packit Service 0a38ef
        _args['cmdcategory'] = cmdcat
Packit Service 0a38ef
    if runasusercat is not None:
Packit Service 0a38ef
        _args['ipasudorunasusercategory'] = runasusercat
Packit Service 0a38ef
    if runasgroupcat is not None:
Packit Service 0a38ef
        _args['ipasudorunasgroupcategory'] = runasgroupcat
Packit Service 0a38ef
    if order is not None:
Packit Service 0a38ef
        _args['sudoorder'] = order
Packit Service 0a38ef
    if nomembers is not None:
Packit Service 0a38ef
        _args['nomembers'] = nomembers
Packit Service 0a38ef
Packit Service 0a38ef
    return _args
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
def main():
Packit Service 0a38ef
    ansible_module = AnsibleModule(
Packit Service 0a38ef
        argument_spec=dict(
Packit Service 0a38ef
            # general
Packit Service 0a38ef
            ipaadmin_principal=dict(type="str", default="admin"),
Packit Service 0a38ef
            ipaadmin_password=dict(type="str", required=False, no_log=True),
Packit Service 0a38ef
Packit Service 0a38ef
            name=dict(type="list", aliases=["cn"], default=None,
Packit Service 0a38ef
                      required=True),
Packit Service 0a38ef
            # present
Packit Service 0a38ef
            description=dict(required=False, type="str", default=None),
Packit Service 0a38ef
            usercategory=dict(required=False, type="str", default=None,
Packit Service 0a38ef
                              choices=["all", ""], aliases=['usercat']),
Packit Service 0a38ef
            hostcategory=dict(required=False, type="str", default=None,
Packit Service 0a38ef
                              choices=["all", ""], aliases=['hostcat']),
Packit Service 0a38ef
            nomembers=dict(required=False, type='bool', default=None),
Packit Service 0a38ef
            host=dict(required=False, type='list', default=None),
Packit Service 0a38ef
            hostgroup=dict(required=False, type='list', default=None),
Packit Service 0a38ef
            user=dict(required=False, type='list', default=None),
Packit Service 0a38ef
            group=dict(required=False, type='list', default=None),
Packit Service 0a38ef
            allow_sudocmd=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            deny_sudocmd=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            allow_sudocmdgroup=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            deny_sudocmdgroup=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            cmdcategory=dict(required=False, type="str", default=None,
Packit Service 0a38ef
                             choices=["all", ""], aliases=['cmdcat']),
Packit Service 0a38ef
            runasusercategory=dict(required=False, type="str", default=None,
Packit Service 0a38ef
                                   choices=["all", ""],
Packit Service 0a38ef
                                   aliases=['runasusercat']),
Packit Service 0a38ef
            runasgroupcategory=dict(required=False, type="str", default=None,
Packit Service 0a38ef
                                    choices=["all", ""],
Packit Service 0a38ef
                                    aliases=['runasgroupcat']),
Packit Service 0a38ef
            runasuser=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            runasgroup=dict(required=False, type="list", default=None),
Packit Service 0a38ef
            order=dict(type="int", required=False, aliases=['sudoorder']),
Packit Service 0a38ef
            sudooption=dict(required=False, type='list', default=None,
Packit Service 0a38ef
                            aliases=["options"]),
Packit Service 0a38ef
            action=dict(type="str", default="sudorule",
Packit Service 0a38ef
                        choices=["member", "sudorule"]),
Packit Service 0a38ef
            # state
Packit Service 0a38ef
            state=dict(type="str", default="present",
Packit Service 0a38ef
                       choices=["present", "absent",
Packit Service 0a38ef
                                "enabled", "disabled"]),
Packit Service 0a38ef
        ),
Packit Service 0a38ef
        supports_check_mode=True,
Packit Service 0a38ef
    )
Packit Service 0a38ef
Packit Service 0a38ef
    ansible_module._ansible_debug = True
Packit Service 0a38ef
Packit Service 0a38ef
    # Get parameters
Packit Service 0a38ef
Packit Service 0a38ef
    # general
Packit Service 0a38ef
    ipaadmin_principal = module_params_get(ansible_module,
Packit Service 0a38ef
                                           "ipaadmin_principal")
Packit Service 0a38ef
    ipaadmin_password = module_params_get(ansible_module, "ipaadmin_password")
Packit Service 0a38ef
    names = module_params_get(ansible_module, "name")
Packit Service 0a38ef
Packit Service 0a38ef
    # present
Packit Service 0a38ef
    # The 'noqa' variables are not used here, but required for vars().
Packit Service 0a38ef
    # The use of 'noqa' ensures flake8 does not complain about them.
Packit Service 0a38ef
    description = module_params_get(ansible_module, "description")  # noqa
Packit Service 0a38ef
    cmdcategory = module_params_get(ansible_module, 'cmdcategory')  # noqa
Packit Service 0a38ef
    usercategory = module_params_get(ansible_module, "usercategory")  # noqa
Packit Service 0a38ef
    hostcategory = module_params_get(ansible_module, "hostcategory")  # noqa
Packit Service 0a38ef
    runasusercategory = module_params_get(ansible_module,           # noqa
Packit Service 0a38ef
                                          "runasusercategory")
Packit Service 0a38ef
    runasgroupcategory = module_params_get(ansible_module,          # noqa
Packit Service 0a38ef
                                           "runasgroupcategory")
Packit Service 0a38ef
    hostcategory = module_params_get(ansible_module, "hostcategory")  # noqa
Packit Service 0a38ef
    nomembers = module_params_get(ansible_module, "nomembers")  # noqa
Packit Service 0a38ef
    host = module_params_get(ansible_module, "host")
Packit Service 0a38ef
    hostgroup = module_params_get(ansible_module, "hostgroup")
Packit Service 0a38ef
    user = module_params_get(ansible_module, "user")
Packit Service 0a38ef
    group = module_params_get(ansible_module, "group")
Packit Service 0a38ef
    allow_sudocmd = module_params_get(ansible_module, 'allow_sudocmd')
Packit Service 0a38ef
    allow_sudocmdgroup = module_params_get(ansible_module,
Packit Service 0a38ef
                                           'allow_sudocmdgroup')
Packit Service 0a38ef
    deny_sudocmd = module_params_get(ansible_module, 'deny_sudocmd')
Packit Service 0a38ef
    deny_sudocmdgroup = module_params_get(ansible_module,
Packit Service 0a38ef
                                          'deny_sudocmdgroup')
Packit Service 0a38ef
    sudooption = module_params_get(ansible_module, "sudooption")
Packit Service 0a38ef
    order = module_params_get(ansible_module, "order")
Packit Service 0a38ef
    runasuser = module_params_get(ansible_module, "runasuser")
Packit Service 0a38ef
    runasgroup = module_params_get(ansible_module, "runasgroup")
Packit Service 0a38ef
    action = module_params_get(ansible_module, "action")
Packit Service 0a38ef
Packit Service 0a38ef
    # state
Packit Service 0a38ef
    state = module_params_get(ansible_module, "state")
Packit Service 0a38ef
Packit Service 0a38ef
    # Check parameters
Packit Service 0a38ef
Packit Service 0a38ef
    if state == "present":
Packit Service 0a38ef
        if len(names) != 1:
Packit Service 0a38ef
            ansible_module.fail_json(
Packit Service 0a38ef
                msg="Only one sudorule can be added at a time.")
Packit Service 0a38ef
        if action == "member":
Packit Service 0a38ef
            invalid = ["description", "usercategory", "hostcategory",
Packit Service 0a38ef
                       "cmdcategory", "runasusercategory",
Packit Service 0a38ef
                       "runasgroupcategory", "order", "nomembers"]
Packit Service 0a38ef
Packit Service 0a38ef
            for arg in invalid:
Packit Service 0a38ef
                if arg in vars() and vars()[arg] is not None:
Packit Service 0a38ef
                    ansible_module.fail_json(
Packit Service 0a38ef
                        msg="Argument '%s' can not be used with action "
Packit Service 0a38ef
                        "'%s'" % (arg, action))
Packit Service 0a38ef
        else:
Packit Service 0a38ef
            if hostcategory == 'all' and any([host, hostgroup]):
Packit Service 0a38ef
                ansible_module.fail_json(
Packit Service 0a38ef
                    msg="Hosts cannot be added when host category='all'")
Packit Service 0a38ef
            if usercategory == 'all' and any([user, group]):
Packit Service 0a38ef
                ansible_module.fail_json(
Packit Service 0a38ef
                    msg="Users cannot be added when user category='all'")
Packit Service 0a38ef
            if cmdcategory == 'all' \
Packit Service 0a38ef
               and any([allow_sudocmd, allow_sudocmdgroup]):
Packit Service 0a38ef
                ansible_module.fail_json(
Packit Service 0a38ef
                    msg="Commands cannot be added when command category='all'")
Packit Service 0a38ef
Packit Service 0a38ef
    elif state == "absent":
Packit Service 0a38ef
        if len(names) < 1:
Packit Service 0a38ef
            ansible_module.fail_json(msg="No name given.")
Packit Service 0a38ef
        invalid = ["description", "usercategory", "hostcategory",
Packit Service 0a38ef
                   "cmdcategory", "runasusercategory",
Packit Service 0a38ef
                   "runasgroupcategory", "nomembers", "order"]
Packit Service 0a38ef
        if action == "sudorule":
Packit Service 0a38ef
            invalid.extend(["host", "hostgroup", "user", "group",
Packit Service 0a38ef
                            "runasuser", "runasgroup", "allow_sudocmd",
Packit Service 0a38ef
                            "allow_sudocmdgroup", "deny_sudocmd",
Packit Service 0a38ef
                            "deny_sudocmdgroup", "sudooption"])
Packit Service 0a38ef
        for arg in invalid:
Packit Service 0a38ef
            if vars()[arg] is not None:
Packit Service 0a38ef
                ansible_module.fail_json(
Packit Service 0a38ef
                    msg="Argument '%s' can not be used with state '%s'" %
Packit Service 0a38ef
                    (arg, state))
Packit Service 0a38ef
Packit Service 0a38ef
    elif state in ["enabled", "disabled"]:
Packit Service 0a38ef
        if len(names) < 1:
Packit Service 0a38ef
            ansible_module.fail_json(msg="No name given.")
Packit Service 0a38ef
        if action == "member":
Packit Service 0a38ef
            ansible_module.fail_json(
Packit Service 0a38ef
                msg="Action member can not be used with states enabled and "
Packit Service 0a38ef
                "disabled")
Packit Service 0a38ef
        invalid = ["description", "usercategory", "hostcategory",
Packit Service 0a38ef
                   "cmdcategory", "runasusercategory", "runasgroupcategory",
Packit Service 0a38ef
                   "nomembers", "nomembers", "host", "hostgroup",
Packit Service 0a38ef
                   "user", "group", "allow_sudocmd", "allow_sudocmdgroup",
Packit Service 0a38ef
                   "deny_sudocmd", "deny_sudocmdgroup", "runasuser",
Packit Service 0a38ef
                   "runasgroup", "order", "sudooption"]
Packit Service 0a38ef
        for arg in invalid:
Packit Service 0a38ef
            if vars()[arg] is not None:
Packit Service 0a38ef
                ansible_module.fail_json(
Packit Service 0a38ef
                    msg="Argument '%s' can not be used with state '%s'" %
Packit Service 0a38ef
                    (arg, state))
Packit Service 0a38ef
    else:
Packit Service 0a38ef
        ansible_module.fail_json(msg="Invalid state '%s'" % state)
Packit Service 0a38ef
Packit Service 0a38ef
    # Init
Packit Service 0a38ef
Packit Service 0a38ef
    changed = False
Packit Service 0a38ef
    exit_args = {}
Packit Service 0a38ef
    ccache_dir = None
Packit Service 0a38ef
    ccache_name = None
Packit Service 0a38ef
    try:
Packit Service 0a38ef
        if not valid_creds(ansible_module, ipaadmin_principal):
Packit Service 0a38ef
            ccache_dir, ccache_name = temp_kinit(ipaadmin_principal,
Packit Service 0a38ef
                                                 ipaadmin_password)
Packit Service 0a38ef
        api_connect()
Packit Service 0a38ef
Packit Service 0a38ef
        commands = []
Packit Service 0a38ef
Packit Service 0a38ef
        for name in names:
Packit Service 0a38ef
            # Make sure sudorule exists
Packit Service 0a38ef
            res_find = find_sudorule(ansible_module, name)
Packit Service 0a38ef
Packit Service 0a38ef
            # Create command
Packit Service 0a38ef
            if state == "present":
Packit Service 0a38ef
                # Generate args
Packit Service 0a38ef
                args = gen_args(description, usercategory, hostcategory,
Packit Service 0a38ef
                                cmdcategory, runasusercategory,
Packit Service 0a38ef
                                runasgroupcategory, order, nomembers)
Packit Service 0a38ef
                if action == "sudorule":
Packit Service 0a38ef
                    # Found the sudorule
Packit Service 0a38ef
                    if res_find is not None:
Packit Service 0a38ef
                        # For all settings is args, check if there are
Packit Service 0a38ef
                        # different settings in the find result.
Packit Service 0a38ef
                        # If yes: modify
Packit Service 0a38ef
                        if not compare_args_ipa(ansible_module, args,
Packit Service 0a38ef
                                                res_find):
Packit Service 0a38ef
                            commands.append([name, "sudorule_mod", args])
Packit Service 0a38ef
                    else:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add", args])
Packit Service 0a38ef
                        # Set res_find to empty dict for next step
Packit Service 0a38ef
                        res_find = {}
Packit Service 0a38ef
Packit Service 0a38ef
                    # Generate addition and removal lists
Packit Service 0a38ef
                    host_add, host_del = gen_add_del_lists(
Packit Service 0a38ef
                        host, res_find.get('member_host', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    hostgroup_add, hostgroup_del = gen_add_del_lists(
Packit Service 0a38ef
                        hostgroup, res_find.get('member_hostgroup', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    user_add, user_del = gen_add_del_lists(
Packit Service 0a38ef
                        user, res_find.get('member_user', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    group_add, group_del = gen_add_del_lists(
Packit Service 0a38ef
                        group, res_find.get('member_group', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    allow_cmd_add, allow_cmd_del = gen_add_del_lists(
Packit Service 0a38ef
                        allow_sudocmd,
Packit Service 0a38ef
                        res_find.get('memberallowcmd_sudocmd', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    allow_cmdgroup_add, allow_cmdgroup_del = gen_add_del_lists(
Packit Service 0a38ef
                        allow_sudocmdgroup,
Packit Service 0a38ef
                        res_find.get('memberallowcmd_sudocmdgroup', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    deny_cmd_add, deny_cmd_del = gen_add_del_lists(
Packit Service 0a38ef
                        deny_sudocmd,
Packit Service 0a38ef
                        res_find.get('memberdenycmd_sudocmd', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    deny_cmdgroup_add, deny_cmdgroup_del = gen_add_del_lists(
Packit Service 0a38ef
                        deny_sudocmdgroup,
Packit Service 0a38ef
                        res_find.get('memberdenycmd_sudocmdgroup', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    sudooption_add, sudooption_del = gen_add_del_lists(
Packit Service 0a38ef
                        sudooption, res_find.get('ipasudoopt', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    runasuser_add, runasuser_del = gen_add_del_lists(
Packit Service 0a38ef
                        runasuser, res_find.get('ipasudorunas_user', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    runasgroup_add, runasgroup_del = gen_add_del_lists(
Packit Service 0a38ef
                        runasgroup, res_find.get('ipasudorunas_group', []))
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add hosts and hostgroups
Packit Service 0a38ef
                    if len(host_add) > 0 or len(hostgroup_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_host",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "host": host_add,
Packit Service 0a38ef
                                             "hostgroup": hostgroup_add,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
                    # Remove hosts and hostgroups
Packit Service 0a38ef
                    if len(host_del) > 0 or len(hostgroup_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_host",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "host": host_del,
Packit Service 0a38ef
                                             "hostgroup": hostgroup_del,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add users and groups
Packit Service 0a38ef
                    if len(user_add) > 0 or len(group_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_user",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "user": user_add,
Packit Service 0a38ef
                                             "group": group_add,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
                    # Remove users and groups
Packit Service 0a38ef
                    if len(user_del) > 0 or len(group_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_user",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "user": user_del,
Packit Service 0a38ef
                                             "group": group_del,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add commands allowed
Packit Service 0a38ef
                    if len(allow_cmd_add) > 0 or len(allow_cmdgroup_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_allow_command",
Packit Service 0a38ef
                                         {"sudocmd": allow_cmd_add,
Packit Service 0a38ef
                                          "sudocmdgroup": allow_cmdgroup_add,
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    if len(allow_cmd_del) > 0 or len(allow_cmdgroup_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_allow_command",
Packit Service 0a38ef
                                         {"sudocmd": allow_cmd_del,
Packit Service 0a38ef
                                          "sudocmdgroup": allow_cmdgroup_del
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add commands denied
Packit Service 0a38ef
                    if len(deny_cmd_add) > 0 or len(deny_cmdgroup_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_deny_command",
Packit Service 0a38ef
                                         {"sudocmd": deny_cmd_add,
Packit Service 0a38ef
                                          "sudocmdgroup": deny_cmdgroup_add,
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    if len(deny_cmd_del) > 0 or len(deny_cmdgroup_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_deny_command",
Packit Service 0a38ef
                                         {"sudocmd": deny_cmd_del,
Packit Service 0a38ef
                                          "sudocmdgroup": deny_cmdgroup_del
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add RunAS Users
Packit Service 0a38ef
                    if len(runasuser_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_runasuser",
Packit Service 0a38ef
                                         {"user": runasuser_add}])
Packit Service 0a38ef
                    # Remove RunAS Users
Packit Service 0a38ef
                    if len(runasuser_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_runasuser",
Packit Service 0a38ef
                                         {"user": runasuser_del}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add RunAS Groups
Packit Service 0a38ef
                    if len(runasgroup_add) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_runasgroup",
Packit Service 0a38ef
                                         {"group": runasgroup_add}])
Packit Service 0a38ef
                    # Remove RunAS Groups
Packit Service 0a38ef
                    if len(runasgroup_del) > 0:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_runasgroup",
Packit Service 0a38ef
                                         {"group": runasgroup_del}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add sudo options
Packit Service 0a38ef
                    for sudoopt in sudooption_add:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_option",
Packit Service 0a38ef
                                         {"ipasudoopt": sudoopt}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove sudo options
Packit Service 0a38ef
                    for sudoopt in sudooption_del:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_option",
Packit Service 0a38ef
                                         {"ipasudoopt": sudoopt}])
Packit Service 0a38ef
Packit Service 0a38ef
                elif action == "member":
Packit Service 0a38ef
                    if res_find is None:
Packit Service 0a38ef
                        ansible_module.fail_json(msg="No sudorule '%s'" % name)
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add hosts and hostgroups
Packit Service 0a38ef
                    if host is not None or hostgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_host",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "host": host,
Packit Service 0a38ef
                                             "hostgroup": hostgroup,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add users and groups
Packit Service 0a38ef
                    if user is not None or group is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_user",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "user": user,
Packit Service 0a38ef
                                             "group": group,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add commands
Packit Service 0a38ef
                    if allow_sudocmd is not None \
Packit Service 0a38ef
                       or allow_sudocmdgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_allow_command",
Packit Service 0a38ef
                                         {"sudocmd": allow_sudocmd,
Packit Service 0a38ef
                                          "sudocmdgroup": allow_sudocmdgroup,
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add commands
Packit Service 0a38ef
                    if deny_sudocmd is not None \
Packit Service 0a38ef
                       or deny_sudocmdgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_deny_command",
Packit Service 0a38ef
                                         {"sudocmd": deny_sudocmd,
Packit Service 0a38ef
                                          "sudocmdgroup": deny_sudocmdgroup,
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add RunAS Users
Packit Service 0a38ef
                    if runasuser is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_runasuser",
Packit Service 0a38ef
                                         {"user": runasuser}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add RunAS Groups
Packit Service 0a38ef
                    if runasgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_add_runasgroup",
Packit Service 0a38ef
                                         {"group": runasgroup}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Add options
Packit Service 0a38ef
                    if sudooption is not None:
Packit Service 0a38ef
                        existing_opts = res_find.get('ipasudoopt', [])
Packit Service 0a38ef
                        for sudoopt in sudooption:
Packit Service 0a38ef
                            if sudoopt not in existing_opts:
Packit Service 0a38ef
                                commands.append([name, "sudorule_add_option",
Packit Service 0a38ef
                                                 {"ipasudoopt": sudoopt}])
Packit Service 0a38ef
Packit Service 0a38ef
            elif state == "absent":
Packit Service 0a38ef
                if action == "sudorule":
Packit Service 0a38ef
                    if res_find is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_del", {}])
Packit Service 0a38ef
Packit Service 0a38ef
                elif action == "member":
Packit Service 0a38ef
                    if res_find is None:
Packit Service 0a38ef
                        ansible_module.fail_json(msg="No sudorule '%s'" % name)
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove hosts and hostgroups
Packit Service 0a38ef
                    if host is not None or hostgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_host",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "host": host,
Packit Service 0a38ef
                                             "hostgroup": hostgroup,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove users and groups
Packit Service 0a38ef
                    if user is not None or group is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_user",
Packit Service 0a38ef
                                         {
Packit Service 0a38ef
                                             "user": user,
Packit Service 0a38ef
                                             "group": group,
Packit Service 0a38ef
                                         }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove allow commands
Packit Service 0a38ef
                    if allow_sudocmd is not None \
Packit Service 0a38ef
                       or allow_sudocmdgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_allow_command",
Packit Service 0a38ef
                                         {"sudocmd": allow_sudocmd,
Packit Service 0a38ef
                                          "sudocmdgroup": allow_sudocmdgroup
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove deny commands
Packit Service 0a38ef
                    if deny_sudocmd is not None \
Packit Service 0a38ef
                       or deny_sudocmdgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_deny_command",
Packit Service 0a38ef
                                         {"sudocmd": deny_sudocmd,
Packit Service 0a38ef
                                          "sudocmdgroup": deny_sudocmdgroup
Packit Service 0a38ef
                                          }])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove RunAS Users
Packit Service 0a38ef
                    if runasuser is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_runasuser",
Packit Service 0a38ef
                                         {"user": runasuser}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove RunAS Groups
Packit Service 0a38ef
                    if runasgroup is not None:
Packit Service 0a38ef
                        commands.append([name, "sudorule_remove_runasgroup",
Packit Service 0a38ef
                                         {"group": runasgroup}])
Packit Service 0a38ef
Packit Service 0a38ef
                    # Remove options
Packit Service 0a38ef
                    if sudooption is not None:
Packit Service 0a38ef
                        existing_opts = res_find.get('ipasudoopt', [])
Packit Service 0a38ef
                        for sudoopt in sudooption:
Packit Service 0a38ef
                            if sudoopt in existing_opts:
Packit Service 0a38ef
                                commands.append([name,
Packit Service 0a38ef
                                                 "sudorule_remove_option",
Packit Service 0a38ef
                                                 {"ipasudoopt": sudoopt}])
Packit Service 0a38ef
Packit Service 0a38ef
            elif state == "enabled":
Packit Service 0a38ef
                if res_find is None:
Packit Service 0a38ef
                    ansible_module.fail_json(msg="No sudorule '%s'" % name)
Packit Service 0a38ef
                # sudorule_enable is not failing on an enabled sudorule
Packit Service 0a38ef
                # Therefore it is needed to have a look at the ipaenabledflag
Packit Service 0a38ef
                # in res_find.
Packit Service 0a38ef
                if "ipaenabledflag" not in res_find or \
Packit Service 0a38ef
                   res_find["ipaenabledflag"][0] != "TRUE":
Packit Service 0a38ef
                    commands.append([name, "sudorule_enable", {}])
Packit Service 0a38ef
Packit Service 0a38ef
            elif state == "disabled":
Packit Service 0a38ef
                if res_find is None:
Packit Service 0a38ef
                    ansible_module.fail_json(msg="No sudorule '%s'" % name)
Packit Service 0a38ef
                # sudorule_disable is not failing on an disabled sudorule
Packit Service 0a38ef
                # Therefore it is needed to have a look at the ipaenabledflag
Packit Service 0a38ef
                # in res_find.
Packit Service 0a38ef
                if "ipaenabledflag" not in res_find or \
Packit Service 0a38ef
                   res_find["ipaenabledflag"][0] != "FALSE":
Packit Service 0a38ef
                    commands.append([name, "sudorule_disable", {}])
Packit Service 0a38ef
Packit Service 0a38ef
            else:
Packit Service 0a38ef
                ansible_module.fail_json(msg="Unkown state '%s'" % state)
Packit Service 0a38ef
Packit Service 0a38ef
        # Execute commands
Packit Service 0a38ef
Packit Service 0a38ef
        errors = []
Packit Service 0a38ef
        for name, command, args in commands:
Packit Service 0a38ef
            try:
Packit Service 0a38ef
                result = api_command(ansible_module, command, name,
Packit Service 0a38ef
                                     args)
Packit Service 0a38ef
Packit Service 0a38ef
                if "completed" in result:
Packit Service 0a38ef
                    if result["completed"] > 0:
Packit Service 0a38ef
                        changed = True
Packit Service 0a38ef
                else:
Packit Service 0a38ef
                    changed = True
Packit Service 0a38ef
            except Exception as ex:
Packit Service 0a38ef
                ansible_module.fail_json(msg="%s: %s: %s" % (command, name,
Packit Service 0a38ef
                                                             str(ex)))
Packit Service 0a38ef
            # Get all errors
Packit Service 0a38ef
            # All "already a member" and "not a member" failures in the
Packit Service 0a38ef
            # result are ignored. All others are reported.
Packit Service 0a38ef
            if "failed" in result and len(result["failed"]) > 0:
Packit Service 0a38ef
                for item in result["failed"]:
Packit Service 0a38ef
                    failed_item = result["failed"][item]
Packit Service 0a38ef
                    for member_type in failed_item:
Packit Service 0a38ef
                        for member, failure in failed_item[member_type]:
Packit Service 0a38ef
                            if "already a member" in failure \
Packit Service 0a38ef
                               or "not a member" in failure:
Packit Service 0a38ef
                                continue
Packit Service 0a38ef
                            errors.append("%s: %s %s: %s" % (
Packit Service 0a38ef
                                command, member_type, member, failure))
Packit Service 0a38ef
        if len(errors) > 0:
Packit Service 0a38ef
            ansible_module.fail_json(msg=", ".join(errors))
Packit Service 0a38ef
Packit Service 0a38ef
    except Exception as ex:
Packit Service 0a38ef
        ansible_module.fail_json(msg=str(ex))
Packit Service 0a38ef
Packit Service 0a38ef
    finally:
Packit Service 0a38ef
        temp_kdestroy(ccache_dir, ccache_name)
Packit Service 0a38ef
Packit Service 0a38ef
    # Done
Packit Service 0a38ef
Packit Service 0a38ef
    ansible_module.exit_json(changed=changed, **exit_args)
Packit Service 0a38ef
Packit Service 0a38ef
Packit Service 0a38ef
if __name__ == "__main__":
Packit Service 0a38ef
    main()