Elffuss BraidHub

kikocis/EDC public

cc2a9ff178c4 · 5.5 KiBRenderedRawPermalink
1# Policy Engine
2
3## Register policy functions
4
5The following will document how to register a constraint function to the policy engine.
6
7### Step 1: Create an extension
8
9As an example, we create the module `:extensions:policy:ids-policy`.
10
111. Add a `build.gradle.kts` file:
12
13 ```
14 val rsApi: String by project
15
16 plugins {
17 `java-library`
18 }
19
20 dependencies {
21 api(project(":spi"))
22 }
23 ```
24
252. Specify the `IdsPolicyExtension` in the `resources/META-INF/services` directory:
26
27 ```
28 org.eclipse.edc.protocol.ids.policy.IdsPolicyExtension
29 ```
30
314. To ensure that the extension will be loaded, add it to the `settings.gradle.kts` at the root of the project:
32
33 ```
34 include(":extensions:policy:ids-policy")
35 ```
36
37### Step 2: Implement a `ServiceExtension`
38
39The new policy extension will provide a service extension class and two constraint functions. In
40`IdsPolicyExtension`, first, the scope `ALL_SCOPES` is bound to the rule type `use`. Next, both policy
41functions are registered to the policy engine.
42
43```java
44public class IdsPolicyExtension implements ServiceExtension {
45
46 private static final String ABS_SPATIAL_POSITION = "ids:absoluteSpatialPosition";
47 private static final String PARTNER_LEVEL = "ids:partnerLevel";
48
49 @Inject
50 private RuleBindingRegistry ruleBindingRegistry;
51
52 @Inject
53 private PolicyEngine policyEngine;
54
55 @Override
56 public String name() {
57 return "IDS Policy";
58 }
59
60 @Override
61 public void initialize(ServiceExtensionContext context) {
62 ruleBindingRegistry.bind("use", ALL_SCOPES);
63
64 policyEngine.registerFunction(ALL_SCOPES, Permission.class, ABS_SPATIAL_POSITION, new AbsSpatialPositionConstraintFunction());
65 policyEngine.registerFunction(ALL_SCOPES, Permission.class, PARTNER_LEVEL, new PartnerLevelConstraintFunction());
66 }
67}
68```
69
70In this example, the functions are registered for all policy scopes, meaning they will be used for every policy
71evaluation. Details on different policy scopes can be found [here](#policy-scopes).
72
73### Step 3: Implement an `AtomicConstraintFunction`
74
75#### Absolute Spatial Position Constraint Function
76
77```java
78public class AbsSpatialPositionConstraintFunction implements AtomicConstraintFunction<Permission> {
79
80 @Override
81 public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext context) {
82 var region = context.getParticipantAgent().getClaims().get("region");
83 switch (operator) {
84 case EQ:
85 return Objects.equals(region, rightValue);
86 case NEQ:
87 return !Objects.equals(region, rightValue);
88 case IN:
89 return ((Collection<?>) rightValue).contains(region);
90 default:
91 return false;
92 }
93 }
94}
95```
96
97#### Partner Level Constraint Function
98
99```java
100public class PartnerLevelConstraintFunction implements AtomicConstraintFunction<Permission> {
101
102 @Override
103 public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext context) {
104 String partnerLevel = context.getParticipantAgent().getClaims().get("partnerLevel");
105 switch (operator) {
106 case EQ:
107 return Objects.equals(partnerLevel, rightValue);
108 case NEQ:
109 return !Objects.equals(partnerLevel, rightValue);
110 case IN:
111 return ((Collection<?>) rightValue).contains(partnerLevel);
112 default:
113 return false;
114 }
115 }
116}
117```
118
119### Policy scopes
120
121By binding a function to a specific scope instead of all scopes, the function will only be included in evaluations for
122that scope. Currently, the EDC core provides 3 different policy scopes, which are explained in the following.
123
124#### Cataloging scope: `contract.cataloging`
125
126This scope is used when contract offers are generated from contract definitions. Here, each contract definition's access
127policy is evaluated to decide which contract definitions may be used to generate offers for the requesting agent.
128
129#### Negotiation scope: `contract.negotiation`
130
131This scope is used during the contract negotiation. The policies from each contract offer and agreement exchanged during
132the negotiation are evaluated with this scope. This scope is also used to re-evaluate a contract agreement's policy
133before a data transfer is initiated.
134
135#### Manifest verification scope: `provision.manifest.verify`
136
137This scope is used during the provisioning phase to evaluate the resource definitions of a generated resource manifest.
138Functions registered in this scope may also modify resource definitions so that they comply with the policy.
139Therefore, a `ResourceManifestContext`, which provides access to a manifest's resource definitions, is available
140through the `PolicyContext` for functions registered in this scope. Using the `ResourceManifestContext`, resource
141definitions can be retrieved and updated by type.
142
143```java
144@Override
145public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext context) {
146 var manifestContext = context.getContextData(ResourceManifestContext.class);
147
148 var bucketDefinitions = manifestContext.getDefinitions(S3BucketResourceDefinition.class);
149
150 // verify and/or modify definitions to comply with policy
151
152 manifestContext.replaceDefinitions(S3BucketResourceDefinition.class, verifiedBucketDefinitions);
153 return true;
154}
155```
156
157If any of the resource definitions cannot be modified to comply with the policy, the function should return `false`.