forked from eclipse-edc/Connector
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dsp transfer process validation (eclipse-edc#3378)
* extract component to handle dsp requests logic * Implement validators * update dependencies * Avoid duplication
- Loading branch information
1 parent
361750b
commit 45de516
Showing
35 changed files
with
1,638 additions
and
1,752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
...in/java/org/eclipse/edc/protocol/dsp/api/configuration/message/DspRequestHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* 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 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.protocol.dsp.api.configuration.message; | ||
|
||
import jakarta.json.JsonObject; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import org.eclipse.edc.protocol.dsp.spi.message.DspRequestHandler; | ||
import org.eclipse.edc.protocol.dsp.spi.message.GetDspRequest; | ||
import org.eclipse.edc.protocol.dsp.spi.message.PostDspRequest; | ||
import org.eclipse.edc.spi.iam.IdentityService; | ||
import org.eclipse.edc.spi.iam.TokenRepresentation; | ||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.spi.result.Result; | ||
import org.eclipse.edc.spi.types.domain.message.ProcessRemoteMessage; | ||
import org.eclipse.edc.spi.types.domain.message.RemoteMessage; | ||
import org.eclipse.edc.transform.spi.TypeTransformerRegistry; | ||
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; | ||
|
||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import static org.eclipse.edc.protocol.dsp.api.configuration.error.DspErrorResponse.type; | ||
import static org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol.DATASPACE_PROTOCOL_HTTP; | ||
|
||
public class DspRequestHandlerImpl implements DspRequestHandler { | ||
|
||
private final Monitor monitor; | ||
private final String callbackAddress; | ||
private final IdentityService identityService; | ||
private final JsonObjectValidatorRegistry validatorRegistry; | ||
private final TypeTransformerRegistry transformerRegistry; | ||
|
||
public DspRequestHandlerImpl(Monitor monitor, String callbackAddress, IdentityService identityService, | ||
JsonObjectValidatorRegistry validatorRegistry, TypeTransformerRegistry transformerRegistry) { | ||
this.monitor = monitor; | ||
this.callbackAddress = callbackAddress; | ||
this.identityService = identityService; | ||
this.validatorRegistry = validatorRegistry; | ||
this.transformerRegistry = transformerRegistry; | ||
} | ||
|
||
@Override | ||
public <R> Response getResource(GetDspRequest<R> request) { | ||
monitor.debug(() -> "DSP: Incoming resource request for %s id %s".formatted(request.getResultClass(), request.getId())); | ||
|
||
var tokenRepresentation = TokenRepresentation.Builder.newInstance().token(request.getToken()).build(); | ||
var claimTokenResult = identityService.verifyJwtToken(tokenRepresentation, callbackAddress); | ||
|
||
if (claimTokenResult.failed()) { | ||
monitor.debug(() -> "DSP: Unauthorized: %s".formatted(claimTokenResult.getFailureDetail())); | ||
return type(request.getErrorType()).unauthorized(); | ||
} | ||
|
||
var serviceResult = request.getServiceCall().apply(request.getId(), claimTokenResult.getContent()); | ||
if (serviceResult.failed()) { | ||
monitor.debug(() -> "DSP: Service call failed: %s".formatted(serviceResult.getFailureDetail())); | ||
return type(request.getErrorType()).processId(request.getId()).from(serviceResult.getFailure()); | ||
} | ||
|
||
var resource = serviceResult.getContent(); | ||
|
||
var transformation = transformerRegistry.transform(resource, JsonObject.class); | ||
if (transformation.failed()) { | ||
var errorCode = UUID.randomUUID(); | ||
monitor.warning("Error transforming %s, error id %s: %s".formatted(request.getResultClass().getSimpleName(), errorCode, transformation.getFailureDetail())); | ||
return type(request.getErrorType()).processId(request.getId()).message(String.format("Error code %s", errorCode)).internalServerError(); | ||
} | ||
|
||
return Response.ok().type(MediaType.APPLICATION_JSON).entity(transformation.getContent()).build(); | ||
} | ||
|
||
@Override | ||
public <I extends RemoteMessage, R> Response createResource(PostDspRequest<I, R> request) { | ||
monitor.debug(() -> "DSP: Incoming %s for %s process%s".formatted( | ||
request.getInputClass().getSimpleName(), | ||
request.getResultClass(), | ||
request.getProcessId() != null ? ": " + request.getProcessId() : "")); | ||
|
||
var tokenRepresentation = TokenRepresentation.Builder.newInstance().token(request.getToken()).build(); | ||
var claimTokenResult = identityService.verifyJwtToken(tokenRepresentation, callbackAddress); | ||
|
||
if (claimTokenResult.failed()) { | ||
monitor.debug(() -> "DSP: Unauthorized: %s".formatted(claimTokenResult.getFailureDetail())); | ||
return type(request.getErrorType()).unauthorized(); | ||
} | ||
|
||
var validation = validatorRegistry.validate(request.getExpectedMessageType(), request.getMessage()); | ||
if (validation.failed()) { | ||
monitor.debug(() -> "DSP: Validation failed: %s".formatted(validation.getFailureMessages())); | ||
return type(request.getErrorType()).badRequest(); | ||
} | ||
|
||
var inputTransformation = transformerRegistry.transform(request.getMessage(), request.getInputClass()) | ||
.compose(message -> { | ||
if (message instanceof ProcessRemoteMessage processRemoteMessage) { | ||
processRemoteMessage.setProtocol(DATASPACE_PROTOCOL_HTTP); | ||
} | ||
return Result.success(message); | ||
}); | ||
|
||
if (inputTransformation.failed()) { | ||
monitor.debug(() -> "DSP: Transformation failed: %s".formatted(inputTransformation.getFailureMessages())); | ||
return type(request.getErrorType()).badRequest(); | ||
} | ||
|
||
var serviceResult = request.getServiceCall().apply(inputTransformation.getContent(), claimTokenResult.getContent()); | ||
if (serviceResult.failed()) { | ||
monitor.debug(() -> "DSP: Service call failed: %s".formatted(serviceResult.getFailureDetail())); | ||
return type(request.getErrorType()).from(serviceResult.getFailure()); | ||
} | ||
|
||
var resource = serviceResult.getContent(); | ||
|
||
var outputTransformation = transformerRegistry.transform(resource, JsonObject.class); | ||
if (outputTransformation.failed()) { | ||
var errorCode = UUID.randomUUID(); | ||
monitor.warning("Error transforming %s, error id %s: %s".formatted(request.getResultClass().getSimpleName(), errorCode, outputTransformation.getFailureDetail())); | ||
return type(request.getErrorType()).message("Error code %s".formatted(errorCode)).internalServerError(); | ||
} | ||
|
||
return Response.ok().type(MediaType.APPLICATION_JSON).entity(outputTransformation.getContent()).build(); | ||
} | ||
|
||
@Override | ||
public <I extends RemoteMessage, R> Response updateResource(PostDspRequest<I, R> request) { | ||
monitor.debug(() -> "DSP: Incoming %s for %s process%s".formatted( | ||
request.getInputClass().getSimpleName(), | ||
request.getResultClass(), | ||
request.getProcessId() != null ? ": " + request.getProcessId() : "")); | ||
|
||
var tokenRepresentation = TokenRepresentation.Builder.newInstance().token(request.getToken()).build(); | ||
var claimTokenResult = identityService.verifyJwtToken(tokenRepresentation, callbackAddress); | ||
|
||
if (claimTokenResult.failed()) { | ||
monitor.debug(() -> "DSP: Unauthorized: %s".formatted(claimTokenResult.getFailureDetail())); | ||
return type(request.getErrorType()).processId(request.getProcessId()).unauthorized(); | ||
} | ||
|
||
var validation = validatorRegistry.validate(request.getExpectedMessageType(), request.getMessage()); | ||
if (validation.failed()) { | ||
monitor.debug(() -> "DSP: Validation failed: %s".formatted(validation.getFailureMessages())); | ||
return type(request.getErrorType()).processId(request.getProcessId()).badRequest(); | ||
} | ||
|
||
var inputTransformation = transformerRegistry.transform(request.getMessage(), request.getInputClass()) | ||
.compose(message -> { | ||
if (message instanceof ProcessRemoteMessage processRemoteMessage) { | ||
processRemoteMessage.setProtocol(DATASPACE_PROTOCOL_HTTP); | ||
|
||
return Objects.equals(request.getProcessId(), processRemoteMessage.getProcessId()) | ||
? Result.success(message) | ||
: Result.failure("DSP: Invalid process ID. Expected: %s, actual: %s" | ||
.formatted(request.getProcessId(), processRemoteMessage.getProcessId())); | ||
} else { | ||
return Result.success(message); | ||
} | ||
}); | ||
|
||
if (inputTransformation.failed()) { | ||
monitor.debug(() -> "DSP: Transformation failed: %s".formatted(validation.getFailureMessages())); | ||
return type(request.getErrorType()).processId(request.getProcessId()).badRequest(); | ||
} | ||
|
||
return request.getServiceCall() | ||
.apply(inputTransformation.getContent(), claimTokenResult.getContent()) | ||
.map(it -> Response.ok().type(MediaType.APPLICATION_JSON_TYPE).build()) | ||
.orElse(failure -> { | ||
monitor.debug(() -> "DSP: Service call failed: %s".formatted(failure.getFailureDetail())); | ||
return type(request.getErrorType()).processId(request.getProcessId()).from(failure); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.