org.springframework.boot.actuate.audit.AuditEvent Java Examples

The following examples show how to use org.springframework.boot.actuate.audit.AuditEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomAuditEventRepositoryIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #2
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example #3
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #4
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEvent() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    assertThat(persistentAuditEvent.getData().get("test-key")).isEqualTo("test-value");
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #5
Source File: CustomAuditEventRepositoryIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEvent() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    assertThat(persistentAuditEvent.getData().get("test-key")).isEqualTo("test-value");
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #6
Source File: CustomAuditEventRepositoryIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example #7
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example #8
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #9
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEvent() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    assertThat(persistentAuditEvent.getData().get("test-key")).isEqualTo("test-value");
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #10
Source File: CustomAuditEventRepositoryIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate()).isEqualTo(event.getTimestamp().toInstant());
}
 
Example #11
Source File: CustomAuditEventRepositoryIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddEventWithWebAuthenticationDetails() {
    HttpSession session = new MockHttpSession(null, "test-session-id");
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    request.setRemoteAddr("1.2.3.4");
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", details);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("remoteAddress")).isEqualTo("1.2.3.4");
    assertThat(persistentAuditEvent.getData().get("sessionId")).isEqualTo("test-session-id");
}
 
Example #12
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEvent() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    assertThat(persistentAuditEvent.getData().get("test-key")).isEqualTo("test-value");
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #13
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void addAuditEventTruncateLargeData() {
    Map<String, Object> data = new HashMap<>();
    StringBuilder largeData = new StringBuilder();
    for (int i = 0; i < EVENT_DATA_COLUMN_MAX_LENGTH + 10; i++) {
        largeData.append("a");
    }
    data.put("test-key", largeData);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getPrincipal()).isEqualTo(event.getPrincipal());
    assertThat(persistentAuditEvent.getAuditEventType()).isEqualTo(event.getType());
    assertThat(persistentAuditEvent.getData()).containsKey("test-key");
    String actualData = persistentAuditEvent.getData().get("test-key");
    assertThat(actualData.length()).isEqualTo(EVENT_DATA_COLUMN_MAX_LENGTH);
    assertThat(actualData).isSubstringOf(largeData);
    assertThat(persistentAuditEvent.getAuditEventDate().truncatedTo(ChronoUnit.MILLIS))
        .isEqualTo(event.getTimestamp().truncatedTo(ChronoUnit.MILLIS));
}
 
Example #14
Source File: CustomAuditEventRepositoryIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void findByPrincipalAndType() {
    persistenceAuditEventRepository.save(testUserEvent);
    persistenceAuditEventRepository.save(testOldUserEvent);

    testOtherUserEvent.setAuditEventType(testUserEvent.getAuditEventType());
    persistenceAuditEventRepository.save(testOtherUserEvent);

    PersistentAuditEvent testUserOtherTypeEvent = new PersistentAuditEvent();
    testUserOtherTypeEvent.setPrincipal(testUserEvent.getPrincipal());
    testUserOtherTypeEvent.setAuditEventType("test-other-type");
    testUserOtherTypeEvent.setAuditEventDate(testUserEvent.getAuditEventDate());
    persistenceAuditEventRepository.save(testUserOtherTypeEvent);

    List<AuditEvent> events = customAuditEventRepository.find("test-user",
        Date.from(testUserEvent.getAuditEventDate().minusSeconds(3600)), "test-type");
    assertThat(events).hasSize(1);
    AuditEvent event = events.get(0);
    assertThat(event.getPrincipal()).isEqualTo(testUserEvent.getPrincipal());
    assertThat(event.getType()).isEqualTo(testUserEvent.getAuditEventType());
    assertThat(event.getData()).containsKey("test-key");
    assertThat(event.getData().get("test-key").toString()).isEqualTo("test-value");
    assertThat(event.getTimestamp()).isEqualTo(Date.from(testUserEvent.getAuditEventDate()));
}
 
Example #15
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void addAuditEventWithAuthorizationFailureType() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "AUTHORIZATION_FAILURE", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(0);
}
 
Example #16
Source File: CustomAuditEventRepository.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void add(AuditEvent event) {
    if (!AUTHORIZATION_FAILURE.equals(event.getType()) &&
        !Constants.ANONYMOUS_USER.equals(event.getPrincipal())) {

        PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent();
        persistentAuditEvent.setPrincipal(event.getPrincipal());
        persistentAuditEvent.setAuditEventType(event.getType());
        persistentAuditEvent.setAuditEventDate(event.getTimestamp());
        Map<String, String> eventData = auditEventConverter.convertDataToStrings(event.getData());
        persistentAuditEvent.setData(truncate(eventData));
        persistenceAuditEventRepository.save(persistentAuditEvent);
    }
}
 
Example #17
Source File: AuditResource.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
/**
 * {@code GET /audits} : get a page of {@link AuditEvent}s.
 *
 * @param pageable the pagination information.
 * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of {@link AuditEvent}s in body.
 */
@GetMapping
public ResponseEntity<List<AuditEvent>> getAll(@RequestParam MultiValueMap<String, String> queryParams, UriComponentsBuilder uriBuilder, Pageable pageable) {
    Page<AuditEvent> page = auditEventService.findAll(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(uriBuilder.queryParams(queryParams), page);
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
 
Example #18
Source File: AuditEventConverter.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a list of PersistentAuditEvent to a list of AuditEvent
 *
 * @param persistentAuditEvents the list to convert
 * @return the converted list.
 */
public List<AuditEvent> convertToAuditEvent(Iterable<PersistentAuditEvent> persistentAuditEvents) {
    if (persistentAuditEvents == null) {
        return Collections.emptyList();
    }
    List<AuditEvent> auditEvents = new ArrayList<>();
    for (PersistentAuditEvent persistentAuditEvent : persistentAuditEvents) {
        auditEvents.add(convertToAuditEvent(persistentAuditEvent));
    }
    return auditEvents;
}
 
Example #19
Source File: AuditEventConverter.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a {@link PersistentAuditEvent} to an {@link AuditEvent}.
 *
 * @param persistentAuditEvent the event to convert.
 * @return the converted list.
 */
public AuditEvent convertToAuditEvent(PersistentAuditEvent persistentAuditEvent) {
    if (persistentAuditEvent == null) {
        return null;
    }
    return new AuditEvent(persistentAuditEvent.getAuditEventDate(), persistentAuditEvent.getPrincipal(),
        persistentAuditEvent.getAuditEventType(), convertDataToObjects(persistentAuditEvent.getData()));
}
 
Example #20
Source File: AuditEventConverter.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a list of {@link PersistentAuditEvent}s to a list of {@link AuditEvent}s.
 *
 * @param persistentAuditEvents the list to convert.
 * @return the converted list.
 */
public List<AuditEvent> convertToAuditEvent(Iterable<PersistentAuditEvent> persistentAuditEvents) {
    if (persistentAuditEvents == null) {
        return Collections.emptyList();
    }
    List<AuditEvent> auditEvents = new ArrayList<>();
    for (PersistentAuditEvent persistentAuditEvent : persistentAuditEvents) {
        auditEvents.add(convertToAuditEvent(persistentAuditEvent));
    }
    return auditEvents;
}
 
Example #21
Source File: CustomAuditEventRepositoryIntTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddEventWithNullData() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", null);
    AuditEvent event = new AuditEvent("test-user", "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(1);
    PersistentAuditEvent persistentAuditEvent = persistentAuditEvents.get(0);
    assertThat(persistentAuditEvent.getData().get("test-key")).isEqualTo("null");
}
 
Example #22
Source File: AuditEventConverter.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a list of PersistentAuditEvent to a list of AuditEvent
 *
 * @param persistentAuditEvents the list to convert
 * @return the converted list.
 */
public List<AuditEvent> convertToAuditEvent(Iterable<PersistentAuditEvent> persistentAuditEvents) {
    if (persistentAuditEvents == null) {
        return Collections.emptyList();
    }
    List<AuditEvent> auditEvents = new ArrayList<>();
    for (PersistentAuditEvent persistentAuditEvent : persistentAuditEvents) {
        auditEvents.add(convertToAuditEvent(persistentAuditEvent));
    }
    return auditEvents;
}
 
Example #23
Source File: AuditResource.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * GET /audits : get a page of AuditEvents.
 *
 * @param pageable the pagination information
 * @return the ResponseEntity with status 200 (OK) and the list of AuditEvents in body
 */
@GetMapping
public ResponseEntity<List<AuditEvent>> getAll(Pageable pageable) {
    Page<AuditEvent> page = auditEventService.findAll(pageable);
    HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/management/audits");
    return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
}
 
Example #24
Source File: CustomAuditEventRepositoryIntTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAfter() {
    persistenceAuditEventRepository.save(testUserEvent);
    persistenceAuditEventRepository.save(testOldUserEvent);

    List<AuditEvent> events =
        customAuditEventRepository.find(Date.from(testUserEvent.getAuditEventDate().minusSeconds(3600)));
    assertThat(events).hasSize(1);
    AuditEvent event = events.get(0);
    assertThat(event.getPrincipal()).isEqualTo(testUserEvent.getPrincipal());
    assertThat(event.getType()).isEqualTo(testUserEvent.getAuditEventType());
    assertThat(event.getData()).containsKey("test-key");
    assertThat(event.getData().get("test-key").toString()).isEqualTo("test-value");
    assertThat(event.getTimestamp()).isEqualTo(Date.from(testUserEvent.getAuditEventDate()));
}
 
Example #25
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void addAuditEventWithAuthorizationFailureType() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent("test-user", "AUTHORIZATION_FAILURE", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(0);
}
 
Example #26
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void addAuditEventWithAnonymousUser() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent(Constants.ANONYMOUS_USER, "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(0);
}
 
Example #27
Source File: CustomAuditEventRepository.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
public List<AuditEvent> find(String principal, Date after) {
    Iterable<PersistentAuditEvent> persistentAuditEvents;
    if (principal == null && after == null) {
        persistentAuditEvents = persistenceAuditEventRepository.findAll();
    } else if (after == null) {
        persistentAuditEvents = persistenceAuditEventRepository.findByPrincipal(principal);
    } else {
        persistentAuditEvents =
            persistenceAuditEventRepository.findByPrincipalAndAuditEventDateAfter(principal, after.toInstant());
    }
    return auditEventConverter.convertToAuditEvent(persistentAuditEvents);
}
 
Example #28
Source File: AuditEventConverter.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a PersistentAuditEvent to an AuditEvent
 *
 * @param persistentAuditEvent the event to convert
 * @return the converted list.
 */
public AuditEvent convertToAuditEvent(PersistentAuditEvent persistentAuditEvent) {
    if (persistentAuditEvent == null) {
        return null;
    }
    return new AuditEvent(Date.from(persistentAuditEvent.getAuditEventDate()), persistentAuditEvent.getPrincipal(),
        persistentAuditEvent.getAuditEventType(), convertDataToObjects(persistentAuditEvent.getData()));
}
 
Example #29
Source File: CustomAuditEventRepository.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void add(AuditEvent event) {
    if (!AUTHORIZATION_FAILURE.equals(event.getType()) &&
        !Constants.ANONYMOUS_USER.equals(event.getPrincipal())) {

        PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent();
        persistentAuditEvent.setPrincipal(event.getPrincipal());
        persistentAuditEvent.setAuditEventType(event.getType());
        persistentAuditEvent.setAuditEventDate(event.getTimestamp().toInstant());
        Map<String, String> eventData = auditEventConverter.convertDataToStrings(event.getData());
        persistentAuditEvent.setData(truncate(eventData));
        persistenceAuditEventRepository.save(persistentAuditEvent);
    }
}
 
Example #30
Source File: CustomAuditEventRepositoryIT.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void addAuditEventWithAnonymousUser() {
    Map<String, Object> data = new HashMap<>();
    data.put("test-key", "test-value");
    AuditEvent event = new AuditEvent(Constants.ANONYMOUS_USER, "test-type", data);
    customAuditEventRepository.add(event);
    List<PersistentAuditEvent> persistentAuditEvents = persistenceAuditEventRepository.findAll();
    assertThat(persistentAuditEvents).hasSize(0);
}