diff --git a/linode_api4/groups/monitor.py b/linode_api4/groups/monitor.py index f3c29e36a..acb898111 100644 --- a/linode_api4/groups/monitor.py +++ b/linode_api4/groups/monitor.py @@ -20,6 +20,7 @@ ) from linode_api4.objects.monitor import ( AkamaiObjectStorageLogsDestinationDetails, + AuditLogsStreamDetails, CustomHTTPSLogsDestinationDetails, LogsStreamDetails, LogsStreamQuota, @@ -557,7 +558,9 @@ def stream_create( label: str, type: Union[LogsStreamType, str], status: Optional[Union[LogsStreamStatus, str]] = None, - details: Optional[LogsStreamDetails] = None, + details: Optional[ + Union[LogsStreamDetails, AuditLogsStreamDetails] + ] = None, ) -> LogsStream: """ Creates a new :any:`LogsStream` for logs on this account. For example:: @@ -572,6 +575,16 @@ def stream_create( type="audit_logs" ) + # audit_logs stream with data events + data_stream = client.monitor.stream_create( + destinations=[1234], + label="Data_events_logs", + type="audit_logs", + details=AuditLogsStreamDetails( + sub_types=["data_events"], + ) + ) + # lke_audit_logs stream with specific clusters lke_stream = client.monitor.stream_create( destinations=[1234], @@ -597,9 +610,12 @@ def stream_create( :param status: (Optional) The availability status of the stream. Possible values are: ``active``, ``inactive``. Defaults to ``active``. :type status: str - :param details: (Optional) Additional stream details. Only applicable for - ``lke_audit_logs`` streams. Omit for ``audit_logs`` streams. - :type details: LogsStreamDetails + :param details: (Optional) Use AuditLogsStreamDetails to select subtypes + for ``audit_logs``, or LogsStreamDetails for + ``lke_audit_logs``. For audit logs, the API defaults to + ``management_events`` and ``login_events`` when subtypes + are omitted or empty. + :type details: LogsStreamDetails or AuditLogsStreamDetails :returns: The newly created logs stream. :rtype: LogsStream diff --git a/linode_api4/objects/monitor.py b/linode_api4/objects/monitor.py index 2a42b3eca..c7b62a383 100644 --- a/linode_api4/objects/monitor.py +++ b/linode_api4/objects/monitor.py @@ -21,6 +21,7 @@ "RuleCriteria", "TriggerConditions", "AkamaiObjectStorageLogsDestinationDetails", + "AuditLogsStreamDetails", "AuthenticationType", "BasicAuthenticationDetails", "ClientCertificateDetails", @@ -774,7 +775,7 @@ class LogsStreamDetails(JSONObject): Additional details for a logs stream. This object only applies to streams with a ``type`` of ``lke_audit_logs``. - Leave it out of requests that use a ``type`` of ``audit_logs``. + For ``audit_logs`` subtypes, use :any:`AuditLogsStreamDetails` instead. .. note:: When updating a stream, any existing settings need to be included to @@ -794,6 +795,34 @@ class LogsStreamDetails(JSONObject): cluster_ids: Optional[List[int]] = None is_auto_add_all_clusters_enabled: bool = False + @classmethod + def from_json( + cls, json: Dict[str, Any] + ) -> Optional[Union["LogsStreamDetails", "AuditLogsStreamDetails"]]: + if json is not None and "sub_types" in json: + return AuditLogsStreamDetails.from_json(json) + + return super().from_json(json) + + +@dataclass +class AuditLogsStreamDetails(JSONObject): + """ + Details for streams with a ``type`` of ``audit_logs``. + + :param sub_types: Optional selection of ``management_events``, + ``login_events``, and ``data_events`` subtypes. When creating a stream, + omitting this field or providing an empty list lets the API select + ``management_events`` and ``login_events``. When updating a stream, a + non-empty list replaces the selection; omitting this field or providing + an empty list preserves it. The API returns subtypes in alphabetical + order. Selecting a disabled subtype returns HTTP 400, unless the stream + already uses that subtype. + :type sub_types: Optional[List[str]] + """ + + sub_types: Optional[List[str]] = None + @dataclass class LogsStreamDestination(JSONObject): diff --git a/test/unit/objects/monitor_test.py b/test/unit/objects/monitor_test.py index 5c42d2aa4..5a45f2d2b 100644 --- a/test/unit/objects/monitor_test.py +++ b/test/unit/objects/monitor_test.py @@ -3,10 +3,12 @@ from linode_api4.objects import ( AlertChannel, + AuditLogsStreamDetails, LogsDestination, LogsDestinationHistory, LogsStream, LogsStreamDestination, + LogsStreamHistory, MonitorDashboard, MonitorService, ) @@ -795,3 +797,120 @@ def test_create_audit_logs_stream_omits_details(self): ) self.assertNotIn("details", m.call_data) + + +class AuditLogsStreamTest(ClientBaseCase): + def test_create_audit_logs_stream_sub_types(self): + for sub_types in ( + ["management_events"], + ["login_events"], + ["data_events"], + ["data_events", "login_events", "management_events"], + [], + None, + ): + with self.subTest(sub_types=sub_types): + requested_details = ( + {} if sub_types is None else {"sub_types": sub_types} + ) + response_sub_types = sub_types or [ + "login_events", + "management_events", + ] + response = { + "id": 5, + "type": "audit_logs", + "details": {"sub_types": response_sub_types}, + } + + with self.mock_post(response) as mock: + stream = self.client.monitor.stream_create( + destinations=[1], + label="Audit stream", + type=LogsStreamType.audit_logs, + details=AuditLogsStreamDetails(sub_types=sub_types), + ) + + self.assertEqual(mock.call_url, "/monitor/streams") + self.assertEqual( + mock.call_data, + { + "destinations": [1], + "label": "Audit stream", + "type": "audit_logs", + "details": requested_details, + }, + ) + self.assertIsInstance(stream.details, AuditLogsStreamDetails) + self.assertEqual(stream.details.sub_types, response_sub_types) + + def test_create_audit_logs_stream_defaults_from_api(self): + response = { + "id": 5, + "type": "audit_logs", + "details": {"sub_types": ["login_events", "management_events"]}, + } + with self.mock_post(response) as mock: + stream = self.client.monitor.stream_create( + destinations=[1], + label="Audit stream", + type="audit_logs", + ) + + self.assertNotIn("details", mock.call_data) + self.assertIsInstance(stream.details, AuditLogsStreamDetails) + self.assertEqual( + stream.details.sub_types, ["login_events", "management_events"] + ) + + def test_load_audit_logs_stream_and_history_sub_types(self): + for model in (LogsStream, LogsStreamHistory): + with self.subTest(model=model.__name__): + stream = model( + self.client, + 5, + { + "id": 5, + "type": "audit_logs", + "details": { + "sub_types": [ + "data_events", + "login_events", + "management_events", + ], + }, + }, + ) + + self.assertIsInstance(stream.details, AuditLogsStreamDetails) + self.assertEqual( + stream.details.sub_types, + ["data_events", "login_events", "management_events"], + ) + + def test_update_audit_logs_stream_sub_types(self): + stream = LogsStream( + self.client, + 5, + { + "id": 5, + "label": "Audit stream", + "status": "active", + "type": "audit_logs", + "details": {"sub_types": ["login_events", "management_events"]}, + }, + ) + stream.details = AuditLogsStreamDetails(sub_types=["data_events"]) + + with self.mock_put({}) as mock: + self.assertTrue(stream.save()) + + self.assertEqual(mock.call_url, "/monitor/streams/5") + self.assertEqual( + mock.call_data, + { + "label": "Audit stream", + "status": "active", + "details": {"sub_types": ["data_events"]}, + }, + )