Skip to content

Commit 6ea6dbd

Browse files
authored
refactor(policy): provide new contexts and function interfaces for policy engine (#4542)
* refactor(policy-engine): provide new contexts and function interfaces * pr remark
1 parent 548b344 commit 6ea6dbd

File tree

28 files changed

+693
-104
lines changed

28 files changed

+693
-104
lines changed

core/control-plane/control-plane-aggregate-services/build.gradle.kts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ plugins {
1717
}
1818

1919
dependencies {
20-
implementation(project(":spi:common:validator-spi"))
21-
implementation(project(":spi:control-plane:control-plane-spi"))
22-
implementation(project(":core:common:lib:util-lib"))
23-
implementation(project(":spi:common:boot-spi"))
20+
implementation(project(":spi:common:policy:request-policy-context-spi"))
2421
implementation(project(":spi:common:transaction-spi"))
22+
implementation(project(":spi:common:validator-spi"))
2523
implementation(project(":spi:control-plane:asset-spi"))
24+
implementation(project(":spi:control-plane:control-plane-spi"))
2625
implementation(project(":spi:control-plane:secrets-spi"))
2726
implementation(project(":spi:control-plane:transfer-data-plane-spi"))
27+
implementation(project(":core:common:lib:util-lib"))
2828

2929
implementation(libs.opentelemetry.instrumentation.annotations)
3030

core/control-plane/control-plane-contract/build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ plugins {
2020
dependencies {
2121
api(project(":spi:common:json-ld-spi"))
2222
api(project(":spi:common:policy-engine-spi"))
23-
api(project(":spi:control-plane:contract-spi"))
2423
api(project(":spi:control-plane:asset-spi"))
24+
api(project(":spi:control-plane:catalog-spi"))
25+
api(project(":spi:control-plane:contract-spi"))
2526

2627
implementation(project(":core:common:lib:state-machine-lib"))
2728
implementation(project(":core:control-plane:lib:control-plane-policies-lib"))

data-protocols/dsp/dsp-http-spi/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ plugins {
1919

2020
dependencies {
2121
api(project(":spi:common:core-spi"))
22+
api(project(":spi:common:policy:request-policy-context-spi"))
2223
api(project(":data-protocols:dsp:dsp-spi"))
2324

2425
api(libs.okhttp)

extensions/common/auth/auth-delegated/src/test/java/org/eclipse/edc/api/auth/delegated/DelegatedAuthenticationServiceTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
class DelegatedAuthenticationServiceTest {
4545

46-
private static final long TEST_CACHE_VALIDITY = 50;
4746
private final TokenValidationRulesRegistry rulesRegistry = mock();
4847
private final PublicKeyResolver publicKeyResolver = mock();
4948
private final ObjectMapper mapper = new ObjectMapper();
@@ -163,4 +162,4 @@ void isAuthenticated_withXapiKeyAndAuthHeader_authTakesPrecedence() {
163162
verifyNoMoreInteractions(publicKeyResolver, rulesRegistry);
164163
}
165164

166-
}
165+
}

extensions/common/iam/identity-trust/identity-trust-core/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55

66
dependencies {
77
api(project(":spi:common:identity-trust-spi"))
8+
api(project(":spi:common:policy:request-policy-context-spi"))
89
implementation(project(":spi:common:keys-spi"))
910
implementation(project(":spi:common:http-spi"))
1011
implementation(project(":spi:common:json-ld-spi"))

settings.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ include(":spi:common:token-spi")
235235
include(":spi:common:oauth2-spi")
236236
include(":spi:common:policy-engine-spi")
237237
include(":spi:common:policy-model")
238+
include(":spi:common:policy:request-policy-context-spi")
238239
include(":spi:common:transaction-datasource-spi")
239240
include(":spi:common:transaction-spi")
240241
include(":spi:common:transform-spi")

spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/AtomicConstraintFunction.java

+4-31
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,16 @@
1414

1515
package org.eclipse.edc.policy.engine.spi;
1616

17-
import org.eclipse.edc.policy.model.Operator;
1817
import org.eclipse.edc.policy.model.Rule;
19-
import org.eclipse.edc.spi.result.Result;
2018

2119
/**
2220
* Invoked during policy evaluation when the left operand of an atomic constraint evaluates to a key associated with this function. The function is responsible for performing
2321
* policy evaluation on the right operand.
22+
*
23+
* @deprecated use {@link AtomicConstraintRuleFunction}.
2424
*/
25+
@Deprecated(since = "0.10.0")
2526
@FunctionalInterface
26-
public interface AtomicConstraintFunction<R extends Rule> {
27-
28-
/**
29-
* Performs the evaluation.
30-
*
31-
* @param operator the operation
32-
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection.
33-
* @param rule the rule associated with the constraint
34-
* @param context the policy context
35-
*/
36-
boolean evaluate(Operator operator, Object rightValue, R rule, PolicyContext context);
37-
38-
/**
39-
* Performs a validation of an atomic constraint
40-
*
41-
* @param operator the operation
42-
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection
43-
* @param rule the rule associated with the constraint
44-
* @return the result of the validation
45-
*/
46-
default Result<Void> validate(Operator operator, Object rightValue, R rule) {
47-
return Result.success();
48-
}
27+
public interface AtomicConstraintFunction<R extends Rule> extends AtomicConstraintRuleFunction<R, PolicyContext> {
4928

50-
/**
51-
* Returns the name of the function
52-
*/
53-
default String name() {
54-
return getClass().getSimpleName();
55-
}
5629
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2021 Microsoft Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Microsoft Corporation - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.policy.engine.spi;
16+
17+
import org.eclipse.edc.policy.model.Operator;
18+
import org.eclipse.edc.policy.model.Rule;
19+
import org.eclipse.edc.spi.result.Result;
20+
21+
/**
22+
* Invoked during policy evaluation when the left operand of an atomic constraint evaluates to a key associated with this function. The function is responsible for performing
23+
* policy evaluation on the right operand.
24+
*/
25+
@FunctionalInterface
26+
public interface AtomicConstraintRuleFunction<R extends Rule, C extends PolicyContext> {
27+
28+
/**
29+
* Performs the evaluation.
30+
*
31+
* @param operator the operation
32+
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection.
33+
* @param rule the rule associated with the constraint
34+
* @param context the policy context
35+
*/
36+
boolean evaluate(Operator operator, Object rightValue, R rule, C context);
37+
38+
/**
39+
* Performs a validation of an atomic constraint
40+
*
41+
* @param operator the operation
42+
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection
43+
* @param rule the rule associated with the constraint
44+
* @return the result of the validation
45+
*/
46+
default Result<Void> validate(Operator operator, Object rightValue, R rule) {
47+
return Result.success();
48+
}
49+
50+
/**
51+
* Returns the name of the function
52+
*/
53+
default String name() {
54+
return getClass().getSimpleName();
55+
}
56+
57+
}

spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/DynamicAtomicConstraintFunction.java

+4-42
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,15 @@
1414

1515
package org.eclipse.edc.policy.engine.spi;
1616

17-
import org.eclipse.edc.policy.model.Operator;
1817
import org.eclipse.edc.policy.model.Rule;
19-
import org.eclipse.edc.spi.result.Result;
2018

2119
/**
2220
* Invoked during policy evaluation as when the left operand of an atomic constraint evaluates to a key that is not bound to a {@link AtomicConstraintFunction}.
2321
* The function is responsible for performing policy evaluation on the right operand and the left operand.
22+
*
23+
* @deprecated use {@link DynamicAtomicConstraintRuleFunction}
2424
*/
25-
public interface DynamicAtomicConstraintFunction<R extends Rule> {
26-
27-
/**
28-
* Performs the evaluation.
29-
*
30-
* @param leftValue the left-side expression for the constraint
31-
* @param operator the operation
32-
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection.
33-
* @param rule the rule associated with the constraint
34-
* @param context the policy context
35-
*/
36-
boolean evaluate(Object leftValue, Operator operator, Object rightValue, R rule, PolicyContext context);
37-
38-
/**
39-
* Returns true if the function can evaluate the input left operand.
40-
*
41-
* @param leftValue the left-side expression for the constraint
42-
* @return true if the function can evaluate the left operand, false otherwise
43-
*/
44-
boolean canHandle(Object leftValue);
45-
46-
/**
47-
* Performs a validation of an atomic constraint
48-
*
49-
* @param leftValue the left-side expression for the constraint
50-
* @param operator the operation
51-
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection
52-
* @param rule the rule associated with the constraint
53-
* @return the result of the validation
54-
*/
55-
default Result<Void> validate(Object leftValue, Operator operator, Object rightValue, R rule) {
56-
return Result.success();
57-
}
58-
59-
/**
60-
* Returns the name of the function
61-
*/
62-
default String name() {
63-
return getClass().getSimpleName();
64-
}
25+
@Deprecated(since = "0.10.0")
26+
public interface DynamicAtomicConstraintFunction<R extends Rule> extends DynamicAtomicConstraintRuleFunction<R, PolicyContext> {
6527

6628
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.policy.engine.spi;
16+
17+
import org.eclipse.edc.policy.model.Operator;
18+
import org.eclipse.edc.policy.model.Rule;
19+
import org.eclipse.edc.spi.result.Result;
20+
21+
/**
22+
* Invoked during policy evaluation as when the left operand of an atomic constraint evaluates to a key that is not bound to a {@link AtomicConstraintRuleFunction}.
23+
* The function is responsible for performing policy evaluation on the right operand and the left operand.
24+
*/
25+
public interface DynamicAtomicConstraintRuleFunction<R extends Rule, C extends PolicyContext> {
26+
27+
/**
28+
* Performs the evaluation.
29+
*
30+
* @param leftValue the left-side expression for the constraint
31+
* @param operator the operation
32+
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection.
33+
* @param rule the rule associated with the constraint
34+
* @param context the policy context
35+
*/
36+
boolean evaluate(Object leftValue, Operator operator, Object rightValue, R rule, C context);
37+
38+
/**
39+
* Returns true if the function can evaluate the input left operand.
40+
*
41+
* @param leftValue the left-side expression for the constraint
42+
* @return true if the function can evaluate the left operand, false otherwise
43+
*/
44+
boolean canHandle(Object leftValue);
45+
46+
/**
47+
* Performs a validation of an atomic constraint
48+
*
49+
* @param leftValue the left-side expression for the constraint
50+
* @param operator the operation
51+
* @param rightValue the right-side expression for the constraint; the concrete type may be a string, primitive or object such as a JSON-LD encoded collection
52+
* @param rule the rule associated with the constraint
53+
* @return the result of the validation
54+
*/
55+
default Result<Void> validate(Object leftValue, Operator operator, Object rightValue, R rule) {
56+
return Result.success();
57+
}
58+
59+
/**
60+
* Returns the name of the function
61+
*/
62+
default String name() {
63+
return getClass().getSimpleName();
64+
}
65+
66+
}

spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/PolicyContext.java

+11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public interface PolicyContext {
4646
* @param type the type class.
4747
* @param <T> the type of data.
4848
* @return the object associated with the type, or null.
49+
* @deprecated implementations should add specific get methods
4950
*/
51+
@Deprecated(since = "0.10.0")
5052
<T> T getContextData(Class<T> type);
5153

5254
/**
@@ -55,7 +57,16 @@ public interface PolicyContext {
5557
* @param type the type class.
5658
* @param data the data.
5759
* @param <T> the type of data.
60+
* @deprecated implementations should add specific set methods
5861
*/
62+
@Deprecated(since = "0.10.0")
5963
<T> void putContextData(Class<T> type, T data);
6064

65+
/**
66+
* The policy scope
67+
*
68+
* @return the policy scope.
69+
*/
70+
String scope();
71+
6172
}

spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/PolicyContextImpl.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
/**
2727
* Default context implementation.
2828
*/
29-
public class PolicyContextImpl implements PolicyContext {
29+
public abstract class PolicyContextImpl implements PolicyContext {
3030
private final List<String> problems = new ArrayList<>();
3131
private final Map<Class<?>, Object> additional = new HashMap<>();
3232

33-
private PolicyContextImpl() {
33+
protected PolicyContextImpl() {
3434
}
3535

3636
@Override
@@ -61,7 +61,13 @@ public <T> void putContextData(Class<T> type, T data) {
6161

6262
public static class Builder {
6363

64-
private final PolicyContextImpl context = new PolicyContextImpl();
64+
private final PolicyContextImpl context = new PolicyContextImpl() {
65+
66+
@Override
67+
public String scope() {
68+
return "";
69+
}
70+
};
6571

6672
private Builder() {
6773

spi/common/policy-engine-spi/src/main/java/org/eclipse/edc/policy/engine/spi/PolicyValidatorFunction.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616

1717
import org.eclipse.edc.policy.model.Policy;
1818

19-
import java.util.function.BiFunction;
20-
2119
/**
2220
* A {@link Policy} validator that can be registered in the {@link PolicyEngine} in pre- or post-evaluation phase.
21+
*
22+
* @deprecated use {@link PolicyValidatorRule}
2323
*/
24+
@Deprecated(since = "0.10.0")
2425
@FunctionalInterface
25-
public interface PolicyValidatorFunction extends BiFunction<Policy, PolicyContext, Boolean> {
26+
public interface PolicyValidatorFunction extends PolicyValidatorRule<PolicyContext> {
2627

27-
/**
28-
* Returns the name of the {@link PolicyValidatorFunction}
29-
*/
30-
default String name() {
31-
return getClass().getSimpleName();
32-
}
3328
}

0 commit comments

Comments
 (0)