Search in sources :

Example 6 with MAuditEventRecord

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord in project midpoint by Evolveum.

the class SqlAuditServiceImpl method insertAuditEventRecord.

/**
 * Inserts audit event record aggregate root without any subentities.
 *
 * @return ID of created audit event record
 */
private Long insertAuditEventRecord(JdbcSession jdbcSession, AuditEventRecord record) {
    QAuditEventRecordMapping aerMapping = QAuditEventRecordMapping.get();
    QAuditEventRecord aer = aerMapping.defaultAlias();
    MAuditEventRecord aerBean = aerMapping.toRowObject(record);
    SQLInsertClause insert = jdbcSession.newInsert(aer).populate(aerBean);
    Map<String, ColumnMetadata> customColumns = aerMapping.getExtensionColumns();
    for (Entry<String, String> property : record.getCustomColumnProperty().entrySet()) {
        String propertyName = property.getKey();
        if (!customColumns.containsKey(propertyName)) {
            throw new IllegalArgumentException("Audit event record table doesn't" + " contains column for property " + propertyName);
        }
        // Like insert.set, but that one is too parameter-type-safe for our generic usage here.
        insert.columns(aer.getPath(propertyName)).values(property.getValue());
    }
    Long returnedId = insert.executeWithKey(aer.id);
    // If returned ID is null, it was provided. If not, it fails, something went bad.
    return returnedId != null ? returnedId : record.getRepoId();
}
Also used : ColumnMetadata(com.querydsl.sql.ColumnMetadata) MAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 7 with MAuditEventRecord

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord in project midpoint by Evolveum.

the class QAuditEventRecordMapping method toRowObject.

/**
 * Transforms {@link AuditEventRecord} to {@link MAuditEventRecord} without any subentities.
 */
public MAuditEventRecord toRowObject(AuditEventRecord record) {
    MAuditEventRecord bean = new MAuditEventRecord();
    // this better be null if we want to insert
    bean.id = record.getRepoId();
    bean.eventIdentifier = record.getEventIdentifier();
    bean.timestamp = MiscUtil.asInstant(record.getTimestamp());
    bean.channel = record.getChannel();
    bean.eventStage = MiscUtil.enumOrdinal(RAuditEventStage.from(record.getEventStage()));
    bean.eventType = MiscUtil.enumOrdinal(RAuditEventType.from(record.getEventType()));
    bean.hostIdentifier = record.getHostIdentifier();
    PrismReferenceValue attorney = record.getAttorneyRef();
    if (attorney != null) {
        bean.attorneyName = attorney.getDescription();
        bean.attorneyOid = attorney.getOid();
    }
    PrismReferenceValue initiator = record.getInitiatorRef();
    if (initiator != null) {
        bean.initiatorName = initiator.getDescription();
        bean.initiatorOid = initiator.getOid();
        bean.initiatorType = targetTypeToRepoOrdinal(initiator);
    }
    bean.message = trim(record.getMessage(), MESSAGE);
    bean.nodeIdentifier = record.getNodeIdentifier();
    bean.outcome = MiscUtil.enumOrdinal(ROperationResultStatus.from(record.getOutcome()));
    bean.parameter = record.getParameter();
    bean.remoteHostAddress = record.getRemoteHostAddress();
    bean.requestIdentifier = record.getRequestIdentifier();
    bean.result = record.getResult();
    bean.sessionIdentifier = record.getSessionIdentifier();
    PrismReferenceValue target = record.getTargetRef();
    if (target != null) {
        bean.targetName = target.getDescription();
        bean.targetOid = target.getOid();
        bean.targetType = targetTypeToRepoOrdinal(target);
    }
    PrismReferenceValue targetOwner = record.getTargetOwnerRef();
    if (targetOwner != null) {
        bean.targetOwnerName = targetOwner.getDescription();
        bean.targetOwnerOid = targetOwner.getOid();
        bean.targetOwnerType = targetTypeToRepoOrdinal(targetOwner);
    }
    bean.taskIdentifier = record.getTaskIdentifier();
    bean.taskOid = record.getTaskOid();
    return bean;
}
Also used : PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) MAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord)

Example 8 with MAuditEventRecord

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord in project midpoint by Evolveum.

the class CleanupTest method testAuditCleanupMaxAge.

@Test
public void testAuditCleanupMaxAge() throws Exception {
    // GIVEN
    prepareAuditEventRecords();
    // WHEN
    Calendar calendar = create_2013_07_12_12_00_Calendar();
    calendar.add(Calendar.HOUR_OF_DAY, 1);
    calendar.add(Calendar.MINUTE, 1);
    final long NOW = System.currentTimeMillis();
    CleanupPolicyType policy = createPolicy(calendar, NOW);
    OperationResult result = new OperationResult("Cleanup audit");
    auditService.cleanupAudit(policy, result);
    result.recomputeStatus();
    then();
    AssertJUnit.assertTrue(result.isSuccess());
    MAuditEventRecord record = assertAndReturnAuditEventRecord(1);
    Date finished = Date.from(record.timestamp);
    Date mark = new Date(NOW);
    Duration duration = policy.getMaxAge();
    duration.addTo(mark);
    AssertJUnit.assertTrue("finished: " + finished + ", mark: " + mark, finished.after(mark));
}
Also used : CleanupPolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType) Calendar(java.util.Calendar) MAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Duration(javax.xml.datatype.Duration) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 9 with MAuditEventRecord

use of com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord in project midpoint by Evolveum.

the class AuditTest method test100AuditSimple.

@Test
public void test100AuditSimple() throws QueryException {
    OperationResult result = createOperationResult();
    when();
    AuditEventRecord record = new AuditEventRecord();
    record.addPropertyValue("prop1", "val1.1");
    record.addPropertyValue("prop1", "val1.2");
    record.addPropertyValue("prop2", "val2");
    record.addPropertyValue("prop3", null);
    AuditReferenceValue refVal1_1 = new AuditReferenceValue("oid1.1", UserType.COMPLEX_TYPE, poly("user1.1"));
    AuditReferenceValue refVal1_2 = new AuditReferenceValue("oid1.2", RoleType.COMPLEX_TYPE, poly("role1.2"));
    AuditReferenceValue refVal2 = new AuditReferenceValue("oid2", null, poly("object2"));
    AuditReferenceValue refVal3 = new AuditReferenceValue();
    record.addReferenceValue("ref1", refVal1_1);
    record.addReferenceValue("ref1", refVal1_2);
    record.addReferenceValue("ref2", refVal2);
    record.addReferenceValue("ref3", refVal3);
    logger.info("Adding audit record {}", record);
    auditService.audit(record, new NullTaskImpl(), result);
    then();
    System.out.println("Record written:\n" + record.debugDump());
    System.out.println("Repo ID: " + record.getRepoId());
    MAuditEventRecord loaded = getAuditEventRecord(1, 0);
    System.out.println("Record loaded:\n" + loaded);
    assertThat(loaded.properties).withFailMessage("Wrong # of properties").hasSize(3);
    assertThat(loaded.properties.get("prop1")).describedAs("prop1 values").containsExactlyInAnyOrder("val1.1", "val1.2");
    assertThat(loaded.properties.get("prop2")).describedAs("prop2 values").containsExactlyInAnyOrder("val2");
    assertThat(loaded.properties.get("prop3")).describedAs("prop3 values").containsExactlyInAnyOrder((String) null);
    assertThat(loaded.refValues).withFailMessage("Wrong # of references").hasSize(3);
    assertThat(loaded.refValues.get("ref1")).describedAs("ref1 values").flatExtracting(this::toAuditReferenceValue).containsExactlyInAnyOrder(refVal1_1, refVal1_2);
    assertThat(loaded.refValues.get("ref2")).describedAs("ref2 values").flatExtracting(this::toAuditReferenceValue).containsExactlyInAnyOrder(refVal2);
    assertThat(loaded.refValues.get("ref3")).describedAs("ref3 values").flatExtracting(this::toAuditReferenceValue).containsExactlyInAnyOrder(refVal3);
}
Also used : MAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) AuditReferenceValue(com.evolveum.midpoint.audit.api.AuditReferenceValue) QAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.querymodel.QAuditEventRecord) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) MAuditEventRecord(com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord) NullTaskImpl(com.evolveum.midpoint.task.api.test.NullTaskImpl) Test(org.testng.annotations.Test)

Aggregations

MAuditEventRecord (com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord)9 QAuditEventRecord (com.evolveum.midpoint.repo.sql.audit.querymodel.QAuditEventRecord)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 Test (org.testng.annotations.Test)3 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 NullTaskImpl (com.evolveum.midpoint.task.api.test.NullTaskImpl)2 ColumnMetadata (com.querydsl.sql.ColumnMetadata)2 SQLInsertClause (com.querydsl.sql.dml.SQLInsertClause)2 AuditReferenceValue (com.evolveum.midpoint.audit.api.AuditReferenceValue)1 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)1 AuditEventRecordType (com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordType)1 CleanupPolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType)1 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Duration (javax.xml.datatype.Duration)1