diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/CleanupIndiciesApi.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/CleanupIndiciesApi.java index c398280a6226e8066ff5be18b6ca2972537651a8..9feff87940140c2f3f7688c0573f973c765842cb 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/CleanupIndiciesApi.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/CleanupIndiciesApi.java @@ -48,6 +48,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.annotation.RequestScope; import springfox.documentation.annotations.ApiIgnore; +import static java.util.Collections.singletonList; @Log @RestController @@ -106,19 +107,21 @@ public class CleanupIndiciesApi { } } - @DeleteMapping(value = "/delete-index-for-kind", produces = MediaType.APPLICATION_JSON_VALUE) + @DeleteMapping(value = "/index", produces = MediaType.APPLICATION_JSON_VALUE) + @PreAuthorize("@authorizationFilter.hasPermission('" + SearchServiceRole.ADMIN + "')") public ResponseEntity deleteIndex(@RequestParam("kind") @NotBlank @ValidKind String kind) { + String index = elasticIndexNameResolver.getIndexNameFromKind(kind); try { - String index = elasticIndexNameResolver.getIndexNameFromKind(kind); boolean responseStatus = indicesService.deleteIndex(index); if (responseStatus) { - return new ResponseEntity(HttpStatus.OK); + this.auditLogger.indexDeleteSuccess(singletonList(index)); } + return new ResponseEntity(HttpStatus.OK); } catch (AppException e) { throw e; } catch (Exception e) { - throw new AppException(HttpStatus.BAD_REQUEST.value(), "Unknown error", "An unknown error has occurred.", e); + this.auditLogger.indexDeleteFail(singletonList(index)); + throw new AppException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "Unknown error", "An unknown error has occurred.", e); } - return new ResponseEntity(HttpStatus.BAD_REQUEST); } } diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java index 703d6747e2510d71ad0b27d1fe9843c697ea5ea8..2801c275d51c09cc9e6e314b05b2d791e3e15e57 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java @@ -60,6 +60,10 @@ public class AuditEvents { private static final String CONFIGURE_PARTITION_ACTION_ID = "IN0012"; private static final String CONFIGURE_PARTITION_OPERATION = "Data partition cluster configuration update"; + private static final String INDEX_DELETE_ACTION_ID = "IN0012"; + private static final String INDEX_DELETE_SUCCESS = "Successfully deleted index"; + private static final String INDEX_DELETE_FAILURE = "Failed deleting index"; + private final String user; public AuditEvents(String user) { @@ -135,6 +139,28 @@ public class AuditEvents { .build(); } + public AuditPayload getIndexDeleteFailEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_DELETE_ACTION_ID) + .message(INDEX_DELETE_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexDeleteSuccessEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_DELETE_ACTION_ID) + .message(INDEX_DELETE_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } + public AuditPayload getIndexPurgeRecordSuccessEvent(List<String> resources) { return AuditPayload.builder() .action(AuditAction.DELETE) diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java index 1a37d76881a1bbc49bb2b4c97cc037f8a8af2a32..d6940eb57a145e2c79115b90c501b168d7dab1a2 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java @@ -65,6 +65,14 @@ public class AuditLogger { this.writeLog(this.getAuditEvents().getIndexDeleteRecordFailEvent(resources)); } + public void indexDeleteSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexDeleteSuccessEvent(resources)); + } + + public void indexDeleteFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexDeleteFailEvent(resources)); + } + public void indexPurgeRecordSuccess(List<String> resources) { this.writeLog(this.getAuditEvents().getIndexPurgeRecordSuccessEvent(resources)); }