diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/api/HealthCheck.java b/partition-core/src/main/java/org/opengroup/osdu/partition/api/HealthCheck.java index 807f1bc1c41dd99462361ab1a1045f81c0d09894..f76c6b02c59495e6604bc605a095b3add06bd543 100644 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/api/HealthCheck.java +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/api/HealthCheck.java @@ -14,6 +14,9 @@ package org.opengroup.osdu.partition.api; +import java.util.Collections; +import org.opengroup.osdu.partition.logging.AuditLogger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -24,9 +27,14 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(path= "/_ah", produces = "application/json") public class HealthCheck { + @Autowired + private AuditLogger auditLogger; + @GetMapping("/liveness_check") public ResponseEntity<String> livenessCheck() { - return new ResponseEntity<>("Partition service is alive", HttpStatus.OK); + ResponseEntity responseEntity = new ResponseEntity<>("Partition service is alive", HttpStatus.OK); + this.auditLogger.readServiceLivenessSuccess(Collections.singletonList(responseEntity.toString())); + return responseEntity; } @GetMapping("/readiness_check") diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java b/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java index 1eb890c15abca738ea5d6922e2d29d6193dc7844..860086cf902a557715cd74807f776fdcec66a4ad 100644 --- a/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/api/PartitionApi.java @@ -14,6 +14,8 @@ package org.opengroup.osdu.partition.api; +import java.util.Collections; +import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; @@ -40,11 +42,15 @@ public class PartitionApi { @Qualifier("cachedPartitionServiceImpl") private IPartitionService partitionService; + @Autowired + private AuditLogger auditLogger; + @PostMapping("/{partitionId}") @PreAuthorize("@authorizationFilter.hasPermissions()") public ResponseEntity create(@PathVariable("partitionId") String partitionId, @RequestBody @Valid PartitionInfo partitionInfo) { this.partitionService.createPartition(partitionId, partitionInfo); URI partitionLocation = ServletUriComponentsBuilder.fromCurrentRequest().buildAndExpand().toUri(); + this.auditLogger.createPartitionSuccess(Collections.singletonList(partitionId)); return ResponseEntity.created(partitionLocation).build(); } @@ -53,12 +59,14 @@ public class PartitionApi { @ResponseStatus(HttpStatus.NO_CONTENT) public void patch(@PathVariable("partitionId") String partitionId, @RequestBody @Valid PartitionInfo partitionInfo) { this.partitionService.updatePartition(partitionId, partitionInfo); + this.auditLogger.updatePartitionSecretSuccess(Collections.singletonList(partitionId)); } @GetMapping("/{partitionId}") @PreAuthorize("@authorizationFilter.hasPermissions()") public ResponseEntity<Map<String, Property>> get(@PathVariable("partitionId") String partitionId) { PartitionInfo partitionInfo = this.partitionService.getPartition(partitionId); + this.auditLogger.readPartitionSuccess(Collections.singletonList(partitionId)); return ResponseEntity.ok(partitionInfo.getProperties()); } @@ -66,6 +74,7 @@ public class PartitionApi { @PreAuthorize("@authorizationFilter.hasPermissions()") public ResponseEntity delete(@PathVariable("partitionId") String partitionId) { this.partitionService.deletePartition(partitionId); + this.auditLogger.deletePartitionSuccess(Collections.singletonList(partitionId)); return ResponseEntity.noContent().build(); } @@ -73,6 +82,8 @@ public class PartitionApi { @PreAuthorize("@authorizationFilter.hasPermissions()") public List<String> list() { List<String> partitions = this.partitionService.getAllPartitions(); + this.auditLogger.readListPartitionSuccess( + Collections.singletonList(String.format("Partition list size = %s", partitions.size()))); return partitions; } } diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditEvents.java b/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditEvents.java new file mode 100644 index 0000000000000000000000000000000000000000..e6c7c30dd2a2f47778e5f3a6c04436510796f8d8 --- /dev/null +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditEvents.java @@ -0,0 +1,129 @@ +/* + Copyright 2002-2021 Google LLC + Copyright 2002-2021 EPAM Systems, Inc + + 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.partition.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 { + + private static final String CREATE_PARTITION_ACTION_ID = "PT001"; + private static final String CREATE_PARTITION_MESSAGE = "Create partition"; + + private static final String READ_PARTITION_ACTION_ID = "PT002"; + private static final String READ_PARTITION_MESSAGE = "Read partition"; + + private static final String DELETE_PARTITION_ACTION_ID = "PT003"; + private static final String DELETE_PARTITION_MESSAGE = "Delete partition"; + + private static final String READ_SERVICE_LIVENESS_ACTION_ID = "PT004"; + private static final String READ_SERVICE_LIVENESS_MESSAGE = "Service run"; + + private static final String UPDATE_PARTITION_ACTION_ID = "PT005"; + private static final String UPDATE_PARTITION_MESSAGE = "Update partition"; + + private static final String READ_LIST_PARTITION_ACTION_ID = "PT006"; + private static final String READ_LIST_PARTITION_MESSAGE = "Read partition list"; + + + 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 getCreatePartitionEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.CREATE) + .status(status) + .user(this.user) + .actionId(CREATE_PARTITION_ACTION_ID) + .message(getStatusMessage(status, CREATE_PARTITION_MESSAGE)) + .resources(resources) + .build(); + } + + public AuditPayload getReadPartitionEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.READ) + .status(status) + .user(this.user) + .actionId(READ_PARTITION_ACTION_ID) + .message(getStatusMessage(status, READ_PARTITION_MESSAGE)) + .resources(resources) + .build(); + } + + public AuditPayload getDeletePartitionEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.DELETE) + .status(status) + .user(this.user) + .actionId(DELETE_PARTITION_ACTION_ID) + .message(getStatusMessage(status, DELETE_PARTITION_MESSAGE)) + .resources(resources) + .build(); + } + + public AuditPayload getReadServiceLivenessEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.READ) + .status(status) + .user(this.user) + .actionId(READ_SERVICE_LIVENESS_ACTION_ID) + .message(getStatusMessage(status, READ_SERVICE_LIVENESS_MESSAGE)) + .resources(resources) + .build(); + } + + public AuditPayload getUpdatePartitionSecretEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.UPDATE) + .status(status) + .user(this.user) + .actionId(UPDATE_PARTITION_ACTION_ID) + .message(getStatusMessage(status, UPDATE_PARTITION_MESSAGE)) + .resources(resources) + .build(); + } + + public AuditPayload getListPartitionEvent(AuditStatus status, List<String> resources) { + return AuditPayload.builder() + .action(AuditAction.READ) + .status(status) + .user(this.user) + .actionId(READ_LIST_PARTITION_ACTION_ID) + .message(getStatusMessage(status, READ_LIST_PARTITION_MESSAGE)) + .resources(resources) + .build(); + } + + private String getStatusMessage(AuditStatus status, String message) { + return format("%s - %s", message, status.name().toLowerCase()); + } +} \ No newline at end of file diff --git a/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditLogger.java b/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditLogger.java new file mode 100644 index 0000000000000000000000000000000000000000..472d5e260d0acbdb1097abe7211046477b8e2730 --- /dev/null +++ b/partition-core/src/main/java/org/opengroup/osdu/partition/logging/AuditLogger.java @@ -0,0 +1,94 @@ +/* + Copyright 2002-2021 Google LLC + Copyright 2002-2021 EPAM Systems, Inc + + 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.partition.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.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +@Component +@RequestScope +@RequiredArgsConstructor +public class AuditLogger { + + private final JaxRsDpsLog logger; + private AuditEvents events = null; + + private AuditEvents getAuditEvents() { + if (this.events == null) { + this.events = new AuditEvents("partitionAccountUser"); + } + return this.events; + } + + public void createPartitionSuccess(List<String> resources) { + writeLog(getAuditEvents().getCreatePartitionEvent(AuditStatus.SUCCESS, resources)); + } + + public void createPartitionFailure(List<String> resources) { + writeLog(getAuditEvents().getCreatePartitionEvent(AuditStatus.FAILURE, resources)); + } + + public void readPartitionSuccess(List<String> resources) { + writeLog(getAuditEvents().getReadPartitionEvent(AuditStatus.SUCCESS, resources)); + } + + public void readPartitionFailure(List<String> resources) { + writeLog(getAuditEvents().getReadPartitionEvent(AuditStatus.FAILURE, resources)); + } + + public void deletePartitionSuccess(List<String> resources) { + writeLog(getAuditEvents().getDeletePartitionEvent(AuditStatus.SUCCESS, resources)); + } + + public void deletePartitionFailure(List<String> resources) { + writeLog(getAuditEvents().getDeletePartitionEvent(AuditStatus.FAILURE, resources)); + } + + public void readServiceLivenessSuccess(List<String> resources) { + writeLog(getAuditEvents().getReadServiceLivenessEvent(AuditStatus.SUCCESS, resources)); + } + + public void readServiceLivenessFailure(List<String> resources) { + writeLog(getAuditEvents().getReadServiceLivenessEvent(AuditStatus.FAILURE, resources)); + } + + public void updatePartitionSecretSuccess(List<String> resources) { + writeLog(getAuditEvents().getUpdatePartitionSecretEvent(AuditStatus.SUCCESS, resources)); + } + + public void updatePartitionSecretFailure(List<String> resources) { + writeLog(getAuditEvents().getUpdatePartitionSecretEvent(AuditStatus.FAILURE, resources)); + } + + public void readListPartitionSuccess(List<String> resources) { + writeLog(getAuditEvents().getListPartitionEvent(AuditStatus.SUCCESS, resources)); + } + + public void readListPartitionFailure(List<String> resources) { + writeLog(getAuditEvents().getListPartitionEvent(AuditStatus.FAILURE, resources)); + } + + private void writeLog(AuditPayload log) { + this.logger.audit(log); + } +} \ No newline at end of file diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/api/HealthCheckTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/api/HealthCheckTest.java index d97325afcf9aba8ba787b8fd8e94013edf8e7f32..f21eaf8b7bb4fb81cd88cb9e48e5ed268ee16719 100644 --- a/partition-core/src/test/java/org/opengroup/osdu/partition/api/HealthCheckTest.java +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/api/HealthCheckTest.java @@ -14,19 +14,24 @@ package org.opengroup.osdu.partition.api; -import org.junit.Before; +import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.partition.logging.AuditLogger; import org.springframework.http.HttpStatus; -import static org.junit.jupiter.api.Assertions.*; - +@RunWith(MockitoJUnitRunner.class) public class HealthCheckTest { - private HealthCheck sut; - @Before - public void setup() { - this.sut = new HealthCheck(); - } + @Mock + private AuditLogger auditLogger; + + @InjectMocks + private HealthCheck sut; @Test public void should_returnHttp200_when_checkLiveness() { diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java index 1cd1b2adf1e0efb18f2d7628a784bcc08ffb305a..60286821afcb6700e56cba95ae84d4c5ffa365ea 100644 --- a/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/api/PartitionApiTest.java @@ -14,12 +14,24 @@ package org.opengroup.osdu.partition.api; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.interfaces.IPartitionService; @@ -28,22 +40,8 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; -import javax.servlet.http.HttpServletRequest; -import java.net.URI; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.*; -import static org.powermock.api.mockito.PowerMockito.mockStatic; - @RunWith(PowerMockRunner.class) @PrepareForTest(ServletUriComponentsBuilder.class) public class PartitionApiTest { @@ -55,6 +53,9 @@ public class PartitionApiTest { @Mock private IPartitionService partitionService; + @Mock + private AuditLogger auditLogger; + @InjectMocks private PartitionApi sut; diff --git a/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..408c1f28f26a9de48aa11ef2f08089ff40b33032 --- /dev/null +++ b/partition-core/src/test/java/org/opengroup/osdu/partition/logging/AuditLoggerTest.java @@ -0,0 +1,116 @@ +package org.opengroup.osdu.partition.logging; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Collections; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; + +@RunWith(MockitoJUnitRunner.class) +public class AuditLoggerTest { + + @Mock + private JaxRsDpsLog log; + + @InjectMocks + private AuditLogger sut; + + private List<String> resources; + + @Before + public void setup() { + resources = Collections.singletonList("resources"); + } + + @Test + public void should_writeCreatePartitionSuccessEvent() { + this.sut.createPartitionSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeCreatePartitionFailureEvent() { + this.sut.createPartitionFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadPartitionSuccessEvent() { + this.sut.readPartitionSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadPartitionFailureEvent() { + this.sut.readPartitionFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeDeletePartitionSuccessEvent() { + this.sut.deletePartitionSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeDeletePartitionFailureEvent() { + this.sut.deletePartitionFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadServiceLivenessSuccessEvent() { + this.sut.readServiceLivenessSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadServiceLivenessFailureEvent() { + this.sut.readServiceLivenessFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeUpdatePartitionSecretSuccessEvent() { + this.sut.updatePartitionSecretSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeUpdatePartitionSecretFailureEvent() { + this.sut.updatePartitionSecretFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadListPartitionSuccessEvent() { + this.sut.readListPartitionSuccess(this.resources); + + verify(this.log, times(1)).audit(any()); + } + + @Test + public void should_writeReadListPartitionFailureEvent() { + this.sut.readListPartitionFailure(this.resources); + + verify(this.log, times(1)).audit(any()); + } +} \ No newline at end of file diff --git a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/security/AuthorizationService.java b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/security/AuthorizationService.java index ff3aeb3c23236ab32a7bccfbbb254f79bd660416..5e3798f407e195c673503e330ce693efeabeefa4 100644 --- a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/security/AuthorizationService.java +++ b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/security/AuthorizationService.java @@ -17,8 +17,10 @@ package org.opengroup.osdu.partition.provider.gcp.security; +import java.util.Objects; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.opengroup.osdu.core.common.model.entitlements.AuthorizationResponse; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.model.http.DpsHeaders; import org.opengroup.osdu.partition.provider.interfaces.IAuthorizationService; @@ -41,7 +43,11 @@ public class AuthorizationService implements IAuthorizationService { @Override public boolean isDomainAdminServiceAccount() { try { - authorizationServiceImpl.authorizeAny(headers, PARTITION_ADMIN_ROLE); + AuthorizationResponse authorizationResponse = authorizationServiceImpl + .authorizeAny(headers, PARTITION_ADMIN_ROLE); + if (Objects.nonNull(authorizationResponse)) { + headers.put("user", authorizationResponse.getUser()); + } } catch (AppException e) { throw e; } catch (Exception e) { diff --git a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java index 608b170ffe4ffe28569b8beee0e556a152cb167d..1c82f49c73cd74d18b13611aabd5dac09f0580f8 100644 --- a/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java +++ b/provider/partition-gcp/src/main/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImpl.java @@ -19,6 +19,7 @@ package org.opengroup.osdu.partition.provider.gcp.service; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,6 +28,7 @@ import lombok.RequiredArgsConstructor; import org.apache.http.HttpStatus; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.provider.interfaces.IKmsClient; +import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.gcp.model.PartitionPropertyEntity; @@ -46,9 +48,12 @@ public class PartitionServiceImpl implements IPartitionService { private final IKmsClient kmsClient; + private final AuditLogger auditLogger; + @Override public PartitionInfo createPartition(String partitionId, PartitionInfo partitionInfo) { if (this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { + this.auditLogger.createPartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_CONFLICT, UNKNOWN_ERROR_REASON, "Partition already exists."); } @@ -81,11 +86,13 @@ public class PartitionServiceImpl implements IPartitionService { @Override public PartitionInfo updatePartition(String partitionId, PartitionInfo partitionInfo) { if (partitionInfo.getProperties().containsKey("id")) { + this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_BAD_REQUEST, "can not update id", "the field id can not be updated"); } if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { + this.auditLogger.updatePartitionSecretFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, "An attempt to update not existing partition."); } @@ -120,11 +127,13 @@ public class PartitionServiceImpl implements IPartitionService { } private PartitionInfo getEncryptedPartition(String partitionId) { + if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { + this.auditLogger.readPartitionFailure(Collections.singletonList(partitionId)); + throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, + "Partition does not exist."); + } List<PartitionPropertyEntity> partitionPropertiesList = this.partitionPropertyEntityRepository - .findByPartitionId(partitionId) - .orElseThrow( - () -> new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, - "Partition does not exist.")); + .findByPartitionId(partitionId).get(); PartitionInfo partitionInfo = new PartitionInfo(); Map<String, Property> partitionInfoProperties = new HashMap<>(); for (PartitionPropertyEntity entity : partitionPropertiesList) { @@ -152,6 +161,7 @@ public class PartitionServiceImpl implements IPartitionService { @Override public boolean deletePartition(String partitionId) { if (!this.partitionPropertyEntityRepository.findByPartitionId(partitionId).isPresent()) { + this.auditLogger.deletePartitionFailure(Collections.singletonList(partitionId)); throw new AppException(HttpStatus.SC_NOT_FOUND, UNKNOWN_ERROR_REASON, "An attempt to delete not existing partition."); } diff --git a/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java index 388f6b17aa525b5356dbf6f2a7ba878dad146c0a..fadb1f153c422417e64e32d9728d8a089d65e431 100644 --- a/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java +++ b/provider/partition-gcp/src/test/java/org/opengroup/osdu/partition/provider/gcp/service/PartitionServiceImplTest.java @@ -33,6 +33,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.partition.logging.AuditLogger; import org.opengroup.osdu.partition.model.PartitionInfo; import org.opengroup.osdu.partition.model.Property; import org.opengroup.osdu.partition.provider.gcp.model.PartitionPropertyEntity; @@ -50,6 +51,9 @@ public class PartitionServiceImplTest { @Mock private PartitionPropertyEntityRepository partitionPropertyEntityRepository; + @Mock + private AuditLogger auditLogger; + @InjectMocks private PartitionServiceImpl partitionServiceImpl;