feat(audit): add quarkus-audit-tools module
All checks were successful
Build / build (pull_request) Successful in 1m57s
All checks were successful
Build / build (pull_request) Successful in 1m57s
This commit is contained in:
parent
db0026b723
commit
f268c4a27a
18 changed files with 834 additions and 0 deletions
1
pom.xml
1
pom.xml
|
@ -7,6 +7,7 @@
|
|||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>quarkus-audit-tools</module>
|
||||
<module>quarkus-clock-service</module>
|
||||
<module>quarkus-json-service</module>
|
||||
<module>quarkus-message-digest-service</module>
|
||||
|
|
88
quarkus-audit-tools/pom.xml
Normal file
88
quarkus-audit-tools/pom.xml
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>ch.phoenix.oss</groupId>
|
||||
<artifactId>quarkus-commons</artifactId>
|
||||
<version>1.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>quarkus-audit-tools</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ch.phoenix.oss</groupId>
|
||||
<artifactId>quarkus-tracing-service</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-hibernate-envers</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-hibernate-orm-panache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-postgresql</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-flyway</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-flyway-postgresql</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-elytron-security-properties-file</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-config-yaml</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- <build>-->
|
||||
<!-- <plugins>-->
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.jacoco</groupId>-->
|
||||
<!-- <artifactId>jacoco-maven-plugin</artifactId>-->
|
||||
<!-- <version>${jacoco-plugin.version}</version>-->
|
||||
<!-- <executions>-->
|
||||
<!-- <execution>-->
|
||||
<!-- <id>jacoco-check</id>-->
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>check</goal>-->
|
||||
<!-- </goals>-->
|
||||
<!-- <phase>test</phase>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <dataFile>${project.build.directory}/jacoco-quarkus.exec</dataFile>-->
|
||||
<!-- <rules>-->
|
||||
<!-- <rule>-->
|
||||
<!-- <element>BUNDLE</element>-->
|
||||
<!-- <limits>-->
|
||||
<!-- <limit>-->
|
||||
<!-- <counter>INSTRUCTION</counter>-->
|
||||
<!-- <value>COVEREDRATIO</value>-->
|
||||
<!-- <minimum>1</minimum>-->
|
||||
<!-- </limit>-->
|
||||
<!-- </limits>-->
|
||||
<!-- </rule>-->
|
||||
<!-- </rules>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </execution>-->
|
||||
<!-- </executions>-->
|
||||
<!-- </plugin>-->
|
||||
<!-- </plugins>-->
|
||||
<!-- </build>-->
|
||||
|
||||
</project>
|
|
@ -0,0 +1,20 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import jakarta.enterprise.inject.spi.CDI;
|
||||
import org.hibernate.envers.RevisionListener;
|
||||
|
||||
public class AuditRevisionListener implements RevisionListener {
|
||||
|
||||
@Override
|
||||
public void newRevision(Object revisionEntity) {
|
||||
var provider = CDI.current().select(RevisionContextProvider.class).get();
|
||||
|
||||
var rev = (Revision) revisionEntity;
|
||||
rev.actor = provider.getActor();
|
||||
rev.traceId = provider.getTraceId();
|
||||
rev.spanId = provider.getSpanId();
|
||||
rev.requestId = provider.getRequestId();
|
||||
rev.clientIp = provider.getClientIp();
|
||||
rev.hostName = provider.getHostName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import java.time.Instant;
|
||||
import org.hibernate.envers.NotAudited;
|
||||
|
||||
/**
|
||||
* The goal of this class is to have on the entity itself the exact same
|
||||
* timestamps as the ones from revisions generated by envers. Because of that,
|
||||
* we can't use @CreationTimestamp and @UpdateTimestamp, as those timestamp values
|
||||
* are managed by different Hibernate classes, so the generated values will drift.
|
||||
* <p>
|
||||
* Manually setting these values to match envers revisions would be error-prone,
|
||||
* verbose and tedious. So, the recommendation is to implement triggers on the
|
||||
* audit tables which will update the main entity whenever a revision is created.
|
||||
* An example of how to do that can be found in this module's integration tests.
|
||||
*/
|
||||
@MappedSuperclass
|
||||
public abstract class AuditedPanacheEntity extends PanacheEntity {
|
||||
|
||||
@NotAudited
|
||||
@Column(updatable = false)
|
||||
public Instant createdAt;
|
||||
|
||||
@NotAudited
|
||||
@Column(updatable = false)
|
||||
public Instant lastUpdatedAt;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import java.time.Instant;
|
||||
import org.hibernate.envers.NotAudited;
|
||||
|
||||
/**
|
||||
* The goal of this class is to have on the entity itself the exact same
|
||||
* timestamps as the ones from revisions generated by envers. Because of that,
|
||||
* we can't use @CreationTimestamp and @UpdateTimestamp, as those timestamp values
|
||||
* are managed by different Hibernate classes, so the generated values will drift.
|
||||
* <p>
|
||||
* Manually setting these values to match envers revisions would be error-prone,
|
||||
* verbose and tedious. So, the recommendation is to implement triggers on the
|
||||
* audit tables which will update the main entity whenever a revision is created.
|
||||
* An example of how to do that can be found in this module's integration tests.
|
||||
*/
|
||||
@MappedSuperclass
|
||||
public abstract class AuditedPanacheEntityBase extends PanacheEntityBase {
|
||||
|
||||
@NotAudited
|
||||
@Column(updatable = false)
|
||||
public Instant createdAt;
|
||||
|
||||
@NotAudited
|
||||
@Column(updatable = false)
|
||||
public Instant lastUpdatedAt;
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import ch.phoenix.oss.quarkus.commons.tracing.TracingService;
|
||||
import io.opentelemetry.instrumentation.annotations.WithSpan;
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import io.quarkus.arc.Unremovable;
|
||||
import io.quarkus.logging.Log;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Unremovable
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
class DefaultRevisionContextProvider implements RevisionContextProvider {
|
||||
|
||||
private static final String UNKNOWN = "unknown";
|
||||
|
||||
private final TracingService tracingService;
|
||||
|
||||
@Inject
|
||||
DefaultRevisionContextProvider(TracingService tracingService) {
|
||||
this.tracingService = tracingService;
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getActor() {
|
||||
return tracingService.getActor();
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getTraceId() {
|
||||
return tracingService.getTraceId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getSpanId() {
|
||||
return tracingService.getSpanId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getRequestId() {
|
||||
return tracingService.getRequestId();
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getClientIp() {
|
||||
return tracingService.getClientIp();
|
||||
}
|
||||
|
||||
@Override
|
||||
@WithSpan
|
||||
public String getHostName() {
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostName();
|
||||
} catch (UnknownHostException e) {
|
||||
Log.error("Unable to determine host name", e);
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
|
||||
import jakarta.persistence.*;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import org.hibernate.envers.RevisionEntity;
|
||||
import org.hibernate.envers.RevisionNumber;
|
||||
import org.hibernate.envers.RevisionTimestamp;
|
||||
|
||||
@Entity
|
||||
@Table(name = "revinfo")
|
||||
@RevisionEntity(AuditRevisionListener.class)
|
||||
public class Revision extends PanacheEntityBase {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@RevisionNumber
|
||||
public long rev;
|
||||
|
||||
@RevisionTimestamp
|
||||
@Column(nullable = false, updatable = false)
|
||||
public Instant timestamp;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String actor;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String traceId;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String spanId;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String requestId;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String clientIp;
|
||||
|
||||
@Column(updatable = false)
|
||||
public String hostName;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (!(o instanceof Revision that)) return false;
|
||||
return rev == that.rev;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(rev);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Revision{rev=" + rev + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
public interface RevisionContextProvider {
|
||||
|
||||
String getActor();
|
||||
|
||||
String getTraceId();
|
||||
|
||||
String getSpanId();
|
||||
|
||||
String getRequestId();
|
||||
|
||||
String getClientIp();
|
||||
|
||||
String getHostName();
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import ch.phoenix.oss.quarkus.commons.tracing.TracingService;
|
||||
import io.quarkus.narayana.jta.QuarkusTransaction;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import java.time.Instant;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class AuditedPanacheEntityBaseTest {
|
||||
|
||||
@Inject
|
||||
EntityManager entityManager;
|
||||
|
||||
@InjectSpy
|
||||
TracingService tracingService;
|
||||
|
||||
@SuppressWarnings("Convert2MethodRef")
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
QuarkusTransaction.requiringNew().run(() -> TestEntity2.deleteAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistAndUpdate() {
|
||||
when(tracingService.getRequestId()).thenReturn("00000000-0000-0000-0000-000000000000");
|
||||
when(tracingService.getSpanId()).thenReturn("0000000000000000");
|
||||
when(tracingService.getTraceId()).thenReturn("00000000000000000000000000000000");
|
||||
when(tracingService.getActor()).thenReturn("unknown");
|
||||
when(tracingService.getClientIp()).thenReturn("unknown");
|
||||
|
||||
var now = Instant.now();
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = new TestEntity2("something");
|
||||
entity.persist();
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity2.findBySomething("something");
|
||||
assertAll(
|
||||
() -> assertThat(entity.createdAt)
|
||||
.as("createdAt should be after or equal to expected value")
|
||||
.isAfterOrEqualTo(now),
|
||||
() -> assertThat(entity.lastUpdatedAt)
|
||||
.as("lastUpdatedAt should be equal to createdAt")
|
||||
.isEqualTo(entity.createdAt));
|
||||
|
||||
var auditReader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
var revisions = auditReader.getRevisions(TestEntity2.class, entity.id);
|
||||
assertThat(revisions).hasSize(1);
|
||||
|
||||
var revInfo = entityManager.find(Revision.class, revisions.getFirst());
|
||||
assertThat(revInfo).isNotNull();
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should be equal to entity's createdAt timestamp")
|
||||
.isEqualTo(entity.createdAt),
|
||||
() -> assertThat(revInfo.actor)
|
||||
.as("actor should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.traceId)
|
||||
.as("traceId should match expected value")
|
||||
.isEqualTo("00000000000000000000000000000000"),
|
||||
() -> assertThat(revInfo.spanId)
|
||||
.as("spanId should match expected value")
|
||||
.isEqualTo("0000000000000000"),
|
||||
() -> assertThat(revInfo.requestId)
|
||||
.as("requestId should match expected value")
|
||||
.isEqualTo("00000000-0000-0000-0000-000000000000"),
|
||||
() -> assertThat(revInfo.clientIp)
|
||||
.as("clientIp should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.hostName)
|
||||
.as("hostName should not be blank")
|
||||
.isNotBlank());
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity2.findBySomething("something");
|
||||
entity.something = "else";
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity2.findBySomething("else");
|
||||
assertAll(() -> assertThat(entity.createdAt)
|
||||
.as("createdAt should be before lastUpdatedAt")
|
||||
.isBefore(entity.lastUpdatedAt));
|
||||
|
||||
var auditReader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
var revisions = auditReader.getRevisions(TestEntity2.class, entity.id);
|
||||
assertThat(revisions).hasSize(2);
|
||||
|
||||
Revision revInfo = Revision.findById(revisions.getLast());
|
||||
assertThat(revInfo).isNotNull();
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should not be equal to entity's createdAt")
|
||||
.isNotEqualTo(entity.createdAt),
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should be equal to entity's lastUpdatedAt")
|
||||
.isEqualTo(entity.lastUpdatedAt),
|
||||
() -> assertThat(revInfo.actor)
|
||||
.as("actor should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.traceId)
|
||||
.as("traceId should match expected value")
|
||||
.isEqualTo("00000000000000000000000000000000"),
|
||||
() -> assertThat(revInfo.spanId)
|
||||
.as("spanId should match expected value")
|
||||
.isEqualTo("0000000000000000"),
|
||||
() -> assertThat(revInfo.requestId)
|
||||
.as("requestId should match expected value")
|
||||
.isEqualTo("00000000-0000-0000-0000-000000000000"),
|
||||
() -> assertThat(revInfo.clientIp)
|
||||
.as("clientIp should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.hostName)
|
||||
.as("hostName should not be blank")
|
||||
.isNotBlank());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import ch.phoenix.oss.quarkus.commons.tracing.TracingService;
|
||||
import io.quarkus.narayana.jta.QuarkusTransaction;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import java.time.Instant;
|
||||
import org.hibernate.envers.AuditReaderFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class AuditedPanacheEntityTest {
|
||||
|
||||
@Inject
|
||||
EntityManager entityManager;
|
||||
|
||||
@InjectSpy
|
||||
TracingService tracingService;
|
||||
|
||||
@SuppressWarnings("Convert2MethodRef")
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
QuarkusTransaction.requiringNew().run(() -> TestEntity.deleteAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
void persistAndUpdate() {
|
||||
when(tracingService.getRequestId()).thenReturn("00000000-0000-0000-0000-000000000000");
|
||||
when(tracingService.getSpanId()).thenReturn("0000000000000000");
|
||||
when(tracingService.getTraceId()).thenReturn("00000000000000000000000000000000");
|
||||
when(tracingService.getActor()).thenReturn("unknown");
|
||||
when(tracingService.getClientIp()).thenReturn("unknown");
|
||||
|
||||
var now = Instant.now();
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = new TestEntity("something");
|
||||
entity.persist();
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity.findBySomething("something");
|
||||
assertAll(
|
||||
() -> assertThat(entity.createdAt)
|
||||
.as("createdAt should be after or equal to expected value")
|
||||
.isAfterOrEqualTo(now),
|
||||
() -> assertThat(entity.lastUpdatedAt)
|
||||
.as("lastUpdatedAt should be equal to createdAt")
|
||||
.isEqualTo(entity.createdAt));
|
||||
|
||||
var auditReader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
var revisions = auditReader.getRevisions(TestEntity.class, entity.id);
|
||||
assertThat(revisions).hasSize(1);
|
||||
|
||||
var revInfo = entityManager.find(Revision.class, revisions.getFirst());
|
||||
assertThat(revInfo).isNotNull();
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should be equal to entity's createdAt timestamp")
|
||||
.isEqualTo(entity.createdAt),
|
||||
() -> assertThat(revInfo.actor)
|
||||
.as("actor should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.traceId)
|
||||
.as("traceId should match expected value")
|
||||
.isEqualTo("00000000000000000000000000000000"),
|
||||
() -> assertThat(revInfo.spanId)
|
||||
.as("spanId should match expected value")
|
||||
.isEqualTo("0000000000000000"),
|
||||
() -> assertThat(revInfo.requestId)
|
||||
.as("requestId should match expected value")
|
||||
.isEqualTo("00000000-0000-0000-0000-000000000000"),
|
||||
() -> assertThat(revInfo.clientIp)
|
||||
.as("clientIp should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.hostName)
|
||||
.as("hostName should not be blank")
|
||||
.isNotBlank());
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity.findBySomething("something");
|
||||
entity.something = "else";
|
||||
});
|
||||
|
||||
QuarkusTransaction.requiringNew().run(() -> {
|
||||
var entity = TestEntity.findBySomething("else");
|
||||
assertAll(() -> assertThat(entity.createdAt)
|
||||
.as("createdAt should be before lastUpdatedAt")
|
||||
.isBefore(entity.lastUpdatedAt));
|
||||
|
||||
var auditReader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
var revisions = auditReader.getRevisions(TestEntity.class, entity.id);
|
||||
assertThat(revisions).hasSize(2);
|
||||
|
||||
Revision revInfo = Revision.findById(revisions.getLast());
|
||||
assertThat(revInfo).isNotNull();
|
||||
|
||||
assertAll(
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should not be equal to entity's createdAt")
|
||||
.isNotEqualTo(entity.createdAt),
|
||||
() -> assertThat(revInfo.timestamp)
|
||||
.as("revision timestamp should be equal to entity's lastUpdatedAt")
|
||||
.isEqualTo(entity.lastUpdatedAt),
|
||||
() -> assertThat(revInfo.actor)
|
||||
.as("actor should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.traceId)
|
||||
.as("traceId should match expected value")
|
||||
.isEqualTo("00000000000000000000000000000000"),
|
||||
() -> assertThat(revInfo.spanId)
|
||||
.as("spanId should match expected value")
|
||||
.isEqualTo("0000000000000000"),
|
||||
() -> assertThat(revInfo.requestId)
|
||||
.as("requestId should match expected value")
|
||||
.isEqualTo("00000000-0000-0000-0000-000000000000"),
|
||||
() -> assertThat(revInfo.clientIp)
|
||||
.as("clientIp should match expected value")
|
||||
.isEqualTo("unknown"),
|
||||
() -> assertThat(revInfo.hostName)
|
||||
.as("hostName should not be blank")
|
||||
.isNotBlank());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class DefaultRevisionContextProviderTest {
|
||||
|
||||
@Inject
|
||||
DefaultRevisionContextProvider underTest;
|
||||
|
||||
@Test
|
||||
void getHostName() {
|
||||
assertThat(underTest.getHostName()).isNotBlank();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getHostNameWhenUnknown() {
|
||||
try (var inetMock = mockStatic(InetAddress.class)) {
|
||||
inetMock.when(InetAddress::getLocalHost).thenThrow(new UnknownHostException("simulated failure"));
|
||||
|
||||
assertThat(underTest.getHostName()).isEqualTo("unknown");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class RevisionTest {
|
||||
|
||||
@Test
|
||||
void testEquals() {
|
||||
var r1 = new Revision();
|
||||
r1.rev = 1;
|
||||
|
||||
var r2 = new Revision();
|
||||
r2.rev = 1;
|
||||
|
||||
assertThat(r1).as("Revisions should be equal").isEqualTo(r2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHashCode() {}
|
||||
|
||||
@Test
|
||||
void testToString() {}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
@Entity
|
||||
@Audited
|
||||
@Table(name = "test_entity")
|
||||
public class TestEntity extends AuditedPanacheEntity {
|
||||
|
||||
public String something;
|
||||
|
||||
public TestEntity() {}
|
||||
|
||||
public TestEntity(String something) {
|
||||
this.something = something;
|
||||
}
|
||||
|
||||
public static TestEntity findBySomething(String something) {
|
||||
return find("something", something).singleResult();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package ch.phoenix.oss.quarkus.commons.audit;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import org.hibernate.envers.Audited;
|
||||
|
||||
@Entity
|
||||
@Audited
|
||||
@Table(name = "test_entity")
|
||||
public class TestEntity2 extends AuditedPanacheEntityBase {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public Long id;
|
||||
|
||||
public String something;
|
||||
|
||||
public TestEntity2() {}
|
||||
|
||||
public TestEntity2(String something) {
|
||||
this.something = something;
|
||||
}
|
||||
|
||||
public static TestEntity2 findBySomething(String something) {
|
||||
return find("something", something).singleResult();
|
||||
}
|
||||
}
|
22
quarkus-audit-tools/src/test/resources/application.yaml
Normal file
22
quarkus-audit-tools/src/test/resources/application.yaml
Normal file
|
@ -0,0 +1,22 @@
|
|||
quarkus:
|
||||
flyway:
|
||||
migrate-at-start: true
|
||||
datasource:
|
||||
db-kind: postgresql
|
||||
hibernate-orm:
|
||||
sql-load-script: no-file
|
||||
schema-management:
|
||||
strategy: none
|
||||
log:
|
||||
sql: true
|
||||
bind-parameters: true
|
||||
hibernate-envers:
|
||||
audit-strategy: org.hibernate.envers.strategy.internal.ValidityAuditStrategy
|
||||
revision-listener: ch.phoenix.oss.quarkus.commons.audit.AuditRevisionListener
|
||||
security:
|
||||
users:
|
||||
embedded:
|
||||
enabled: true
|
||||
plain-text: true
|
||||
users:
|
||||
jon: doe
|
|
@ -0,0 +1,46 @@
|
|||
create sequence revinfo_seq start with 1 increment by 50;
|
||||
create sequence test_entity_seq start with 1 increment by 50;
|
||||
|
||||
create table revinfo
|
||||
(
|
||||
rev bigint not null,
|
||||
timestamp timestamp(6) with time zone not null,
|
||||
actor varchar(255),
|
||||
spanId varchar(255),
|
||||
traceId varchar(255),
|
||||
requestId varchar(255),
|
||||
clientIp varchar(255),
|
||||
hostName varchar(255),
|
||||
primary key (rev)
|
||||
);
|
||||
|
||||
create table test_entity
|
||||
(
|
||||
|
||||
id bigint primary key not null,
|
||||
something varchar(255),
|
||||
createdAt timestamp(6) with time zone,
|
||||
lastUpdatedAt timestamp(6) with time zone
|
||||
);
|
||||
|
||||
create table test_entity_aud
|
||||
(
|
||||
revtype smallint,
|
||||
rev bigint not null,
|
||||
revend bigint,
|
||||
id bigint not null,
|
||||
something varchar(255),
|
||||
primary key (rev, id)
|
||||
);
|
||||
|
||||
alter table if exists test_entity_aud
|
||||
add constraint fk_rev__revinfo_rev
|
||||
foreign key (rev)
|
||||
references revinfo;
|
||||
|
||||
|
||||
alter table if exists test_entity_aud
|
||||
add constraint fk_revend__revinfo_rev
|
||||
foreign key (revend)
|
||||
references revinfo;
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
CREATE OR REPLACE FUNCTION trg_test_entity_aud_apply_rev()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $func$
|
||||
DECLARE
|
||||
ts TIMESTAMP;
|
||||
BEGIN
|
||||
-- fetch the exact revision timestamp from revinfo
|
||||
SELECT r.timestamp
|
||||
INTO ts
|
||||
FROM revinfo r
|
||||
WHERE r.rev = NEW.rev;
|
||||
|
||||
-- only set created_at once, when still NULL
|
||||
UPDATE test_entity
|
||||
SET createdAt = ts
|
||||
WHERE id = NEW.id
|
||||
AND createdAt IS NULL;
|
||||
|
||||
-- always bump last_updated_at
|
||||
UPDATE test_entity
|
||||
SET lastUpdatedAt = ts
|
||||
WHERE id = NEW.id;
|
||||
|
||||
RETURN NULL;
|
||||
END;
|
||||
$func$;
|
||||
|
||||
DROP TRIGGER IF EXISTS trg_test_entity_aud_after_insert
|
||||
ON test_entity_aud;
|
||||
|
||||
CREATE TRIGGER trg_test_entity_aud_after_insert
|
||||
AFTER INSERT ON test_entity_aud
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION trg_test_entity_aud_apply_rev();
|
37
quarkus-audit-tools/src/test/resources/import.sql
Normal file
37
quarkus-audit-tools/src/test/resources/import.sql
Normal file
|
@ -0,0 +1,37 @@
|
|||
-- 1) Create or replace the trigger function
|
||||
CREATE OR REPLACE FUNCTION trg_test_entity_aud_apply_rev()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
AS $func$
|
||||
DECLARE
|
||||
ts TIMESTAMP;
|
||||
BEGIN
|
||||
-- fetch the exact revision timestamp from revinfo
|
||||
SELECT r.timestamp
|
||||
INTO ts
|
||||
FROM revinfo r
|
||||
WHERE r.rev = NEW.rev;
|
||||
|
||||
-- only set created_at once, when still NULL
|
||||
UPDATE test_entity
|
||||
SET created_at = ts
|
||||
WHERE id = NEW.id
|
||||
AND created_at IS NULL;
|
||||
|
||||
-- always bump last_updated_at
|
||||
UPDATE test_entity
|
||||
SET last_updated_at = ts
|
||||
WHERE id = NEW.id;
|
||||
|
||||
RETURN NULL; -- AFTER trigger ignores return value
|
||||
END;
|
||||
$func$;
|
||||
|
||||
-- 2) Drop any existing trigger, then attach the new one
|
||||
DROP TRIGGER IF EXISTS trg_test_entity_aud_after_insert
|
||||
ON test_entity_aud;
|
||||
|
||||
CREATE TRIGGER trg_test_entity_aud_after_insert
|
||||
AFTER INSERT ON test_entity_aud
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION trg_test_entity_aud_apply_rev();
|
Loading…
Add table
Add a link
Reference in a new issue