def mock_audit_log_entry(action: str, diff_text: str) -> AuditLogStore.Entry: return AuditLogStore.Entry(time=0, object_ref=None, user_id="", action=action, text="", diff_text=diff_text)
def test_log_audit_with_object_diff(request_context): old = { "a": "b", "b": "c", } new = { "b": "c", } with on_time("2018-04-15 16:50", "CET"): log_audit( object_ref=None, action="bla", message="Message", user_id=UserId("calvin"), diff_text=make_diff_text(old, new), ) store = AuditLogStore(AuditLogStore.make_path()) assert store.read() == [ AuditLogStore.Entry( time=1523811000, object_ref=None, user_id="calvin", action="bla", text="Message", diff_text='Attribute "a" with value "b" removed.', ), ]
def test_clear(self, store): entry = AuditLogStore.Entry(int(time.time()), None, "user", "action", "Mässädsch", None) store.append(entry) assert list(store.read()) == [entry] store.clear() assert list(store.read()) == [] archive_path = store._path.with_name(store._path.name + time.strftime(".%Y-%m-%d")) assert archive_path.exists()
def _read_pre_2_0_audit_log(self, old_path: Path): with old_path.open(encoding="utf-8") as fp: for line in fp: splitted = line.rstrip().split(None, 4) if len(splitted) == 5 and splitted[0].isdigit(): t, linkinfo, user, action, text = splitted yield AuditLogStore.Entry( time=int(t), object_ref=self._object_ref_from_linkinfo(linkinfo), user_id=user, action=action, text=text, diff_text=None, )
def test__migrate_pre_2_0_audit_log(tmp_path, old_audit_log, new_path): uc = update_config.UpdateConfig(cmk.utils.log.logger, argparse.Namespace()) assert not new_path.exists() assert old_audit_log.exists() uc._migrate_pre_2_0_audit_log() assert new_path.exists() assert not old_audit_log.exists() # Now try to parse the migrated log with the new logic store = AuditLogStore(new_path) assert store.read() == [ AuditLogStore.Entry( time=1604991356, object_ref=None, user_id='cmkadmin', action='liveproxyd-activate', text='Activating changes of Livestatus Proxy configuration', diff_text=None), AuditLogStore.Entry( time=1604991356, object_ref=None, user_id='cmkadmin', action='liveproxyd-activate', text='Activating changes of Livestatus Proxy configuration', diff_text=None), AuditLogStore.Entry(time=1604992040, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id='cmkadmin', action='create-host', text='Created new host heute2.', diff_text=None), AuditLogStore.Entry(time=1604992159, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id='cmkadmin', action='delete-host', text='Deleted host heute2', diff_text=None), AuditLogStore.Entry(time=1604992163, object_ref=ObjectRef(ObjectRefType.Host, "heute1"), user_id='cmkadmin', action='create-host', text='Created new host heute1.', diff_text=None), AuditLogStore.Entry(time=1604992166, object_ref=ObjectRef(ObjectRefType.Host, "heute12"), user_id='cmkadmin', action='create-host', text='Created new host heute12.', diff_text=None), ]
def test_log_audit_with_html_message(request_context): with on_time("2018-04-15 16:50", "CET"): log_audit( object_ref=None, user_id=UserId("calvin"), action="bla", message=HTML("Message <b>bla</b>"), ) store = AuditLogStore(AuditLogStore.make_path()) assert store.read() == [ AuditLogStore.Entry( time=1523811000, object_ref=None, user_id="calvin", action="bla", text=HTML("Message <b>bla</b>"), diff_text=None, ), ]
def test_clear_produced_archive_file_per_clear(self, store): entry = AuditLogStore.Entry(int(time.time()), None, "user", "action", "Mässädsch", None) for n in range(5): store.append(entry) assert list(store.read()) == [entry] store.clear() assert list(store.read()) == [] for archive_num in range(n + 1): archive_path = store._path.with_name( store._path.name + time.strftime(".%Y-%m-%d")) if archive_num != 0: archive_path = archive_path.with_name(archive_path.name + "-%d" % (archive_num + 1)) assert archive_path.exists()
def test_log_audit_with_html_message(register_builtin_html): with on_time('2018-04-15 16:50', 'CET'): log_audit( object_ref=None, user_id=UserId('calvin'), action="bla", message=HTML("Message <b>bla</b>"), ) store = AuditLogStore(AuditLogStore.make_path()) assert store.read() == [ AuditLogStore.Entry( time=1523811000, object_ref=None, user_id='calvin', action='bla', text=HTML("Message <b>bla</b>"), diff_text=None, ), ]
def test__migrate_pre_2_0_audit_log( uc: update_config.UpdateConfig, old_audit_log: Path, new_path: Path, ) -> None: assert not new_path.exists() assert old_audit_log.exists() uc._migrate_pre_2_0_audit_log() assert new_path.exists() assert not old_audit_log.exists() # Now try to parse the migrated log with the new logic log_store = AuditLogStore(new_path) assert log_store.read() == [ AuditLogStore.Entry( time=1604991356, object_ref=None, user_id="cmkadmin", action="liveproxyd-activate", text="Activating changes of Livestatus Proxy configuration", diff_text=None, ), AuditLogStore.Entry( time=1604991356, object_ref=None, user_id="cmkadmin", action="liveproxyd-activate", text="Activating changes of Livestatus Proxy configuration", diff_text=None, ), AuditLogStore.Entry( time=1604992040, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id="cmkadmin", action="create-host", text="Created new host heute2.", diff_text=None, ), AuditLogStore.Entry( time=1604992159, object_ref=ObjectRef(ObjectRefType.Host, "heute2"), user_id="cmkadmin", action="delete-host", text="Deleted host heute2", diff_text=None, ), AuditLogStore.Entry( time=1604992163, object_ref=ObjectRef(ObjectRefType.Host, "heute1"), user_id="cmkadmin", action="create-host", text="Created new host heute1.", diff_text=None, ), AuditLogStore.Entry( time=1604992166, object_ref=ObjectRef(ObjectRefType.Host, "heute12"), user_id="cmkadmin", action="create-host", text="Created new host heute12.", diff_text=None, ), ]
def test_transport_html(self, store, request_context): entry = AuditLogStore.Entry(int(time.time()), None, "user", "action", HTML("Mäss<b>ädsch</b>"), None) store.append(entry) assert list(store.read()) == [entry]
def test_append_multiple(self, store): entry = AuditLogStore.Entry(int(time.time()), None, "user", "action", "Mässädsch", None) store.append(entry) store.append(entry) assert list(store.read()) == [entry, entry]
def test_append(self, store): entry = AuditLogStore.Entry(int(time.time()), "link", "user", "action", "Mässädsch") store.append(entry) assert list(store.read()) == [entry]