Skip to content

Commit 9b8d662

Browse files
authored
feat: add contract definition empty asset selector validator (#1786)
* adds extension and unit tests * Add ITs * Add docs * renamed module * remove not needed stuff * this was actually needed * add bypass * wording * review suggestions * removed bypass * deps * deps * deps * bump aws deps to 2.30.17 * deps * deps * deps
1 parent b66af6d commit 9b8d662

File tree

12 files changed

+615
-35
lines changed

12 files changed

+615
-35
lines changed

DEPENDENCIES

Lines changed: 44 additions & 34 deletions
Large diffs are not rendered by default.

edc-controlplane/edc-controlplane-base/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
runtimeOnly(project(":edc-extensions:edr:edr-callback"))
3232
runtimeOnly(project(":edc-extensions:tokenrefresh-handler"))
3333
runtimeOnly(project(":edc-extensions:agreements"))
34+
runtimeOnly(project(":edc-extensions:validators:empty-asset-selector"))
3435

3536
runtimeOnly(libs.edc.core.edrstore)
3637
runtimeOnly(libs.edc.edr.store.receiver)
@@ -92,4 +93,5 @@ dependencies {
9293
runtimeOnly(libs.edc.fc.core)
9394
runtimeOnly(libs.edc.fc.api)
9495

96+
9597
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License, Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
********************************************************************************/
19+
20+
plugins {
21+
`maven-publish`
22+
`java-library`
23+
}
24+
25+
dependencies {
26+
api(libs.edc.spi.controlplane)
27+
implementation(libs.edc.lib.validator)
28+
29+
testImplementation(libs.edc.junit)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License, Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
********************************************************************************/
19+
20+
package org.eclipse.tractusx.edc.validators.emptyassetselector;
21+
22+
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
23+
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
24+
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
25+
import org.eclipse.edc.spi.monitor.Monitor;
26+
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
27+
import org.eclipse.edc.spi.system.ServiceExtension;
28+
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;
29+
30+
import static org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_TYPE;
31+
32+
@Extension(value = EmptyAssetSelectorBlockerExtension.NAME)
33+
public class EmptyAssetSelectorBlockerExtension implements ServiceExtension {
34+
35+
public static final String NAME = "Empty Asset Selector Blocker extension";
36+
37+
private static final String BLOCKER_DISABLED = "false";
38+
39+
@Setting(description = "Block contract definitions from being created/updated with an empty asset selector.", defaultValue = BLOCKER_DISABLED, key = "tx.edc.validator.contractdefinitions.block-empty-asset-selector")
40+
private boolean blockerEnabled;
41+
42+
@Inject
43+
JsonObjectValidatorRegistry validatorRegistry;
44+
45+
@Inject
46+
CriterionOperatorRegistry criterionOperatorRegistry;
47+
48+
@Inject
49+
Monitor monitor;
50+
51+
@Override
52+
public String name() {
53+
return NAME;
54+
}
55+
56+
@Override
57+
public void prepare() {
58+
if (blockerEnabled) {
59+
monitor.info("ContractDefinition validator that blocks empty assetsSelector has been enabled");
60+
validatorRegistry.register(CONTRACT_DEFINITION_TYPE, EmptyAssetSelectorValidator.instance(criterionOperatorRegistry));
61+
}
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/********************************************************************************
2+
* Copyright (c) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Apache License, Version 2.0 which is available at
9+
* https://www.apache.org/licenses/LICENSE-2.0.
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
********************************************************************************/
19+
20+
package org.eclipse.tractusx.edc.validators.emptyassetselector;
21+
22+
import org.eclipse.edc.spi.query.CriterionOperatorRegistry;
23+
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator;
24+
import org.eclipse.edc.validator.jsonobject.validators.MandatoryArray;
25+
import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue;
26+
import org.eclipse.edc.validator.jsonobject.validators.OptionalIdNotBlank;
27+
import org.eclipse.edc.validator.jsonobject.validators.model.CriterionValidator;
28+
29+
import static org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ACCESSPOLICY_ID;
30+
import static org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ASSETS_SELECTOR;
31+
import static org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_CONTRACTPOLICY_ID;
32+
33+
public class EmptyAssetSelectorValidator {
34+
35+
public static JsonObjectValidator instance(CriterionOperatorRegistry criterionOperatorRegistry) {
36+
return JsonObjectValidator.newValidator()
37+
.verifyId(OptionalIdNotBlank::new)
38+
.verify(CONTRACT_DEFINITION_ACCESSPOLICY_ID, MandatoryValue::new)
39+
.verify(CONTRACT_DEFINITION_CONTRACTPOLICY_ID, MandatoryValue::new)
40+
.verify(CONTRACT_DEFINITION_ASSETS_SELECTOR, MandatoryArray.min(1))
41+
.verifyArrayItem(CONTRACT_DEFINITION_ASSETS_SELECTOR, path -> CriterionValidator.instance(path, criterionOperatorRegistry))
42+
.build();
43+
}
44+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contract Definitions Validator: Empty Asset Selector
2+
3+
The goal of this extension is to provide a replacement validator for contract definition entities.
4+
It is used to validate requests that create or update contract definitions via the data management API endpoint.
5+
When enabled, it prevents contract definitions with no asset selector, or an empty one, from being created which
6+
elsewise leads to all available assets being included in the Contract Definition.
7+
8+
This extension is included with the standard tractusx-edc distribution, but is disabled by default. To enable it,
9+
you can set `tx.edc.validator.contractdefinitions.block-empty-asset-selector`to `true` in your connector configuration.
10+
11+
## Example
12+
13+
When the validator extension is enabled, creating the following contract definition will lead to a 400 error.
14+
15+
```json
16+
{
17+
"@context": {
18+
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
19+
},
20+
"@type": "ContractDefinition",
21+
"@id": "myContractDefinitionId",
22+
"accessPolicyId": "myAccessPolicyId",
23+
"contractPolicyId": "myContractPolicyId"
24+
}
25+
```
26+
27+
Above, the `assetSelector` property is missing from the request, so an empty one is added by default.
28+
The validator will block this contract definition from being created.
29+
30+
Similarly, this will also fail:
31+
32+
```json
33+
{
34+
"@context": {
35+
"@vocab": "https://w3id.org/edc/v0.0.1/ns/"
36+
},
37+
"@type": "ContractDefinition",
38+
"@id": "myContractDefinitionId",
39+
"accessPolicyId": "myAccessPolicyId",
40+
"contractPolicyId": "myContractPolicyId",
41+
"assetSelector": []
42+
}
43+
```
44+
45+
The `assetSelector` property exists, but since it's an empty list the validator will also block this contract
46+
definition from being created. A valid contract definition should have at least one criterion.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#################################################################################
2+
# Copyright (c) 2025 Bayerische Motoren Werke Aktiengesellschaft
3+
#
4+
# See the NOTICE file(s) distributed with this work for additional
5+
# information regarding copyright ownership.
6+
#
7+
# This program and the accompanying materials are made available under the
8+
# terms of the Apache License, Version 2.0 which is available at
9+
# https://www.apache.org/licenses/LICENSE-2.0.
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
#
17+
# SPDX-License-Identifier: Apache-2.0
18+
#################################################################################
19+
20+
org.eclipse.tractusx.edc.validators.emptyassetselector.EmptyAssetSelectorBlockerExtension

0 commit comments

Comments
 (0)