Skip to content

Commit

Permalink
fix(irs-edc-client):[#436] fix nullpointer on missing and or or const…
Browse files Browse the repository at this point in the history
…raint
  • Loading branch information
ds-jhartmann committed Feb 29, 2024
1 parent 6da77c9 commit 72b0396
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- Update to Spring Boot 3.1.9 to fix CVE's.
- Fixed a case where Policy validation could result in a NullPointerException if either orConstraint or andConstraint of
accepted policies were null

## [4.6.0] - 2024-02-20
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.policy;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.edc.policy.model.AndConstraint;
Expand Down Expand Up @@ -63,12 +65,14 @@ private boolean isSameAs(final Constraint constraint, final Constraints accepted
if (constraint instanceof AndConstraint andConstraint) {
return andConstraint.getConstraints()
.stream()
.allMatch(constr -> isInList(constr, acceptedConstraints.getAnd()));
.allMatch(constr -> isInList(constr, Optional.ofNullable(acceptedConstraints.getAnd())
.orElse(Collections.emptyList())));
}
if (constraint instanceof OrConstraint orConstraint) {
return orConstraint.getConstraints()
.stream()
.anyMatch(constr -> isInList(constr, acceptedConstraints.getOr()));
.anyMatch(constr -> isInList(constr, Optional.ofNullable(acceptedConstraints.getOr())
.orElse(Collections.emptyList())));
}
return false;
}
Expand All @@ -87,9 +91,9 @@ private boolean isSameAs(final AtomicConstraint atomicConstraint,
return AtomicConstraintValidator.builder()
.atomicConstraint(atomicConstraint)
.leftExpressionValue(acceptedConstraint.getLeftOperand())
.rightExpressionValue(
acceptedConstraint.getRightOperand())
.expectedOperator(Operator.valueOf(acceptedConstraint.getOperator().getOperatorType().name()))
.rightExpressionValue(acceptedConstraint.getRightOperand())
.expectedOperator(Operator.valueOf(
acceptedConstraint.getOperator().getOperatorType().name()))
.build()
.isValid();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,42 @@ void shouldRejectOrConstraintIfAnyMatch() {
assertThat(result).isFalse();
}

@Test
void shouldBeNullsafeOnNoAnd() {
final OrConstraint orConstraint = createOrConstraint(
List.of(createAtomicConstraint(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE)));
final AndConstraint andConstraint = createAndConstraint(
List.of(createAtomicConstraint(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE)));

final List<Operand> operands = List.of(new Operand(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE));
final List<Constraint> constraints = operands.stream()
.map(operand -> new Constraint(operand.left(),
new Operator(OperatorType.EQ), operand.right()))
.toList();

final Policy acceptedOrPolicy = createPolicyWithConstraint(new Constraints(null, constraints));
final boolean resultOr = cut.hasAllConstraint(acceptedOrPolicy, List.of(orConstraint, andConstraint));
assertThat(resultOr).isFalse();
}

@Test
void shouldBeNullsafeOnNoOr() {
final OrConstraint orConstraint = createOrConstraint(
List.of(createAtomicConstraint(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE)));
final AndConstraint andConstraint = createAndConstraint(
List.of(createAtomicConstraint(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE)));

final List<Operand> operands = List.of(new Operand(TestConstants.PURPOSE, TestConstants.ID_3_1_TRACE));
final List<Constraint> constraints = operands.stream()
.map(operand -> new Constraint(operand.left(),
new Operator(OperatorType.EQ), operand.right()))
.toList();

final Policy acceptedAndPolicy = createPolicyWithConstraint(new Constraints(constraints, null));
final boolean resultAnd = cut.hasAllConstraint(acceptedAndPolicy, List.of(orConstraint, andConstraint));
assertThat(resultAnd).isFalse();
}

private Policy createPolicyWithAndConstraint(List<Operand> operands) {
List<Constraint> and = operands.stream()
.map(operand -> new Constraint(operand.left, new Operator(OperatorType.EQ),
Expand Down

0 comments on commit 72b0396

Please sign in to comment.