|
| 1 | +import logging |
| 2 | + |
| 3 | +from ibmsecurity.utilities import tools |
| 4 | + |
| 5 | +try: |
| 6 | + basestring |
| 7 | +except NameError: |
| 8 | + basestring = (str, bytes) |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +# URI for this module |
| 13 | +uri = "/iam/access/v8/audit/components" |
| 14 | +requires_modules = ["mga", "federation"] |
| 15 | +requires_version = None |
| 16 | + |
| 17 | + |
| 18 | +def get_all(isamAppliance, check_mode=False, force=False): |
| 19 | + """ |
| 20 | + Retrieve audit configuration |
| 21 | + """ |
| 22 | + return isamAppliance.invoke_get("Retrieve all audit component configuration", uri, requires_modules=requires_modules, |
| 23 | + requires_version=requires_version) |
| 24 | + |
| 25 | + |
| 26 | +def search(isamAppliance, component_name: str, check_mode=False, force=False): |
| 27 | + """ |
| 28 | + Get the id for the component by (group) name |
| 29 | + """ |
| 30 | + ret_obj = None |
| 31 | + |
| 32 | + ret_obj = get_all(isamAppliance) |
| 33 | + if ret_obj.get("data", None): |
| 34 | + for obj in ret_obj.get("data"): |
| 35 | + if obj['group'] == component_name: |
| 36 | + logger.info(f"Found name {component_name} id: {obj['id']}") |
| 37 | + return obj['id'] |
| 38 | + return None |
| 39 | + else: |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def get(isamAppliance, group_name: str = None, component_id: str = None, type_id: str = None, check_mode=False, force=False): |
| 44 | + """ |
| 45 | + Retrieve specific audit configuration component group by component_id, by type or by group name |
| 46 | + """ |
| 47 | + requires_version = None |
| 48 | + warnings = [] |
| 49 | + if component_id is None and type_id is None and group_name is None: |
| 50 | + warnings = ['No group_name, component_id nor type_id passed'] |
| 51 | + return isamAppliance.create_return_object(warnings) |
| 52 | + elif group_name: |
| 53 | + # translate name to component_id |
| 54 | + component_id = search(isamAppliance, component_name=group_name) |
| 55 | + if component_id: |
| 56 | + uri_part = component_id |
| 57 | + else: |
| 58 | + warnings = [f"Cannot find group by name of: {group_name}"] |
| 59 | + return isamAppliance.create_return_object(warnings) |
| 60 | + elif component_id: |
| 61 | + # Ignore type_id in this case |
| 62 | + uri_part = component_id |
| 63 | + else: |
| 64 | + # valid values are runtime or management |
| 65 | + if type_id in ['runtime', 'management']: |
| 66 | + requires_version = "10.0.7.0" |
| 67 | + uri_part = type_id |
| 68 | + else: |
| 69 | + warnings = [f"Invalid type_id passed to function: {type_id}"] |
| 70 | + return isamAppliance.create_return_object(warnings) |
| 71 | + return isamAppliance.invoke_get("Retrieve audit configuration component ", f"{uri}/{uri_part}", requires_modules=requires_modules, |
| 72 | + requires_version=requires_version, warnings=warnings) |
| 73 | + |
| 74 | + |
| 75 | +def set(isamAppliance, component_id: str, enabled=True, check_mode=False, force=False): |
| 76 | + """ |
| 77 | + Update Audit Configuration Component by id |
| 78 | + This simply enables or disables the group. |
| 79 | + TODO: Add set by type and set_all |
| 80 | + """ |
| 81 | + if isinstance(enabled, str): |
| 82 | + if enabled.upper() in ['TRUE', 'YES']: |
| 83 | + enabled = True |
| 84 | + else: |
| 85 | + enabled = False |
| 86 | + update_required = _check(isamAppliance, component_id, enabled) |
| 87 | + if enabled: |
| 88 | + json_data = { |
| 89 | + 'enabled': True |
| 90 | + } |
| 91 | + else: |
| 92 | + json_data = { |
| 93 | + 'enabled': False |
| 94 | + } |
| 95 | + if force or update_required: |
| 96 | + if check_mode: |
| 97 | + return isamAppliance.create_return_object(changed=True) |
| 98 | + else: |
| 99 | + return isamAppliance.invoke_put( |
| 100 | + "Update Audit Configuration Component", |
| 101 | + f"{uri}/{component_id}", |
| 102 | + json_data, |
| 103 | + requires_modules=requires_modules, |
| 104 | + requires_version=requires_version) |
| 105 | + |
| 106 | + |
| 107 | +def _check(isamAppliance, component_id: str, enabled: bool): |
| 108 | + """ |
| 109 | + Check and return True if update needed |
| 110 | + """ |
| 111 | + update_required = False |
| 112 | + |
| 113 | + ret_obj = get(isamAppliance, component_id=component_id) |
| 114 | + cmp_cfg = ret_obj.get("data", None) |
| 115 | + logger.debug(f"\n\n{cmp_cfg}\n\n") |
| 116 | + if cmp_cfg is not None and str(cmp_cfg.get('enabled', "frottekop")) != str(enabled): |
| 117 | + update_required = True |
| 118 | + logger.debug(f"\n\nAudit Configuration Component requires an update {cmp_cfg.get('enabled')} <> {enabled}") |
| 119 | + else: |
| 120 | + logger.warning("Audit Configuration Component does not need an update or does not exist.") |
| 121 | + |
| 122 | + return update_required |
| 123 | + |
| 124 | + |
| 125 | +def compare(isamAppliance1, isamAppliance2): |
| 126 | + """ |
| 127 | + Compare Audit Configuration Components between two appliances |
| 128 | + """ |
| 129 | + ret_obj1 = get_all(isamAppliance1) |
| 130 | + ret_obj2 = get_all(isamAppliance2) |
| 131 | + |
| 132 | + for obj in ret_obj1['data']: |
| 133 | + del obj['id'] |
| 134 | + for obj in ret_obj2['data']: |
| 135 | + del obj['id'] |
| 136 | + |
| 137 | + return tools.json_compare(ret_obj1, ret_obj2, deleted_keys=['id']) |
0 commit comments