Added null checks

This commit is contained in:
Nick Goupinets 2021-06-22 15:12:10 -04:00
parent 912b44c3fa
commit 95012623f6
2 changed files with 48 additions and 4 deletions

View File

@ -43,12 +43,16 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@Service @Service
public class MdmStorageInterceptor implements IMdmStorageInterceptor { public class MdmStorageInterceptor implements IMdmStorageInterceptor {
private static final Logger ourLog = LoggerFactory.getLogger(MdmStorageInterceptor.class); private static final Logger ourLog = LoggerFactory.getLogger(MdmStorageInterceptor.class);
@Autowired @Autowired
private ExpungeEverythingService myExpungeEverythingService; private ExpungeEverythingService myExpungeEverythingService;
@Autowired @Autowired
@ -63,13 +67,21 @@ public class MdmStorageInterceptor implements IMdmStorageInterceptor {
@Hook(Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED) @Hook(Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED)
public void blockManualResourceManipulationOnCreate(IBaseResource theBaseResource, RequestDetails theRequestDetails, ServletRequestDetails theServletRequestDetails) { public void blockManualResourceManipulationOnCreate(IBaseResource theBaseResource, RequestDetails theRequestDetails, ServletRequestDetails theServletRequestDetails) {
ourLog.debug("Starting pre-storage resource created hook for {}, {}, {}", theBaseResource, theRequestDetails, theServletRequestDetails);
if (theBaseResource == null) {
ourLog.warn("Attempting to block golden resource manipulation on a null resource");
return;
}
//If running in single EID mode, forbid multiple eids. //If running in single EID mode, forbid multiple eids.
if (myMdmSettings.isPreventMultipleEids()) { if (myMdmSettings.isPreventMultipleEids()) {
ourLog.debug("Forbidding multiple EIDs on ", theBaseResource);
forbidIfHasMultipleEids(theBaseResource); forbidIfHasMultipleEids(theBaseResource);
} }
// TODO GGG MDM find a better way to identify internal calls? // TODO GGG MDM find a better way to identify internal calls?
if (isInternalRequest(theRequestDetails)) { if (isInternalRequest(theRequestDetails)) {
ourLog.debug("Internal request - completed processing");
return; return;
} }
@ -78,8 +90,16 @@ public class MdmStorageInterceptor implements IMdmStorageInterceptor {
@Hook(Pointcut.STORAGE_PRESTORAGE_RESOURCE_UPDATED) @Hook(Pointcut.STORAGE_PRESTORAGE_RESOURCE_UPDATED)
public void blockManualGoldenResourceManipulationOnUpdate(IBaseResource theOldResource, IBaseResource theUpdatedResource, RequestDetails theRequestDetails, ServletRequestDetails theServletRequestDetails) { public void blockManualGoldenResourceManipulationOnUpdate(IBaseResource theOldResource, IBaseResource theUpdatedResource, RequestDetails theRequestDetails, ServletRequestDetails theServletRequestDetails) {
ourLog.debug("Starting pre-storage resource updated hook for {}, {}, {}, {}", theOldResource, theUpdatedResource, theRequestDetails, theServletRequestDetails);
if (theUpdatedResource == null) {
ourLog.warn("Attempting to block golden resource manipulation on a null resource");
return;
}
//If running in single EID mode, forbid multiple eids. //If running in single EID mode, forbid multiple eids.
if (myMdmSettings.isPreventMultipleEids()) { if (myMdmSettings.isPreventMultipleEids()) {
ourLog.debug("Forbidding multiple EIDs on ", theUpdatedResource);
forbidIfHasMultipleEids(theUpdatedResource); forbidIfHasMultipleEids(theUpdatedResource);
} }
@ -92,10 +112,16 @@ public class MdmStorageInterceptor implements IMdmStorageInterceptor {
} }
if (isInternalRequest(theRequestDetails)) { if (isInternalRequest(theRequestDetails)) {
ourLog.debug("Internal request - completed processing");
return; return;
} }
if (theOldResource != null) {
forbidIfMdmManagedTagIsPresent(theOldResource); forbidIfMdmManagedTagIsPresent(theOldResource);
forbidModifyingMdmTag(theUpdatedResource, theOldResource); forbidModifyingMdmTag(theUpdatedResource, theOldResource);
} else {
ourLog.warn("Null theOldResource for {} {}", theUpdatedResource == null ? "null updated resource" : theUpdatedResource.getIdElement(), theRequestDetails);
}
if (myMdmSettings.isPreventEidUpdates()) { if (myMdmSettings.isPreventEidUpdates()) {
forbidIfModifyingExternalEidOnTarget(theUpdatedResource, theOldResource); forbidIfModifyingExternalEidOnTarget(theUpdatedResource, theOldResource);
@ -111,8 +137,15 @@ public class MdmStorageInterceptor implements IMdmStorageInterceptor {
} }
private void forbidIfModifyingExternalEidOnTarget(IBaseResource theNewResource, IBaseResource theOldResource) { private void forbidIfModifyingExternalEidOnTarget(IBaseResource theNewResource, IBaseResource theOldResource) {
List<CanonicalEID> newExternalEids = myEIDHelper.getExternalEid(theNewResource); List<CanonicalEID> newExternalEids = Collections.emptyList();
List<CanonicalEID> oldExternalEids = myEIDHelper.getExternalEid(theOldResource); List<CanonicalEID> oldExternalEids = Collections.emptyList();
if (theNewResource != null) {
newExternalEids = myEIDHelper.getExternalEid(theNewResource);
}
if (theOldResource != null) {
oldExternalEids = myEIDHelper.getExternalEid(theOldResource);
}
if (oldExternalEids.isEmpty()) { if (oldExternalEids.isEmpty()) {
return; return;
} }
@ -152,6 +185,11 @@ public class MdmStorageInterceptor implements IMdmStorageInterceptor {
} }
private void forbidIfMdmManagedTagIsPresent(IBaseResource theResource) { private void forbidIfMdmManagedTagIsPresent(IBaseResource theResource) {
if (theResource == null) {
ourLog.warn("Attempting to forbid MDM on a null resource");
return;
}
if (MdmResourceUtil.isMdmManaged(theResource)) { if (MdmResourceUtil.isMdmManaged(theResource)) {
throwModificationBlockedByMdm(); throwModificationBlockedByMdm();
} }

View File

@ -70,10 +70,16 @@ public final class MdmResourceUtil {
} }
private static boolean resourceHasTag(IBaseResource theTheBaseResource, String theSystem, String theCode) { private static boolean resourceHasTag(IBaseResource theTheBaseResource, String theSystem, String theCode) {
if (theTheBaseResource == null) {
return false;
}
return theTheBaseResource.getMeta().getTag(theSystem, theCode) != null; return theTheBaseResource.getMeta().getTag(theSystem, theCode) != null;
} }
private static boolean resourceHasTagWithSystem(IBaseResource theTheBaseResource, String theSystem) { private static boolean resourceHasTagWithSystem(IBaseResource theTheBaseResource, String theSystem) {
if (theTheBaseResource == null) {
return false;
}
return theTheBaseResource.getMeta().getTag().stream().anyMatch(tag -> tag.getSystem().equalsIgnoreCase(theSystem)); return theTheBaseResource.getMeta().getTag().stream().anyMatch(tag -> tag.getSystem().equalsIgnoreCase(theSystem));
} }