|
31 | 31 | from _incydr_sdk.enums.file_events import RiskIndicators |
32 | 32 | from _incydr_sdk.enums.file_events import RiskSeverity |
33 | 33 | from _incydr_sdk.file_events.models.event import FileEventV2 |
| 34 | +from _incydr_sdk.file_events.models.response import FileEventGroup |
34 | 35 | from _incydr_sdk.file_events.models.response import SavedSearch |
35 | 36 | from _incydr_sdk.queries.file_events import EventQuery |
| 37 | +from _incydr_sdk.queries.file_events import GroupingEventQuery |
36 | 38 | from _incydr_sdk.utils import model_as_card |
37 | 39 |
|
38 | 40 |
|
@@ -100,14 +102,15 @@ def search( |
100 | 102 | elif advanced_query: |
101 | 103 | if not isinstance(advanced_query, str): |
102 | 104 | advanced_query = advanced_query.read() |
103 | | - query = EventQuery.parse_raw(advanced_query) |
| 105 | + query = EventQuery.model_validate_json(advanced_query) |
104 | 106 | else: |
105 | 107 | if not start: |
106 | 108 | raise BadOptionUsage( |
107 | 109 | "start", |
108 | 110 | "--start option required if not using --saved-search or --advanced-query options.", |
109 | 111 | ) |
110 | 112 | query = _create_query( |
| 113 | + cls=EventQuery, |
111 | 114 | start=start, |
112 | 115 | end=end, |
113 | 116 | event_action=event_action, |
@@ -191,6 +194,115 @@ def yield_all_events(q: EventQuery): |
191 | 194 | console.print("No results found.") |
192 | 195 |
|
193 | 196 |
|
| 197 | +@file_events.command(cls=IncydrCommand) |
| 198 | +@click.option( |
| 199 | + "--group-by", |
| 200 | + default=None, |
| 201 | + help="(required) The term by which approximate counts will be grouped. Example: `user.email`.", |
| 202 | + required=True, |
| 203 | +) |
| 204 | +@table_format_option |
| 205 | +@columns_option |
| 206 | +@output_options |
| 207 | +@advanced_query_option |
| 208 | +@saved_search_option |
| 209 | +@event_filter_options |
| 210 | +@logging_options |
| 211 | +def search_groups( |
| 212 | + format_: TableFormat, |
| 213 | + columns: Optional[str], |
| 214 | + output: Optional[str], |
| 215 | + certs: Optional[str], |
| 216 | + ignore_cert_validation: Optional[bool], |
| 217 | + advanced_query: Optional[Union[str, File]], |
| 218 | + saved_search: Optional[str], |
| 219 | + start: Optional[str], |
| 220 | + end: Optional[str], |
| 221 | + event_action: Optional[str], |
| 222 | + username: Optional[str], |
| 223 | + md5: Optional[str], |
| 224 | + sha256: Optional[str], |
| 225 | + source_category: Optional[str], |
| 226 | + destination_category: Optional[str], |
| 227 | + file_name: Optional[str], |
| 228 | + file_directory: Optional[str], |
| 229 | + file_category: Optional[str], |
| 230 | + risk_indicator: Optional[RiskIndicators], |
| 231 | + risk_severity: Optional[RiskSeverity], |
| 232 | + risk_score: Optional[int], |
| 233 | + group_by: Optional[str], |
| 234 | +): |
| 235 | + """ |
| 236 | + Retrieve approximate aggregated file event counts. Various options are provided to filter query results. |
| 237 | +
|
| 238 | + Use the `--saved-search` or the `--advanced-query` option if the available filters don't satisfy your requirements. |
| 239 | +
|
| 240 | + Results will be output to the console by default, use the `--output` option to send data to a server. |
| 241 | +
|
| 242 | + This method returns approximate counts, grouped by the provided term. To obtain full event details, use the `search` method. |
| 243 | + """ |
| 244 | + if output: |
| 245 | + format_ = TableFormat.json_lines |
| 246 | + |
| 247 | + client = Client() |
| 248 | + |
| 249 | + if saved_search: |
| 250 | + saved_search = client.file_events.v2.get_saved_search(saved_search) |
| 251 | + query = GroupingEventQuery.from_saved_search(saved_search) |
| 252 | + elif advanced_query: |
| 253 | + if not isinstance(advanced_query, str): |
| 254 | + advanced_query = advanced_query.read() |
| 255 | + query = GroupingEventQuery.model_validate_json(advanced_query) |
| 256 | + else: |
| 257 | + if not start: |
| 258 | + raise BadOptionUsage( |
| 259 | + "start", |
| 260 | + "--start option required if not using --saved-search or --advanced-query options.", |
| 261 | + ) |
| 262 | + query = _create_query( |
| 263 | + cls=GroupingEventQuery, |
| 264 | + start=start, |
| 265 | + end=end, |
| 266 | + event_action=event_action, |
| 267 | + username=username, |
| 268 | + md5=md5, |
| 269 | + sha256=sha256, |
| 270 | + source_category=source_category, |
| 271 | + destination_category=destination_category, |
| 272 | + file_name=file_name, |
| 273 | + file_directory=file_directory, |
| 274 | + file_category=file_category, |
| 275 | + risk_indicator=risk_indicator, |
| 276 | + risk_severity=risk_severity, |
| 277 | + risk_score=risk_score, |
| 278 | + ) |
| 279 | + |
| 280 | + query.group_by(group_by).maximum_size(10000) |
| 281 | + |
| 282 | + groups = client.file_events.v2.search_groups(query).groups or [] |
| 283 | + |
| 284 | + if output: |
| 285 | + logger = get_server_logger(output, certs, ignore_cert_validation) |
| 286 | + for group in groups: |
| 287 | + logger.info(json.dumps(group.dict())) |
| 288 | + return |
| 289 | + |
| 290 | + if format_ == TableFormat.csv: |
| 291 | + render.csv(FileEventGroup, groups, columns=columns, flat=True) |
| 292 | + elif format_ == TableFormat.table: |
| 293 | + render.table(FileEventGroup, groups, columns=columns, flat=False) |
| 294 | + else: |
| 295 | + printed = False |
| 296 | + for group in groups: |
| 297 | + printed = True |
| 298 | + if format_ == TableFormat.json_pretty: |
| 299 | + console.print_json(data=group) |
| 300 | + else: |
| 301 | + click.echo(json.dumps(group.dict())) |
| 302 | + if not printed: |
| 303 | + console.print("No results found.") |
| 304 | + |
| 305 | + |
194 | 306 | @file_events.command() |
195 | 307 | @click.argument("checkpoint-name") |
196 | 308 | def clear_checkpoint(checkpoint_name: str): |
@@ -262,8 +374,8 @@ def list_saved_searches( |
262 | 374 | } |
263 | 375 |
|
264 | 376 |
|
265 | | -def _create_query(**kwargs): |
266 | | - query = EventQuery(start_date=kwargs["start"], end_date=kwargs["end"]) |
| 377 | +def _create_query(cls, **kwargs): |
| 378 | + query = cls(start_date=kwargs["start"], end_date=kwargs["end"]) |
267 | 379 | for k, v in kwargs.items(): |
268 | 380 | if v: |
269 | 381 | if k in ["start", "end"]: |
|
0 commit comments