diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java index b4a7f30a05db4ad8d2422698eb582c61b8906f6c..caeeac9c9c13c3c5b9ce76611c99df0bdb9e3bd0 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/ReindexApi.java @@ -15,7 +15,7 @@ package org.opengroup.osdu.indexer.api; import org.opengroup.osdu.core.common.model.coreis.SearchServiceRole; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest; import org.opengroup.osdu.indexer.service.ReindexService; import org.springframework.http.ResponseEntity; 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 new file mode 100644 index 0000000000000000000000000000000000000000..0530c5d4316af4ca91ebeafa55f08ad25c39adce --- /dev/null +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java @@ -0,0 +1,218 @@ +// Copyright 2017-2019, Schlumberger +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.logging; + +import com.google.common.base.Strings; +import org.opengroup.osdu.core.common.service.logging.audit.AuditAction; +import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload; +import org.opengroup.osdu.core.common.service.logging.audit.AuditStatus; + +import java.util.List; + +public class AuditEvents { + + private static final String INDEX_CREATE_RECORD_ACTION_ID = "IN001"; + private static final String INDEX_CREATE_RECORDS_SUCCESS = "Successfully created record in index"; + private static final String INDEX_CREATE_RECORDS_FAILURE = "Failed creating record in index"; + + private static final String INDEX_UPDATE_RECORD_ACTION_ID = "IN002"; + private static final String INDEX_UPDATE_RECORDS_SUCCESS = "Successfully updated record in index"; + private static final String INDEX_UPDATE_RECORDS_FAILURE = "Failed updating record in index"; + + private static final String INDEX_DELETE_RECORD_ACTION_ID = "IN003"; + private static final String INDEX_DELETE_RECORDS_SUCCESS = "Successfully deleted record in index"; + private static final String INDEX_DELETE_RECORDS_FAILURE = "Failed deleting record in index"; + + private static final String INDEX_PURGE_RECORD_ACTION_ID = "IN004"; + + private static final String REINDEX_KIND_ACTION_ID = "IN007"; + private static final String REINDEX_KIND_OPERATION = "Reindex kind"; + + private static final String COPY_INDEX_ACTION_ID = "IN008"; + private static final String COPY_INDEX_OPERATION = "Copy index"; + + private static final String GET_TASK_STATUS_ACTION_ID = "IN009"; + private static final String GET_TASK_STATUS_OPERATION = "Get task status"; + + private static final String RUN_JOB_ACTION_ID = "IN010"; + private static final String RUN_JOB_MESSAGE_SUCCESS = "Index clean-up status job run success"; + + private static final String INDEX_MAPPING_UPDATE_ACTION_ID = "IN0011"; + private static final String INDEX_MAPPING_UPDATE_SUCCESS = "Successfully updated index mapping"; + private static final String INDEX_MAPPING_UPDATE_FAILURE = "Failed updating index mapping"; + + 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 getIndexCreateRecordSuccessEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.CREATE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_CREATE_RECORD_ACTION_ID) + .message(INDEX_CREATE_RECORDS_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexCreateRecordFailEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.CREATE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_CREATE_RECORD_ACTION_ID) + .message(INDEX_CREATE_RECORDS_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexUpdateRecordSuccessEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.UPDATE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_UPDATE_RECORD_ACTION_ID) + .message(INDEX_UPDATE_RECORDS_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexUpdateRecordFailEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.UPDATE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_UPDATE_RECORD_ACTION_ID) + .message(INDEX_UPDATE_RECORDS_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexDeleteRecordSuccessEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_DELETE_RECORD_ACTION_ID) + .message(INDEX_DELETE_RECORDS_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexDeleteRecordFailEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_DELETE_RECORD_ACTION_ID) + .message(INDEX_DELETE_RECORDS_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexPurgeRecordSuccessEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_PURGE_RECORD_ACTION_ID) + .message(INDEX_DELETE_RECORDS_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexPurgeRecordFailEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_PURGE_RECORD_ACTION_ID) + .message(INDEX_DELETE_RECORDS_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getReindexEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.CREATE) + .status(AuditStatus.SUCCESS) + .actionId(REINDEX_KIND_ACTION_ID) + .message(REINDEX_KIND_OPERATION) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getCopyIndexEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.CREATE) + .status(AuditStatus.SUCCESS) + .actionId(COPY_INDEX_ACTION_ID) + .message(COPY_INDEX_OPERATION) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getTaskStatusEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.READ) + .status(AuditStatus.SUCCESS) + .actionId(GET_TASK_STATUS_ACTION_ID) + .message(GET_TASK_STATUS_OPERATION) + .resources(resources) + .user(this.user) + .build(); + } + + public AuditPayload getIndexCleanUpJobRunEvent(List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.JOB_RUN) + .status(AuditStatus.SUCCESS) + .user(this.user) + .actionId(RUN_JOB_ACTION_ID) + .message(RUN_JOB_MESSAGE_SUCCESS) + .resources(resources) + .build(); + } + + public AuditPayload getIndexMappingUpdateEvent(List<String> resources, boolean isSuccess) { + if (isSuccess) { + return AuditPayload.builder() + .action(AuditAction.UPDATE) + .status(AuditStatus.SUCCESS) + .actionId(INDEX_MAPPING_UPDATE_ACTION_ID) + .message(INDEX_MAPPING_UPDATE_SUCCESS) + .resources(resources) + .user(this.user) + .build(); + } else { + return AuditPayload.builder() + .action(AuditAction.UPDATE) + .status(AuditStatus.FAILURE) + .actionId(INDEX_MAPPING_UPDATE_ACTION_ID) + .message(INDEX_MAPPING_UPDATE_FAILURE) + .resources(resources) + .user(this.user) + .build(); + } + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..48b7644a1dd4546434716a99a4734b141ddc6046 --- /dev/null +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java @@ -0,0 +1,102 @@ +// Copyright 2017-2019, Schlumberger +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.opengroup.osdu.indexer.logging; + +import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog; +import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload; +import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +import javax.inject.Inject; +import java.util.List; + +@Component +@RequestScope +public class AuditLogger { + + @Inject + private JaxRsDpsLog logger; + @Inject + private IHeadersInfo headers; + + private AuditEvents events = null; + + private AuditEvents getAuditEvents() { + if (this.events == null) { + this.events = new AuditEvents(this.headers.getUser()); + } + return this.events; + } + + public void indexCreateRecordSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexCreateRecordSuccessEvent(resources)); + } + + public void indexCreateRecordFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexCreateRecordFailEvent(resources)); + } + + public void indexUpdateRecordSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexUpdateRecordSuccessEvent(resources)); + } + + public void indexUpdateRecordFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexUpdateRecordFailEvent(resources)); + } + + public void indexDeleteRecordSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexDeleteRecordSuccessEvent(resources)); + } + + public void indexDeleteRecordFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexDeleteRecordFailEvent(resources)); + } + + public void indexPurgeRecordSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexPurgeRecordSuccessEvent(resources)); + } + + public void indexPurgeRecordFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexPurgeRecordFailEvent(resources)); + } + + public void getReindex(List<String> resources) { + this.writeLog(this.getAuditEvents().getReindexEvent(resources)); + } + + public void copyIndex(List<String> resources) { + this.writeLog(this.getAuditEvents().getCopyIndexEvent(resources)); + } + + public void getTaskStatus(List<String> resources) { + this.writeLog(this.getAuditEvents().getTaskStatusEvent(resources)); + } + + public void getIndexCleanUpJobRun(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexCleanUpJobRunEvent(resources)); + } + + public void indexMappingUpdateSuccess(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexMappingUpdateEvent(resources,true)); + } + public void indexMappingUpdateFail(List<String> resources) { + this.writeLog(this.getAuditEvents().getIndexMappingUpdateEvent(resources,false)); + } + + private void writeLog(AuditPayload log) { + this.logger.audit(log); + } +} \ No newline at end of file diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java index 19280c31e1c4435628434644ce4db1b1c4e4e8a9..fb79db44f844e6720d7445cae6fc4fde66c7aa3b 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImpl.java @@ -30,7 +30,7 @@ import org.opengroup.osdu.core.common.model.core.DpsHeaders; import org.opengroup.osdu.core.common.model.core.TenantInfo; import org.opengroup.osdu.core.common.model.AppException; import org.opengroup.osdu.core.common.service.coreis.*; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo; import org.opengroup.osdu.is.core.service.ElasticSettingService; import org.opengroup.osdu.is.core.util.ElasticClientHandler; diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java index 78536790b969b863cbd4d81cb90c573b69f90d18..23fbea57c47f329a0135a136d386884e34b01861 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IndexerServiceImpl.java @@ -37,6 +37,7 @@ import org.opengroup.osdu.core.common.model.indexer.*; import org.opengroup.osdu.core.common.model.storage.ConversionStatus; import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.spi.indexer.IPublisher; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.indexer.util.IndexerQueueTaskBuilder; import org.opengroup.osdu.core.common.model.coreis.RequestStatus; import org.opengroup.osdu.core.common.model.coreis.RecordChangedMessages; diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java index eda802f62da817fc23fbaa8f905aa03a39404857..212baccf067101cb90ed560284a60f0567be4f6c 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/api/ReindexApiTest.java @@ -20,7 +20,7 @@ import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.opengroup.osdu.core.common.model.AppException; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.core.common.model.indexer.RecordReindexRequest; import org.opengroup.osdu.indexer.service.ReindexService; diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java index 05a62448a8be05b3847bd74b4f4256acddfe5c94..3157d0e3a45ef1243a0af09a784b8897c4c0af73 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditEventsTest.java @@ -18,7 +18,6 @@ import com.google.common.collect.Lists; import org.junit.Test; import org.junit.runner.RunWith; import org.opengroup.osdu.core.common.service.logging.audit.AuditAction; -import org.opengroup.osdu.core.common.model.indexer.AuditEvents; import org.opengroup.osdu.core.common.service.logging.audit.AuditStatus; import org.springframework.test.context.junit4.SpringRunner; diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java index 5c6ed9873046a09e6359dc4de846691b089b9d9b..70fdbdf2cf6f06991cb728a45e21a2f21b0cb78e 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/logging/AuditLoggerTest.java @@ -21,7 +21,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; import org.opengroup.osdu.core.common.service.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.service.logging.audit.AuditPayload; import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo; diff --git a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java index 8d17050f3ca014aea14b5bb25fdfee6325dbde36..3fb280b7ea236ec78e5058d1060f38eeb13b20ec 100644 --- a/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java +++ b/provider/indexer-azure/src/test/java/org/opengroup/osdu/indexer/azure/service/IndexCopyServiceImplTest.java @@ -31,7 +31,7 @@ import org.mockito.ArgumentMatchers; import org.mockito.InjectMocks; import org.mockito.Mock; import org.opengroup.osdu.core.common.model.core.DpsHeaders; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.indexer.service.IndexCopyServiceImpl; import org.opengroup.osdu.indexer.service.IndexerMappingService; import org.opengroup.osdu.core.common.model.core.ClusterSettings; diff --git a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java b/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java index b1a6cccc1e1087633a1b6af9d61f0a8404ffd06b..295eed07876ac9d50c49cc0ca568b278be4a736b 100644 --- a/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java +++ b/provider/indexer-gcp/src/test/java/org/opengroup/osdu/indexer/service/IndexCopyServiceImplTest.java @@ -34,7 +34,7 @@ import org.mockito.Mock; import org.opengroup.osdu.core.common.model.core.ClusterSettings; import org.opengroup.osdu.core.common.model.core.DpsHeaders; import org.opengroup.osdu.core.common.model.AppException; -import org.opengroup.osdu.core.common.model.indexer.AuditLogger; +import org.opengroup.osdu.indexer.logging.AuditLogger; import org.opengroup.osdu.core.common.spi.coreis.IHeadersInfo; import org.opengroup.osdu.core.common.spi.coreis.IRequestInfo; import org.opengroup.osdu.is.core.service.ElasticSettingService;