Commit 342565ea authored by Anastasiia Gelmut's avatar Anastasiia Gelmut Committed by Rostislav Dublin (EPAM)
Browse files

GONRG-1192

Updated poms.
parent 6ad0d526
......@@ -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")
......
......@@ -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;
}
}
/*
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
/*
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
......@@ -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() {
......
......@@ -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;
......
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
......@@ -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) {
......
......@@ -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.");
}