Skip to content

Commit

Permalink
Merge pull request #501 from catenax-ng/feature/596-tracex-extend-edc…
Browse files Browse the repository at this point in the history
…-policy-queries

Feature/596 tracex extend edc policy queries
  • Loading branch information
ds-jhartmann authored Mar 22, 2024
2 parents 87a6600 + 9a87106 commit f731e2e
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
_**For better traceability add the corresponding GitHub issue number in each changelog entry, please.**_

## [Unreleased]
### Added
- Extended EdcPolicyDefinitionService to check if a policy in the edc exists

## [4.8.0] - 2024-03-18
### Changed
Expand Down
2 changes: 2 additions & 0 deletions charts/irs-helm/templates/configmap-spring-app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ data:
bpnEndpoint: {{ tpl (.Values.bpdm.bpnEndpoint | default "") . | quote }}
irs-edc-client:
callback:
mapping: {{ .Values.edc.callbackMapping | default "/internal/endpoint-data-reference" | quote }}
callback-url: {{ tpl (.Values.edc.callbackurl | default (printf "http://%s%s" .Release.Name "-irs-helm:8181/internal/endpoint-data-reference")) . | quote }}
asyncTimeout: {{ tpl .Values.edc.asyncTimeout . | default "PT10M" | quote }}
controlplane:
Expand Down
3 changes: 2 additions & 1 deletion charts/irs-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ edc:
apikey:
header: "X-Api-Key" # Name of the EDC api key header field
secret: "" # <edc-api-key>
callbackurl:
callbackMapping: # The callback endpoint path mapping - used to expose callback endpoint
callbackurl: # The URL where the EDR token callback will be sent to.
asyncTimeout: PT10M # Timout for future.get requests as ISO 8601 Duration
submodel:
request:
Expand Down
2 changes: 2 additions & 0 deletions irs-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ resilience4j:
baseConfig: default

irs-edc-client:
callback:
mapping: /internal/endpoint-data-reference # The callback endpoint mapping
callback-url: ${EDC_TRANSFER_CALLBACK_URL:} # The URL where the EDR token callback will be sent to.
asyncTimeout: PT10M # Timout for future.get requests as ISO 8601 Duration
controlplane:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
@Slf4j
@RestController("irsEdcClientEdcCallbackController")
@RequestMapping("${irs-edc-client.callback.mapping:internal/endpoint-data-reference}")
@RequestMapping("${irs-edc-client.callback.mapping}")
@Hidden
@RequiredArgsConstructor
public class EdcCallbackController {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/********************************************************************************
* 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.edc.client.policy.model.exception;

/**
* GetEdcPolicyDefinitionException used policy creation failed case
*/

public class GetEdcPolicyDefinitionException extends Exception {

public GetEdcPolicyDefinitionException(final String message) {
super(message);
}

public GetEdcPolicyDefinitionException(final Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.CreateEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.DeleteEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.EdcPolicyDefinitionAlreadyExists;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.GetEdcPolicyDefinitionException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -99,6 +100,28 @@ public String createAccessPolicy(final EdcCreatePolicyDefinitionRequest policyRe
throw new CreateEdcPolicyDefinitionException("Failed to create EDC policy definition for asset");
}

public boolean policyDefinitionExists(final String policyName) throws GetEdcPolicyDefinitionException {

try {
final ResponseEntity<String> getPolicyDefinitionRequest = restTemplate.getForEntity(
config.getControlplane().getEndpoint().getPolicyDefinition() + "/" + policyName, String.class);

final HttpStatusCode responseCode = getPolicyDefinitionRequest.getStatusCode();

if (responseCode.value() == HttpStatus.OK.value()) {
return true;
}
} catch (HttpClientErrorException e) {
if (e.getStatusCode().value() == HttpStatus.NOT_FOUND.value()) {
log.info(String.format("Policy with id %s not found within the edc", policyName));
return false;
} else {
throw new GetEdcPolicyDefinitionException(e);
}
}
return false;
}

public EdcCreatePolicyDefinitionRequest createPolicyDefinition(final String policyName,
final String accessPolicyId) {
final EdcPolicyPermissionConstraintExpression constraint = EdcPolicyPermissionConstraintExpression.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -34,6 +36,7 @@
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.CreateEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.DeleteEdcPolicyDefinitionException;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.EdcPolicyDefinitionAlreadyExists;
import org.eclipse.tractusx.irs.edc.client.policy.model.exception.GetEdcPolicyDefinitionException;
import org.json.JSONException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -127,6 +130,42 @@ void givenPolicy_WhenCreateAccessPolicy_ThenCreateIt() throws CreateEdcPolicyDef
assertThat(result).isNotBlank();
}

@Test
void givenPolicyName_WhenGetPolicy_ThenExists() throws GetEdcPolicyDefinitionException {
// given
when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig);
when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig);
String policyName = "policyName";
when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions" + "/" + policyName);

ResponseEntity<String> responseEntity = ResponseEntity.ok("Mocked response");
when(restTemplate.getForEntity(anyString(), eq(String.class))).thenReturn(responseEntity);

// when
boolean result = service.policyDefinitionExists(policyName);

// then
assertThat(result).isTrue();
}

@Test
void givenPolicyName_WhenGetPolicy_ThenNotExists() throws GetEdcPolicyDefinitionException {
// given
when(edcConfiguration.getControlplane()).thenReturn(controlplaneConfig);
when(controlplaneConfig.getEndpoint()).thenReturn(endpointConfig);
String policyName = "policyName";
when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions" + "/" + policyName);

ResponseEntity<String> responseEntity = ResponseEntity.notFound().build();
when(restTemplate.getForEntity(anyString(), eq(String.class))).thenReturn(responseEntity);

// when
boolean result = service.policyDefinitionExists(policyName);

// then
assertThat(result).isFalse();
}

@Test
void givenCreatePolicy_whenConflict_thenReturnExstingPolicyId() throws CreateEdcPolicyDefinitionException {
// given
Expand All @@ -136,15 +175,7 @@ void givenCreatePolicy_whenConflict_thenReturnExstingPolicyId() throws CreateEdc
final String policyName = "policyName";
when(restTemplate.postForEntity(any(String.class), any(EdcCreatePolicyDefinitionRequest.class),
any())).thenThrow(
HttpClientErrorException.create(
"Surprise",
HttpStatus.CONFLICT,
"",
null,
null,
null
)
);
HttpClientErrorException.create("Surprise", HttpStatus.CONFLICT, "", null, null, null));

// when/then
assertThrows(EdcPolicyDefinitionAlreadyExists.class, () -> service.createAccessPolicy(policyName));
Expand All @@ -171,14 +202,8 @@ void givenCreatePolicy_whenRestClientException_thenThrowException() {
when(endpointConfig.getPolicyDefinition()).thenReturn("/management/v2/policydefinitions");
String policyName = "policyName";
when(restTemplate.postForEntity(any(String.class), any(EdcCreatePolicyDefinitionRequest.class),
any())).thenThrow(HttpClientErrorException.create(
"Surprise",
HttpStatus.EARLY_HINTS,
"",
null,
null,
null
));
any())).thenThrow(
HttpClientErrorException.create("Surprise", HttpStatus.EARLY_HINTS, "", null, null, null));

assertThrows(CreateEdcPolicyDefinitionException.class, () -> service.createAccessPolicy(policyName));
}
Expand All @@ -193,8 +218,7 @@ void givenDeletePolicy_whenRestClientException_thenThrowException() {
doThrow(new RestClientException("Surprise")).when(restTemplate).delete(any(String.class));

// when/then
assertThrows(DeleteEdcPolicyDefinitionException.class,
() -> service.deleteAccessPolicy(policyName));
assertThrows(DeleteEdcPolicyDefinitionException.class, () -> service.deleteAccessPolicy(policyName));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.eclipse.tractusx.irs.registryclient.exceptions.RegistryServiceException;
import org.eclipse.tractusx.irs.registryclient.exceptions.ShellNotFoundException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand Down Expand Up @@ -303,6 +304,7 @@ public Stream<? extends Arguments> provideArguments(final ExtensionContext exten
}

@Test
@Disabled("See GH Issue https://github.com/eclipse-tractusx/item-relationship-service/issues/431")
void lookupShellIdentifiers_multipleEDCs_multipleDTRs() throws RegistryServiceException, EdcRetrieverException {
// Arrange
givenThat(postDiscoveryFinder200());
Expand Down

0 comments on commit f731e2e

Please sign in to comment.