Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6bba3cb
draft implementation - no support for typefilter yet
Oct 17, 2025
f795113
revise and create draft of new permissions arg format w/ implied reso…
Oct 20, 2025
4d57ea8
add new rule for group query, with tests
Oct 21, 2025
c80280d
Merge branch 'master' into jd-20251017-bulk-export-permissions
Oct 21, 2025
384ff31
- formatting
Oct 21, 2025
e070201
add Group rule builder code
Oct 21, 2025
33d0df7
implement basic Patient rule with tests
Oct 23, 2025
5575871
Implement rule building for patient by matcher
Oct 24, 2025
6ff269b
spotless
Oct 24, 2025
6c5d860
type level ITs, fix some tests and bugs
Oct 28, 2025
640c9ef
spotless
Oct 28, 2025
2f02a09
add javadocs, remove unused code after some design changes, updated s…
Oct 29, 2025
10fd885
spotless
Oct 29, 2025
af123a9
round 2 - add javadocs, remove unused code after some design changes,…
Oct 29, 2025
bd3ca50
round 4 - add javadocs, remove unused code after some design changes,…
Oct 29, 2025
5873e18
round 5 - remove todos
Oct 29, 2025
0d906c4
changelogs, more java docs
Oct 29, 2025
78324a9
Merge remote-tracking branch 'origin/rel_8_6' into jd-20251017-bulk-e…
Oct 29, 2025
9727733
round 1 of addressing review comments
Oct 30, 2025
f8b3d41
spotless
Oct 30, 2025
7fdc3a4
refactor rule classes to have a base class for common logic
Oct 30, 2025
31d143c
spotless
Oct 31, 2025
17ed1ad
Revert "spotless"
Oct 31, 2025
5f52b36
Revert "refactor rule classes to have a base class for common logic"
Oct 31, 2025
5390b9b
round 2 - address code review comments
Oct 31, 2025
1f83598
spotless
Oct 31, 2025
b50a5aa
draft implementation of combining RuleBuilder apis
Nov 17, 2025
d558587
spotless
Nov 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
type: add
issue: 7342
title: "Enhanced the bulk export RuleBuilder code to support the identification of allowable Groups/Patients to export by a FHIR query matcher."
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import ca.uhn.fhir.jpa.entity.TermValueSet;
import ca.uhn.fhir.jpa.esr.ExternallyStoredResourceServiceRegistry;
import ca.uhn.fhir.jpa.graphql.DaoRegistryGraphQLStorageServices;
import ca.uhn.fhir.jpa.interceptor.AuthResourceResolver;
import ca.uhn.fhir.jpa.interceptor.CascadingDeleteInterceptor;
import ca.uhn.fhir.jpa.interceptor.JpaConsentContextServices;
import ca.uhn.fhir.jpa.interceptor.OverridePathBasedReferentialIntegrityForDeletesInterceptor;
Expand Down Expand Up @@ -205,6 +206,7 @@
import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
import ca.uhn.fhir.rest.server.interceptor.ResponseTerminologyTranslationInterceptor;
import ca.uhn.fhir.rest.server.interceptor.ResponseTerminologyTranslationSvc;
import ca.uhn.fhir.rest.server.interceptor.auth.IAuthResourceResolver;
import ca.uhn.fhir.rest.server.interceptor.consent.IConsentContextServices;
import ca.uhn.fhir.rest.server.interceptor.partition.RequestTenantPartitionInterceptor;
import ca.uhn.fhir.rest.server.util.ISearchParamRegistry;
Expand Down Expand Up @@ -530,6 +532,11 @@ public IConsentContextServices consentContextServices() {
return new JpaConsentContextServices();
}

@Bean
public IAuthResourceResolver authResourceResolver(DaoRegistry theDaoRegistry) {
return new AuthResourceResolver(theDaoRegistry);
}

@Bean
@Lazy
public DiffProvider diffProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*-
* #%L
* HAPI FHIR JPA Server
* %%
* Copyright (C) 2014 - 2025 Smile CDR, 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.
* #L%
*/
package ca.uhn.fhir.jpa.interceptor;

import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.searchparam.SearchParameterMap;
import ca.uhn.fhir.rest.api.Constants;
import ca.uhn.fhir.rest.api.server.SystemRequestDetails;
import ca.uhn.fhir.rest.param.TokenOrListParam;
import ca.uhn.fhir.rest.server.interceptor.auth.IAuthResourceResolver;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;

import java.util.List;

/**
* Small service class to inject DB access into an interceptor
* For example, used in bulk export security to allow querying for resource to match against permission argument filters
*/
public class AuthResourceResolver implements IAuthResourceResolver {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me nervous. I get the design, but it's breaking the well-established pattern we have that auth interceptor doesn't resolve anything, it just looks at the data it's passed and makes a decision.

Instead of creating a loading infrastructure, could we add a new parameter object to STORAGE_PRE_INITIATE_BULK_EXPORT so that BulkExportJobService could handle loading the Group and pass it to AuthInterceptor as a part of the interceptor request? That way AuthInterceptor could look at it and make its decisions without needing to know how to fetch anything

private final DaoRegistry myDaoRegistry;

public AuthResourceResolver(DaoRegistry myDaoRegistry) {
this.myDaoRegistry = myDaoRegistry;
}

public IBaseResource resolveResourceById(IIdType theResourceId) {
return myDaoRegistry
.getResourceDao(theResourceId.getResourceType())
.read(theResourceId, new SystemRequestDetails());
}

public List<IBaseResource> resolveResourcesByIds(List<String> theResourceIds, String theResourceType) {
TokenOrListParam t = new TokenOrListParam(null, theResourceIds.toArray(String[]::new));

SearchParameterMap m = new SearchParameterMap();
m.add(Constants.PARAM_ID, t);
return myDaoRegistry.getResourceDao(theResourceType).searchForResources(m, new SystemRequestDetails());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ private boolean applyTesters(
return retVal;
}

/**
* Apply testers, and return true if at least 1 tester matches.
* Returns false if all testers do not match.
*/
boolean atLeastOneTesterMatches(
RestOperationTypeEnum theOperation,
RequestDetails theRequestDetails,
IBaseResource theInputResource,
IRuleApplier theRuleApplier) {

boolean retVal = false;

IAuthRuleTester.RuleTestRequest inputRequest = new IAuthRuleTester.RuleTestRequest(
myMode, theOperation, theRequestDetails, null, theInputResource, theRuleApplier);

for (IAuthRuleTester next : getTesters()) {
if (next.matches(inputRequest)) {
retVal = true;
break;
}
}

return retVal;
}

PolicyEnum getMode() {
return myMode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*-
* #%L
* HAPI FHIR - Server Framework
* %%
* Copyright (C) 2014 - 2025 Smile CDR, 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.
* #L%
*/
package ca.uhn.fhir.rest.server.interceptor.auth;

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;

import java.util.List;

/**
* Small service class to inject DB access into an interceptor
* For example, used in bulk export security to allow querying for resource to match against permission argument filters
*/
public interface IAuthResourceResolver {
IBaseResource resolveResourceById(IIdType theResourceId);

/**
* Resolve a list of resources by ID. All resources should be the same type.
* @param theResourceIds the FHIR id of the resource(s)
* @param theResourceType the type of resource
* @return A list of resources resolved by ID
*/
List<IBaseResource> resolveResourcesByIds(List<String> theResourceIds, String theResourceType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ default IAuthRuleBuilderRuleBulkExportWithTarget groupExportOnGroup(@Nonnull IId
*/
IAuthRuleBuilderRuleBulkExportWithTarget groupExportOnGroup(@Nonnull String theFocusResourceId);

/**
* Allow/deny <b>group-level</b> export rule applies to the Group by matching on the provided FHIR query filter,
* e.g. <code>?identifier=foo|bar</code>
* Note that resource type is implied to be Group
*
* @since 8.6.0
*/
IAuthRuleBuilderRuleBulkExportWithTarget groupExportOnFilter(@Nonnull String theCompartmentFilterMatcher);

/**
* Allow/deny <b>patient-level</b> export rule applies to the Patient by matching on the provided FHIR query filter,
* e.g. <code>?identifier=foo|bar</code>
* Note that resource type is implied to be Patient
*
* @since 8.6.0
*/
IAuthRuleBuilderRuleBulkExportWithTarget patientExportOnFilter(@Nonnull String theCompartmentFilterMatcher);

/**
* Allow/deny <b>patient-level</b> export rule applies to the Group with the given resource ID, e.g. <code>Group/123</code>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,15 @@ Verdict applyRulesAndReturnDecision(
default IAuthorizationSearchParamMatcher getSearchParamMatcher() {
return null;
}
;

/**
* The auth resource resolve is a service that allows you to query the DB for a resource, given a resource ID
* WARNING: This is slow, and should have limited use in authorization.
*
* It is currently used for bulk-export, to support permissible Group/Patient exports by matching a FHIR query
* This is ok, since bulk-export is a slow and (relatively) rare operation
*/
default IAuthResourceResolver getAuthResourceResolver() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private class RuleBuilderRule implements IAuthRuleBuilderRule {
private final String myRuleName;
private RuleBuilderRuleOp myReadRuleBuilder;
private RuleBuilderRuleOp myWriteRuleBuilder;
private RuleBuilderBulkExport ruleBuilderBulkExport;
private RuleBuilderBulkExport myRuleBuilderBulkExport;

RuleBuilderRule(PolicyEnum theRuleMode, String theRuleName) {
myRuleMode = theRuleMode;
Expand Down Expand Up @@ -341,10 +341,10 @@ public IAuthRuleBuilderGraphQL graphQL() {

@Override
public IAuthRuleBuilderRuleBulkExport bulkExport() {
if (ruleBuilderBulkExport == null) {
ruleBuilderBulkExport = new RuleBuilderBulkExport();
if (myRuleBuilderBulkExport == null) {
myRuleBuilderBulkExport = new RuleBuilderBulkExport();
}
return ruleBuilderBulkExport;
return myRuleBuilderBulkExport;
}

@Override
Expand Down Expand Up @@ -888,6 +888,8 @@ public IAuthRuleFinished any() {

private class RuleBuilderBulkExport implements IAuthRuleBuilderRuleBulkExport {
private RuleBulkExportImpl myRuleBulkExport;
private RuleGroupBulkExportByCompartmentMatcherImpl myRuleGroupBulkExportByCompartmentMatcher;
private RulePatientBulkExportByCompartmentMatcherImpl myRulePatientBulkExportByCompartmentMatcher;

@Override
public IAuthRuleBuilderRuleBulkExportWithTarget groupExportOnGroup(@Nonnull String theFocusResourceId) {
Expand Down Expand Up @@ -985,6 +987,51 @@ public IAuthRuleBuilderRuleBulkExportWithTarget any() {
return new RuleBuilderBulkExportWithTarget(rule);
}

@Override
public IAuthRuleBuilderRuleBulkExportWithTarget groupExportOnFilter(
@Nonnull String theCompartmentFilterMatcher) {
if (myRuleGroupBulkExportByCompartmentMatcher == null) {
RuleGroupBulkExportByCompartmentMatcherImpl rule =
new RuleGroupBulkExportByCompartmentMatcherImpl(myRuleName);
rule.setAppliesToGroupExportOnGroup(theCompartmentFilterMatcher);
rule.setMode(myRuleMode);
myRuleGroupBulkExportByCompartmentMatcher = rule;
} else {
myRuleGroupBulkExportByCompartmentMatcher.setAppliesToGroupExportOnGroup(
theCompartmentFilterMatcher);
}

// prevent duplicate rules from being added
if (!myRules.contains(myRuleGroupBulkExportByCompartmentMatcher)) {
myRules.add(myRuleGroupBulkExportByCompartmentMatcher);
}

return new RuleBuilderGroupBulkExportWithFilter(myRuleGroupBulkExportByCompartmentMatcher);
}

@Override
public IAuthRuleBuilderRuleBulkExportWithTarget patientExportOnFilter(
@Nonnull String theCompartmentFilterMatcher) {
if (myRulePatientBulkExportByCompartmentMatcher == null) {
RulePatientBulkExportByCompartmentMatcherImpl rule =
new RulePatientBulkExportByCompartmentMatcherImpl(myRuleName);

rule.addAppliesToPatientExportOnPatient(theCompartmentFilterMatcher);
rule.setMode(myRuleMode);
myRulePatientBulkExportByCompartmentMatcher = rule;
} else {
myRulePatientBulkExportByCompartmentMatcher.addAppliesToPatientExportOnPatient(
theCompartmentFilterMatcher);
}

// prevent duplicate rules from being added
if (!myRules.contains(myRulePatientBulkExportByCompartmentMatcher)) {
myRules.add(myRulePatientBulkExportByCompartmentMatcher);
}

return new RuleBuilderPatientBulkExportWithFilter(myRulePatientBulkExportByCompartmentMatcher);
}

private class RuleBuilderBulkExportWithTarget extends RuleBuilderFinished
implements IAuthRuleBuilderRuleBulkExportWithTarget {
private final RuleBulkExportImpl myRule;
Expand All @@ -1000,6 +1047,38 @@ public IAuthRuleBuilderRuleBulkExportWithTarget withResourceTypes(Collection<Str
return this;
}
}

private class RuleBuilderGroupBulkExportWithFilter extends RuleBuilderFinished
implements IAuthRuleBuilderRuleBulkExportWithTarget {
private final RuleGroupBulkExportByCompartmentMatcherImpl myRule;

private RuleBuilderGroupBulkExportWithFilter(RuleGroupBulkExportByCompartmentMatcherImpl theRule) {
super(theRule);
myRule = theRule;
}

@Override
public IAuthRuleBuilderRuleBulkExportWithTarget withResourceTypes(Collection<String> theResourceTypes) {
myRule.setResourceTypes(theResourceTypes);
return this;
}
}

private class RuleBuilderPatientBulkExportWithFilter extends RuleBuilderFinished
implements IAuthRuleBuilderRuleBulkExportWithTarget {
private final RulePatientBulkExportByCompartmentMatcherImpl myRule;

private RuleBuilderPatientBulkExportWithFilter(RulePatientBulkExportByCompartmentMatcherImpl theRule) {
super(theRule);
myRule = theRule;
}

@Override
public IAuthRuleBuilderRuleBulkExportWithTarget withResourceTypes(Collection<String> theResourceTypes) {
myRule.setResourceTypes(theResourceTypes);
return this;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static ca.uhn.fhir.rest.server.interceptor.auth.AuthorizationInterceptor.REQUEST_ATTRIBUTE_BULK_DATA_EXPORT_OPTIONS;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
Expand Down Expand Up @@ -70,9 +71,8 @@ public AuthorizationInterceptor.Verdict applyRule(
return null;
}

BulkExportJobParameters inboundBulkExportRequestOptions = (BulkExportJobParameters) theRequestDetails
.getUserData()
.get(AuthorizationInterceptor.REQUEST_ATTRIBUTE_BULK_DATA_EXPORT_OPTIONS);
BulkExportJobParameters inboundBulkExportRequestOptions = (BulkExportJobParameters)
theRequestDetails.getUserData().get(REQUEST_ATTRIBUTE_BULK_DATA_EXPORT_OPTIONS);
// if style doesn't match - abstain
if (!myWantAnyStyle && inboundBulkExportRequestOptions.getExportStyle() != myWantExportStyle) {
return null;
Expand Down Expand Up @@ -120,11 +120,12 @@ public AuthorizationInterceptor.Verdict applyRule(

// 1. If each of the requested resource IDs in the parameters are present in the users permissions, Approve
// 2. If any requested ID is not present in the users permissions, Deny.
if (myWantExportStyle == BulkExportJobParameters.ExportStyle.PATIENT)
if (myWantExportStyle == BulkExportJobParameters.ExportStyle.PATIENT) {
// Unfiltered Type Level
if (myAppliesToAllPatients) {
return allowVerdict;
}
}

// Instance level, or filtered type level
if (isNotEmpty(myPatientIds)) {
Expand Down
Loading
Loading