Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(irs-apiirs-edc-client):[#256] added cache mechanism for edr tokens #679

Merged
merged 17 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
bf1f6b7
feat(irs-apiirs-edc-client):[#256] added cache mechanism for edr tokens
ds-psosnowski Dec 1, 2023
645ee4f
feat(irs-apiirs-edc-client):[#256] fix for potential null pointer exc…
ds-psosnowski Dec 4, 2023
cb2c223
feat(irs-apiirs-edc-client):[#256] removed unnecessary stubbing
ds-psosnowski Dec 4, 2023
1e2fb19
feat(irs-apiirs-edc-client):[#256] sonar finding fix
ds-psosnowski Dec 4, 2023
e8158a6
feat(irs-apiirs-edc-client):[#256] fix findbug findings
ds-psosnowski Dec 4, 2023
54f62bc
feat(irs-apiirs-edc-client):[#256] fix findbug findings
ds-psosnowski Dec 4, 2023
2495ac1
feat(irs-apiirs-edc-client):[#256] address code smells
ds-psosnowski Dec 4, 2023
9654457
feat(irs-apiirs-edc-client):[#256] removed dependency
ds-psosnowski Dec 4, 2023
6b8e0db
feat(irs-api):[#256] spotbug fix
ds-psosnowski Dec 6, 2023
d9d43af
feat(irs-api):[#256] spotbug fix
ds-psosnowski Dec 6, 2023
2f3994f
feat(irs-registry-client):[#256] added cache for send notification
ds-psosnowski Dec 8, 2023
1e321b5
feat(irs-registry-client):[#256] removed unused method
ds-psosnowski Dec 8, 2023
bf3a437
Merge branch 'main' into feat/256-edr-token-caching
ds-psosnowski Dec 8, 2023
667f575
feat(irs-registry-client):[#256] added changelog record
ds-psosnowski Dec 11, 2023
a249df9
feat(irs-registry-client):[#202] merge main
ds-psosnowski Dec 14, 2023
20eff99
feat(irs-registry-client):[#256] corrections after review
ds-psosnowski Dec 20, 2023
e5de031
feat(irs-registry-client):[#256] added test
ds-psosnowski Dec 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions irs-edc-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,17 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>${findbug.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@
import org.eclipse.tractusx.irs.edc.client.exceptions.UsagePolicyException;
import org.eclipse.tractusx.irs.edc.client.model.CatalogItem;
import org.eclipse.tractusx.irs.edc.client.model.ContractOfferDescription;
import org.eclipse.tractusx.irs.edc.client.model.EDRAuthCode;
import org.eclipse.tractusx.irs.edc.client.model.NegotiationRequest;
import org.eclipse.tractusx.irs.edc.client.model.NegotiationResponse;
import org.eclipse.tractusx.irs.edc.client.model.Response;
import org.eclipse.tractusx.irs.edc.client.model.TransferProcessDataDestination;
import org.eclipse.tractusx.irs.edc.client.model.TransferProcessRequest;
import org.eclipse.tractusx.irs.edc.client.model.TransferProcessResponse;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyCheckerService;
import org.eclipse.tractusx.irs.edc.client.util.EndpointDataReferenceStatus;
import org.eclipse.tractusx.irs.edc.client.util.Masker;
import org.springframework.stereotype.Service;

/**
Expand All @@ -56,45 +59,89 @@ public class ContractNegotiationService {

public static final String EDC_PROTOCOL = "dataspace-protocol-http";
private final EdcControlPlaneClient edcControlPlaneClient;

private final PolicyCheckerService policyCheckerService;

private final EdcConfiguration config;

public NegotiationResponse negotiate(final String providerConnectorUrl, final CatalogItem catalogItem)
throws ContractNegotiationException, UsagePolicyException, TransferProcessException {
if (!policyCheckerService.isValid(catalogItem.getPolicy())) {
log.info("Policy was not allowed, canceling negotiation.");
throw new UsagePolicyException(catalogItem.getItemId());
}

final NegotiationRequest negotiationRequest = createNegotiationRequestFromCatalogItem(providerConnectorUrl,
catalogItem);
return this.negotiate(providerConnectorUrl, catalogItem,
new EndpointDataReferenceStatus(null, EndpointDataReferenceStatus.TokenStatus.REQUIRED_NEW));
}

final Response negotiationId = edcControlPlaneClient.startNegotiations(negotiationRequest);
@SuppressWarnings("PMD.AvoidReassigningParameters")
ds-psosnowski marked this conversation as resolved.
Show resolved Hide resolved
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE",
justification = "Not reachable code")
public NegotiationResponse negotiate(final String providerConnectorUrl, final CatalogItem catalogItem,
EndpointDataReferenceStatus endpointDataReferenceStatus)
throws ContractNegotiationException, UsagePolicyException, TransferProcessException {

log.info("Fetch negotiation id: {}", negotiationId.getResponseId());
if (endpointDataReferenceStatus == null) {
log.info(
"Missing information about endpoint data reference from storage, setting token status to REQUIRED_NEW.");
endpointDataReferenceStatus = new EndpointDataReferenceStatus(null,
EndpointDataReferenceStatus.TokenStatus.REQUIRED_NEW);
}

final CompletableFuture<NegotiationResponse> responseFuture = edcControlPlaneClient.getNegotiationResult(
negotiationId);
final NegotiationResponse negotiationResponse = Objects.requireNonNull(getNegotiationResponse(responseFuture));
NegotiationResponse negotiationResponse = null;
ds-psosnowski marked this conversation as resolved.
Show resolved Hide resolved
String contractAgreementId = null;
ds-psosnowski marked this conversation as resolved.
Show resolved Hide resolved

switch (endpointDataReferenceStatus.tokenStatus()) {
case REQUIRED_NEW -> {
final CompletableFuture<NegotiationResponse> responseFuture = startNewNegotiation(providerConnectorUrl,
catalogItem);
negotiationResponse = Objects.requireNonNull(getNegotiationResponse(responseFuture));
contractAgreementId = negotiationResponse.getContractAgreementId();
}
case EXPIRED -> {
if (endpointDataReferenceStatus.endpointDataReference().getAuthKey() == null) {
throw new IllegalStateException("Missing information about AuthKey.");
}
contractAgreementId = EDRAuthCode.fromAuthCodeToken(
endpointDataReferenceStatus.endpointDataReference().getAuthKey()).getCid();
log.info(
"Cached endpoint data reference has expired token. Refreshing token without new contract negotiation for contractAgreementId: {}",
Masker.mask(contractAgreementId));
github-advanced-security[bot] marked this conversation as resolved.
Show resolved Hide resolved
}
case VALID -> throw new IllegalStateException(
"Token is present and valid. Contract negotiation should not be started.");
default -> throw new IllegalStateException(
"Unknown token status.");
}

final TransferProcessRequest transferProcessRequest = createTransferProcessRequest(providerConnectorUrl,
catalogItem, negotiationResponse);
catalogItem, contractAgreementId);

final Response transferProcessId = edcControlPlaneClient.startTransferProcess(transferProcessRequest);

// can be added to cache after completed
final CompletableFuture<TransferProcessResponse> transferProcessFuture = edcControlPlaneClient.getTransferProcess(
transferProcessId);
final TransferProcessResponse transferProcessResponse = Objects.requireNonNull(
getTransferProcessResponse(transferProcessFuture));
log.info("Transfer process completed for transferProcessId: {}", transferProcessResponse.getResponseId());

return negotiationResponse;
}

private CompletableFuture<NegotiationResponse> startNewNegotiation(final String providerConnectorUrl,
final CatalogItem catalogItem) throws UsagePolicyException {
log.info("Staring new contract negotiation.");

if (!policyCheckerService.isValid(catalogItem.getPolicy())) {
log.info("Policy was not allowed, canceling negotiation.");
throw new UsagePolicyException(catalogItem.getItemId());
}

final NegotiationRequest negotiationRequest = createNegotiationRequestFromCatalogItem(providerConnectorUrl,
catalogItem);
final Response negotiationId = edcControlPlaneClient.startNegotiations(negotiationRequest);
log.info("Fetch negotiation id: {}", negotiationId.getResponseId());

return edcControlPlaneClient.getNegotiationResult(negotiationId);
}

private TransferProcessRequest createTransferProcessRequest(final String providerConnectorUrl,
final CatalogItem catalogItem, final NegotiationResponse response) {
final CatalogItem catalogItem, final String agreementId) {
final var destination = DataAddress.Builder.newInstance()
.type(TransferProcessDataDestination.DEFAULT_TYPE)
.build();
Expand All @@ -105,7 +152,7 @@ private TransferProcessRequest createTransferProcessRequest(final String provide
TransferProcessRequest.DEFAULT_MANAGED_RESOURCES)
.connectorId(catalogItem.getConnectorId())
.connectorAddress(providerConnectorUrl)
.contractId(response.getContractAgreementId())
.contractId(agreementId)
.assetId(catalogItem.getAssetPropId())
.dataDestination(destination);
if (StringUtils.isNotBlank(config.getCallbackUrl())) {
Expand Down
Loading