示例#1
0
def get_agent_groups(group_list=None,
                     offset=0,
                     limit=None,
                     sort=None,
                     search=None,
                     hash_algorithm='md5'):
    """Gets the existing groups.

    :param group_list: List of Group names.
    :param offset: First item to return.
    :param limit: Maximum number of items to return.
    :param sort: Fields to sort the items by.
    :param search: Text to search.
    :param hash_algorithm: hash algorithm used to get mergedsum and configsum.
    :return: AffectedItemsWazuhResult.
    """
    affected_groups = list()
    result = AffectedItemsWazuhResult(
        all_msg='All selected groups information was returned',
        some_msg='Some groups information was not returned',
        none_msg='No group information was returned')
    if group_list:

        # Add failed items
        for invalid_group in set(group_list) - get_groups():
            result.add_failed_item(id_=invalid_group,
                                   error=WazuhResourceNotFound(1710))

        rbac_filters = get_rbac_filters(system_resources=get_groups(),
                                        permitted_resources=group_list)

        group_query = WazuhDBQueryGroup(offset=offset,
                                        limit=limit,
                                        sort=sort,
                                        search=search,
                                        **rbac_filters)
        query_data = group_query.run()

        for group in query_data['items']:
            full_entry = path.join(common.shared_path, group['name'])

            # merged.mg and agent.conf sum
            merged_sum = get_hash(path.join(full_entry, "merged.mg"),
                                  hash_algorithm)
            conf_sum = get_hash(path.join(full_entry, "agent.conf"),
                                hash_algorithm)

            if merged_sum:
                group['mergedSum'] = merged_sum

            if conf_sum:
                group['configSum'] = conf_sum
            affected_groups.append(group)

        result.affected_items = affected_groups
        result.total_affected_items = query_data['totalItems']

    return result
示例#2
0
文件: agent.py 项目: ignhub/wazuh
def get_group_files(group_list=None, offset=0, limit=None, search_text=None, search_in_fields=None,
                    complementary_search=False, sort_by=None, sort_ascending=True, hash_algorithm='md5'):
    """Gets the group files.

    :param group_list: List of Group names.
    :param offset: First item to return.
    :param limit: Maximum number of items to return.
    :param sort_by: Fields to sort the items by.
    :param sort_ascending: Sort in ascending (true) or descending (false) order.
    :param search_text: Text to search.
    :param complementary_search: Find items without the text to search.
    :param search_in_fields: Fields to search in.
    :param hash_algorithm: hash algorithm used to get mergedsum and configsum.
    :return: WazuhResult.
    """
    # We access unique group_id from list, this may change if and when we decide to add option to get files for
    # a list of groups
    group_id = group_list[0]
    group_path = common.shared_path
    result = AffectedItemsWazuhResult(all_msg='All selected groups files were returned',
                                      some_msg='Some groups files were not returned',
                                      none_msg='No groups files were returned'
                                      )
    if group_id:
        if not Agent.group_exists(group_id):
            result.add_failed_item(id_=group_id, error=WazuhResourceNotFound(1710))
            return result
        group_path = path.join(common.shared_path, group_id)

    if not path.exists(group_path):
        result.add_failed_item(id_=group_path, error=WazuhError(1006))
        return result

    try:
        data = []
        for entry in listdir(group_path):
            item = dict()
            item['filename'] = entry
            item['hash'] = get_hash(path.join(group_path, entry), hash_algorithm)
            data.append(item)

        # ar.conf
        ar_path = path.join(common.shared_path, 'ar.conf')
        data.append({'filename': "ar.conf", 'hash': get_hash(ar_path, hash_algorithm)})
        data = process_array(data, search_text=search_text, search_in_fields=search_in_fields,
                             complementary_search=complementary_search, sort_by=sort_by,
                             sort_ascending=sort_ascending, offset=offset, limit=limit)
        result.affected_items = data['items']
        result.total_affected_items = data['totalItems']
    except WazuhError as e:
        result.add_failed_item(id_=group_path, error=e)
        raise e
    except Exception as e:
        raise WazuhInternalError(1727, extra_message=str(e))

    return result