diff --git a/edc/Dockerfile b/edc/Dockerfile index c4e082a6..ab1623d1 100644 --- a/edc/Dockerfile +++ b/edc/Dockerfile @@ -6,6 +6,7 @@ WORKDIR /app # Copy the build files COPY gradle/* . +COPY gradle/extensions extensions # Setup wrapper RUN gradle wrapper diff --git a/edc/gradle/build.gradle.kts b/edc/gradle/build.gradle.kts index 4603b369..163cb296 100644 --- a/edc/gradle/build.gradle.kts +++ b/edc/gradle/build.gradle.kts @@ -37,6 +37,8 @@ dependencies { implementation("$groupId:data-plane-core:$edcVersion") implementation("$groupId:transfer-data-plane:$edcVersion") + + implementation(project(":extensions:policy-contract-negotiation-policy-functions")) } application { diff --git a/edc/gradle/extensions/policy-contract-negotiation-policy-functions/build.gradle.kts b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/build.gradle.kts new file mode 100644 index 00000000..16938dbb --- /dev/null +++ b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/build.gradle.kts @@ -0,0 +1,14 @@ +plugins { + `java-library` + id("application") +} + +val groupId: String by project +val edcVersion: String by project + +dependencies { + implementation("$groupId:runtime-metamodel:$edcVersion") + + implementation("$groupId:data-plane-spi:$edcVersion") + implementation("$groupId:control-plane-core:$edcVersion") +} diff --git a/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/PolicyFunctionsExtension.java b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/PolicyFunctionsExtension.java new file mode 100644 index 00000000..a0fc61d4 --- /dev/null +++ b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/PolicyFunctionsExtension.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation + * + */ + +package org.eclipse.edc.sample.extension.policy; + + +import org.eclipse.edc.policy.engine.spi.PolicyEngine; +import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry; +import org.eclipse.edc.policy.model.Permission; +import org.eclipse.edc.runtime.metamodel.annotation.Inject; +import org.eclipse.edc.spi.system.ServiceExtension; +import org.eclipse.edc.spi.system.ServiceExtensionContext; + +import static org.eclipse.edc.policy.engine.spi.PolicyEngine.ALL_SCOPES; + +public class PolicyFunctionsExtension implements ServiceExtension { + private final String policyTimeKey = "POLICY_EVALUATION_TIME"; + + @Inject + private RuleBindingRegistry ruleBindingRegistry; + + @Inject + private PolicyEngine policyEngine; + + + @Override + public String name() { + return "Policy - contract-negotiation policies"; + } + + @Override + public void initialize(ServiceExtensionContext context) { + var monitor = context.getMonitor(); + + ruleBindingRegistry.bind("USE", ALL_SCOPES); + ruleBindingRegistry.bind(policyTimeKey, ALL_SCOPES); + policyEngine.registerFunction(ALL_SCOPES, Permission.class, policyTimeKey, new TimeIntervalFunction(monitor)); + + context.getMonitor().info("Policy Extension for date and time restrictions initialized!"); + } + +} diff --git a/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/TimeIntervalFunction.java b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/TimeIntervalFunction.java new file mode 100644 index 00000000..857b7474 --- /dev/null +++ b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/java/org/eclipse/edc/sample/extension/policy/TimeIntervalFunction.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Fraunhofer Institute for Software and Systems Engineering + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Fraunhofer Institute for Software and Systems Engineering - initial API and implementation + * + */ + +package org.eclipse.edc.sample.extension.policy; + +import org.eclipse.edc.policy.engine.spi.AtomicConstraintFunction; +import org.eclipse.edc.policy.engine.spi.PolicyContext; +import org.eclipse.edc.policy.model.Operator; +import org.eclipse.edc.policy.model.Permission; +import org.eclipse.edc.spi.monitor.Monitor; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class TimeIntervalFunction implements AtomicConstraintFunction { + + private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + private final Monitor monitor; + + public TimeIntervalFunction(Monitor monitor) { + this.monitor = monitor; + } + + + + @Override + public boolean evaluate(Operator operator, Object rightValue, Permission rule, PolicyContext context) { + try { + var policyDate = DATE_FORMAT.parse((String) rightValue); + var nowDate = new Date(); + return switch (operator) { + case LT -> nowDate.before(policyDate); + case LEQ -> nowDate.before(policyDate) || nowDate.equals(policyDate); + case GT -> nowDate.after(policyDate); + case GEQ -> nowDate.after(policyDate) || nowDate.equals(policyDate); + case EQ -> nowDate.equals(policyDate); + case NEQ -> !nowDate.equals(policyDate); + default -> false; + }; + } catch (ParseException e) { + monitor.severe("Failed to parse right value of constraint to date."); + return false; + } + } +} + + diff --git a/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension new file mode 100644 index 00000000..1b26f404 --- /dev/null +++ b/edc/gradle/extensions/policy-contract-negotiation-policy-functions/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension @@ -0,0 +1 @@ +org.eclipse.edc.sample.extension.policy.PolicyFunctionsExtension \ No newline at end of file diff --git a/edc/gradle/gradle.properties b/edc/gradle/gradle.properties index ccbc6438..1756ad56 100644 --- a/edc/gradle/gradle.properties +++ b/edc/gradle/gradle.properties @@ -1,4 +1,4 @@ groupId=org.eclipse.edc -edcVersion=0.1.0 +edcVersion=0.1.2 defaultVersion=0.0.1-SNAPSHOT javaVersion=11 diff --git a/edc/gradle/settings.gradle.kts b/edc/gradle/settings.gradle.kts index 0c6c7173..8a461423 100644 --- a/edc/gradle/settings.gradle.kts +++ b/edc/gradle/settings.gradle.kts @@ -22,3 +22,5 @@ dependencyResolutionManagement { mavenLocal() } } + +include(":extensions:policy-contract-negotiation-policy-functions")