Skip to content

Commit

Permalink
Merge branch 'master' into feature/checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Oct 18, 2019
2 parents fbc3237 + 9740a9d commit 7aed69d
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 65 deletions.
Expand Up @@ -163,35 +163,35 @@ protected <F extends FocusType> void customizationFocusInfoBoxType(InfoBoxType i
String icon, String keyPrefix, OperationResult result, Task task) {
}

protected List<AuditEventRecordType> listAuditRecords(Map<String, Object> parameters, List<String> conditions) {
Date date = new Date(System.currentTimeMillis() - (24*3600000));
conditions.add("aer.timestamp >= :from");
parameters.put("from", XmlTypeConverter.createXMLGregorianCalendar(date));
conditions.add("aer.eventStage = :auditStageType");
parameters.put("auditStageType", AuditEventStageType.EXECUTION);
String query = "from RAuditEventRecord as aer";
if (!conditions.isEmpty()) {
query += " where ";
}
query += conditions.stream().collect(Collectors.joining(" and "));
query += " order by aer.timestamp desc";


List<AuditEventRecord> auditRecords;
auditRecords = getAuditService().listRecords(query, parameters);
if (auditRecords == null) {
auditRecords = new ArrayList<>();
}
List<AuditEventRecordType> auditRecordList = new ArrayList<>();
for (AuditEventRecord record : auditRecords){
auditRecordList.add(record.createAuditEventRecordType());
}
return auditRecordList;
}
// protected List<AuditEventRecordType> listAuditRecords(Map<String, Object> parameters, List<String> conditions) {
//
// Date date = new Date(System.currentTimeMillis() - (24*3600000));
// conditions.add("aer.timestamp >= :from");
// parameters.put("from", XmlTypeConverter.createXMLGregorianCalendar(date));
// conditions.add("aer.eventStage = :auditStageType");
// parameters.put("auditStageType", AuditEventStageType.EXECUTION);
//
// String query = "from RAuditEventRecord as aer";
// if (!conditions.isEmpty()) {
// query += " where ";
// }
//
// query += conditions.stream().collect(Collectors.joining(" and "));
// query += " order by aer.timestamp desc";
//
//
// List<AuditEventRecord> auditRecords;
// auditRecords = getAuditService().listRecords(query, parameters);
//
// if (auditRecords == null) {
// auditRecords = new ArrayList<>();
// }
// List<AuditEventRecordType> auditRecordList = new ArrayList<>();
// for (AuditEventRecord record : auditRecords){
// auditRecordList.add(record.createAuditEventRecordType());
// }
// return auditRecordList;
// }

protected String formatPercentage(int totalItems, int actualItems) {
float percentage = (totalItems==0 ? 0 : actualItems*100.0f/totalItems);
Expand Down
Expand Up @@ -72,7 +72,7 @@ public void audit(AuditEventRecord record, Task task, OperationResult result) th
@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params, Task task, OperationResult result) throws SecurityViolationException, SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException {
authorize(ModelAuthorizationAction.AUDIT_READ, task, result);
return auditService.listRecords(query, params);
return auditService.listRecords(query, params, result);
}

/* (non-Javadoc)
Expand Down Expand Up @@ -106,7 +106,7 @@ public <O extends ObjectType> PrismObject<O> reconstructObject(Class<O> type, St
O currentObjectType = objectResolver.getObjectSimple(type, oid, null, task, result);
PrismObject<O> currentObject = (PrismObject<O>) currentObjectType.asPrismObject();

List<AuditEventRecord> changeTrail = getChangeTrail(oid, eventIdentifier);
List<AuditEventRecord> changeTrail = getChangeTrail(oid, eventIdentifier, result);
LOGGER.trace("Found change trail for {} containing {} events", oid, changeTrail.size());

LOGGER.info("TRAIL:\n{}", DebugUtil.debugDump(changeTrail, 1));
Expand All @@ -121,15 +121,15 @@ public <O extends ObjectType> PrismObject<O> reconstructObject(Class<O> type, St
return reconstructedObject;
}

private List<AuditEventRecord> getChangeTrail(String targetOid, String finalEventIdentifier) throws ObjectNotFoundException {
AuditEventRecord finalEvent = findEvent(finalEventIdentifier);
private List<AuditEventRecord> getChangeTrail(String targetOid, String finalEventIdentifier, OperationResult result) throws ObjectNotFoundException {
AuditEventRecord finalEvent = findEvent(finalEventIdentifier, result);
if (finalEvent == null) {
throw new ObjectNotFoundException("Audit event ID "+finalEventIdentifier+" was not found");
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Final event:\n{}", finalEvent.debugDump(1));
}
List<AuditEventRecord> changeTrail = getChangeTrail(targetOid, XmlTypeConverter.createXMLGregorianCalendar(finalEvent.getTimestamp()));
List<AuditEventRecord> changeTrail = getChangeTrail(targetOid, XmlTypeConverter.createXMLGregorianCalendar(finalEvent.getTimestamp()), result);

// The search may have returned more events that we want to, e.g. if two
// events happened in the same millisecond.
Expand All @@ -147,20 +147,21 @@ private List<AuditEventRecord> getChangeTrail(String targetOid, String finalEven
return changeTrail;
}

private List<AuditEventRecord> getChangeTrail(String targetOid, XMLGregorianCalendar from) {
private List<AuditEventRecord> getChangeTrail(String targetOid, XMLGregorianCalendar from, OperationResult result) {
Map<String,Object> params = new HashMap<>();
params.put("from", from);
params.put("targetOid", targetOid);
params.put("stage", AuditEventStage.EXECUTION);
return auditService.listRecords(
"select * from m_audit_event as aer where (aer.timestampValue >= :from) and (aer.targetOid = :targetOid) and (aer.eventStage = :stage) order by aer.timestampValue desc",
params);
params, result);
}

private AuditEventRecord findEvent(String eventIdentifier) {
private AuditEventRecord findEvent(String eventIdentifier, OperationResult result) {
Map<String,Object> params = new HashMap<>();
params.put("eventIdentifier", eventIdentifier);
List<AuditEventRecord> listRecords = auditService.listRecords("select * from m_audit_event as aer where (aer.eventIdentifier = :eventIdentifier)", params);
List<AuditEventRecord> listRecords = auditService
.listRecords("select * from m_audit_event as aer where (aer.eventIdentifier = :eventIdentifier)", params, result);
if (listRecords == null || listRecords.isEmpty()) {
return null;
}
Expand Down
Expand Up @@ -98,7 +98,7 @@ public int getProgress() {
while (true) {
params.put("setFirstResult", firstResult);
params.put("setMaxResults", maxResults);
List<AuditEventRecord> records = auditService.listRecords(null, params);
List<AuditEventRecord> records = auditService.listRecords(null, params, opResult);
if (CollectionUtils.isNotEmpty(records)){
for (AuditEventRecord record : records) {
resultHandler.handle(record);
Expand Down
Expand Up @@ -215,8 +215,11 @@ public List<AuditEventRecord> searchAuditRecords(String query, Map<String, Objec
if (StringUtils.isBlank(query)) {
return new ArrayList<>();
}
OperationResult result = new OperationResult("searchAuditRecords");

return auditService.listRecords(query, ReportUtils.jasperParamsToAuditParams(jasperParams));
List<AuditEventRecord> records = auditService.listRecords(query, ReportUtils.jasperParamsToAuditParams(jasperParams), result);
result.computeStatus();
return records;
}

public List<AuditEventRecord> searchAuditRecordsAsWorkflows(String query, Map<String, Object> params) {
Expand Down
Expand Up @@ -460,7 +460,7 @@ private ContainerTag createTableBoxForWidget(DashboardWidget widgetData, Task ta
long startMillis = clock.currentTimeMillis();
String query = DashboardUtils
.getQueryForListRecords(DashboardUtils.createQuery(collection, parameters, false, clock));
List<AuditEventRecord> records = auditService.listRecords(query, parameters);
List<AuditEventRecord> records = auditService.listRecords(query, parameters, result);
if (records == null || records.isEmpty()) {
return null;
}
Expand Down
Expand Up @@ -236,17 +236,17 @@ public Collection<PrismContainerValue<? extends Containerable>> evaluateScript(P
}


private Collection<AuditEventRecord> runAuditQuery(String sqlWhereClause, TypedValue<VariablesMap> jasperAuditParams) {
private Collection<AuditEventRecord> runAuditQuery(String sqlWhereClause, TypedValue<VariablesMap> jasperAuditParams, OperationResult result) {
if (StringUtils.isBlank(sqlWhereClause)) {
return new ArrayList<>();
}

String query = "select * from m_audit_event as aer " + sqlWhereClause;
LOGGER.info("AAAAAAA: query: {}", query);
LOGGER.trace("AAAAAAA: query: {}", query);
Map<String, Object> auditParams = ReportUtils.jasperParamsToAuditParams((VariablesMap)jasperAuditParams.getValue());
LOGGER.info("AAAAAAA: auditParams:\n{}", auditParams);
List<AuditEventRecord> auditRecords = auditService.listRecords(query, auditParams);
LOGGER.info("AAAAAAA: {} records", auditRecords==null?null:auditRecords.size());
LOGGER.trace("AAAAAAA: auditParams:\n{}", auditParams);
List<AuditEventRecord> auditRecords = auditService.listRecords(query, auditParams, result);
LOGGER.trace("AAAAAAA: {} records", auditRecords==null?null:auditRecords.size());
return auditRecords;
}

Expand Down Expand Up @@ -318,7 +318,7 @@ public Collection<AuditEventRecord> evaluateAuditScript(PrismObject<ReportType>
throw new SchemaException("Jasper reportType not set, cannot determine how to use string query");
}
if (reportType.equals(JasperReportTypeType.AUDIT_SQL)) {
return runAuditQuery((String)o, auditParams);
return runAuditQuery((String)o, auditParams, result);
} else {
throw new SchemaException("Jasper reportType is not set to auditSql, cannot determine how to use string query");
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.xml.ns._public.common.common_3.CleanupPolicyType;
import jdk.dynalink.Operation;

/**
* @author semancik
Expand All @@ -33,9 +34,9 @@ public interface AuditService {
/**
* @throws UnsupportedOperationException if object retrieval is not supported
*/
List<AuditEventRecord> listRecords(String query, Map<String, Object> params);
List<AuditEventRecord> listRecords(String query, Map<String, Object> params, OperationResult result);

void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler auditResultHandler);
void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler auditResultHandler, OperationResult result);

/**
* Reindex items, e.g. if new columns were created for audit table according to which the search should be possible
Expand Down
Expand Up @@ -153,7 +153,7 @@ private String formatDeltaSummary(Collection<ObjectDeltaOperation<? extends Obje
}

@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params) {
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params, OperationResult result) {
throw new UnsupportedOperationException("Object retrieval not supported");
}

Expand All @@ -175,7 +175,7 @@ private void fakeMethod() {

@Override
public void listRecordsIterative(String query, Map<String, Object> params,
AuditResultHandler auditResultHandler) {
AuditResultHandler auditResultHandler, OperationResult result) {
throw new UnsupportedOperationException("Object retrieval not supported");

}
Expand Down
Expand Up @@ -78,6 +78,10 @@ public class SqlAuditServiceImpl extends SqlBaseService implements AuditService

public static final String OP_CLEANUP_AUDIT_MAX_AGE = "cleanupAuditMaxAge";
public static final String OP_CLEANUP_AUDIT_MAX_RECORDS = "cleanupAuditMaxRecords";
public static final String OP_LIST_RECORDS = "listRecords";
public static final String OP_LIST_RECORDS_ATTEMPT = "listRecordsAttempt";
public static final String OP_LOAD_AUDIT_DELTA = "loadAuditDelta";

@Autowired
private BaseHelper baseHelper;

Expand Down Expand Up @@ -117,13 +121,17 @@ public void audit(AuditEventRecord record, Task task) {
}

@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params) {
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params, OperationResult parentResult) {
final String operation = "listRecords";
SqlPerformanceMonitorImpl pm = getPerformanceMonitor();
long opHandle = pm.registerOperationStart(operation, AuditEventRecord.class);
int attempt = 1;

OperationResult result = parentResult.createSubresult(OP_LIST_RECORDS);
result.addParam("query", query);

while (true) {
OperationResult attemptResult = result.createMinorSubresult(OP_LIST_RECORDS_ATTEMPT);
try {
final List<AuditEventRecord> auditEventRecords = new ArrayList<>();

Expand All @@ -140,32 +148,43 @@ public int getProgress() {
return 0;
}
};
listRecordsIterativeAttempt(query, params, handler);
listRecordsIterativeAttempt(query, params, handler, attemptResult);
return auditEventRecords;
} catch (RuntimeException ex) {
attempt = baseHelper.logOperationAttempt(null, operation, attempt, ex, null);
pm.registerOperationNewAttempt(opHandle, attempt);
LOGGER.error("Error while trying to list audit records, {}", ex.getMessage(), ex);
attemptResult.recordFatalError("Error while trying to list audit records, " + ex.getMessage(), ex);
} finally {
pm.registerOperationFinish(opHandle, attempt);
attemptResult.computeStatus();
result.computeStatus();
result.cleanupResult();
}
}
}

@Override
public void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler handler) {
public void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler handler, OperationResult parentResult) {
// TODO operation recording ... but beware, this method is called from within listRecords
// (fortunately, currently it is not used from the outside, so it does not matter that it skips recording)
final String operation = "listRecordsIterative";
int attempt = 1;

while (true) {
OperationResult result = parentResult.createMinorSubresult(OP_LIST_RECORDS_ATTEMPT);
try {
listRecordsIterativeAttempt(query, params, handler);
listRecordsIterativeAttempt(query, params, handler, result);
result.recordSuccess();
return;
} catch (RuntimeException ex) {
attempt = baseHelper.logOperationAttempt(null, operation, attempt, ex, null);
LOGGER.error("Error while trying to list audit record, {}, attempt: {}", ex.getMessage(), attempt, ex);
result.recordFatalError("Error while trying to list audit record " + ex.getMessage() + ", attempt: " + attempt, ex);
}
}


}

@Override
Expand Down Expand Up @@ -220,7 +239,7 @@ private void reindexEntryAttempt(AuditEventRecord record) {
}

private void listRecordsIterativeAttempt(String query, Map<String, Object> params,
AuditResultHandler handler) {
AuditResultHandler handler, OperationResult result) {
Session session = null;

if (LOGGER.isTraceEnabled()) {
Expand Down Expand Up @@ -281,7 +300,8 @@ public void execute(Connection con) throws SQLException {
PreparedStatement subStmt = con.prepareStatement(deltaQuery);
subStmt.setLong(1, resultList.getLong(RAuditEventRecord.ID_COLUMN_NAME));
ResultSet subResultList = subStmt.executeQuery();


OperationResult deltaResult = result.createMinorSubresult(OP_LOAD_AUDIT_DELTA);
try {
while (subResultList.next()) {
try {
Expand All @@ -291,15 +311,16 @@ public void execute(Connection con) throws SQLException {
}
} catch (DtoTranslationException ex) {
LOGGER.error("Cannot convert stored audit delta. Reason: {}", ex.getMessage(), ex);
//do not throw an error. rather audit record without delta then fatal error.
// TODO: consider using OperationResult
deltaResult.recordPartialError("Cannot convert stored audit delta. Reason: " + ex.getMessage(), ex);
//do not throw an error. rather audit record without delta than fatal error.
continue;
}

}
} finally {
subResultList.close();
subStmt.close();
deltaResult.computeStatus();
}

//query for properties
Expand Down Expand Up @@ -370,6 +391,7 @@ public void execute(Connection con) throws SQLException {
}
}finally {
stmt.close();
result.computeStatus();
}

//
Expand Down
Expand Up @@ -487,7 +487,7 @@ public String debugDump(int indent) {
}

@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params) {
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params, OperationResult result) {
throw new UnsupportedOperationException("Object retrieval not supported");
}

Expand All @@ -503,7 +503,7 @@ public boolean supportsRetrieval() {

@Override
public void listRecordsIterative(String query, Map<String, Object> params,
AuditResultHandler auditResultHandler) {
AuditResultHandler auditResultHandler, OperationResult result) {
// TODO Auto-generated method stub

}
Expand Down
Expand Up @@ -201,11 +201,11 @@ private void completeRecord(AuditEventRecord record, Task task) {
}

@Override
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params) {
public List<AuditEventRecord> listRecords(String query, Map<String, Object> params, OperationResult parentResult) {
List<AuditEventRecord> result = new ArrayList<>();
for (AuditService service : services) {
if (service.supportsRetrieval()) {
List<AuditEventRecord> records = service.listRecords(query, params);
List<AuditEventRecord> records = service.listRecords(query, params, parentResult);
if (records != null && !records.isEmpty()) {
result.addAll(records);
}
Expand All @@ -215,10 +215,10 @@ public List<AuditEventRecord> listRecords(String query, Map<String, Object> para
}

@Override
public void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler handler) {
public void listRecordsIterative(String query, Map<String, Object> params, AuditResultHandler handler, OperationResult result) {
for (AuditService service : services) {
if (service.supportsRetrieval()) {
service.listRecordsIterative(query, params, handler);
service.listRecordsIterative(query, params, handler, result);
}
}
}
Expand Down

0 comments on commit 7aed69d

Please sign in to comment.