| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Splunk Finding module utilities for Ansible.""" |
| 3 |
|
| 4 |
# Copyright 2026 Red Hat Inc. |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
|
| 8 |
import re |
| 9 |
|
| 10 |
from typing import Any, Optional |
| 11 |
|
| 12 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import ( |
| 13 |
DEFAULT_API_APP_SECURITY_SUITE, |
| 14 |
DEFAULT_API_NAMESPACE, |
| 15 |
DEFAULT_API_USER, |
| 16 |
DISPOSITION_FROM_API, |
| 17 |
STATUS_FROM_API, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
# Key transformation: API param -> module param |
| 22 |
FINDING_KEY_TRANSFORM = { |
| 23 |
"rule_title": "title", |
| 24 |
"rule_description": "description", |
| 25 |
"security_domain": "security_domain", |
| 26 |
"risk_object": "entity", |
| 27 |
"risk_object_type": "entity_type", |
| 28 |
"risk_score": "finding_score", |
| 29 |
"owner": "owner", |
| 30 |
"status": "status", |
| 31 |
"urgency": "urgency", |
| 32 |
"disposition": "disposition", |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
def build_finding_api_path( |
| 37 |
namespace: str = DEFAULT_API_NAMESPACE, |
| 38 |
user: str = DEFAULT_API_USER, |
| 39 |
app: str = DEFAULT_API_APP_SECURITY_SUITE, |
| 40 |
) -> str: |
| 41 |
"""Build the findings API path from components. |
| 42 |
|
| 43 |
Args: |
| 44 |
namespace: The namespace portion of the path. Defaults to 'servicesNS'. |
| 45 |
user: The user portion of the path. Defaults to 'nobody'. |
| 46 |
app: The app portion of the path. Defaults to 'SplunkEnterpriseSecuritySuite'. |
| 47 |
|
| 48 |
Returns: |
| 49 |
The complete findings API path. |
| 50 |
""" |
| 51 |
return f"{namespace}/{user}/{app}/public/v2/findings" |
| 52 |
|
| 53 |
|
| 54 |
def extract_notable_time(ref_id: str) -> Optional[str]: |
| 55 |
"""Extract the notable_time from a finding reference ID. |
| 56 |
|
| 57 |
The ref_id format is typically: uuid@@notable@@time{timestamp} |
| 58 |
Example: 2008e99d-af14-4fec-89da-b9b17a81820a@@notable@@time1768225865 |
| 59 |
|
| 60 |
Args: |
| 61 |
ref_id: The finding reference ID. |
| 62 |
|
| 63 |
Returns: |
| 64 |
The extracted notable_time as a string, or None if not found. |
| 65 |
""" |
| 66 |
if not ref_id: |
| 67 |
return None |
| 68 |
|
| 69 |
# Pattern to match 'time' followed by digits at the end |
| 70 |
match = re.search(r"time(\d+)$", ref_id) |
| 71 |
if match: |
| 72 |
return match.group(1) |
| 73 |
|
| 74 |
return None |
| 75 |
|
| 76 |
|
| 77 |
# Buffer in seconds to subtract from notable_time when used as 'earliest' |
| 78 |
# in time-range queries. The Splunk API may treat 'earliest' as exclusive |
| 79 |
# or the finding's _time may have sub-second precision slightly before the |
| 80 |
# integer epoch in the ref_id, so a small buffer avoids missing the finding. |
| 81 |
_EARLIEST_BUFFER_SECONDS = 1 |
| 82 |
|
| 83 |
|
| 84 |
def get_earliest_from_ref_id(ref_id: str) -> Optional[str]: |
| 85 |
"""Get a buffered earliest time suitable for Splunk time-range queries. |
| 86 |
|
| 87 |
Extracts the notable time from the ref_id and subtracts a small buffer |
| 88 |
to ensure the finding falls within the query's time range. |
| 89 |
|
| 90 |
Args: |
| 91 |
ref_id: The finding reference ID. |
| 92 |
|
| 93 |
Returns: |
| 94 |
The buffered earliest time as a string, or None if extraction fails. |
| 95 |
""" |
| 96 |
notable_time = extract_notable_time(ref_id) |
| 97 |
if notable_time is None: |
| 98 |
return None |
| 99 |
return str(int(notable_time) - _EARLIEST_BUFFER_SECONDS) |
| 100 |
|
| 101 |
|
| 102 |
def map_finding_from_api( |
| 103 |
config: dict[str, Any], |
| 104 |
key_transform: dict[str, str] = None, |
| 105 |
) -> dict[str, Any]: |
| 106 |
"""Convert finding API response to module params format. |
| 107 |
|
| 108 |
Args: |
| 109 |
config: The API response config dictionary. |
| 110 |
key_transform: Optional key transformation dict. Defaults to FINDING_KEY_TRANSFORM. |
| 111 |
|
| 112 |
Returns: |
| 113 |
Dictionary with module parameter names and normalized values. |
| 114 |
""" |
| 115 |
from ansible_collections.splunk.es.plugins.module_utils.splunk_utils import map_params_to_obj |
| 116 |
|
| 117 |
if key_transform is None: |
| 118 |
key_transform = FINDING_KEY_TRANSFORM |
| 119 |
|
| 120 |
res = {} |
| 121 |
|
| 122 |
# Extract ref_id from finding_id field |
| 123 |
if "finding_id" in config: |
| 124 |
res["ref_id"] = config["finding_id"] |
| 125 |
|
| 126 |
# Use the helper from module_utils |
| 127 |
res.update(map_params_to_obj(config, key_transform)) |
| 128 |
|
| 129 |
# Handle status conversion |
| 130 |
if "status" in res and res["status"]: |
| 131 |
res["status"] = STATUS_FROM_API.get(str(res["status"]), res["status"]) |
| 132 |
|
| 133 |
# Handle disposition conversion |
| 134 |
if "disposition" in res and res["disposition"]: |
| 135 |
res["disposition"] = DISPOSITION_FROM_API.get(str(res["disposition"]), res["disposition"]) |
| 136 |
|
| 137 |
# Normalize finding_score to int (API returns string like "25.0") |
| 138 |
if "finding_score" in res and res["finding_score"]: |
| 139 |
try: |
| 140 |
res["finding_score"] = int(float(res["finding_score"])) |
| 141 |
except (ValueError, TypeError): |
| 142 |
pass |
| 143 |
|
| 144 |
return res |