-
Notifications
You must be signed in to change notification settings - Fork 518
/
Copy pathgeneric_loader.py
195 lines (153 loc) · 7.58 KB
/
generic_loader.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
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Elastic License
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
"""Load generic toml formatted files for exceptions and actions."""
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Union
import pytoml
from .action import TOMLAction, TOMLActionContents
from .action_connector import TOMLActionConnector, TOMLActionConnectorContents
from .config import parse_rules_config
from .exception import TOMLException, TOMLExceptionContents
from .rule_loader import dict_filter
from .schemas import definitions
RULES_CONFIG = parse_rules_config()
GenericCollectionTypes = Union[TOMLAction, TOMLActionConnector, TOMLException]
GenericCollectionContentTypes = Union[TOMLActionContents, TOMLActionConnectorContents, TOMLExceptionContents]
def metadata_filter(**metadata) -> Callable[[GenericCollectionTypes], bool]:
"""Get a filter callback based off item metadata"""
flt = dict_filter(metadata)
def callback(item: GenericCollectionTypes) -> bool:
target_dict = item.contents.metadata.to_dict()
return flt(target_dict)
return callback
class GenericCollection:
"""Generic collection for action and exception objects."""
items: list
__default = None
def __init__(self, items: Optional[List[GenericCollectionTypes]] = None):
self.id_map: Dict[definitions.UUIDString, GenericCollectionTypes] = {}
self.file_map: Dict[Path, GenericCollectionTypes] = {}
self.name_map: Dict[definitions.RuleName, GenericCollectionTypes] = {}
self.items: List[GenericCollectionTypes] = []
self.errors: Dict[Path, Exception] = {}
self.frozen = False
self._toml_load_cache: Dict[Path, dict] = {}
for items in (items or []):
self.add_item(items)
def __len__(self) -> int:
"""Get the total amount of exceptions in the collection."""
return len(self.items)
def __iter__(self) -> Iterable[GenericCollectionTypes]:
"""Iterate over all items in the collection."""
return iter(self.items)
def __contains__(self, item: GenericCollectionTypes) -> bool:
"""Check if an item is in the map by comparing IDs."""
return item.id in self.id_map
def filter(self, cb: Callable[[TOMLException], bool]) -> 'GenericCollection':
"""Retrieve a filtered collection of items."""
filtered_collection = GenericCollection()
for item in filter(cb, self.items):
filtered_collection.add_item(item)
return filtered_collection
@staticmethod
def deserialize_toml_string(contents: Union[bytes, str]) -> dict:
"""Deserialize a TOML string into a dictionary."""
return pytoml.loads(contents)
def _load_toml_file(self, path: Path) -> dict:
"""Load a TOML file into a dictionary."""
if path in self._toml_load_cache:
return self._toml_load_cache[path]
# use pytoml instead of toml because of annoying bugs
# https://github.com/uiri/toml/issues/152
# might also be worth looking at https://github.com/sdispater/tomlkit
with path.open("r", encoding="utf-8") as f:
toml_dict = self.deserialize_toml_string(f.read())
self._toml_load_cache[path] = toml_dict
return toml_dict
def _get_paths(self, directory: Path, recursive=True) -> List[Path]:
"""Get all TOML files in a directory."""
return sorted(directory.rglob('*.toml') if recursive else directory.glob('*.toml'))
def _assert_new(self, item: GenericCollectionTypes) -> None:
"""Assert that the item is new and can be added to the collection."""
file_map = self.file_map
name_map = self.name_map
assert not self.frozen, f"Unable to add item {item.name} to a frozen collection"
assert item.name not in name_map, \
f"Rule Name {item.name} collides with {name_map[item.name].name}"
if item.path is not None:
item_path = item.path.resolve()
assert item_path not in file_map, f"Item file {item_path} already loaded"
file_map[item_path] = item
def add_item(self, item: GenericCollectionTypes) -> None:
"""Add a new item to the collection."""
self._assert_new(item)
self.name_map[item.name] = item
self.items.append(item)
def load_dict(self, obj: dict, path: Optional[Path] = None) -> GenericCollectionTypes:
"""Load a dictionary into the collection."""
if 'exceptions' in obj:
contents = TOMLExceptionContents.from_dict(obj)
item = TOMLException(path=path, contents=contents)
elif 'actions' in obj:
contents = TOMLActionContents.from_dict(obj)
item = TOMLAction(path=path, contents=contents)
elif 'action_connectors' in obj:
contents = TOMLActionConnectorContents.from_dict(obj)
item = TOMLActionConnector(path=path, contents=contents)
else:
raise ValueError("Invalid object type")
self.add_item(item)
return item
def load_file(self, path: Path) -> GenericCollectionTypes:
"""Load a single file into the collection."""
try:
path = path.resolve()
# use the default generic loader as a cache.
# if it already loaded the item, then we can just use it from that
if self.__default is not None and self is not self.__default:
if path in self.__default.file_map:
item = self.__default.file_map[path]
self.add_item(item)
return item
obj = self._load_toml_file(path)
return self.load_dict(obj, path=path)
except Exception:
print(f"Error loading item in {path}")
raise
def load_files(self, paths: Iterable[Path]) -> None:
"""Load multiple files into the collection."""
for path in paths:
self.load_file(path)
def load_directory(
self, directory: Path, recursive=True, toml_filter: Optional[Callable[[dict], bool]] = None
) -> None:
"""Load all TOML files in a directory."""
paths = self._get_paths(directory, recursive=recursive)
if toml_filter is not None:
paths = [path for path in paths if toml_filter(self._load_toml_file(path))]
self.load_files(paths)
def load_directories(
self, directories: Iterable[Path], recursive=True, toml_filter: Optional[Callable[[dict], bool]] = None
) -> None:
"""Load all TOML files in multiple directories."""
for path in directories:
self.load_directory(path, recursive=recursive, toml_filter=toml_filter)
def freeze(self) -> None:
"""Freeze the generic collection and make it immutable going forward."""
self.frozen = True
@classmethod
def default(cls) -> 'GenericCollection':
"""Return the default item collection, which retrieves from default config location."""
if cls.__default is None:
collection = GenericCollection()
if RULES_CONFIG.exception_dir:
collection.load_directory(RULES_CONFIG.exception_dir)
if RULES_CONFIG.action_dir:
collection.load_directory(RULES_CONFIG.action_dir)
if RULES_CONFIG.action_connector_dir:
collection.load_directory(RULES_CONFIG.action_connector_dir)
collection.freeze()
cls.__default = collection
return cls.__default