Skip to content

Commit

Permalink
use single factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Oct 21, 2024
1 parent b210f95 commit 5c4dc69
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.edc.demo.dcp.core;

import org.eclipse.edc.iam.identitytrust.spi.scope.ScopeExtractor;
import org.eclipse.edc.policy.context.request.spi.RequestPolicyContext;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.model.Operator;

Expand All @@ -26,7 +27,7 @@ class DataAccessCredentialScopeExtractor implements ScopeExtractor {
public static final String DATA_PROCESSOR_CREDENTIAL_TYPE = "DataProcessorCredential";

@Override
public Set<String> extractScopes(Object leftValue, Operator operator, Object rightValue, PolicyContext context) {
public Set<String> extractScopes(Object leftValue, Operator operator, Object rightValue, RequestPolicyContext context) {
Set<String> scopes = Set.of();
if (leftValue instanceof String leftOperand) {
if (leftOperand.startsWith(DATA_ACCESS_CONSTRAINT_PREFIX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,23 @@

package org.eclipse.edc.demo.dcp.policy;

import org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.participant.spi.ParticipantAgent;
import org.eclipse.edc.participant.spi.ParticipantAgentPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.model.Duty;
import org.eclipse.edc.policy.model.Operator;

import java.util.Objects;

public abstract class DataAccessLevelFunction<C extends ParticipantAgentPolicyContext> extends AbstractCredentialEvaluationFunction implements AtomicConstraintRuleFunction<Duty, C> {
public class DataAccessLevelFunction<C extends ParticipantAgentPolicyContext> extends AbstractCredentialEvaluationFunction implements AtomicConstraintRuleFunction<Duty, C> {

private static final String DATAPROCESSOR_CRED_TYPE = "DataProcessorCredential";

public static DataAccessLevelFunction<TransferProcessPolicyContext> createForTransferProcess() {
return new DataAccessLevelFunction<>() {
@Override
protected ParticipantAgent getAgent(TransferProcessPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}
private DataAccessLevelFunction() {

public static DataAccessLevelFunction<ContractNegotiationPolicyContext> createForNegotiation() {
return new DataAccessLevelFunction<>() {
@Override
protected ParticipantAgent getAgent(ContractNegotiationPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}

public static DataAccessLevelFunction<CatalogPolicyContext> createForCatalog() {
public static <C extends ParticipantAgentPolicyContext> DataAccessLevelFunction<C> create() {
return new DataAccessLevelFunction<>() {
@Override
protected ParticipantAgent getAgent(CatalogPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}

Expand All @@ -62,7 +40,7 @@ public boolean evaluate(Operator operator, Object rightOperand, Duty duty, C pol
policyContext.reportProblem("Cannot evaluate operator %s, only %s is supported".formatted(operator, Operator.EQ));
return false;
}
var pa = getAgent(policyContext);
var pa = policyContext.participantAgent();
if (pa == null) {
policyContext.reportProblem("ParticipantAgent not found on PolicyContext");
return false;
Expand All @@ -88,6 +66,4 @@ public boolean evaluate(Operator operator, Object rightOperand, Duty duty, C pol

}

protected abstract ParticipantAgent getAgent(C policyContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,27 @@

package org.eclipse.edc.demo.dcp.policy;

import org.eclipse.edc.connector.controlplane.catalog.spi.policy.CatalogPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.ContractNegotiationPolicyContext;
import org.eclipse.edc.connector.controlplane.contract.spi.policy.TransferProcessPolicyContext;
import org.eclipse.edc.participant.spi.ParticipantAgent;
import org.eclipse.edc.participant.spi.ParticipantAgentPolicyContext;
import org.eclipse.edc.policy.engine.spi.AtomicConstraintRuleFunction;
import org.eclipse.edc.policy.engine.spi.PolicyContext;
import org.eclipse.edc.policy.model.Operator;
import org.eclipse.edc.policy.model.Permission;

import java.time.Instant;
import java.util.Map;

public abstract class MembershipCredentialEvaluationFunction<C extends PolicyContext> extends AbstractCredentialEvaluationFunction implements AtomicConstraintRuleFunction<Permission, C> {
public class MembershipCredentialEvaluationFunction<C extends ParticipantAgentPolicyContext> extends AbstractCredentialEvaluationFunction implements AtomicConstraintRuleFunction<Permission, C> {
public static final String MEMBERSHIP_CONSTRAINT_KEY = "MembershipCredential";

private static final String MEMBERSHIP_CLAIM = "membership";
private static final String SINCE_CLAIM = "since";
private static final String ACTIVE = "active";

public static MembershipCredentialEvaluationFunction<CatalogPolicyContext> createForCatalog() {
return new MembershipCredentialEvaluationFunction<>() {
private MembershipCredentialEvaluationFunction(){

@Override
protected ParticipantAgent getAgent(CatalogPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}

public static MembershipCredentialEvaluationFunction<TransferProcessPolicyContext> createForTransfer() {
public static <C extends ParticipantAgentPolicyContext> MembershipCredentialEvaluationFunction<C> create() {
return new MembershipCredentialEvaluationFunction<>() {

@Override
protected ParticipantAgent getAgent(TransferProcessPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}

public static MembershipCredentialEvaluationFunction<ContractNegotiationPolicyContext> createForNegotiation() {
return new MembershipCredentialEvaluationFunction<>() {

@Override
protected ParticipantAgent getAgent(ContractNegotiationPolicyContext policyContext) {
return policyContext.participantAgent();
}
};
}

Expand All @@ -75,7 +50,7 @@ public boolean evaluate(Operator operator, Object rightOperand, Permission permi
return false;
}

var pa = getAgent(policyContext);
var pa = policyContext.participantAgent();
if (pa == null) {
policyContext.reportProblem("No ParticipantAgent found on context.");
return false;
Expand All @@ -97,6 +72,4 @@ public boolean evaluate(Operator operator, Object rightOperand, Permission permi
});
}

protected abstract ParticipantAgent getAgent(C policyContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ public class PolicyEvaluationExtension implements ServiceExtension {
@Override
public void initialize(ServiceExtensionContext context) {


bindPermissionFunction(MembershipCredentialEvaluationFunction.createForTransfer(), TransferProcessPolicyContext.class, TransferProcessPolicyContext.TRANSFER_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);
bindPermissionFunction(MembershipCredentialEvaluationFunction.createForNegotiation(), ContractNegotiationPolicyContext.class, ContractNegotiationPolicyContext.NEGOTIATION_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);
bindPermissionFunction(MembershipCredentialEvaluationFunction.createForCatalog(), CatalogPolicyContext.class, CatalogPolicyContext.CATALOG_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);

policyEngine.registerFunction(TransferProcessPolicyContext.class, Permission.class, );
bindPermissionFunction(MembershipCredentialEvaluationFunction.create(), TransferProcessPolicyContext.class, TransferProcessPolicyContext.TRANSFER_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);
bindPermissionFunction(MembershipCredentialEvaluationFunction.create(), ContractNegotiationPolicyContext.class, ContractNegotiationPolicyContext.NEGOTIATION_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);
bindPermissionFunction(MembershipCredentialEvaluationFunction.create(), CatalogPolicyContext.class, CatalogPolicyContext.CATALOG_SCOPE, MEMBERSHIP_CONSTRAINT_KEY);

registerDataAccessLevelFunction();

Expand All @@ -55,17 +52,17 @@ public void initialize(ServiceExtensionContext context) {
private void registerDataAccessLevelFunction() {
var accessLevelKey = "DataAccess.level";

bindDutyFunction(DataAccessLevelFunction.createForTransferProcess(), TransferProcessPolicyContext.class, TransferProcessPolicyContext.TRANSFER_SCOPE, accessLevelKey);
bindDutyFunction(DataAccessLevelFunction.createForNegotiation(), ContractNegotiationPolicyContext.class, ContractNegotiationPolicyContext.NEGOTIATION_SCOPE, accessLevelKey);
bindDutyFunction(DataAccessLevelFunction.createForCatalog(), CatalogPolicyContext.class, CatalogPolicyContext.CATALOG_SCOPE, accessLevelKey);
bindDutyFunction(DataAccessLevelFunction.create(), TransferProcessPolicyContext.class, TransferProcessPolicyContext.TRANSFER_SCOPE, accessLevelKey);
bindDutyFunction(DataAccessLevelFunction.create(), ContractNegotiationPolicyContext.class, ContractNegotiationPolicyContext.NEGOTIATION_SCOPE, accessLevelKey);
bindDutyFunction(DataAccessLevelFunction.create(), CatalogPolicyContext.class, CatalogPolicyContext.CATALOG_SCOPE, accessLevelKey);
}

private <C extends PolicyContext> void bindPermissionFunction(AtomicConstraintRuleFunction<Permission, C> function, Class<C> contextClass, String scope, String constraintType) {
ruleBindingRegistry.bind("use", scope);
ruleBindingRegistry.bind(ODRL_SCHEMA + "use", scope);
ruleBindingRegistry.bind(constraintType, scope);

// policyEngine.registerFunction(contextClass, Permission.class, constraintType, function);
policyEngine.registerFunction(contextClass, Permission.class, constraintType, function);
}

private <C extends PolicyContext> void bindDutyFunction(AtomicConstraintRuleFunction<Duty, C> function, Class<C> contextClass, String scope, String constraintType) {
Expand Down

0 comments on commit 5c4dc69

Please sign in to comment.