Skip to content

Commit 2f3dce4

Browse files
AlexRuiz7Jorgesnchz
authored andcommitted
Update commands index data model (#453)
* Update commands index data model * Update commands event generator * Move agent fields as extended
1 parent 068d59a commit 2f3dce4

File tree

6 files changed

+64
-43
lines changed

6 files changed

+64
-43
lines changed

ecs/command/event-generator/event_generator.py

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#!/bin/python3
22

3-
# This script generates sample events and injects them into an OpenSearch index.
4-
# The events follow the provided template structure with command-related data fields.
5-
# Additional fields are generated when the --index option is passed.
6-
73
import random
84
import json
95
import requests
106
import warnings
117
import logging
128
import argparse
9+
import uuid
1310

1411
LOG_FILE = 'generate_data.log'
1512
GENERATED_DATA_FILE = 'generatedData.json'
@@ -22,32 +19,37 @@
2219

2320

2421
def generate_random_command(include_all_fields=False):
25-
command = {
26-
"source": random.choice(["Users/Services", "Engine", "Content manager"]),
27-
"user": f"user{random.randint(1, 100)}",
28-
"target": f"WazuhServerCluster{random.randint(1, 10)}",
29-
"type": random.choice(["agent_group", "agent", "wazuh_server"]),
30-
"action": {
31-
"type": random.choice(["Agent groups", "Agent", "Server cluster"]),
32-
"args": [f"/path/to/executable/arg{random.randint(1, 10)}"],
33-
"version": f"v{random.randint(1, 10)}"
34-
},
35-
"timeout": random.randint(10, 100)
22+
document = {
23+
"command": {
24+
"source": random.choice(["Users/Services", "Engine", "Content manager"]),
25+
"user": f"user{random.randint(1, 100)}",
26+
"target": {
27+
"id": f"target{random.randint(1, 10)}",
28+
"type": random.choice(["agent", "group", "server"])
29+
},
30+
"action": {
31+
"name": random.choice(["restart", "update", "change_group", "apply_policy"]),
32+
"args": [f"/path/to/executable/arg{random.randint(1, 10)}"],
33+
"version": f"v{random.randint(1, 5)}"
34+
},
35+
"timeout": random.randint(10, 100)
36+
}
3637
}
3738

3839
if include_all_fields:
39-
command["status"] = random.choice(
40-
["pending", "sent", "success", "failure"]
41-
)
42-
command["result"] = {
40+
document["agent"]["groups"] = [f"group{random.randint(1, 5)}"],
41+
document["command"]["status"] = random.choice(
42+
["pending", "sent", "success", "failure"])
43+
document["command"]["result"] = {
4344
"code": random.randint(0, 255),
4445
"message": f"Result message {random.randint(1, 1000)}",
4546
"data": f"Result data {random.randint(1, 100)}"
4647
}
47-
command["request_id"] = random.randint(1000, 9999)
48-
command["order_id"] = random.randint(1000, 9999)
48+
# Generate UUIDs for request_id and order_id
49+
document["command"]["request_id"] = str(uuid.uuid4())
50+
document["command"]["order_id"] = str(uuid.uuid4())
4951

50-
return command
52+
return document
5153

5254

5355
def generate_random_data(number, include_all_fields=False):
@@ -58,8 +60,6 @@ def generate_random_data(number, include_all_fields=False):
5860

5961

6062
def inject_events(ip, port, index, username, password, data, use_index=False):
61-
url = f'https://{ip}:{port}/_plugins/_commandmanager'
62-
6363
session = requests.Session()
6464
session.auth = (username, password)
6565
session.verify = False
@@ -68,8 +68,12 @@ def inject_events(ip, port, index, username, password, data, use_index=False):
6868
try:
6969
for event_data in data:
7070
if use_index:
71-
id = event_data["request_id"] + event_data["order_id"]
72-
url = f'https://{ip}:{port}/{index}/_doc/{id}'
71+
# Generate UUIDs for the document id
72+
doc_id = str(uuid.uuid4())
73+
url = f'https://{ip}:{port}/{index}/_doc/{doc_id}'
74+
else:
75+
# Default URL for command manager API without the index
76+
url = f'https://{ip}:{port}/_plugins/_commandmanager'
7377

7478
response = session.post(url, json=event_data, headers=headers)
7579
if response.status_code != 201:
@@ -83,7 +87,8 @@ def inject_events(ip, port, index, username, password, data, use_index=False):
8387

8488
def main():
8589
parser = argparse.ArgumentParser(
86-
description="Generate and optionally inject events into an OpenSearch index or Command Manager.")
90+
description="Generate and optionally inject events into an OpenSearch index or Command Manager."
91+
)
8792
parser.add_argument(
8893
"--index",
8994
action="store_true",
@@ -108,7 +113,8 @@ def main():
108113
logging.info('Data generation completed.')
109114

110115
inject = input(
111-
"Do you want to inject the generated data into your indexer/command manager? (y/n) ").strip().lower()
116+
"Do you want to inject the generated data into your indexer/command manager? (y/n) "
117+
).strip().lower()
112118
if inject == 'y':
113119
ip = input("Enter the IP of your Indexer: ")
114120
port = input("Enter the port of your Indexer: ")

ecs/command/fields/custom/agent.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
- name: agent
3+
title: Wazuh Agents
4+
short: Wazuh Inc. custom fields.
5+
type: group
6+
group: 2
7+
fields:
8+
- name: groups
9+
type: keyword
10+
level: custom
11+
description: >
12+
The groups the agent belongs to.

ecs/command/fields/custom/command.yml

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
level: custom
1818
description: >
1919
The user that originated the request.
20-
- name: target
20+
- name: target.id
2121
type: keyword
2222
level: custom
2323
description: >
24-
Wazuh Server Cluster name to send the command to.
25-
- name: type
24+
Unique identifier of the destination to send the command to.
25+
- name: target.type
2626
type: keyword
2727
level: custom
2828
description: >
29-
The requested action type. One of 'agent_group', 'agent', 'wazuh_server'.
30-
- name: action.type
29+
The destination type. One of [`group`, `agent`, `server`]
30+
- name: action.name
3131
type: keyword
3232
level: custom
3333
description: >
34-
The actual requested action. One of Agent groups, Agent, Server cluster.
34+
The requested action type. Examples: `restart`, `update`, `change_group`, `apply_policy`, ...
3535
- name: action.args
3636
type: keyword
3737
level: custom
@@ -51,7 +51,7 @@
5151
type: keyword
5252
level: custom
5353
description: >
54-
Status within the Command Manager's context. One of 'pending', 'sent', 'success', 'failure'.
54+
Status within the Command Manager's context. One of ['pending', 'sent', 'success', 'failure'].
5555
- name: result.code
5656
type: short
5757
level: custom
@@ -71,9 +71,9 @@
7171
type: keyword
7272
level: custom
7373
description: >
74-
Unique identifier generated by the Command Manager. UUID.
74+
UUID generated by the Command Manager.
7575
- name: order_id
7676
type: keyword
7777
level: custom
7878
description: >
79-
Unique identifier generated by the Command Manager. UUID.
79+
UUID generated by the Command Manager.

ecs/command/fields/subset.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ fields:
44
base:
55
fields:
66
tags: []
7+
agent:
8+
fields:
9+
groups: {}
710
command:
811
fields: "*"

ecs/command/fields/template-settings-legacy.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"number_of_replicas": "0",
1111
"refresh_interval": "5s",
1212
"query.default_field": [
13-
"command.source",
14-
"command.target",
15-
"command.status",
16-
"command.type"
13+
"command.source",
14+
"command.target.type",
15+
"command.status",
16+
"command.action.name"
1717
]
1818
}
1919
}

ecs/command/fields/template-settings.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
"refresh_interval": "5s",
1313
"query.default_field": [
1414
"command.source",
15-
"command.target",
15+
"command.target.type",
1616
"command.status",
17-
"command.type"
17+
"command.action.name"
1818
]
1919
}
2020
}

0 commit comments

Comments
 (0)