Skip to content

Commit

Permalink
Minor: Domain only access policy prevents bots from listing (#19017)
Browse files Browse the repository at this point in the history
* Minor: Domain only access policy prevents bots listing

* Minor: Domain only access policy prevents bots listing

* Add Entity Type condition to limit the scope of NOT IN

---------

Co-authored-by: mohitdeuex <mohit.y@deuexsolutions.com>
(cherry picked from commit e04e2de)
  • Loading branch information
harshach authored and mohityadav766 committed Dec 12, 2024
1 parent a43106d commit 319c681
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.openmetadata.service.util.FullyQualifiedName;

public class ListFilter extends Filter<ListFilter> {
public static final String NULL_PARAM = "null";

public ListFilter() {
this(Include.NON_DELETED);
}
Expand Down Expand Up @@ -107,12 +109,24 @@ public String getTestSuiteFQNCondition() {

private String getDomainCondition(String tableName) {
String domainId = getQueryParam("domainId");
return domainId == null
? ""
: String.format(
"(%s in (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' AND entity_relationship.fromId IN (%s) AND "
+ "relation=10))",
nullOrEmpty(tableName) ? "id" : String.format("%s.id", tableName), domainId);
String entityIdColumn = nullOrEmpty(tableName) ? "id" : (tableName + ".id");
if (domainId == null) {
return "";
} else if (NULL_PARAM.equals(domainId)) {
String entityType = getQueryParam("entityType");
String entityTypeCondition =
nullOrEmpty(entityType)
? ""
: String.format("AND entity_relationship.toEntity='%s'", entityType);
return String.format(
"(%s NOT IN (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' %s AND relation=10))",
entityIdColumn, entityTypeCondition);
} else {
return String.format(
"(%s in (SELECT entity_relationship.toId FROM entity_relationship WHERE entity_relationship.fromEntity='domain' AND entity_relationship.fromId IN (%s) AND "
+ "relation=10))",
entityIdColumn, domainId);
}
}

public String getApiCollectionCondition(String apiEndpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public ResultList<T> listInternal(
authorizer.authorize(securityContext, operationContext, resourceContext);

// Add Domain Filter
EntityUtil.addDomainQueryParam(securityContext, filter);
EntityUtil.addDomainQueryParam(securityContext, filter, entityType);

// List
ResultList<T> resultList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty;
import static org.openmetadata.schema.type.Include.ALL;
import static org.openmetadata.schema.type.Include.NON_DELETED;
import static org.openmetadata.service.jdbi3.ListFilter.NULL_PARAM;
import static org.openmetadata.service.jdbi3.RoleRepository.DOMAIN_ONLY_ACCESS_ROLE;
import static org.openmetadata.service.security.DefaultAuthorizer.getSubjectContext;

Expand Down Expand Up @@ -682,7 +683,8 @@ public static List<EntityReference> mergedInheritedEntityRefs(
return result.stream().toList();
}

public static void addDomainQueryParam(SecurityContext securityContext, ListFilter filter) {
public static void addDomainQueryParam(
SecurityContext securityContext, ListFilter filter, String entityType) {
SubjectContext subjectContext = getSubjectContext(securityContext);
// If the User is admin then no need to add domainId in the query param
// Also if there are domain restriction on the subject context via role
Expand All @@ -691,8 +693,8 @@ public static void addDomainQueryParam(SecurityContext securityContext, ListFilt
filter.addQueryParam(
"domainId", getCommaSeparatedIdsFromRefs(subjectContext.getUserDomains()));
} else {
// TODO: Hack :(
filter.addQueryParam("domainId", "null");
filter.addQueryParam("domainId", NULL_PARAM);
filter.addQueryParam("entityType", entityType);
}
}
}
Expand Down

0 comments on commit 319c681

Please sign in to comment.