Skip to content

Commit

Permalink
catalog: create events for all sample methods, #TASK-4178
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Oct 17, 2024
1 parent 34a1216 commit f8b1da4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.catalog.models.InternalGetDataResult;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.catalog.utils.UuidUtils;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.config.Configuration;
import org.opencb.opencga.core.events.OpencgaEvent;
Expand Down Expand Up @@ -397,7 +398,8 @@ protected <T> OpenCGAResult<T> runForSingleEntry(ObjectMap params, Enums.Resourc

String eventId = resource.name().toLowerCase() + "." + action.name().toLowerCase();

OpencgaEvent opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, userId, tokenPayload.getToken());
String eventUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.EVENT);
OpencgaEvent opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, userId, tokenPayload.getToken());
CatalogEvent catalogEvent = CatalogEvent.build(opencgaEvent);
try {
// Get study
Expand Down Expand Up @@ -466,7 +468,8 @@ protected <T> OpenCGAResult<T> runForQueryOperation(ObjectMap params, Enums.Reso

String eventId = resource.name().toLowerCase() + "." + action.name().toLowerCase();

OpencgaEvent opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, userId, tokenPayload.getToken());
String eventUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.EVENT);
OpencgaEvent opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, userId, tokenPayload.getToken());
CatalogEvent catalogEvent = CatalogEvent.build(opencgaEvent);
try {
// Get study
Expand Down Expand Up @@ -534,7 +537,8 @@ protected <T> OpenCGAResult<T> runForMultiOperation(ObjectMap params, Enums.Reso

String eventId = resource.name().toLowerCase() + "." + action.name().toLowerCase();

OpencgaEvent opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, userId, tokenPayload.getToken());
String eventUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.EVENT);
OpencgaEvent opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, userId, tokenPayload.getToken());
CatalogEvent catalogEvent = CatalogEvent.build(opencgaEvent);
try {
// Get study
Expand Down Expand Up @@ -599,7 +603,8 @@ protected <T> OpenCGAResult<T> runIterator(ObjectMap params, Enums.Resource reso

String eventId = resource.name().toLowerCase() + "." + action.name().toLowerCase();

OpencgaEvent opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, userId, tokenPayload.getToken());
String eventUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.EVENT);
OpencgaEvent opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, userId, tokenPayload.getToken());
CatalogEvent catalogEvent = CatalogEvent.build(opencgaEvent);
Study study;
try {
Expand Down Expand Up @@ -631,7 +636,7 @@ protected <T> OpenCGAResult<T> runIterator(ObjectMap params, Enums.Resource reso
while (iterator.hasNext()) {
T object = iterator.next();

opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, study.getFqn(), study.getUuid(), userId,
opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, study.getFqn(), study.getUuid(), userId,
tokenPayload.getToken());
catalogEvent = CatalogEvent.build(opencgaEvent);
EntryParam entryParam = new EntryParam();
Expand Down Expand Up @@ -677,7 +682,8 @@ protected <T> OpenCGAResult<T> runList(ObjectMap params, Enums.Resource resource

String eventId = resource.name().toLowerCase() + "." + action.name().toLowerCase();

OpencgaEvent opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, userId, tokenPayload.getToken());
String eventUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.EVENT);
OpencgaEvent opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, userId, tokenPayload.getToken());
CatalogEvent catalogEvent = CatalogEvent.build(opencgaEvent);
Study study;
try {
Expand All @@ -691,7 +697,7 @@ protected <T> OpenCGAResult<T> runList(ObjectMap params, Enums.Resource resource
// Execute code
OpenCGAResult<T> result = OpenCGAResult.empty();
for (String id : idList) {
opencgaEvent = OpencgaEvent.build(eventId, params, organizationId, study.getFqn(), study.getUuid(), userId,
opencgaEvent = OpencgaEvent.build(eventUuid, eventId, params, organizationId, study.getFqn(), study.getUuid(), userId,
tokenPayload.getToken());
catalogEvent = CatalogEvent.build(opencgaEvent);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ public AuditManager(AuthorizationManager authorizationManager, CatalogManager ca
Enums.Resource resource = Enums.Resource.valueOf(split[0].toUpperCase());
Enums.Action action = Enums.Action.valueOf(split[1].toUpperCase());
if (CollectionUtils.isNotEmpty(opencgaEvent.getEntries())) {
auditBatchOperation(opencgaEvent.getOrganizationId(), operationUuid -> {
for (EntryParam entry : opencgaEvent.getEntries()) {
audit(opencgaEvent.getOrganizationId(), operationUuid, opencgaEvent.getUserId(), action, resource, entry.getId(),
entry.getUuid(), opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(), opencgaEvent.getInputParams(),
new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS));
}
});
for (EntryParam entry : opencgaEvent.getEntries()) {
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getOperationId(), opencgaEvent.getUserId(), action, resource,
entry.getId(), entry.getUuid(), opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(),
opencgaEvent.getInputParams(), new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS));
}
} else {
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getUserId(), action, resource, "", "", opencgaEvent.getStudyFqn(),
opencgaEvent.getStudyUuid(), opencgaEvent.getInputParams(),
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getOperationId(), opencgaEvent.getUserId(), action, resource, "", "",
opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(), opencgaEvent.getInputParams(),
new AuditRecord.Status(AuditRecord.Status.Result.SUCCESS));
}
}, (throwable, opencgaEvent) -> {
Expand All @@ -92,35 +90,22 @@ public AuditManager(AuthorizationManager authorizationManager, CatalogManager ca
Enums.Action action = Enums.Action.valueOf(split[1].toUpperCase());

if (CollectionUtils.isNotEmpty(opencgaEvent.getEntries())) {
auditBatchOperation(opencgaEvent.getOrganizationId(), operationUuid -> {
for (EntryParam entry : opencgaEvent.getEntries()) {
audit(opencgaEvent.getOrganizationId(), operationUuid, opencgaEvent.getUserId(), action, resource,
entry.getId(), entry.getUuid(), opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(),
opencgaEvent.getInputParams(), new AuditRecord.Status(AuditRecord.Status.Result.ERROR,
new Error(0, throwable.getMessage(), throwable.getLocalizedMessage())));
}
});
for (EntryParam entry : opencgaEvent.getEntries()) {
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getOperationId(), opencgaEvent.getUserId(), action, resource,
entry.getId(), entry.getUuid(), opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(),
opencgaEvent.getInputParams(), new AuditRecord.Status(AuditRecord.Status.Result.ERROR,
new Error(0, throwable.getMessage(), throwable.getLocalizedMessage())));
}
} else {
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getUserId(), action, resource, "", "", opencgaEvent.getStudyFqn(),
opencgaEvent.getStudyUuid(), opencgaEvent.getInputParams(), new AuditRecord.Status(AuditRecord.Status.Result.ERROR,
audit(opencgaEvent.getOrganizationId(), opencgaEvent.getOperationId(), opencgaEvent.getUserId(), action, resource, "", "",
opencgaEvent.getStudyFqn(), opencgaEvent.getStudyUuid(), opencgaEvent.getInputParams(),
new AuditRecord.Status(AuditRecord.Status.Result.ERROR,
new Error(0, throwable.getMessage(), throwable.getLocalizedMessage())));
}

}, true));
}

private interface AuditBatchOperation {
void execute(String operationUuid);
}

private void auditBatchOperation(String organizationId, AuditBatchOperation operation) {
String operationUuid = UuidUtils.generateOpenCgaUuid(UuidUtils.Entity.AUDIT);
initAuditBatch(operationUuid);
operation.execute(operationUuid);
finishAuditBatch(organizationId, operationUuid);
}


public void audit(String organizationId, AuditRecord auditRecord) throws CatalogException {
dbAdaptorFactory.getCatalogAuditDbAdaptor(organizationId).insertAuditRecord(auditRecord);
}
Expand Down
Loading

0 comments on commit f8b1da4

Please sign in to comment.