misp-galaxy/tools/tidal-api/main.py

135 lines
4.8 KiB
Python
Raw Normal View History

2024-02-21 15:24:48 +00:00
from api.api import TidalAPI
from models.galaxy import Galaxy
from models.cluster import Cluster
from utils.extractor import extract_links
2024-02-22 09:18:18 +00:00
from utils.config import load_config
2024-02-21 15:24:48 +00:00
import argparse
CLUSTER_PATH = "../../clusters/"
GALAXY_PATH = "../../galaxies/"
2024-02-22 09:52:23 +00:00
config = load_config("./config.json")
UUIDS = config["UUIDS"]
GALAXY_CONFIGS = config["GALAXY_CONFIGS"]
CLUSTER_CONFIGS = config["CLUSTER_CONFIGS"]
VALUE_FIELDS = config["VALUE_FIELDS"]
2024-02-21 15:24:48 +00:00
def create_cluster_values(data, cluster, add_private):
2024-02-21 15:24:48 +00:00
value_fields = VALUE_FIELDS[cluster.internal_type]
for entry in data["data"]:
values = {}
for key, value in value_fields.items():
match key:
case "description":
values[value] = entry.get(key)
case "meta":
metadata = create_metadata(entry, value)
values["meta"] = metadata
case "related":
relations = create_relations(entry, value, add_private)
2024-02-21 15:24:48 +00:00
values["related"] = relations
case "uuid":
values[key] = entry.get(value)
case "value":
values[key] = entry.get(value)
case _:
2024-02-22 09:52:23 +00:00
print(
f"Error: Invalid configuration for {key} in {cluster.internal_type} value fields."
)
2024-02-21 15:24:48 +00:00
cluster.add_value(values)
2024-02-22 09:52:23 +00:00
2024-02-21 15:24:48 +00:00
def create_metadata(data, format):
metadata = {}
for meta_key, meta_value in format.items():
if isinstance(meta_value, dict):
if meta_value.get("extract") == "single" and data.get(meta_value["key"]):
2024-02-22 09:52:23 +00:00
metadata[meta_key] = data.get(meta_value["key"])[0].get(
meta_value["subkey"]
)
elif meta_value.get("extract") == "multiple" and data.get(
meta_value["key"]
):
metadata[meta_key] = [
entry.get(meta_value["subkey"])
for entry in data.get(meta_value["key"])
]
2024-02-22 09:18:18 +00:00
elif meta_value.get("extract") == "reverse" and data.get(meta_value["key"]):
metadata[meta_key] = [data.get(meta_value["key"])]
2024-02-21 15:24:48 +00:00
elif data.get(meta_value):
metadata[meta_key] = data.get(meta_value)
return metadata
2024-02-22 09:52:23 +00:00
2024-02-21 15:24:48 +00:00
def create_relations(data, format, add_private):
2024-02-21 15:24:48 +00:00
relations = []
for i in range(len(list(format))):
for relation in data[list(format)[i]]:
if not add_private and list(format.values())[i].get("mode") == "private":
continue
2024-02-21 15:24:48 +00:00
relation_entry = {}
for relation_key, relation_value in list(format.values())[i].items():
if relation_key != "type":
if relation_key == "mode":
continue
2024-02-21 15:24:48 +00:00
relation_entry[relation_key] = relation.get(relation_value)
else:
relation_entry[relation_key] = relation_value
relations.append(relation_entry)
return relations
2024-02-22 09:52:23 +00:00
2024-02-21 15:24:48 +00:00
def create_galaxy_and_cluster(galaxy_type, version, add_private=False):
2024-02-21 15:24:48 +00:00
api = TidalAPI()
galaxy = Galaxy(**GALAXY_CONFIGS[galaxy_type], version=version)
galaxy.save_to_file(f"{GALAXY_PATH}/tidal-{galaxy_type}.json")
cluster = Cluster(**CLUSTER_CONFIGS[galaxy_type], internal_type=galaxy_type)
data = api.get_data(galaxy_type)
create_cluster_values(data, cluster, add_private)
2024-02-21 15:24:48 +00:00
cluster.save_to_file(f"{CLUSTER_PATH}/tidal-{galaxy_type}.json")
2024-02-22 09:18:18 +00:00
print(f"Galaxy tidal-{galaxy_type} created")
2024-02-21 15:24:48 +00:00
2024-02-22 09:52:23 +00:00
2024-02-21 15:24:48 +00:00
def create_galaxy(args):
if args.all:
for galaxy_type in GALAXY_CONFIGS:
create_galaxy_and_cluster(galaxy_type, args.version, args.addprivate)
2024-02-21 15:24:48 +00:00
else:
create_galaxy_and_cluster(args.type, args.version, args.addprivate)
2024-02-21 15:24:48 +00:00
2024-02-22 09:52:23 +00:00
2024-02-21 15:24:48 +00:00
if __name__ == "__main__":
2024-02-22 09:52:23 +00:00
parser = argparse.ArgumentParser(
description="Create a galaxy and cluster for Tidal API"
)
2024-02-21 15:24:48 +00:00
subparsers = parser.add_subparsers(dest="command")
2024-02-22 09:52:23 +00:00
galaxy_parser = subparsers.add_parser(
"create_galaxy", help="Create a galaxy from the Tidal API"
)
galaxy_parser.add_argument(
"--type",
choices=list(GALAXY_CONFIGS.keys()) + ["all"],
help="The type of the galaxy",
)
galaxy_parser.add_argument(
"-v", "--version", type=int, required=True, help="The version of the galaxy"
)
galaxy_parser.add_argument(
"--all", action="store_true", help="Flag to create all predefined galaxy types"
)
galaxy_parser.add_argument(
"--addprivate", action="store_true", help="Flag to add private relations"
)
2024-02-21 15:24:48 +00:00
galaxy_parser.set_defaults(func=create_galaxy)
args = parser.parse_args()
2024-02-22 09:52:23 +00:00
if hasattr(args, "func"):
2024-02-21 15:24:48 +00:00
args.func(args)
else:
parser.print_help()