| 1 | from typing import List |
| 2 | |
| 3 | from fastapi import HTTPException |
| 4 | from loguru import logger |
| 5 | from pydantic import parse_obj_as |
| 6 | |
| 7 | from app.connectors.graylog.schema.content_packs import ContentPack |
| 8 | from app.connectors.graylog.schema.content_packs import ContentPackList |
| 9 | from app.connectors.graylog.utils.universal import send_get_request |
| 10 | from app.connectors.graylog.utils.universal import send_post_request |
| 11 | from app.connectors.graylog.utils.universal import send_post_request_create_entity |
| 12 | |
| 13 | |
| 14 | async def get_content_packs() -> List[ContentPack]: |
| 15 | """Get content packs from Graylog. |
| 16 | |
| 17 | Returns: |
| 18 | List[ContentPack]: The list of collected content packs. |
| 19 | |
| 20 | Raises: |
| 21 | HTTPException: If there is an error collecting the content packs. |
| 22 | """ |
| 23 | logger.info("Getting content packs from Graylog") |
| 24 | try: |
| 25 | content_packs_collected = await send_get_request( |
| 26 | endpoint="/api/system/content_packs", |
| 27 | ) |
| 28 | if content_packs_collected.get("success"): |
| 29 | logger.info("Content packs collected successfully") |
| 30 | content_packs_list = parse_obj_as(ContentPackList, content_packs_collected.get("data")) |
| 31 | return content_packs_list.content_packs |
| 32 | else: |
| 33 | raise HTTPException(status_code=500, detail="Content packs collection unsuccessful") |
| 34 | except KeyError as e: |
| 35 | error_msg = f"Failed to collect content packs key: {e}" |
| 36 | logger.error(error_msg) |
| 37 | raise HTTPException(status_code=500, detail=error_msg) |
| 38 | except Exception as e: |
| 39 | error_msg = f"Failed to collect content packs: {e}" |
| 40 | logger.error(error_msg) |
| 41 | raise HTTPException(status_code=500, detail=error_msg) |
| 42 | |
| 43 | |
| 44 | async def insert_content_pack(content_pack: ContentPack) -> bool: |
| 45 | """Insert a content pack into Graylog. |
| 46 | |
| 47 | Args: |
| 48 | content_pack (ContentPack): The content pack to insert. |
| 49 | |
| 50 | Returns: |
| 51 | bool: True if the content pack was inserted successfully, False if it was not. |
| 52 | |
| 53 | Raises: |
| 54 | HTTPException: If there is an error inserting the content pack. |
| 55 | """ |
| 56 | logger.info(f"Inserting content pack {content_pack} into Graylog") |
| 57 | try: |
| 58 | content_pack_inserted = await send_post_request( |
| 59 | endpoint="/api/system/content_packs", |
| 60 | data=content_pack, |
| 61 | ) |
| 62 | logger.info(f"Content pack inserted: {content_pack_inserted}") |
| 63 | if content_pack_inserted["success"] is True: |
| 64 | logger.info("Content pack inserted successfully") |
| 65 | return True |
| 66 | else: |
| 67 | raise HTTPException(status_code=500, detail="Content pack insertion unsuccessful") |
| 68 | except HTTPException as e: |
| 69 | if "Content pack" in e.detail and "already found" in e.detail and "PROCESSING_PIPELINE" in content_pack["name"]: |
| 70 | logger.info("Content pack with PROCESSING_PIPELINE already exists, skipping") |
| 71 | return False |
| 72 | else: |
| 73 | error_msg = f"Failed to insert content pack: {e}" |
| 74 | logger.error(error_msg) |
| 75 | raise HTTPException(status_code=500, detail=error_msg) |
| 76 | except KeyError as e: |
| 77 | error_msg = f"Failed to insert content pack key: {e}" |
| 78 | logger.error(error_msg) |
| 79 | raise HTTPException(status_code=500, detail=error_msg) |
| 80 | except Exception as e: |
| 81 | error_msg = f"Failed to insert content pack: {e}" |
| 82 | logger.error(error_msg) |
| 83 | raise HTTPException(status_code=500, detail=error_msg) |
| 84 | |
| 85 | |
| 86 | async def install_content_pack(content_pack_id: str, revision: int) -> bool: |
| 87 | """Install a content pack in Graylog. |
| 88 | |
| 89 | Args: |
| 90 | content_pack_id (str): The ID of the content pack to install. |
| 91 | revision (int): The revision of the content pack to install. |
| 92 | |
| 93 | Returns: |
| 94 | bool: True if the content pack was installed successfully, False if it was not. |
| 95 | |
| 96 | Raises: |
| 97 | HTTPException: If there is an error installing the content pack. |
| 98 | """ |
| 99 | logger.info(f"Installing content pack {content_pack_id} in Graylog") |
| 100 | try: |
| 101 | content_pack_installed = await send_post_request_create_entity( |
| 102 | endpoint=f"/api/system/content_packs/{content_pack_id}/{revision}/installations", |
| 103 | entity={ |
| 104 | "comment": "Installed by SOCFortress CoPilot", |
| 105 | }, |
| 106 | ) |
| 107 | logger.info(f"Content pack installed: {content_pack_installed}") |
| 108 | if content_pack_installed["success"] is True: |
| 109 | logger.info("Content pack installed successfully") |
| 110 | return True |
| 111 | else: |
| 112 | raise HTTPException(status_code=500, detail="Content pack installation unsuccessful") |
| 113 | except KeyError as e: |
| 114 | error_msg = f"Failed to install content pack key: {e}" |
| 115 | logger.error(error_msg) |
| 116 | raise HTTPException(status_code=500, detail=error_msg) |
| 117 | except Exception as e: |
| 118 | error_msg = f"Failed to install content pack: {e}" |
| 119 | logger.error(error_msg) |
| 120 | raise HTTPException(status_code=500, detail=error_msg) |