-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
330 lines (281 loc) · 11 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import meilisearch
import json
import requests
import os
import subprocess
import argparse
with open("./src/config.json", "r") as config_file:
config = json.load(config_file)
API_KEY = config.get("MEILI_ADMIN_API_KEY")
HOST = config.get("MEILI_URL")
client = meilisearch.Client(HOST, API_KEY)
# ============================
# GitHub API functions
# ============================
def fetch_files_from_github(owner, repo, path, token, branch="main"):
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}?ref={branch}"
headers = {"Authorization": f"token {token}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def fetch_subdir_files_from_github(owner, repo, path, token, branch="main"):
all_files = []
items = fetch_files_from_github(owner, repo, path, token, branch)
for item in items:
if item["type"] == "file":
all_files.append(item)
elif item["type"] == "dir":
all_files.extend(
fetch_subdir_files_from_github(owner, repo, item["path"], branch)
)
return all_files
def load_clusters_from_github(token):
print("Fetching data from MISP Galaxy GitHub repository (API mode)...")
docs = []
file_list = fetch_files_from_github(
"MISP", "misp-galaxy", "clusters", token, "main"
)
for file_info in file_list:
if file_info["name"].endswith(".json"):
file_url = file_info["download_url"]
galaxy = file_info["name"].removesuffix(".json")
file_response = requests.get(file_url)
file_response.raise_for_status()
try:
doc = file_response.json()
for cluster in doc["values"]:
cluster["galaxy"] = galaxy
docs.append(cluster)
except Exception as e:
print(f"Error processing {file_info['name']}: {e}")
print(f"Loaded {len(docs)} documents from GitHub.")
return docs
def load_objects_from_github(token):
print("Fetching data from MISP Objects GitHub repository (API mode)...")
docs = []
file_list = fetch_subdir_files_from_github(
"MISP", "misp-objects", "objects", token, "main"
)
for file_info in file_list:
if file_info["name"].endswith(".json"):
file_url = file_info["download_url"]
file_response = requests.get(file_url)
file_response.raise_for_status()
try:
doc = file_response.json()
docs.append(doc)
except Exception as e:
print(f"Error processing {file_info['name']}: {e}")
print(f"Loaded {len(docs)} documents from GitHub.")
return docs
def load_taxonomies_from_github(token):
print("Fetching data from MISP Taxonomies GitHub repository (API mode)...")
docs = []
file_list = fetch_subdir_files_from_github(
"MISP", "misp-taxonomies", "", token, "main"
)
for file_info in file_list:
if file_info["name"] == "machinetag.json":
file_url = file_info["download_url"]
file_response = requests.get(file_url)
file_response.raise_for_status()
try:
doc = file_response.json()
ns = doc["namespace"]
for predicate in doc["predicates"]:
predicate["namespace"] = ns
docs.append(predicate)
try:
for value in doc["values"]:
pred = value["predicate"]
for entry in value["entry"]:
entry["namespace"] = ns
entry["predicate"] = pred
docs.append(entry)
except Exception:
pass
docs.append(doc)
except Exception as e:
print(f"Error processing {file_info['name']}: {e}")
print(f"Loaded {len(docs)} documents from GitHub.")
return docs
# ============================
# Local mode functions
# ============================
def clone_repo(owner, repo, local_path, branch="main"):
if not os.path.exists(local_path):
print(f"Cloning repository {repo} into {local_path}...")
subprocess.run(
[
"git",
"clone",
"--branch",
branch,
f"https://github.com/{owner}/{repo}.git",
local_path,
],
check=True,
)
else:
print(
f"Repository {repo} already exists at {local_path}. Pulling latest changes..."
)
subprocess.run(["git", "-C", local_path, "pull"], check=True)
def load_clusters_from_local():
print("Fetching data from local MISP Galaxy repository...")
docs = []
repo_dir = "./data/misp-galaxy"
clusters_dir = os.path.join(repo_dir, "clusters")
if not os.path.exists(clusters_dir):
print(f"Directory {clusters_dir} does not exist.")
return docs
for filename in os.listdir(clusters_dir):
if filename.endswith(".json"):
galaxy = filename.removesuffix(".json")
file_path = os.path.join(clusters_dir, filename)
try:
with open(file_path, "r") as f:
doc = json.load(f)
for cluster in doc["values"]:
cluster["galaxy"] = galaxy
docs.append(cluster)
except Exception as e:
print(f"Error processing {filename}: {e}")
print(f"Loaded {len(docs)} documents from local repository.")
return docs
def load_objects_from_local():
print("Fetching data from local MISP Objects repository...")
docs = []
repo_dir = "./data/misp-objects"
objects_dir = os.path.join(repo_dir, "objects")
if not os.path.exists(objects_dir):
print(f"Directory {objects_dir} does not exist.")
return docs
for root, dirs, files in os.walk(objects_dir):
for filename in files:
if filename.endswith(".json"):
file_path = os.path.join(root, filename)
try:
with open(file_path, "r") as f:
doc = json.load(f)
docs.append(doc)
except Exception as e:
print(f"Error processing {file_path}: {e}")
print(f"Loaded {len(docs)} documents from local repository.")
return docs
def load_taxonomies_from_local():
print("Fetching data from local MISP Taxonomies repository...")
docs = []
repo_dir = "./data/misp-taxonomies"
for root, dirs, files in os.walk(repo_dir):
for filename in files:
if filename == "machinetag.json":
file_path = os.path.join(root, filename)
try:
with open(file_path, "r") as f:
doc = json.load(f)
ns = doc["namespace"]
for predicate in doc["predicates"]:
predicate["namespace"] = ns
docs.append(predicate)
try:
for value in doc["values"]:
pred = value["predicate"]
for entry in value["entry"]:
entry["namespace"] = ns
entry["predicate"] = pred
docs.append(entry)
except Exception:
pass
docs.append(doc)
except Exception as e:
print(f"Error processing {file_path}: {e}")
if not docs:
print("machinetag.json not found in local repository.")
else:
print(f"Loaded {len(docs)} documents from local repository.")
return docs
# ============================
# Indexing function
# ============================
def index_documents(docs, index_name, primaryKey="uuid"):
for doc in docs:
client.index(index_name).update_documents([doc])
# ============================
# Main functions for each mode
# ============================
def main_api():
token = config.get("GITHUB_PAT")
clusters = load_clusters_from_github(token)
index_documents(clusters, "misp-galaxy")
objects = load_objects_from_github(token)
index_documents(objects, "misp-objects")
taxonomies = load_taxonomies_from_github(token)
index_documents(taxonomies, "misp-taxonomies")
client.index("misp-taxonomies").update_filterable_attributes(
["version", "namespace", "predicate"]
)
def main_local():
clone_repo("MISP", "misp-galaxy", "./data/misp-galaxy", "main")
clone_repo("MISP", "misp-objects", "./data/misp-objects", "main")
clone_repo("MISP", "misp-taxonomies", "./data/misp-taxonomies", "main")
clusters = load_clusters_from_local()
index_documents(clusters, "misp-galaxy")
objects = load_objects_from_local()
index_documents(objects, "misp-objects")
taxonomies = load_taxonomies_from_local()
index_documents(taxonomies, "misp-taxonomies")
client.index("misp-taxonomies").update_filterable_attributes(
["version", "namespace", "predicate"]
)
def main_update():
clone_repo("MISP", "misp-galaxy", "./data/misp-galaxy", "main")
clone_repo("MISP", "misp-objects", "./data/misp-objects", "main")
clone_repo("MISP", "misp-taxonomies", "./data/misp-taxonomies", "main")
clusters = load_clusters_from_local()
index_documents(clusters, "misp-galaxy_new")
objects = load_objects_from_local()
index_documents(objects, "misp-objects_new")
taxonomies = load_taxonomies_from_local()
index_documents(taxonomies, "misp-taxonomies_new")
client.index("misp-taxonomies_new").update_filterable_attributes(
["version", "namespace", "predicate"]
)
client.swap_indexes(
[
{"indexes": ["misp-galaxy", "misp-galaxy_new"]},
{"indexes": ["misp-objects", "misp-objects_new"]},
{"indexes": ["misp-taxonomies", "misp-taxonomies_new"]},
]
)
client.index("misp-galaxy_new").delete()
client.index("misp-objects_new").delete()
client.index("misp-taxonomies_new").delete()
# ============================
# Argument parsing and entry point
# ============================
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Index MISP data from GitHub (API mode) or local repositories (local mode)."
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"--api", action="store_true", help="Fetch files using the GitHub API"
)
group.add_argument(
"--local",
action="store_true",
help="Clone repositories locally and load files from disk",
)
group.add_argument(
"--update",
action="store_true",
help="Update indexes during production from local files cloning repositories",
)
args = parser.parse_args()
if args.api:
main_api()
elif args.local:
main_local()
elif args.update:
main_update()