Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,9 +1,9 @@
# Policy conditions are rewritten through the SpEL AST, and a call naming only deleted entities becomes false

- **Status:** Accepted
- **Revisions:** v1 2026-10-30 (initial) · v2 2026-11-03 (team and role conditions rewritten on hard delete only)
- **Revisions:** v1 2026-10-30 (initial) · v2 2026-11-03 (team and role conditions rewritten on hard delete only) · v3 2026-11-25 (domain conditions rewritten on rename and delete)
- **Deciders:** Ram Narayan Balaji
- **Guard:** `PolicyConditionUpdaterTest`, `OpenSearchRBACConditionEvaluatorTest#falseLiteralMakesAnAllowRuleMatchNothing`, `PolicyResourceIT#test_deletingTheOnlyTagLeavesAConditionThatMatchesNothing`, `PolicyResourceIT#test_softDeletedTeamAndRoleLeavePolicyConditionAsWrittenThroughRestore`
- **Guard:** `PolicyConditionUpdaterTest`, `OpenSearchRBACConditionEvaluatorTest#falseLiteralMakesAnAllowRuleMatchNothing`, `PolicyResourceIT#test_deletingTheOnlyTagLeavesAConditionThatMatchesNothing`, `PolicyResourceIT#test_softDeletedTeamAndRoleLeavePolicyConditionAsWrittenThroughRestore`, `PolicyResourceIT#test_renamedDomainIsUpdatedInPolicyCondition`, `PolicyResourceIT#test_deletedDomainMakesItsPolicyConditionMatchNothing`
- **Related:** #26942

## Context
Expand Down Expand Up @@ -52,3 +52,16 @@ matches nothing while it is deleted, and a restore brings the team or role back
that still name it. Team and role conditions are therefore rewritten only on hard delete, like
services, classifications and glossaries. Soft delete and restore of a team or role drop the
cached policies of every user.

## Amendment: domain conditions are rewritten on rename and delete (v3)

`matchAnyDomain()` names domains by fully qualified name, so it joins the functions the updater
rewrites. A domain rename rewrites its arguments by prefix, so a condition naming a sub-domain
follows a rename of its parent. Domain delete is always hard and recursive, so it removes the
domain and every sub-domain from the condition, and a call left with no argument becomes `false`.

`matchAnyDomain()` matches the exact FQN it names: `matchAnyDomain('Finance')` does not match an
asset in `Finance.Payments`, unlike `hasDomain()`, which includes sub-domains. Existing policies
depend on that, so widening it later would need its own function or an explicit opt-in.
`PolicyConditionUpdaterTest#everyFunctionTakingArgumentsIsRewrittenOrListedAsNotNeedingIt` fails
when a policy function that takes arguments is in no rewrite set and not listed as needing none.
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import org.openmetadata.schema.api.classification.CreateTag;
import org.openmetadata.schema.api.data.CreateGlossary;
import org.openmetadata.schema.api.data.CreateGlossaryTerm;
import org.openmetadata.schema.api.domains.CreateDomain;
import org.openmetadata.schema.api.domains.CreateDomain.DomainType;
import org.openmetadata.schema.api.policies.CreatePolicy;
import org.openmetadata.schema.api.teams.CreateRole;
import org.openmetadata.schema.api.teams.CreateTeam;
import org.openmetadata.schema.entity.classification.Classification;
import org.openmetadata.schema.entity.classification.Tag;
import org.openmetadata.schema.entity.data.Glossary;
import org.openmetadata.schema.entity.data.GlossaryTerm;
import org.openmetadata.schema.entity.domains.Domain;
import org.openmetadata.schema.entity.policies.Policy;
import org.openmetadata.schema.entity.policies.accessControl.Rule;
import org.openmetadata.schema.entity.policies.accessControl.Rule.Effect;
Expand Down Expand Up @@ -1240,6 +1243,81 @@ void test_deletedGlossaryRemovesTermsFromPolicyCondition(TestNamespace ns) {
assertEquals("false", fetched.getRules().get(0).getCondition());
}

@Test
void test_renamedDomainIsUpdatedInPolicyCondition(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Domain parent = createDomain(client, ns.prefix("RenameDomain"), null);
Domain child =
createDomain(client, ns.prefix("RenameSubDomain"), parent.getFullyQualifiedName());
String policyId =
createDomainConditionPolicy(
ns.prefix("renameDomainPolicy"),
"matchAnyDomain('"
+ parent.getFullyQualifiedName()
+ "', '"
+ child.getFullyQualifiedName()
+ "')")
.getId()
.toString();

parent.setName(ns.prefix("RenamedDomain"));
Domain renamedParent = client.domains().update(parent.getId().toString(), parent);
String renamedChildFqn = client.domains().get(child.getId().toString()).getFullyQualifiedName();

// The sub-domain FQN changes with its parent's name, so both arguments must follow.
assertEquals(
"matchAnyDomain('%s', '%s')"
.formatted(renamedParent.getFullyQualifiedName(), renamedChildFqn),
getEntity(policyId).getRules().get(0).getCondition());
}

@Test
void test_deletedDomainMakesItsPolicyConditionMatchNothing(TestNamespace ns) {
OpenMetadataClient client = SdkClients.adminClient();
Domain parent = createDomain(client, ns.prefix("DeleteDomain"), null);
Domain child =
createDomain(client, ns.prefix("DeleteSubDomain"), parent.getFullyQualifiedName());
String policyId =
createDomainConditionPolicy(
ns.prefix("deleteDomainPolicy"),
"matchAnyDomain('" + child.getFullyQualifiedName() + "')")
.getId()
.toString();

client
.domains()
.delete(parent.getId().toString(), Map.of("hardDelete", "true", "recursive", "true"));

// A domain created later with the same name must not fall under the old rule.
assertEquals("false", getEntity(policyId).getRules().get(0).getCondition());
}

private static Domain createDomain(OpenMetadataClient client, String name, String parentFqn) {
return client
.domains()
.create(
new CreateDomain()
.withName(name)
.withDomainType(DomainType.AGGREGATE)
.withDescription("Domain named in a policy condition")
.withParent(parentFqn));
}

private Policy createDomainConditionPolicy(String name, String condition) {
Rule rule =
new Rule()
.withName("domainConditionRule")
.withResources(List.of(ALL_RESOURCES))
.withOperations(List.of(MetadataOperation.VIEW_ALL))
.withEffect(Effect.DENY)
.withCondition(condition);
return createEntity(
new CreatePolicy()
.withName(name)
.withRules(List.of(rule))
.withDescription("Policy naming a domain"));
}

/**
* A soft-deleted classification keeps its child tags' labels on assets, so a condition naming
* them still matches and must be left as written. Rewriting it would switch off a Deny rule that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.openmetadata.service.search.QueryFilterBuilder;
import org.openmetadata.service.security.DefaultAuthorizer;
import org.openmetadata.service.security.PatchRequester;
import org.openmetadata.service.security.policyevaluator.PolicyConditionUpdater;
import org.openmetadata.service.security.policyevaluator.SubjectCache;
import org.openmetadata.service.security.policyevaluator.SubjectContext;
import org.openmetadata.service.util.EntityUtil;
Expand Down Expand Up @@ -611,6 +612,22 @@ private String buildDryRunImpactMessage(
return message.toString();
}

@Override
protected void postDelete(Domain entity, boolean hardDelete) {
super.postDelete(entity, hardDelete);
// Domain delete is hard and recursive, so a condition naming this domain or a sub-domain can
// no longer match; leaving the name would also let a domain created later with the same name
// fall under the old rule. See ADR:2026-10-30-deleted-policy-condition-references-become-false.
if (hardDelete) {
PolicyConditionUpdater.updateAllPolicyConditions(
condition ->
PolicyConditionUpdater.removeByPrefixFromCondition(
condition,
entity.getFullyQualifiedName(),
PolicyConditionUpdater.DOMAIN_FUNCTIONS));
}
}

@Override
protected void deleteChildren(UUID id, boolean recursive, boolean hardDelete, String updatedBy) {
boolean rootDomainHardDelete = hardDelete && domainHardDeleteSubtree.get() == null;
Expand Down Expand Up @@ -969,6 +986,11 @@ private void updateName(Domain updated) {
updateEntityLinks(oldFqn, newFqn, updated);
updateSearchIndexes(oldFqn, newFqn, updated);
updateTagUsage(oldFqn, newFqn);
// By prefix, so conditions naming a sub-domain follow their parent's rename.
PolicyConditionUpdater.updateAllPolicyConditions(
condition ->
PolicyConditionUpdater.renamePrefixInCondition(
condition, oldFqn, newFqn, PolicyConditionUpdater.DOMAIN_FUNCTIONS));

// Any asset (table/dashboard/...) that carries this domain in its `domains` reference
// now has a stale FQN embedded in its cache. Invalidate them so next read rebuilds with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ private void handleMethodReference(SpelNode node, ConditionCollector collector)
hasAnyRole(roles, collector);
}
case "hasDomain" -> hasDomain(collector);
case "matchAnyDomain" -> {
List<String> domainFqns = extractMethodArguments(methodRef);
matchAnyDomain(domainFqns, collector);
}
case "inAnyTeam" -> {
List<String> teams = extractMethodArguments(methodRef);
inAnyTeam(teams, collector);
Expand Down Expand Up @@ -432,20 +436,48 @@ private void matchAnyServiceType(List<String> serviceTypes, ConditionCollector c
collector.addMust(queryBuilderFactory.boolQuery().should(clauses));
}

/**
* A Domain document carries no {@code domains} field, while the REST evaluator treats a domain as
* its own domain ({@code ResourceContext.getDomains}). The named Domain documents are matched by
* FQN as well, so an Allow does not hide them from search and a Deny does not leak them.
*/
public void matchAnyDomain(List<String> domainFqns, ConditionCollector collector) {
if (domainFqns.isEmpty()) {
// No domains requested: the condition can never match, so make the rule deny-all instead
// of adding an empty bool query (Elasticsearch/OpenSearch treat an empty bool query as
// match_all, which would turn an ALLOW rule into a full index grant).
collector.setMatchNothing(true);
} else {
List<OMQueryBuilder> domainQueries = new ArrayList<>();
for (String domainFqn : domainFqns) {
domainQueries.add(queryBuilderFactory.termQuery("domains.fullyQualifiedName", domainFqn));
}
domainQueries.add(
queryBuilderFactory
.boolQuery()
.must(
List.of(
queryBuilderFactory.termQuery("entityType", Entity.DOMAIN),
queryBuilderFactory.termsQuery("fullyQualifiedName", domainFqns))));
collector.addMust(queryBuilderFactory.boolQuery().should(domainQueries));
}
}

public void matchAnyCertification(
List<String> certificationLabels, ConditionCollector collector) {
List<OMQueryBuilder> certificationQueries = new ArrayList<>();
for (String certificationLabel : certificationLabels) {
certificationQueries.add(
queryBuilderFactory.termQuery("certification.tagLabel.tagFQN", certificationLabel));
}
OMQueryBuilder certificationQueriesCombined;
if (certificationQueries.size() == 1) {
certificationQueriesCombined = certificationQueries.get(0);
} else {
certificationQueriesCombined = queryBuilderFactory.boolQuery().should(certificationQueries);
switch (certificationQueries.size()) {
case 0 ->
// No certifications requested: deny-all instead of emitting an empty bool query, which
// Elasticsearch/OpenSearch evaluate as match_all and would turn an ALLOW into a grant.
collector.setMatchNothing(true);
case 1 -> collector.addMust(certificationQueries.get(0));
default -> collector.addMust(queryBuilderFactory.boolQuery().should(certificationQueries));
}
collector.addMust(certificationQueriesCombined);
}

public void isOwner(User user, ConditionCollector collector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

/**
* Utility for updating SpEL condition strings in policy rules when referenced entities (tags,
* roles, teams, services) are renamed or deleted.
* roles, teams, services, domains) are renamed or deleted.
*
* <p>Conditions are rewritten through the SpEL AST, so an entity name is always handled as one
* string literal whatever characters it contains. Only calls whose arguments are all string
Expand All @@ -63,6 +63,7 @@ public final class PolicyConditionUpdater {
public static final Set<String> ROLE_FUNCTIONS = Set.of("hasAnyRole");
public static final Set<String> TEAM_FUNCTIONS = Set.of("inAnyTeam");
public static final Set<String> SERVICE_FUNCTIONS = Set.of("matchAnyServiceName");
public static final Set<String> DOMAIN_FUNCTIONS = Set.of("matchAnyDomain");

/** What a call evaluates to once the entities it needs to match are gone. */
private static final String NEVER_MATCHES = "false";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,42 @@ public boolean noDomain() {
return nullOrEmpty(resourceContext.getDomains());
}

@Function(
name = "matchAnyDomain",
input = "List of comma separated Domain fully qualified names",
description =
"Returns true if the entity being accessed belongs to at least one of the domains given "
+ "as input. Matches the exact domain FQN; sub-domains are not included.",
examples = {"matchAnyDomain('Finance')", "matchAnyDomain('Finance', 'Finance.Payments')"})
@SuppressWarnings("unused")
public boolean matchAnyDomain(String... domainFQNs) {
if (expressionValidation) {
for (String domainFQN : domainFQNs) {
validateEntityReference(Entity.DOMAIN, domainFQN);
}
return false;
}
if (resourceContext == null) {
return false;
}
List<EntityReference> domains = resourceContext.getDomains();
if (nullOrEmpty(domains)) {
LOG.debug("No Domains found for resource");
return false;
}
LOG.debug(
"matchAnyDomain {} resourceDomains {}",
Arrays.toString(domainFQNs),
Arrays.toString(domains.toArray()));
for (String domainFQN : domainFQNs) {
boolean found = domains.stream().anyMatch(d -> domainFQN.equals(d.getFullyQualifiedName()));
if (found) {
return true;
}
}
return false;
}

@Function(
name = "matchAllTags",
input = "List of comma separated tag or glossary fully qualified names",
Expand Down
Loading
Loading