Skip to content

Commit

Permalink
Add better hosts seed test data
Browse files Browse the repository at this point in the history
  • Loading branch information
marshyski committed Apr 12, 2024
1 parent 8215f60 commit d6ea08d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/db/db_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from json import dumps
from config.config import ES_TLS_VERIFY, ES_USER, ES_PW, HEADERS
from flask import Response
from urllib3.exceptions import InsecureRequestWarning
from utils.id_generator import gen_id

requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)

def post_request(request_url: str, request_data: dict) -> Response:
"""
Expand Down
54 changes: 51 additions & 3 deletions elk/seed_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
# autopep8: off
import json, time, sys, os.path
from string import ascii_letters
from random import choices
from random import randint
from datetime import datetime, timezone

# Changing path from paradrop/elk to paradrop/api to be able to import things from that folder
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) + '/api/')
from config.config import ES_HOSTS_URL, ES_USERS_URL, ES_EVENTS_URL, ES_EVENT_TRIGGERS_URL, ES_REPORTS_URL
Expand All @@ -15,6 +17,35 @@
number_of_events: int = 0
number_of_event_triggers: int = 0
number_of_reports: int = 0
hostnames: dict = {}

def generate_lastrun() -> str:
return datetime.now(timezone.utc).isoformat()[:-13] + 'Z'

def generate_ip() -> str:
base_ip = "192.168."

third_octet = randint(1, 254)
fourth_octet = randint(1, 255)

return f"{base_ip}{third_octet}.{fourth_octet}"

def hostname_incr(hostname) -> str:
try:
current_number = int(hostname.split("-")[-1])
except ValueError:
current_number = 0

host_split = hostname.split("-")
host_prefix = "-".join(host_split[:2]) + "-"

while True:
new_hostname = host_prefix + str(current_number)
if new_hostname not in hostnames:
hostnames[new_hostname] = ""
return new_hostname
else:
current_number += 1

# ADDING HOSTS MOCK DATA
hosts_mock_data: list = []
Expand All @@ -30,12 +61,29 @@
with open("elk/desktop-win10-1.json", 'r') as server_file4:
hosts_mock_data.append(json.load(server_file4))

with open("elk/vm-centos8-1.json", 'r') as server_file5:
hosts_mock_data.append(json.load(server_file5))

with open("elk/vm-centos9-1.json", 'r') as server_file6:
hosts_mock_data.append(json.load(server_file6))

with open("elk/vm-rocky9-1.json", 'r') as server_file7:
hosts_mock_data.append(json.load(server_file7))

with open("elk/vm-winsrv2022-1.json", 'r') as server_file8:
hosts_mock_data.append(json.load(server_file8))

with open("elk/desktop-win11-1.json", 'r') as server_file9:
hosts_mock_data.append(json.load(server_file9))

while number_of_hosts > 0:
for data in hosts_mock_data:
if number_of_hosts > 0:
data["hostname"] = "".join(choices(ascii_letters, k=10))
data["hostname"] = hostname_incr(data["hostname"])
data["ip_address"] = generate_ip()
data["id"] = gen_id()
post_request(ES_HOSTS_URL + "/_doc/", data)
data["last_run"] = generate_lastrun()
print(data["hostname"] + " " + post_request(ES_HOSTS_URL + "/_doc/", data).text)
number_of_hosts -= 1
else:
break
Expand Down

0 comments on commit d6ea08d

Please sign in to comment.