Skip to content

RANGER-5842: Evaluate constraint policies in the policy engine and return applicable constraints in the access result - #1316

Merged
vyommani merged 5 commits into
apache:masterfrom
vyommani:RANGER-5842
Nov 2, 2026
Merged

vyommani merged 5 commits into
apache:masterfrom
vyommani:RANGER-5842

Conversation

@vyommani

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

Depends on #1309 (RANGER-5838). Only the last commit is new; I’ll rebase once #1309 is merged.

What changes were proposed in this pull request?

The policy engine now evaluates constraint policies (POLICY_TYPE_CONSTRAINTS, added in RANGER-5838). When an access request is allowed, every matching constraint is returned in the access result, and the calling service enforces them. Examples: pause for human approval (requireApproval), or limit the rows returned (maxRows).

Integration for services

There’s no change to isAccessAllowed(request).
On an allowed result, RangerAccessResult.getConstraintResults() returns each matching constraint (RangerConstraintResult): the constraint name, parameter values, policy ID and version, and match type.
On a denied result, the list is always empty.

Engine changes

RangerPolicyRepository keeps constraint policy evaluators in a separate list and resource trie. POLICY_TYPE_CONSTRAINTS is not added to POLICY_TYPES, so existing evaluation paths are unchanged. Policy deltas (create, update, delete) are supported.
RangerPolicyEngineImpl evaluates constraint policies after the access decision, and only when the result is allowed. It collects all matching constraints and ignores policy priority. It returns early when a service has no constraint policies.
Matching uses the resource, policy conditions, validity schedule, users/groups/roles, access type and item conditions, including ATTR conditions (RANGER-5822).
RangerDefaultConstraintPolicyItemEvaluator is the only reader of RangerPolicyItemConstraintInfo. Constraint items never change the allow/deny decision.
RangerServiceDefHelper uses the access resource hierarchy for constraint policies, and RangerPolicyDeltaUtil accepts constraint policy deltas.

Limitations (follow-up sub-tasks)

Only request.getAccessType() is matched. Requests for several access types, and _any requests, aren’t expanded.
Resource-based policies only; tag-based constraint policies come later.
The engine doesn’t check constraintDefs. Validating constraint policies against the service-def happens in Admin.
Not yet included: registering the constraints a plugin supports (with deny for unsupported ones), audit of returned constraints, constraints in RangerAuthzResult for authz-api/PDP, and the UI.

How was this patch tested?

New engine test cases in test_policyengine_constraints.json
mvn -pl agents-common verify passes

@mneethiraj mneethiraj left a comment

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.

@vyommani - I suggest separating this PR into 2 parts:

  1. to capture updates to RangerAccessResult - so that plugins can start validating the integration
  2. policy evaluation updates for constraints


if (policyType == RangerPolicy.POLICY_TYPE_AUDIT) {
// Audit and constraint policies are outside POLICY_TYPES and use the access resource hierarchy.
if (policyType == RangerPolicy.POLICY_TYPE_AUDIT || policyType == RangerPolicy.POLICY_TYPE_CONSTRAINTS) {

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.

Each constraintDef has its own resources list. So, either this method should ake an additional parameter, constraint, or add a new method that return resources for a speciific constraint.

public Set<List<RangerResourceDef>> getResourceHierarchiesForConstraint(String constraint, Collection<String> keys) {
  ...

@vyommani vyommani changed the title Ranger 5842: Evaluate constraint policies in the policy engine and return applicable constraints in the access result RANGER-5842: Evaluate constraint policies in the policy engine and return applicable constraints in the access result Oct 17, 2026
@vyommani
vyommani requested a review from mneethiraj October 18, 2026 14:04
@vyommani

Copy link
Copy Markdown
Contributor Author

Updated: rebased on #1320, renamed to constraints, and policyPriority is set on each result. Added getResourceHierarchiesForConstraint(): it returns the access-hierarchy prefixes that fall within the constraintDef’s resources, and the matcher uses it for constraint policies. Open question: should a constraint policy be limited to a single constraint name? Today the matcher takes the union of hierarchies across the policy’s constraints.

@vyommani

Copy link
Copy Markdown
Contributor Author

@mneethiraj
Addressed all review comments:

Split: RangerAccessResult / RangerConstraintResult changes merged separately in #1320 (RANGER-5848); this PR now covers evaluation only.
Added RangerServiceDefHelper.getResourceHierarchiesForConstraint(); constraint hierarchies are the access-hierarchy prefixes within the constraintDef’s resources, so sibling resources (e.g. database→table, database→udf) work.
Renamed to constraints (in #1320).
Added policyPriority and CONSTRAINT_RESULT_COMPARATOR (in #1320).

Also rebased on master and added RANGER_PLUGIN_CAPABILITY_CONSTRAINT_POLICIES. On mixed constraints in one policy: Admin validation (RANGER-5852) will require the policy’s resources to be valid for every constraint used by its items.

private RangerConstraintDef getConstraintDef(String constraint) {
RangerConstraintDef ret = null;

if (StringUtils.isNotEmpty(constraint) && CollectionUtils.isNotEmpty(delegate.servicedef.getConstraintDefs())) {

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.

Instead of iterating at each call, consider caching in RangerServiceDefHelper.

@vyommani

Copy link
Copy Markdown
Contributor Author

@mneethiraj , addressed your review comments in the latest commit:

constraint hierarchies are built from each constraintDef’s resources (graph, as for data-mask/row-filter) and cached per constraint in RangerServiceDefHelper
constraintDefs cached by name
constraint names computed once per policy evaluator
constraint policy evaluators sorted by priority at init; engine test now checks result order

@vyommani
vyommani requested a review from mneethiraj October 31, 2026 06:12
@vyommani
vyommani requested a review from mneethiraj November 2, 2026 04:05
@vyommani
vyommani merged commit 75d5790 into apache:master Nov 2, 2026
4 checks passed
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.

2 participants