Skip to content

Commit

Permalink
feat(impl): [eclipse-tractusx#734] Handling for modification attempts…
Browse files Browse the repository at this point in the history
… on read-only default policies
  • Loading branch information
dsmf committed Jul 19, 2024
1 parent a7b1633 commit 66f74c4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _**For better traceability add the corresponding GitHub issue number in each cha

### Changed
- Default policies are now configured using JSON in accordance with the ODRL schema. #542
- Improved the exception handling for modification attempts on read-only default policies. Such actions now result in a 400 BAD REQUEST response with a user-friendly error message. #734

## [5.3.0] - 2024-07-15

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.policystore.exceptions;

/**
* Resource does not exist exception.
*/
public class ResourceDoesNotExistException extends PolicyStoreException {

public ResourceDoesNotExistException(final String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.tractusx.irs.common.persistence.BlobPersistenceException;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException;
import org.eclipse.tractusx.irs.policystore.exceptions.ResourceDoesNotExistException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -89,7 +90,7 @@ public void delete(final String bpn, final String policyId) {
final var policies = readAll(bpn);
final var modifiedPolicies = policies.stream().filter(p -> !p.getPolicyId().equals(policyId)).toList();
if (policies.size() == modifiedPolicies.size()) {
throw new PolicyStoreException("Policy with id '" + policyId + "' doesn't exists!");
throw new ResourceDoesNotExistException("Policy with id '%s' doesn't exists!".formatted(policyId));
}
save(bpn, modifiedPolicies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.eclipse.tractusx.irs.edc.client.transformer.EdcTransformer;
import org.eclipse.tractusx.irs.policystore.config.DefaultAcceptedPoliciesConfig;
import org.eclipse.tractusx.irs.policystore.exceptions.PolicyStoreException;
import org.eclipse.tractusx.irs.policystore.exceptions.ResourceDoesNotExistException;
import org.eclipse.tractusx.irs.policystore.models.CreatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.models.UpdatePolicyRequest;
import org.eclipse.tractusx.irs.policystore.persistence.PolicyPersistence;
Expand All @@ -62,7 +63,8 @@
@Service
@Slf4j
@SuppressWarnings({ "PMD.ExcessiveImports",
"PMD.TooManyMethods"
"PMD.TooManyMethods",
"PMD.GodClass"
})
public class PolicyStoreService implements AcceptedPoliciesProvider {

Expand Down Expand Up @@ -205,6 +207,9 @@ public void deletePolicy(final String policyId) {
try {
log.info("Deleting policy with id {}", policyId);
bpnsContainingPolicyId.forEach(bpn -> persistence.delete(bpn, policyId));
} catch (final ResourceDoesNotExistException e) {
handleTrialToModifyConfiguredDefaultPolicy(policyId, e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e);
} catch (final PolicyStoreException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
Expand All @@ -216,11 +221,28 @@ public void deletePolicyForEachBpn(final String policyId, final List<String> bpn
for (final String bpn : bpnList) {
persistence.delete(bpn, policyId);
}
} catch (final ResourceDoesNotExistException e) {
handleTrialToModifyConfiguredDefaultPolicy(policyId, e);
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e);
} catch (final PolicyStoreException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
}

private void handleTrialToModifyConfiguredDefaultPolicy(final String policyId, final Exception cause) {
if (isConfiguredDefaultPolicy(policyId)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
("The policy '%s' is a configured default policy which cannot be modified. "
+ "However you can define custom default policies via the API by registering a policy "
+ "without a business partner number. "
+ "These will take precedence over configured default policies.").formatted(policyId), cause);
}
}

private boolean isConfiguredDefaultPolicy(final String policyId) {
return this.allowedPoliciesFromConfig.stream().anyMatch(p -> p.getPolicyId().equals(policyId));
}

public void updatePolicies(final UpdatePolicyRequest request) {
for (final String policyId : request.policyIds()) {
updatePolicy(policyId, request.validUntil(),
Expand Down

0 comments on commit 66f74c4

Please sign in to comment.