|
| 1 | +from datetime import datetime |
| 2 | +from typing import List |
| 3 | + |
1 | 4 | from cement import Controller, ex |
2 | 5 |
|
| 6 | +from banyan.api.event_v2 import EventV2API |
| 7 | +from banyan.model.event_v2 import EventV2, EventV2Type, EventV2Subtype |
| 8 | + |
3 | 9 |
|
4 | | -class EventV1Controller(Controller): |
| 10 | +class EventV2Controller(Controller): |
5 | 11 | class Meta: |
6 | 12 | label = 'event' |
7 | 13 | stacked_type = 'nested' |
8 | 14 | stacked_on = 'base' |
9 | 15 | help = 'report on security and audit events' |
10 | 16 |
|
11 | | - # @property |
12 | | - # def _client(self) -> RoleAPI: |
13 | | - # return self.app.client.roles |
| 17 | + @property |
| 18 | + def _client(self) -> EventV2API: |
| 19 | + return self.app.client.events |
14 | 20 |
|
15 | | - # TODO: implement /security_events |
16 | | - @ex(help='list events') |
| 21 | + @ex(help='list events', |
| 22 | + arguments=[ |
| 23 | + (['--type'], |
| 24 | + { |
| 25 | + 'help': 'Filters events of one event type.', |
| 26 | + 'choices': EventV2Type.choices(), |
| 27 | + }), |
| 28 | + (['--subtype'], |
| 29 | + { |
| 30 | + 'help': 'Filters events of one subtype (conditionally depends on type).', |
| 31 | + 'choices': EventV2Subtype.choices(), |
| 32 | + }), |
| 33 | + (['--action'], |
| 34 | + { |
| 35 | + 'help': 'Filters events by one of the possible event action values (e.g. "grant", "deny").', |
| 36 | + }), |
| 37 | + (['--email'], |
| 38 | + { |
| 39 | + 'help': 'Filters events associated with a single user, based on their email address.', |
| 40 | + }), |
| 41 | + (['--device-id'], |
| 42 | + { |
| 43 | + 'help': 'Filters events associated with a single device, based on its device ID.', |
| 44 | + }), |
| 45 | + (['--serial-number'], |
| 46 | + { |
| 47 | + 'help': 'Filters events associated with a single device, based on its serial number.', |
| 48 | + }), |
| 49 | + (['--container-id'], |
| 50 | + { |
| 51 | + 'help': 'Filters events associated with a single workload, based on its container ID.', |
| 52 | + }), |
| 53 | + (['--service-name'], |
| 54 | + { |
| 55 | + 'help': 'Filters events associated with a single service, based on its service name.', |
| 56 | + }), |
| 57 | + (['--event-id'], |
| 58 | + { |
| 59 | + 'help': 'Retrieve a single event based on its event ID.', |
| 60 | + }), |
| 61 | + (['--before'], |
| 62 | + { |
| 63 | + 'help': 'Filters events that occurred before a specific time.', |
| 64 | + 'type': datetime.fromisoformat, |
| 65 | + }), |
| 66 | + (['--after'], |
| 67 | + { |
| 68 | + 'help': 'Filters events that occurred after a specific time.', |
| 69 | + 'type': datetime.fromisoformat, |
| 70 | + }), |
| 71 | + (['--order'], |
| 72 | + { |
| 73 | + 'help': 'Sets the order for returned events based on created_at timestamp. Supported values ' |
| 74 | + 'ASC, DESC. Default is DESC.', |
| 75 | + 'choices': ('ASC', 'DESC') |
| 76 | + }), |
| 77 | + ] |
| 78 | + ) |
17 | 79 | def list(self): |
18 | | - pass |
19 | | - |
20 | | - # TODO: implement /security_events_type_count |
21 | | - @ex(help='show summary of events in database') |
22 | | - def summary(self): |
23 | | - pass |
| 80 | + events: List[EventV2] = self._client.list(before_dt=self.app.pargs.before, after_dt=self.app.pargs.after, |
| 81 | + order=self.app.pargs.order, event_type=self.app.pargs.type, |
| 82 | + subtype=self.app.pargs.subtype, |
| 83 | + action=self.app.pargs.action, email_address=self.app.pargs.email, |
| 84 | + device_id=self.app.pargs.device_id, |
| 85 | + device_serial=self.app.pargs.serial_number, |
| 86 | + container_id=self.app.pargs.container_id, |
| 87 | + service_name=self.app.pargs.service_name, |
| 88 | + event_id=self.app.pargs.event_id) |
| 89 | + event_json = EventV2.Schema().dump(events, many=True) |
| 90 | + self.app.render(event_json, handler='json', indent=2, sort_keys=True) |
0 commit comments