Commit b3c5ded6 authored by Artem Dobrynin (EPAM)'s avatar Artem Dobrynin (EPAM)
Browse files

GONRG-1547: Audit. Implement some audit events for Schema [publish]

https://jiraeu.epam.com/browse/GONRG-1547

* Added audit logger
* Added logging events in the `SchemaService`
parent 0f045088
package org.opengroup.osdu.schema.logging;
import static java.lang.String.format;
import com.google.common.base.Strings;
import java.util.List;
import org.opengroup.osdu.core.common.logging.audit.AuditAction;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.opengroup.osdu.core.common.logging.audit.AuditStatus;
public class AuditEvents {
public static final String SCHEMA_REGISTERED_ID = "SC001";
public static final String SCHEMA_REGISTERED_MESSAGE = "Schema registered ";
public static final String SCHEMA_RETRIEVED_ID = "SC002";
public static final String SCHEMA_RETRIEVED_MESSAGE = "Schema retrieved";
public static final String SEARCH_FOR_SCHEMA_ID = "SC003";
public static final String SEARCH_FOR_SCHEMA_MESSAGE = "Search for schema";
public static final String SCHEMA_UPDATED_ID = "SC004";
public static final String SCHEMA_UPDATED_MESSAGE = "Schema update";
private final String user;
public AuditEvents(String user) {
if (Strings.isNullOrEmpty(user)) {
throw new IllegalArgumentException("User not provided for audit events.");
}
this.user = user;
}
public AuditPayload getSchemaRegistered(AuditStatus status, List<String> resources){
return AuditPayload.builder()
.action(AuditAction.CREATE)
.status(status)
.user(this.user)
.actionId(SCHEMA_REGISTERED_ID)
.message(getStatusMessage(status, SCHEMA_REGISTERED_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getSchemaRetrieved(AuditStatus status, List<String> resources){
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(SCHEMA_RETRIEVED_ID)
.message(getStatusMessage(status, SCHEMA_RETRIEVED_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getSearchForSchema(AuditStatus status, List<String> resources){
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(SEARCH_FOR_SCHEMA_ID)
.message(getStatusMessage(status, SEARCH_FOR_SCHEMA_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getSchemaUpdated(AuditStatus status, List<String> resources){
return AuditPayload.builder()
.action(AuditAction.UPDATE)
.status(status)
.user(this.user)
.actionId(SCHEMA_UPDATED_ID)
.message(getStatusMessage(status, SCHEMA_UPDATED_MESSAGE))
.resources(resources)
.build();
}
private String getStatusMessage(AuditStatus status, String message) {
return format("%s %s", message, status.name().toLowerCase());
}
}
package org.opengroup.osdu.schema.logging;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.logging.audit.AuditPayload;
import org.opengroup.osdu.core.common.logging.audit.AuditStatus;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
@Component
@RequiredArgsConstructor
@RequestScope
public class AuditLogger {
private final JaxRsDpsLog logger;
private final DpsHeaders headers;
private AuditEvents events = null;
private AuditEvents getAuditEvents() {
if (this.events == null) {
this.events = new AuditEvents(this.headers.getUserEmail());
}
return this.events;
}
public void schemaRegisteredSuccess(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaRegistered(AuditStatus.SUCCESS, resources));
}
public void schemaRegisteredFailure(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaRegistered(AuditStatus.FAILURE, resources));
}
public void schemaRetrievedSuccess(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaRetrieved(AuditStatus.SUCCESS, resources));
}
public void schemaRetrievedFailure(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaRetrieved(AuditStatus.FAILURE, resources));
}
public void searchSchemaSuccess(List<String> resources){
this.writeLog(this.getAuditEvents().getSearchForSchema(AuditStatus.SUCCESS, resources));
}
public void searchSchemaFailure(List<String> resources){
this.writeLog(this.getAuditEvents().getSearchForSchema(AuditStatus.FAILURE, resources));
}
public void schemaUpdatedSuccess(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaUpdated(AuditStatus.SUCCESS, resources));
}
public void schemaUpdatedFailure(List<String> resources){
this.writeLog(this.getAuditEvents().getSchemaUpdated(AuditStatus.FAILURE, resources));
}
private void writeLog(AuditPayload log) {
this.logger.audit(log);
}
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package org.opengroup.osdu.schema.service.serviceimpl; ...@@ -3,6 +3,7 @@ package org.opengroup.osdu.schema.service.serviceimpl;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
...@@ -20,6 +21,7 @@ import org.opengroup.osdu.schema.exceptions.ApplicationException; ...@@ -20,6 +21,7 @@ import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.exceptions.BadRequestException; import org.opengroup.osdu.schema.exceptions.BadRequestException;
import org.opengroup.osdu.schema.exceptions.NoSchemaFoundException; import org.opengroup.osdu.schema.exceptions.NoSchemaFoundException;
import org.opengroup.osdu.schema.exceptions.NotFoundException; import org.opengroup.osdu.schema.exceptions.NotFoundException;
import org.opengroup.osdu.schema.logging.AuditLogger;
import org.opengroup.osdu.schema.model.QueryParams; import org.opengroup.osdu.schema.model.QueryParams;
import org.opengroup.osdu.schema.model.SchemaIdentity; import org.opengroup.osdu.schema.model.SchemaIdentity;
import org.opengroup.osdu.schema.model.SchemaInfo; import org.opengroup.osdu.schema.model.SchemaInfo;
...@@ -50,6 +52,9 @@ import com.google.gson.Gson; ...@@ -50,6 +52,9 @@ import com.google.gson.Gson;
@Service @Service
public class SchemaService implements ISchemaService { public class SchemaService implements ISchemaService {
@Autowired
private AuditLogger auditLogger;
@Autowired @Autowired
private ISchemaInfoStore schemaInfoStore; private ISchemaInfoStore schemaInfoStore;
...@@ -99,11 +104,13 @@ public class SchemaService implements ISchemaService { ...@@ -99,11 +104,13 @@ public class SchemaService implements ISchemaService {
schema = schemaStore.getSchema(sharedTenant, schemaId); schema = schemaStore.getSchema(sharedTenant, schemaId);
} }
auditLogger.schemaRetrievedSuccess(Collections.singletonList(schema.toString()));
return schema; return schema;
} }
private void validateSchemaId(String schemaId) throws BadRequestException { private void validateSchemaId(String schemaId) throws BadRequestException {
if (StringUtils.isEmpty(schemaId)) { if (StringUtils.isEmpty(schemaId)) {
auditLogger.schemaRetrievedFailure(Collections.singletonList(schemaId));
log.error(SchemaConstants.EMPTY_ID); log.error(SchemaConstants.EMPTY_ID);
throw new BadRequestException(SchemaConstants.EMPTY_ID); throw new BadRequestException(SchemaConstants.EMPTY_ID);
} }
...@@ -144,8 +151,12 @@ public class SchemaService implements ISchemaService { ...@@ -144,8 +151,12 @@ public class SchemaService implements ISchemaService {
try { try {
SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest); SchemaInfo schemaInfo = schemaInfoStore.createSchemaInfo(schemaRequest);
schemaStore.createSchema(schemaId, schema); schemaStore.createSchema(schemaId, schema);
auditLogger.schemaRegisteredSuccess(
Collections.singletonList(schemaRequest.toString()));
return schemaInfo; return schemaInfo;
} catch (ApplicationException ex) { } catch (ApplicationException ex) {
auditLogger.schemaRegisteredFailure(
Collections.singletonList(schemaRequest.toString()));
log.warning(SchemaConstants.SCHEMA_CREATION_FAILED); log.warning(SchemaConstants.SCHEMA_CREATION_FAILED);
schemaInfoStore.cleanSchema(schemaId); schemaInfoStore.cleanSchema(schemaId);
schemaStore.cleanSchemaProject(schemaId); schemaStore.cleanSchemaProject(schemaId);
...@@ -197,10 +208,12 @@ public class SchemaService implements ISchemaService { ...@@ -197,10 +208,12 @@ public class SchemaService implements ISchemaService {
Gson gson = new Gson(); Gson gson = new Gson();
String schema = schemaResolver.resolveSchema(gson.toJson(schemaRequest.getSchema())); String schema = schemaResolver.resolveSchema(gson.toJson(schemaRequest.getSchema()));
SchemaInfo schInfo = schemaInfoStore.updateSchemaInfo(schemaRequest); SchemaInfo schInfo = schemaInfoStore.updateSchemaInfo(schemaRequest);
auditLogger.schemaUpdatedSuccess(Collections.singletonList(schemaRequest.toString()));
schemaStore.createSchema(schemaRequest.getSchemaInfo().getSchemaIdentity().getId(), schema); schemaStore.createSchema(schemaRequest.getSchemaInfo().getSchemaIdentity().getId(), schema);
log.info(SchemaConstants.SCHEMA_UPDATED); log.info(SchemaConstants.SCHEMA_UPDATED);
return schInfo; return schInfo;
} else { } else {
auditLogger.schemaUpdatedFailure(Collections.singletonList(schemaRequest.toString()));
log.error(SchemaConstants.SCHEMA_UPDATE_ERROR); log.error(SchemaConstants.SCHEMA_UPDATE_ERROR);
throw new BadRequestException(SchemaConstants.SCHEMA_UPDATE_EXCEPTION); throw new BadRequestException(SchemaConstants.SCHEMA_UPDATE_EXCEPTION);
} }
...@@ -258,6 +271,13 @@ public class SchemaService implements ISchemaService { ...@@ -258,6 +271,13 @@ public class SchemaService implements ISchemaService {
.sorted(compareByCreatedDate) .sorted(compareByCreatedDate)
.limit(queryParams.getLimit()).collect(Collectors.toList()); .limit(queryParams.getLimit()).collect(Collectors.toList());
if (schemaFinalList.isEmpty()){
auditLogger.searchSchemaFailure(Collections.singletonList(queryParams.toString()));
} else {
auditLogger.searchSchemaSuccess(schemaFinalList.stream()
.map(SchemaInfo::toString)
.collect(Collectors.toList()));
}
return SchemaInfoResponse.builder().schemaInfos(schemaFinalList).count(schemaFinalList.size()) return SchemaInfoResponse.builder().schemaInfos(schemaFinalList).count(schemaFinalList.size())
.offset(queryParams.getOffset()).totalCount(schemaList.size()).build(); .offset(queryParams.getOffset()).totalCount(schemaList.size()).build();
} }
......
...@@ -29,6 +29,7 @@ import org.opengroup.osdu.schema.exceptions.ApplicationException; ...@@ -29,6 +29,7 @@ import org.opengroup.osdu.schema.exceptions.ApplicationException;
import org.opengroup.osdu.schema.exceptions.BadRequestException; import org.opengroup.osdu.schema.exceptions.BadRequestException;
import org.opengroup.osdu.schema.exceptions.NoSchemaFoundException; import org.opengroup.osdu.schema.exceptions.NoSchemaFoundException;
import org.opengroup.osdu.schema.exceptions.NotFoundException; import org.opengroup.osdu.schema.exceptions.NotFoundException;
import org.opengroup.osdu.schema.logging.AuditLogger;
import org.opengroup.osdu.schema.model.QueryParams; import org.opengroup.osdu.schema.model.QueryParams;
import org.opengroup.osdu.schema.model.SchemaIdentity; import org.opengroup.osdu.schema.model.SchemaIdentity;
import org.opengroup.osdu.schema.model.SchemaInfo; import org.opengroup.osdu.schema.model.SchemaInfo;
...@@ -80,6 +81,9 @@ public class SchemaServiceTest { ...@@ -80,6 +81,9 @@ public class SchemaServiceTest {
@Mock @Mock
JaxRsDpsLog log; JaxRsDpsLog log;
@Mock
AuditLogger auditLogger;
@Value("${shared.tenant.name:common}") @Value("${shared.tenant.name:common}")
private String sharedTenant; private String sharedTenant;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment