public AuditEventRecord prepareProcessInstanceAuditRecord(WfTask wfTask, AuditEventStage stage, OperationResult result) { WfContextType wfc = wfTask.getTask().getWorkflowContext(); AuditEventRecord record = new AuditEventRecord(); record.setEventType(WORKFLOW_PROCESS_INSTANCE); record.setEventStage(stage); record.setInitiator(wfTask.getRequesterIfExists(result)); // set real principal in case of explicitly requested process termination (MID-4263) ObjectReferenceType objectRef = resolveIfNeeded(wfc.getObjectRef(), result); record.setTarget(objectRef.asReferenceValue()); record.setOutcome(OperationResultStatus.SUCCESS); record.addReferenceValueIgnoreNull(WorkflowConstants.AUDIT_OBJECT, objectRef); record.addReferenceValueIgnoreNull(WorkflowConstants.AUDIT_TARGET, resolveIfNeeded(wfc.getTargetRef(), result)); if (stage == EXECUTION) { String stageInfo = wfTask.getCompleteStageInfo(); record.setParameter(stageInfo); String answer = wfTask.getAnswerNice(); record.setResult(answer); record.setMessage(stageInfo != null ? stageInfo + " : " + answer : answer); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_NUMBER, wfc.getStageNumber()); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_COUNT, WfContextUtil.getStageCount(wfc)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_NAME, WfContextUtil.getStageName(wfc)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_DISPLAY_NAME, WfContextUtil.getStageDisplayName(wfc)); } record.addPropertyValue(WorkflowConstants.AUDIT_PROCESS_INSTANCE_ID, wfc.getProcessInstanceId()); OperationBusinessContextType businessContext = WfContextUtil.getBusinessContext(wfc); String requesterComment = businessContext != null ? businessContext.getComment() : null; if (requesterComment != null) { record.addPropertyValue(WorkflowConstants.AUDIT_REQUESTER_COMMENT, requesterComment); } return record; }
private String toSummary(AuditEventRecord record) { return formatTimestamp(record.getTimestamp()) + " eid=" + record.getEventIdentifier() + ", et=" + record.getEventType() + ", es=" + record.getEventStage() + ", sid=" + record.getSessionIdentifier() + ", tid=" + record.getTaskIdentifier() + ", toid=" + record.getTaskOID() + ", hid=" + record.getHostIdentifier() + ", nid=" + record.getNodeIdentifier() + ", raddr=" + record.getRemoteHostAddress() + ", I=" + formatObject(record.getInitiator()) + ", T=" + formatReference(record.getTarget()) + ", TO=" + formatObject(record.getTargetOwner()) + ", D=" + formatDeltaSummary(record.getDeltas()) + ", ch=" + record.getChannel() + ", o=" + record.getOutcome() + ", p=" + record.getParameter() + ", m=" + record.getMessage(); }
protected void auditLogin(Task task) { AuditEventRecord record = new AuditEventRecord(AuditEventType.CREATE_SESSION, AuditEventStage.REQUEST); record.setInitiatorAndLoginParameter(task.getOwner()); record.setChannel(SchemaConstants.CHANNEL_WEB_SERVICE_URI); record.setTimestamp(System.currentTimeMillis()); record.setOutcome(OperationResultStatus.SUCCESS); auditService.audit(record, task); }
private void assertRecordSanity(String recordDesc, AuditEventRecord record) { assert record != null : "Null audit record ("+recordDesc+")"; assert !StringUtils.isEmpty(record.getEventIdentifier()) : "No event identifier in audit record ("+recordDesc+")"; assert !StringUtils.isEmpty(record.getTaskIdentifier()) : "No task identifier in audit record ("+recordDesc+")"; // TODO }
protected List<AuditEventRecord> filter(List<AuditEventRecord> records, AuditEventType type, AuditEventStage stage) { return records.stream() .filter(r -> r.getEventType() == type && r.getEventStage() == stage) .collect(Collectors.toList()); }
if (record.getEventIdentifier() == null) { id = lightweightIdentifierGenerator.generate(); record.setEventIdentifier(id.toString()); if (record.getTimestamp() == null) { if (id == null) { record.setTimestamp(System.currentTimeMillis()); } else { record.setTimestamp(id.getTimestamp()); if (record.getTaskIdentifier() == null && task != null) { record.setTaskIdentifier(task.getTaskIdentifier()); if (record.getTaskOID() == null && task != null) { record.setTaskOID(task.getOid()); if (record.getChannel() == null && task != null) { record.setChannel(task.getChannel()); if (record.getInitiator() == null && task != null) { record.setInitiator(task.getOwner()); if (record.getNodeIdentifier() == null && taskManager != null) { record.setNodeIdentifier(taskManager.getNodeId()); if (record.getSessionIdentifier() == null) { record.setSessionIdentifier(connInfo.getSessionId());
if (record.getRepoId() != null) { repo.setId(record.getRepoId()); repo.setChannel(record.getChannel()); if (record.getTimestamp() != null) { repo.setTimestamp(new Timestamp(record.getTimestamp())); repo.setEventStage(RAuditEventStage.toRepo(record.getEventStage())); repo.setEventType(RAuditEventType.toRepo(record.getEventType())); repo.setSessionIdentifier(record.getSessionIdentifier()); repo.setEventIdentifier(record.getEventIdentifier()); repo.setHostIdentifier(record.getHostIdentifier()); repo.setRemoteHostAddress(record.getRemoteHostAddress()); repo.setNodeIdentifier(record.getNodeIdentifier()); repo.setParameter(record.getParameter()); repo.setMessage(RUtil.trimString(record.getMessage(), AuditService.MAX_MESSAGE_SIZE)); if (record.getOutcome() != null) { repo.setOutcome(RUtil.getRepoEnumValue(record.getOutcome().createStatusType(), ROperationResultStatus.class)); repo.setTaskIdentifier(record.getTaskIdentifier()); repo.setTaskOID(record.getTaskOID()); repo.setResult(record.getResult()); if (record.getTarget() != null) { PrismReferenceValue target = record.getTarget(); repo.setTargetName(getOrigName(target)); repo.setTargetOid(target.getOid());
AuditEventRecord audit = new AuditEventRecord(); audit.setChannel(repo.getChannel()); audit.setEventIdentifier(repo.getEventIdentifier()); if (repo.getEventStage() != null) { audit.setEventStage(repo.getEventStage().getStage()); audit.setEventType(repo.getEventType().getType()); audit.setHostIdentifier(repo.getHostIdentifier()); audit.setRemoteHostAddress(repo.getRemoteHostAddress()); audit.setNodeIdentifier(repo.getNodeIdentifier()); audit.setMessage(repo.getMessage()); audit.setOutcome(repo.getOutcome().getStatus()); audit.setParameter(repo.getParameter()); audit.setResult(repo.getResult()); audit.setSessionIdentifier(repo.getSessionIdentifier()); audit.setTaskIdentifier(repo.getTaskIdentifier()); audit.setTaskOID(repo.getTaskOID()); if (repo.getTimestamp() != null) { audit.setTimestamp(repo.getTimestamp().getTime()); audit.getDeltas().addAll((Collection) odos); audit.addPropertyValue(rPropertyValue.getName(), rPropertyValue.getValue()); audit.addReferenceValue(rRefValue.getName(), rRefValue.fromRepo());
public AuditEventRecord prepareWorkItemAuditReportCommon(WorkItemType workItem, WfTask wfTask, AuditEventStage stage, OperationResult result) throws WorkflowException { AuditEventRecord record = new AuditEventRecord(); record.setEventType(AuditEventType.WORK_ITEM); record.setEventStage(stage); ObjectReferenceType objectRef = resolveIfNeeded(WfContextUtil.getObjectRef(workItem), result); record.setTarget(objectRef.asReferenceValue()); record.setOutcome(OperationResultStatus.SUCCESS); record.setParameter(wfTask.getCompleteStageInfo()); record.addReferenceValueIgnoreNull(WorkflowConstants.AUDIT_OBJECT, objectRef); record.addReferenceValueIgnoreNull(WorkflowConstants.AUDIT_TARGET, resolveIfNeeded(WfContextUtil.getTargetRef(workItem), result)); record.addReferenceValueIgnoreNull(WorkflowConstants.AUDIT_ORIGINAL_ASSIGNEE, resolveIfNeeded(workItem.getOriginalAssigneeRef(), result)); record.addReferenceValues(WorkflowConstants.AUDIT_CURRENT_ASSIGNEE, resolveIfNeeded(workItem.getAssigneeRef(), result)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_NUMBER, workItem.getStageNumber()); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_COUNT, WfContextUtil.getStageCount(workItem)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_NAME, WfContextUtil.getStageName(workItem)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_STAGE_DISPLAY_NAME, WfContextUtil.getStageDisplayName(workItem)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_ESCALATION_LEVEL_NUMBER, WfContextUtil.getEscalationLevelNumber(workItem)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_ESCALATION_LEVEL_NAME, WfContextUtil.getEscalationLevelName(workItem)); record.addPropertyValueIgnoreNull(WorkflowConstants.AUDIT_ESCALATION_LEVEL_DISPLAY_NAME, WfContextUtil.getEscalationLevelDisplayName(workItem)); record.addPropertyValue(WorkflowConstants.AUDIT_WORK_ITEM_ID, workItem.getExternalId()); record.addPropertyValue(WorkflowConstants.AUDIT_PROCESS_INSTANCE_ID, WfContextUtil.getProcessInstanceId(workItem)); return record; }
private void auditLogin(@Nullable String username, @Nullable UserType user, @NotNull ConnectionEnvironment connEnv, @NotNull OperationResultStatus status, @Nullable String message) { Task task = taskManager.createTaskInstance(); task.setChannel(connEnv.getChannel()); LOGGER.debug("Login {} username={}, channel={}: {}", status == OperationResultStatus.SUCCESS ? "success" : "failure", username, connEnv.getChannel(), message); AuditEventRecord record = new AuditEventRecord(AuditEventType.CREATE_SESSION, AuditEventStage.REQUEST); record.setParameter(username); if (user != null ) { record.setInitiator(user.asPrismObject()); } record.setTimestamp(System.currentTimeMillis()); record.setOutcome(status); record.setMessage(message); storeConnectionEnvironment(record, connEnv); auditService.audit(record, task); }
@Test public void testAudit() { AuditEventRecord record = new AuditEventRecord(); record.setChannel("http://midpoint.evolveum.com/xml/ns/public/provisioning/channels-3#import"); record.setEventIdentifier("1511974895961-0-1"); record.setEventStage(AuditEventStage.EXECUTION); record.setEventType(AuditEventType.ADD_OBJECT); ObjectDeltaOperation delta = new ObjectDeltaOperation(); delta.setObjectDelta(prismContext.deltaFactory().object().createModificationAddReference(UserType.class, "1234", UserType.F_LINK_REF, "123")); record.getDeltas().add(delta); delta = new ObjectDeltaOperation(); delta.setObjectDelta(prismContext.deltaFactory().object().createModificationAddReference(UserType.class, "1234", UserType.F_LINK_REF, "124")); record.getDeltas().add(delta); auditService.audit(record, new SimpleTaskAdapter()); }
AuditEventRecord auditRecord = new AuditEventRecord(eventType, stage); auditRecord.setTarget(primaryObject.clone(), prismContext); auditRecord.setChannel(context.getChannel()); auditRecord.addDeltas(clonedDeltas); if (auditRecord.getTarget() == null) { auditRecord.setTarget(ModelImplUtils.determineAuditTargetDeltaOps(clonedDeltas, context.getPrismContext())); auditRecord.setOutcome(result.getComputeStatus()); Collection<ObjectDeltaOperation<? extends ObjectType>> unauditedExecutedDeltas = context.getUnauditedExecutedDeltas(); if (!alwaysAudit && (unauditedExecutedDeltas == null || unauditedExecutedDeltas.isEmpty())) { auditRecord.addDeltas(clonedDeltas); } else { throw new IllegalStateException("Unknown audit stage "+stage); auditRecord.setTimestamp(XmlTypeConverter.toMillis(timestamp));
AuditEventRecord auditRecord = new AuditEventRecord(AuditEventType.EXECUTE_CHANGES_RAW, AuditEventStage.REQUEST); auditRecord.addDeltas(ObjectDeltaOperation.cloneDeltaCollection(deltas)); auditRecord.setTarget(ModelImplUtils.determineAuditTarget(deltas, prismContext)); auditRecord.setTimestamp(System.currentTimeMillis()); auditRecord.setOutcome(result.getStatus()); auditRecord.setEventStage(AuditEventStage.EXECUTION); auditRecord.getDeltas().clear(); auditRecord.getDeltas().addAll(executedDeltas); auditService.audit(auditRecord, task);
private void processErrorFinal(TaskRunResult runResult, String errorDesc, Exception ex, TaskRunResultStatus runResultStatus, PrismObject<ResourceType> resource, Task task, OperationResult opResult) { String message = errorDesc+": "+ex.getMessage(); LOGGER.error("Reconciliation: {}-{}", new Object[]{message, ex}); opResult.recordFatalError(message, ex); TaskHandlerUtil.appendLastFailuresInformation(OperationConstants.RECONCILIATION, task, opResult); // TODO implement more seriously runResult.setRunResultStatus(runResultStatus); AuditEventRecord executionRecord = new AuditEventRecord(AuditEventType.RECONCILIATION, AuditEventStage.EXECUTION); executionRecord.setTarget(resource, prismContext); executionRecord.setOutcome(OperationResultStatus.FATAL_ERROR); executionRecord.setMessage(ex.getMessage()); auditService.audit(executionRecord , task); }
while (iterator.hasNext()) { AuditEventRecord record = iterator.next(); if (record.getTaskOID() != null && !record.getTaskOID().equals(taskOid)) { while (i < (auditRecords.size() - 1)) { AuditEventRecord reconStartRecord = auditRecords.get(i); if (reconStartRecord.getEventType() == AuditEventType.EXECUTE_CHANGES_RAW) { i++; continue; assertEquals("Wrong stage in reconStartRecord audit record: "+reconStartRecord, AuditEventStage.REQUEST, reconStartRecord.getEventStage()); assertEquals("Wrong type in reconStartRecord audit record: "+reconStartRecord, AuditEventType.RECONCILIATION, reconStartRecord.getEventType()); assertTrue("Unexpected delta in reconStartRecord audit record "+reconStartRecord, reconStartRecord.getDeltas() == null || reconStartRecord.getDeltas().isEmpty()); i++; break; if (record.getEventStage() == AuditEventStage.EXECUTION && record.getEventType() == AuditEventType.RECONCILIATION) { if (record.getEventStage() == AuditEventStage.REQUEST) { record = auditRecords.get(i); i++; assertEquals("Got this instead of execution audit record (" + i + "): " + record, AuditEventStage.EXECUTION, record.getEventStage()); assertTrue("Empty deltas in execution audit record " + record, record.getDeltas() != null && !record.getDeltas().isEmpty()); modifications++; assertEquals("Wrong stage in reconStopRecord audit record: "+reconStopRecord, AuditEventStage.EXECUTION, reconStopRecord.getEventStage()); assertEquals("Wrong type in reconStopRecord audit record: "+reconStopRecord, AuditEventType.RECONCILIATION, reconStopRecord.getEventType());
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, new PolyString("user1.1")); AuditReferenceValue refVal1_2 = new AuditReferenceValue("oid1.2", RoleType.COMPLEX_TYPE, new PolyString("role1.2")); AuditReferenceValue refVal2 = new AuditReferenceValue("oid2", null, new PolyString("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 SimpleTaskAdapter()); System.out.println("Record written:\n" + record.debugDump()); System.out.println("Repo ID: " + record.getRepoId()); AuditEventRecord loaded = getAuditEventRecord(1, 0); System.out.println("Record loaded:\n" + loaded.debugDump()); System.out.println("Repo ID: " + loaded.getRepoId()); assertEquals("Wrong # of properties", 3, loaded.getProperties().size()); assertEquals("Wrong prop1 values", new HashSet<>(Arrays.asList("val1.1", "val1.2")), loaded.getPropertyValues("prop1")); assertEquals("Wrong prop2 values", new HashSet<>(Collections.singletonList("val2")), loaded.getPropertyValues("prop2")); assertEquals("Wrong prop3 values", new HashSet<>(Collections.singletonList(null)), loaded.getPropertyValues("prop3")); assertEquals("Wrong # of references", 3, loaded.getReferences().size()); assertEquals("Wrong ref1 values", new HashSet<>(Arrays.asList(refVal1_1, refVal1_2)), loaded.getReferenceValues("ref1")); assertEquals("Wrong ref2 values", new HashSet<>(Collections.singletonList(refVal2)), loaded.getReferenceValues("ref2")); assertEquals("Wrong ref3 values", new HashSet<>(Collections.singletonList(refVal3)), loaded.getReferenceValues("ref3"));
private void prepareAuditEventRecords() throws Exception { long start = System.currentTimeMillis(); for (int i = 0; i < RECORDS;) { AuditEventRecord record = new AuditEventRecord(); record.addDelta(createObjectDeltaOperation(i)); record.setTimestamp(System.currentTimeMillis()); record.addPropertyValue("prop1", "val1"); record.addReferenceValue("ref1", ObjectTypeUtil.createObjectRef("oid1", ObjectTypes.USER).asReferenceValue()); auditService.audit(record, new SimpleTaskAdapter()); i++; if (i%1000 == 0 || i == RECORDS) { long duration = System.currentTimeMillis() - start; System.out.println(i + " records created in " + duration + " ms (" + duration / i + " ms per record)"); } } try (Session session = getFactory().openSession()) { session.beginTransaction(); Query query = session.createQuery("select count(*) from " + RAuditEventRecord.class.getSimpleName()); Long count = (Long) query.uniqueResult(); AssertJUnit.assertEquals(RECORDS, (long) count); session.getTransaction().commit(); } }
/** * Checks that the first record is login and the last is logout. */ public void assertLoginLogout(String expectedChannel) { AuditEventRecord firstRecord = records.get(0); assertEquals("Wrong type of first audit record: "+firstRecord.getEventType(), AuditEventType.CREATE_SESSION, firstRecord.getEventType()); assertEquals("Wrong outcome of first audit record: "+firstRecord.getOutcome(), OperationResultStatus.SUCCESS, firstRecord.getOutcome()); AuditEventRecord lastRecord = records.get(records.size()-1); assertEquals("Wrong type of last audit record: "+lastRecord.getEventType(), AuditEventType.TERMINATE_SESSION, lastRecord.getEventType()); assertEquals("Wrong outcome of last audit record: "+lastRecord.getOutcome(), OperationResultStatus.SUCCESS, lastRecord.getOutcome()); assertEquals("Audit session ID does not match", firstRecord.getSessionIdentifier(), lastRecord.getSessionIdentifier()); assertFalse("Same login and logout event IDs", firstRecord.getEventIdentifier().equals(lastRecord.getEventIdentifier())); if (expectedChannel != null) { assertEquals("Wrong channel in first audit record", expectedChannel, firstRecord.getChannel()); assertEquals("Wrong channel in last audit record", expectedChannel, lastRecord.getChannel()); } }
private String toDetails(AuditEventRecord record) { StringBuilder sb = new StringBuilder("Details of event "); sb.append(record.getEventIdentifier()).append(" stage ").append(record.getEventStage()).append("\n"); sb.append("Deltas:"); for (ObjectDeltaOperation<?> delta: record.getDeltas()) { sb.append("\n"); if (delta == null) { sb.append("null"); } else { sb.append(delta.debugDump(1)); } } // TODO: target? return sb.toString(); }
@Test public void test110AuditSecond() throws Exception { LOGGER.info("===[ test110AuditSecond ]==="); // WHEN AuditEventRecord record = new AuditEventRecord(); record.addPropertyValue("prop", "val"); LOGGER.info("Adding audit record {}", record); auditService.audit(record, new SimpleTaskAdapter()); // THEN System.out.println("Record written:\n" + record.debugDump()); System.out.println("Repo ID: " + record.getRepoId()); AuditEventRecord loaded = getAuditEventRecord(2, 1); System.out.println("Record loaded:\n" + loaded.debugDump()); System.out.println("Repo ID: " + loaded.getRepoId()); assertEquals("Wrong # of properties", 1, loaded.getProperties().size()); assertEquals("Wrong prop values", new HashSet<>(Collections.singletonList("val")), loaded.getPropertyValues("prop")); assertEquals("Wrong # of references", 0, loaded.getReferences().size()); }