|
| 1 | +package gov.cms.bfd.server.war; |
| 2 | + |
| 3 | +import static gov.cms.bfd.server.war.commons.StringUtils.parseBooleansFromRequest; |
| 4 | + |
| 5 | +import ca.uhn.fhir.rest.api.server.RequestDetails; |
| 6 | +import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; |
| 7 | +import ca.uhn.fhir.rest.server.interceptor.consent.ConsentOutcome; |
| 8 | +import ca.uhn.fhir.rest.server.interceptor.consent.IConsentContextServices; |
| 9 | +import ca.uhn.fhir.rest.server.interceptor.consent.IConsentService; |
| 10 | +import gov.cms.bfd.server.war.commons.AbstractResourceProvider; |
| 11 | +import gov.cms.bfd.server.war.commons.CommonTransformerUtils; |
| 12 | +import gov.cms.bfd.sharedutils.TagCode; |
| 13 | +import org.hl7.fhir.instance.model.api.IBaseCoding; |
| 14 | +import org.hl7.fhir.instance.model.api.IBaseResource; |
| 15 | +import org.hl7.fhir.r4.model.Bundle; |
| 16 | +import org.hl7.fhir.r4.model.Resource; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +/** |
| 21 | + * SAMHSAConsentInterceptor handles filtering based on the SAMHSA (42CFRPart2) tags in the request. |
| 22 | + * When the feature flag is enabled, the interceptor scrubs resources tagged with the 42CFRPart2 |
| 23 | + * security tag. If the feature flag is disabled, no filtering occurs. |
| 24 | + */ |
| 25 | +public class V2SamhsaConsentInterceptor implements IConsentService { |
| 26 | + |
| 27 | + /** The logger. */ |
| 28 | + private static final Logger logger = LoggerFactory.getLogger(V2SamhsaConsentInterceptor.class); |
| 29 | + |
| 30 | + /** |
| 31 | + * Invoked once at the start of every request. |
| 32 | + * |
| 33 | + * @param theRequestDetails theRequestDetails |
| 34 | + * @param theContextServices theContextServices |
| 35 | + */ |
| 36 | + @Override |
| 37 | + public ConsentOutcome startOperation( |
| 38 | + RequestDetails theRequestDetails, IConsentContextServices theContextServices) { |
| 39 | + return ConsentOutcome.PROCEED; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Can a given resource be returned to the user. |
| 44 | + * |
| 45 | + * @param theRequestDetails theRequestDetails |
| 46 | + * @param theResource theResource |
| 47 | + * @param theContextServices theContextServices |
| 48 | + */ |
| 49 | + @Override |
| 50 | + public ConsentOutcome willSeeResource( |
| 51 | + RequestDetails theRequestDetails, |
| 52 | + IBaseResource theResource, |
| 53 | + IConsentContextServices theContextServices) { |
| 54 | + |
| 55 | + logger.debug("SAMHSAConsentInterceptor - Processing willSeeResource."); |
| 56 | + |
| 57 | + // Determine if SAMHSA filtering is required from request parameters |
| 58 | + boolean excludeSamhsaParam = |
| 59 | + parseBooleansFromRequest(theRequestDetails, AbstractResourceProvider.EXCLUDE_SAMHSA) |
| 60 | + .stream() |
| 61 | + .findFirst() |
| 62 | + .orElse(false); |
| 63 | + boolean shouldFilterSamhsa = |
| 64 | + CommonTransformerUtils.shouldFilterSamhsa( |
| 65 | + String.valueOf(excludeSamhsaParam), theRequestDetails); |
| 66 | + |
| 67 | + if (!shouldFilterSamhsa) { |
| 68 | + return ConsentOutcome.PROCEED; // No filtering needed, proceed |
| 69 | + } |
| 70 | + |
| 71 | + // If the resource is a Bundle, check each entry for SAMHSA security tags |
| 72 | + if (theResource instanceof Bundle bundle) { |
| 73 | + for (Bundle.BundleEntryComponent entry : bundle.getEntry()) { |
| 74 | + if (shouldRedactResource(entry.getResource())) { |
| 75 | + redactSensitiveData(entry); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return ConsentOutcome.PROCEED; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks if a resource should be redacted based on SAMHSA security tags. |
| 85 | + * |
| 86 | + * @param baseResource The resource to check. |
| 87 | + * @return true if the resource should be redacted, false otherwise. |
| 88 | + */ |
| 89 | + private boolean shouldRedactResource(IBaseResource baseResource) { |
| 90 | + if (baseResource instanceof Resource resource && resource.getMeta() != null) { |
| 91 | + for (IBaseCoding securityTag : resource.getMeta().getSecurity()) { |
| 92 | + // Check for SAMHSA-related tags |
| 93 | + if (isSamhsaSecurityTag(securityTag)) { |
| 94 | + logger.info("Matched SAMHSA security tag, redacting resource."); |
| 95 | + return true; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return false; // No matching SAMHSA tags found |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Determines if a security tag is related to SAMHSA (42CFRPart2). |
| 104 | + * |
| 105 | + * @param securityTag the security tag |
| 106 | + * @return true if it has Samhsa security tag, false otherwise |
| 107 | + */ |
| 108 | + private boolean isSamhsaSecurityTag(IBaseCoding securityTag) { |
| 109 | + String code = securityTag.getCode(); |
| 110 | + return TagCode._42CFRPart2.toString().equalsIgnoreCase(code); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Redacts sensitive data in the resource. |
| 115 | + * |
| 116 | + * @param entry the entry |
| 117 | + */ |
| 118 | + private void redactSensitiveData(Bundle.BundleEntryComponent entry) { |
| 119 | + logger.debug("V2SamhsaConsentInterceptor - redactSensitiveData."); |
| 120 | + entry.setResource(null); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void completeOperationSuccess( |
| 125 | + RequestDetails theRequestDetails, IConsentContextServices theContextServices) { |
| 126 | + logger.debug("V2SamhsaConsentInterceptor - completeOperationSuccess."); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void completeOperationFailure( |
| 131 | + RequestDetails theRequestDetails, |
| 132 | + BaseServerResponseException theException, |
| 133 | + IConsentContextServices theContextServices) { |
| 134 | + logger.info("V2SamhsaConsentInterceptor - Operation failed.", theException); |
| 135 | + } |
| 136 | +} |
0 commit comments