Skip to content

[2.0] Fixes #34826: apply a removed policy rule on the next authorization decision (#34940) - #37996

Merged
mohityadav766 merged 2 commits into
2.0from
backport/2.0/34940-policy-revocation
Nov 17, 2026
Merged

mohityadav766 merged 2 commits into
2.0from
backport/2.0/34940-policy-revocation

Conversation

@mohityadav766

@mohityadav766 mohityadav766 commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #34826

Backport of #34940 to 2.0.

PolicyEnforcementIT.aDisabledPolicyGrantsNothing fails on every 2.0 PR, on all three engine lanes (MySQL + ES, Postgres + ES + Redis, Postgres + OpenSearch; e.g. run 44081196310 on #37962). A user keeps a grant after the policy behind it is disabled.

On 2.0 the per-user policy cache (SubjectCache, two-minute TTL) is not dropped when a policy's enabled flag changes:

  • PolicyRepository invalidated it only for rule changes, inline and before the write was stored.
  • The post-commit path calls CacheBundle.invalidateEntity, but 2.0's SubjectCache invalidator ignores policy and role writes, so the Redis lane fails too. With cacheProvider=none the invalidator is not even registered.

Carried from #34940 (as on main):

  • CacheBundle registers SubjectCache.invalidator() for every cache provider.
  • PolicyRepository drops the pre-store SubjectCache.invalidateAll(); the post-commit path does it now.
  • RevocableLoadingCache backs both authorization caches. Invalidation retires the cache generation, so a load that started before an edit cannot publish the old rules.
  • Policy and user-context loads run in a FreshReadScope.
  • MCP tool and prompt callbacks clear per-request thread state on entry and exit.
  • ADR 2026-10-13-authorization-caches-retire-generations-after-commit, with the guard list matched to 2.0.

Needed on 2.0 in addition:

Not carried:

As on main since #33632, a team, role or policy write now drops every cached policy entry, and Redis pub/sub carries that to the other pods. These are administrative writes.

Type of change:

  • Bug fix

High-level design:

See the lists above. The 2.0-specific parts are the invalidator cases and the request-cache scope; everything else matches main's #34940. The ADR records the decision.

Tests:

Use cases covered

  • A disabled policy grants nothing on the next request (PolicyEnforcementIT).
  • Readers that start after a policy edit returns never get the removed rule, for a direct and a team-inherited role (PolicyRevocationConcurrencyIT).
  • Removing a create grant (PUT or PATCH) denies the next MCP and REST creates, and MCP creates and patches record the MCP bot as impersonator (PolicyRevocationConcurrencyIT).
  • State left on a reused MCP worker thread never reaches the next call (McpServerRequestContextTest).

Unit tests

Backend integration tests

Ingestion integration tests

  • Not applicable (no ingestion changes).

🤖 Generated with Claude Code

SubjectCache now loads policies and user contexts in a FreshReadScope so
that a retry after an invalidation cannot read the pre-edit policy back
from the thread-local request cache. On 2.0 a fresh read still read and
filled that cache (under fromCache=false keys), so a load retried on the
same thread could compile the old rules into the new cache generation
(SubjectCacheTest.testLoadIgnoresPolicyLeftInTheThreadsRequestCache).

RequestEntityCache, FreshReadScope and RequestEntityCacheTest are taken
as they are on main after #34209; 2.0's copies matched main before
#34089. The javadoc drops main's claim that fresh reads skip the
not-found markers, which 2.0's find() still checks. #34209's
EventSubscriptionScheduler change is not part of this.
…ecision (#34940)

Backport of #34940 to 2.0, adapted where 2.0 differs from main:

- SubjectCache's invalidator drops the policy cache on team, role and
  policy writes, and a user write drops that user's policy entry too.
  Main got that with #33632, which 2.0 does not have. Without it the
  post-commit invalidation this change moves to never reaches the policy
  cache, so a disabled policy kept granting access until the two-minute
  TTL ran out (PolicyEnforcementIT.aDisabledPolicyGrantsNothing).
  SubjectCacheTest gains main's team/role/user invalidator tests and a
  policy-write one, and keeps a single putUserWithPolicy helper (2.0
  already had one).
- invalidateAll() has no team-hierarchy cache to drop on 2.0.
- McpServer: 2.0's tool callback reads no active-persona header and its
  prompt callback resolves the token from the transport context. Both now
  run inside the clean request context. McpServerRequestContextTest
  follows that: a persona left on the worker replaces the header test,
  and the prompt tests pass the token in the context.
- CacheBundleTest puts back the user repository it replaced in the
  JVM-wide registry. Its mock answers every user name, and in 2.0's unit
  run it made ODCSConverterTest's owner lookups find users.
- Not carried: RdfPolicyRevocationIT (2.0 has no agent SPARQL endpoint),
  the implementation plan and the docs/index.md entry (2.0 has no index).
  The ADR is carried with its guard list matched to 2.0.

(cherry picked from commit 0ce7ed0)
@mohityadav766
mohityadav766 requested a review from a team as a code owner November 17, 2026 14:46
@mohityadav766
mohityadav766 requested review from pmbrull and removed request for a team November 17, 2026 14:46
@github-actions

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

  • No GitHub issue is linked. Link an issue in the Development section of the PR (or add Fixes #12345 to the description). For a same-org cross-repo issue, add Fixes open-metadata/<repo>#123 to the description.

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

Comment on lines 78 to 87
private static final Invalidatable INVALIDATOR =
(type, id, fqn) -> {
if (Entity.PERSONA.equals(type) || Entity.TEAM.equals(type)) {
if (Entity.TEAM.equals(type) || Entity.ROLE.equals(type) || Entity.POLICY.equals(type)) {
invalidateAll();
} else if (Entity.PERSONA.equals(type)) {
invalidateAllUserContexts();
} else if (Entity.USER.equals(type) && fqn != null) {
invalidateUserContextByFqn(fqn);
invalidateUserByFqn(fqn);
}
};

@gitar-bot gitar-bot Bot Nov 17, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Performance: Team ref-change pub/sub now flushes every peer's policy cache

The new invalidator calls invalidateAll() for every team message, and it does not check op or fqn. On 2.0, EntityRepository.addRelationship/deleteRelationship call invalidateCacheForEntity(toEntity, toId, null). Team is a cacheable type, so that path publishes a ref-change message for the team on any relationship change that involves one. That includes making a team the owner of a table, adding a user to a team, and setting a team as domain expert. The CacheBundle pub/sub handler passes every message to all registered Invalidatables. So on every other pod, each of these ordinary writes (ingestion that assigns team owners, for example) now drops every user's compiled policies and every user context. The next requests then rebuild them from the database: user, roles, team hierarchy and policies.

This breaks the PR's assumption that these are "rare administrative writes". Under ingestion load the 2-minute policy cache stays close to empty on peers.

Fix: only call invalidateAll() for team messages that can change grants, i.e. a write to the team entity itself, which carries an fqn. Skip the fqn-less ref-change signals, or filter on op. A team-membership change already arrives as a user write and drops that user's entry.

Skip relationship-only ref-change signals (published with a null fqn) for team:

(type, id, fqn) -> {
  if (Entity.ROLE.equals(type) || Entity.POLICY.equals(type)
      || (Entity.TEAM.equals(type) && fqn != null)) {
    invalidateAll();
  } else if (Entity.PERSONA.equals(type) || Entity.TEAM.equals(type)) {
    invalidateAllUserContexts();
  } else if (Entity.USER.equals(type) && fqn != null) {
    invalidateUserByFqn(fqn);
  }
};

Was this helpful? React with 👍 / 👎

@gitar-bot

gitar-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Code Review ⚠️ Changes requested 🔴 High risk 0 closed / 1 findings

Authorization cache invalidation changes access decisions after policy, role, and team writes; stale grants could otherwise permit unauthorized operations.

Backports the policy revocation fix so a disabled or removed policy takes effect on the next authorization decision, adding post-commit cache invalidation, revocable caches, and fresh-read request-cache scoping. Changes requested: the new team invalidator calls invalidateAll() on every team ref-change pub/sub message, including relationship changes such as team ownership assignments that do not alter grants. That would drop every peer's policy cache on ordinary ingestion writes, so it should be limited to team entity writes that carry an fqn.

⚠️ Performance: Team ref-change pub/sub now flushes every peer's policy cache

📄 openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java:78-87 📄 openmetadata-service/src/main/java/org/openmetadata/service/cache/CacheBundle.java:54-56

The new invalidator calls invalidateAll() for every team message, and it does not check op or fqn. On 2.0, EntityRepository.addRelationship/deleteRelationship call invalidateCacheForEntity(toEntity, toId, null). Team is a cacheable type, so that path publishes a ref-change message for the team on any relationship change that involves one. That includes making a team the owner of a table, adding a user to a team, and setting a team as domain expert. The CacheBundle pub/sub handler passes every message to all registered Invalidatables. So on every other pod, each of these ordinary writes (ingestion that assigns team owners, for example) now drops every user's compiled policies and every user context. The next requests then rebuild them from the database: user, roles, team hierarchy and policies.

This breaks the PR's assumption that these are "rare administrative writes". Under ingestion load the 2-minute policy cache stays close to empty on peers.

Fix: only call invalidateAll() for team messages that can change grants, i.e. a write to the team entity itself, which carries an fqn. Skip the fqn-less ref-change signals, or filter on op. A team-membership change already arrives as a user write and drops that user's entry.

Skip relationship-only ref-change signals (published with a null fqn) for team
(type, id, fqn) -> {
  if (Entity.ROLE.equals(type) || Entity.POLICY.equals(type)
      || (Entity.TEAM.equals(type) && fqn != null)) {
    invalidateAll();
  } else if (Entity.PERSONA.equals(type) || Entity.TEAM.equals(type)) {
    invalidateAllUserContexts();
  } else if (Entity.USER.equals(type) && fqn != null) {
    invalidateUserByFqn(fqn);
  }
};
🤖 Prompt for agents
Code Review: Backports the policy revocation fix so a disabled or removed policy takes effect on the next authorization decision, adding post-commit cache invalidation, revocable caches, and fresh-read request-cache scoping. Changes requested: the new team invalidator calls `invalidateAll()` on every team `ref-change` pub/sub message, including relationship changes such as team ownership assignments that do not alter grants. That would drop every peer's policy cache on ordinary ingestion writes, so it should be limited to team entity writes that carry an fqn.

1. ⚠️ Performance: Team ref-change pub/sub now flushes every peer's policy cache
   Files: openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java:78-87, openmetadata-service/src/main/java/org/openmetadata/service/cache/CacheBundle.java:54-56

   The new invalidator calls `invalidateAll()` for every `team` message, and it does not check `op` or `fqn`. On 2.0, `EntityRepository.addRelationship`/`deleteRelationship` call `invalidateCacheForEntity(toEntity, toId, null)`. Team is a cacheable type, so that path publishes a `ref-change` message for the team on any relationship change that involves one. That includes making a team the owner of a table, adding a user to a team, and setting a team as domain expert. The CacheBundle pub/sub handler passes every message to all registered Invalidatables. So on every other pod, each of these ordinary writes (ingestion that assigns team owners, for example) now drops every user's compiled policies and every user context. The next requests then rebuild them from the database: user, roles, team hierarchy and policies.
   
   This breaks the PR's assumption that these are "rare administrative writes". Under ingestion load the 2-minute policy cache stays close to empty on peers.
   
   Fix: only call `invalidateAll()` for team messages that can change grants, i.e. a write to the team entity itself, which carries an fqn. Skip the fqn-less `ref-change` signals, or filter on `op`. A team-membership change already arrives as a user write and drops that user's entry.

   Fix (Skip relationship-only ref-change signals (published with a null fqn) for team):
   (type, id, fqn) -> {
     if (Entity.ROLE.equals(type) || Entity.POLICY.equals(type)
         || (Entity.TEAM.equals(type) && fqn != null)) {
       invalidateAll();
     } else if (Entity.PERSONA.equals(type) || Entity.TEAM.equals(type)) {
       invalidateAllUserContexts();
     } else if (Entity.USER.equals(type) && fqn != null) {
       invalidateUserByFqn(fqn);
     }
   };

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Compact
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Powered by Gitar — free for open source

Comment on lines +234 to +238
Deque<Scope> scopes = SCOPES.get();
if (scopes == null) {
return;
}
scopes.remove(this);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 New helpers break Java requirements

The added helpers violate explicit requirements in the Java guide. Here, Scope.close() adds an early return and leaves the unchanged local scopes non-final. The guide requires one trailing return and final on unchanged locals and parameters.

The new callToolWith() also exceeds the five-parameter limit, and cachePausingFirstLoad() uses a raw string in .equals() where the guide requires a constant. Bring these new helpers into line with the repository requirements before merging.

Context Used: CLAUDE.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@mohityadav766
mohityadav766 merged commit 7c86957 into 2.0 Nov 17, 2026
63 of 70 checks passed
@mohityadav766
mohityadav766 deleted the backport/2.0/34940-policy-revocation branch November 17, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant