{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Requester\n", "\n", "This presentations goal is it to introduce the features of the `Requester` and how to configure it." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### The challenge\n", "\n", "* I want to trigger external systems with or without field values from the currently processed event in the requests payload\n", "* I want to enrich events by external systems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "from this:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "document = {\n", " 'message': {\n", " \"hostname\": \"H34222S3\"\n", " }\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "to this:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "expected = {\n", " 'message': {\n", " 'hostname': 'H34222S3',\n", " 'location': {\n", " 'building': 'H5', \n", " 'floor': '3', \n", " 'room': '123'\n", " }\n", " }\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create rule and processor" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the rule:" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "153" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sys\n", "sys.path.append(\"../../../../../\")\n", "import tempfile\n", "from pathlib import Path\n", "\n", "rule_yaml = \"\"\"---\n", "filter: \"message.hostname\"\n", "requester:\n", " target_field_mapping:\n", " location: message.location\n", " method: GET\n", " url: http://localhost:32000/requester_api_example.json\n", " \n", "\"\"\"\n", "\n", "rule_path = Path(tempfile.gettempdir()) / \"requester\"\n", "rule_path.mkdir(exist_ok=True)\n", "rule_file = rule_path / \"requester.yml\"\n", "rule_file.write_text(rule_yaml)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the processor config:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "processor_config = {\n", " \"cmdbrequests\":{ \n", " \"type\": \"requester\",\n", " \"rules\": [str(rule_path)],\n", " }\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "create the processor with the factory:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "requester" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from unittest import mock\n", "from logprep.factory import Factory\n", "\n", "mock_logger = mock.MagicMock()\n", "requester = Factory.create(processor_config)\n", "requester" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Process event" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-12-13 02:24:31,539 urllib3.connectionpool DEBUG : Starting new HTTP connection (1): localhost:32000\n", "127.0.0.1 - - [13/Dec/2022 02:24:31] \"GET /api.json HTTP/1.1\" 200 -\n", "2022-12-13 02:24:31,542 urllib3.connectionpool DEBUG : http://localhost:32000 \"GET /api.json HTTP/1.1\" 200 97\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "before: {'message': {'hostname': 'H34222S3'}}\n", "after: {'message': {'hostname': 'H34222S3', 'location': {'building': 'H5', 'floor': '3', 'room': '123'}}}\n", "True\n" ] } ], "source": [ "from copy import deepcopy\n", "from tests.acceptance.util import TestServer\n", "mydocument = deepcopy(document)\n", "\n", "with TestServer.run_in_thread():\n", " print(f\"before: {mydocument}\")\n", " requester.process(mydocument)\n", " print(f\"after: {mydocument}\")\n", " print(mydocument == expected)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.11.0 ('.venv': venv)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "586280540a85d3e21edc698fe7b86af2848b9b02644e6c22463da25c40a3f1be" } } }, "nbformat": 4, "nbformat_minor": 2 }