Commit 780340d8 authored by Anastasiia Gelmut's avatar Anastasiia Gelmut
Browse files

GONRG-1761 Implement Some Audit Events

parent 42df850a
......@@ -17,6 +17,7 @@
package org.opengroup.osdu.register.api;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Collections;
import org.opengroup.osdu.register.action.model.Action;
import org.opengroup.osdu.register.action.model.CreateActionDto;
import org.opengroup.osdu.register.action.model.ParsedAction;
......@@ -26,7 +27,7 @@ import org.opengroup.osdu.register.provider.interfaces.action.IRetrieveActionSer
import org.opengroup.osdu.register.action.services.TestActionService;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.logging.AuditLog;
import org.opengroup.osdu.register.logging.AuditLogger;
import org.opengroup.osdu.register.utils.ServiceRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -54,7 +55,7 @@ public class ActionApi {
@Autowired
private TestActionService testActionService;
@Autowired
private AuditLog auditLog;
private AuditLogger auditLogger;
@Autowired
private JaxRsDpsLog log;
@Autowired
......@@ -65,7 +66,7 @@ public class ActionApi {
public ResponseEntity<Action> createAction(@RequestBody @Valid CreateActionDto createActionDto) {
Action action = new Action(createActionDto);
Action output = repo.createAction(action);
auditLog.createActionSuccess(action);
this.auditLogger.createActionSuccess(Collections.singletonList(action.toString()));
return new ResponseEntity<>(output, HttpStatus.CREATED);
}
......@@ -75,6 +76,7 @@ public class ActionApi {
try {
List<Action> query = repo.getAllActions();
List<Action> output = this.retrieveActionService.getActions(query, jsonObject);
this.auditLogger.readMultipleActionsSuccess(Collections.singletonList(output.toString()));
return new ResponseEntity<>(output, HttpStatus.OK);
} catch (Exception e) {
this.log.error("retrieve action failed", e);
......@@ -86,7 +88,7 @@ public class ActionApi {
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "')")
public ResponseEntity deleteAction(@PathVariable("id") @NotBlank @Pattern(regexp = "^[A-Za-z0-9-]{2,50}") String id) {
if (repo.delete(id)) {
auditLog.deleteActionSuccess(id);
this.auditLogger.deleteActionSuccess(Collections.singletonList(id));
return new ResponseEntity(HttpStatus.NO_CONTENT);
} else {
throw new AppException(SC_NOT_FOUND, "Not found", String.format("Could not delete Action with id %s because it could not be found", id));
......@@ -96,13 +98,16 @@ public class ActionApi {
@GetMapping("action/{id}")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "', '" + ServiceRole.VIEWER + "')")
public ResponseEntity<Action> getAction(@PathVariable("id") @NotBlank @Pattern(regexp = "^[A-Za-z0-9-]{2,50}") String id) {
return new ResponseEntity<>(repo.get(id), HttpStatus.OK);
Action action = this.repo.get(id);
this.auditLogger.readActionSuccess(Collections.singletonList(action.toString()));
return new ResponseEntity<>(action, HttpStatus.OK);
}
@PostMapping("action:test")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "', '" + ServiceRole.VIEWER + "')")
public ResponseEntity<ParsedAction> testAction(@RequestBody @Valid TestActionRequest testActionRequest) {
ParsedAction response = this.testActionService.validateRegex(testActionRequest);
this.auditLogger.validateActionSuccess(Collections.singletonList(response.toString()));
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
......@@ -16,12 +16,13 @@
package org.opengroup.osdu.register.api;
import java.util.Collections;
import javax.validation.constraints.NotBlank;
import org.opengroup.osdu.register.ddms.model.Ddms;
import org.opengroup.osdu.register.provider.interfaces.ddms.IDdmsRepository;
import org.opengroup.osdu.register.ddms.services.ConsumptionService;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.logging.AuditLog;
import org.opengroup.osdu.register.logging.AuditLogger;
import org.opengroup.osdu.register.utils.ServiceRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -45,7 +46,7 @@ public class DdmsApi {
@Autowired
private IDdmsRepository dmsRepository;
@Autowired
private AuditLog auditLog;
private AuditLogger auditLogger;
@Autowired
private ConsumptionService consumptionService;
......@@ -53,7 +54,7 @@ public class DdmsApi {
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.EDITOR + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.OPS + "')")
public ResponseEntity<Ddms> postDMS(@RequestBody @Valid Ddms spec) {
dmsRepository.create(spec);
auditLog.createDmsSuccess(spec.getId());
this.auditLogger.createDmsSuccess(Collections.singletonList(spec.getId()));
return new ResponseEntity<>(spec, HttpStatus.CREATED);
}
......@@ -76,7 +77,7 @@ public class DdmsApi {
public ResponseEntity deleteDMS(@PathVariable("id") @NotBlank @Pattern(regexp = "^[A-Za-z0-9-]{2,50}") String id) {
boolean result = dmsRepository.delete(id);
if (result) {
auditLog.deleteDmsSuccess(id);
this.auditLogger.deleteDmsSuccess(Collections.singletonList(id));
return new ResponseEntity(HttpStatus.NO_CONTENT);
} else {
throw new AppException(org.apache.http.HttpStatus.SC_NOT_FOUND, "Not found", String.format("Could not delete DDMS with id %s because it could not be found", id));
......
......@@ -16,9 +16,11 @@
package org.opengroup.osdu.register.api;
import java.util.Collections;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.api.dto.SubscriptionInfo;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.logging.AuditLogger;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ISubscriptionRepository;
......@@ -57,11 +59,14 @@ public class SubscriberApi {
private Provider<ISubscriptionRepository> subscriptionRepository;
@Autowired
private AvailableTopics topics;
@Autowired
private AuditLogger auditLogger;
@PostMapping("subscription")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "')")
public ResponseEntity<Subscription> createSubscription(@RequestBody @Valid Subscription input) throws Exception {
input = createSubscription.get().run(input);
this.auditLogger.createSubscriptionSuccess(Collections.singletonList(input.toString()));
return new ResponseEntity<>(input, HttpStatus.CREATED);
}
......@@ -76,6 +81,7 @@ public class SubscriberApi {
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "')")
public ResponseEntity<SubscriptionInfo> getSubscription(@PathVariable("id") @NotBlank String id) {
Subscription output = subscriptionRepository.get().get(id);
this.auditLogger.readSubscriptionSuccess(Collections.singletonList(output.toString()));
return new ResponseEntity<>(new SubscriptionInfo(output), HttpStatus.OK);
}
......@@ -83,6 +89,7 @@ public class SubscriberApi {
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "')")
public ResponseEntity updateSecret(@RequestBody @Valid Secret input, @PathVariable("id") @NotBlank String id) {
if (updateSubscriptionSecret.get().run(input, id)) {
this.auditLogger.updateSubscriptionSecretSuccess(Collections.singletonList(input.toString()));
return new ResponseEntity(HttpStatus.NO_CONTENT);
} else {
throw new AppException(org.apache.http.HttpStatus.SC_NOT_FOUND, "Not found",
......@@ -93,11 +100,14 @@ public class SubscriberApi {
@DeleteMapping("subscription/{id}")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "')")
public ResponseEntity deleteSubscription(@PathVariable("id") @NotBlank String id) {
if (deleteSubscription.get().run(id))
if (deleteSubscription.get().run(id)) {
this.auditLogger.deleteSubscriptionSuccess(Collections.singletonList(id));
return new ResponseEntity(HttpStatus.NO_CONTENT);
else
} else {
throw new AppException(org.apache.http.HttpStatus.SC_NOT_FOUND, "Not found",
String.format("Could not delete subscription with id %s because it could not be found", id));
String.format(
"Could not delete subscription with id %s because it could not be found", id));
}
}
@GetMapping("topics")
......
/*
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.register.logging;
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_DMS_ACTION_ID = "RG10001";
private static final String CREATE_DMS_MESSAGE = "Registered new DDMS";
private static final String DELETE_DMS_ACTION_ID = "RG10002";
private static final String DELETE_DMS_MESSAGE = "Deleted DDMS";
private static final String CREATE_SUBSCRIPTION_ACTION_ID = "RG10003";
private static final String CREATE_SUBSCRIPTION_MESSAGE = "Create new subscription";
private static final String DELETE_SUBSCRIPTION_ACTION_ID = "RG10004";
private static final String DELETE_SUBSCRIPTION_MESSAGE = "Deleted subscription";
private static final String UPDATE_SUBSCRIPTION_SECRET_ACTION_ID = "RG10005";
private static final String UPDATE_SUBSCRIPTION_SECRET_MESSAGE = "Updated subscription";
private static final String READ_ACTION_ACTION_ID = "RG10006";
private static final String READ_ACTION_MESSAGE = "Read action";
private static final String READ_MULTIPLE_ACTIONS_ACTION_ID = "RG10007";
private static final String READ_MULTIPLE_ACTIONS_MESSAGE = "Read multiple actions";
private static final String CREATE_ACTION_ACTION_ID = "RG10008";
private static final String CREATE_ACTION_MESSAGE = "Create new action";
private static final String DELETE_ACTION_ACTION_ID = "RG10009";
private static final String DELETE_ACTION_MESSAGE = "Deleted action";
private static final String VALIDATE_ACTION_ACTION_ID = "RG10010";
private static final String VALIDATE_ACTION_MESSAGE = "Validate action";
private static final String READ_SUBSCRIPTION_ACTION_ID = "RG10011";
private static final String READ_SUBSCRIPTION_MESSAGE = "Read subscription";
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 getCreateDmsEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.CREATE)
.status(status)
.user(this.user)
.actionId(CREATE_DMS_ACTION_ID)
.message(getStatusMessage(status, CREATE_DMS_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getDeleteDmsEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.DELETE)
.status(status)
.user(this.user)
.actionId(DELETE_DMS_ACTION_ID)
.message(getStatusMessage(status, DELETE_DMS_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getCreateSubscriptionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.CREATE)
.status(status)
.user(this.user)
.actionId(CREATE_SUBSCRIPTION_ACTION_ID)
.message(getStatusMessage(status, CREATE_SUBSCRIPTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getDeleteSubscriptionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.DELETE)
.status(status)
.user(this.user)
.actionId(DELETE_SUBSCRIPTION_ACTION_ID)
.message(getStatusMessage(status, DELETE_SUBSCRIPTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getUpdateSubscriptionSecretEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.UPDATE)
.status(status)
.user(this.user)
.actionId(UPDATE_SUBSCRIPTION_SECRET_ACTION_ID)
.message(getStatusMessage(status, UPDATE_SUBSCRIPTION_SECRET_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getReadActionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(READ_ACTION_ACTION_ID)
.message(getStatusMessage(status, READ_ACTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getReadMultipleActionsEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(READ_MULTIPLE_ACTIONS_ACTION_ID)
.message(getStatusMessage(status, READ_MULTIPLE_ACTIONS_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getCreateActionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.CREATE)
.status(status)
.user(this.user)
.actionId(CREATE_ACTION_ACTION_ID)
.message(getStatusMessage(status, CREATE_ACTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getDeleteActionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.DELETE)
.status(status)
.user(this.user)
.actionId(DELETE_ACTION_ACTION_ID)
.message(getStatusMessage(status, DELETE_ACTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getValidateActionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(VALIDATE_ACTION_ACTION_ID)
.message(getStatusMessage(status, VALIDATE_ACTION_MESSAGE))
.resources(resources)
.build();
}
public AuditPayload getReadSubscriptionEvent(AuditStatus status, List<String> resources) {
return AuditPayload.builder()
.action(AuditAction.READ)
.status(status)
.user(this.user)
.actionId(READ_SUBSCRIPTION_ACTION_ID)
.message(getStatusMessage(status, READ_SUBSCRIPTION_MESSAGE))
.resources(resources)
.build();
}
private String getStatusMessage(AuditStatus status, String message) {
return String.format("%s - %s", message, status.name().toLowerCase());
}
}
/*
* Copyright 2017-2020, 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.register.logging;
import org.opengroup.osdu.register.action.model.Action;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
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;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
import java.util.Collections;
import static org.opengroup.osdu.core.common.logging.audit.AuditPayload.builder;
@Component
@RequestScope
public class AuditLog {
@Autowired
private JaxRsDpsLog logger;
@Autowired
private DpsHeaders dpsHeaders;
public void createDmsSuccess(String id) {
AuditPayload payload = builder()
.action(AuditAction.CREATE)
.status(AuditStatus.SUCCESS)
.actionId("RG10001")
.message("Registered new DDMS")
.resources(Collections.singletonList(id))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void deleteDmsSuccess(String id) {
AuditPayload payload = builder()
.action(AuditAction.DELETE)
.status(AuditStatus.SUCCESS)
.actionId("RG10002")
.message("Deleted DDMS")
.resources(Collections.singletonList(id))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void createSubscriptionSuccess(Subscription subscription) {
AuditPayload payload = builder()
.action(AuditAction.CREATE)
.status(AuditStatus.SUCCESS)
.actionId("RG10003")
.message("Create new subscription")
.resources(Collections.singletonList(subscription.toString()))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void deleteSubscriptionSuccess(String id) {
AuditPayload payload = builder()
.action(AuditAction.DELETE)
.status(AuditStatus.SUCCESS)
.actionId("RG10004")
.message("Deleted subscription")
.resources(Collections.singletonList(id))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void updateSubscriptionSecretSuccess(String id) {
AuditPayload payload = builder()
.action(AuditAction.UPDATE)
.status(AuditStatus.SUCCESS)
.actionId("RG10005")
.message("Updated subscription")
.resources(Collections.singletonList(id))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void createActionSuccess(Action action) {
AuditPayload payload = builder()
.action(AuditAction.CREATE)
.status(AuditStatus.SUCCESS)
.actionId("RG10008")
.message("Create new action")
.resources(Collections.singletonList(action.toString()))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
public void deleteActionSuccess(String id) {
AuditPayload payload = builder()
.action(AuditAction.DELETE)
.status(AuditStatus.SUCCESS)
.actionId("RG10009")
.message("Deleted action")
.resources(Collections.singletonList(id))
.user(dpsHeaders.getUserEmail())
.build();
writeLog(payload);
}
private void writeLog(AuditPayload log) {
logger.audit(log);
}
}
/*
* Copyright 2017-2020, 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.register.logging;
import java.util.List;
import java.util.Objects;
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.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;
@Component
@RequestScope
@RequiredArgsConstructor
public class AuditLogger {
private final JaxRsDpsLog logger;
private final DpsHeaders dpsHeaders;
private AuditEvents auditEvents;
private AuditEvents getAuditEvents() {
if (Objects.isNull(this.auditEvents)) {
this.auditEvents = new AuditEvents(this.dpsHeaders.getUserEmail());
}
return this.auditEvents;
}
public void createDmsSuccess(List<String> resources) {
writeLog(getAuditEvents().getCreateDmsEvent(AuditStatus.SUCCESS, resources));
}
public void createDmsFailure(List<String> resources) {
writeLog(getAuditEvents().getCreateDmsEvent(AuditStatus.FAILURE, resources));
}
public void deleteDmsSuccess(List<String> resources) {
writeLog(getAuditEvents().getDeleteDmsEvent(AuditStatus.SUCCESS, resources));
}
public void deleteDmsFailure(List<String> resources) {
writeLog(getAuditEvents().getDeleteDmsEvent(AuditStatus.FAILURE, resources));
}
public void createSubscriptionSuccess(List<String> resources) {
writeLog(getAuditEvents().getCreateSubscriptionEvent(AuditStatus.SUCCESS, resources));
}
public void createSubscriptionFailure(List<String> resources) {
writeLog(getAuditEvents().getCreateSubscriptionEvent(AuditStatus.FAILURE, resources));
}