|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Cofinity-X |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Apache License, Version 2.0 which is available at |
| 6 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Cofinity-X - initial API and implementation |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +package org.eclipse.edc.test.e2e; |
| 16 | + |
| 17 | +import jakarta.ws.rs.Consumes; |
| 18 | +import jakarta.ws.rs.GET; |
| 19 | +import jakarta.ws.rs.Path; |
| 20 | +import jakarta.ws.rs.Produces; |
| 21 | +import jakarta.ws.rs.container.ContainerRequestContext; |
| 22 | +import jakarta.ws.rs.core.Context; |
| 23 | +import jakarta.ws.rs.core.HttpHeaders; |
| 24 | +import jakarta.ws.rs.core.Response; |
| 25 | +import org.eclipse.edc.connector.dataplane.spi.Endpoint; |
| 26 | +import org.eclipse.edc.connector.dataplane.spi.iam.DataPlaneAuthorizationService; |
| 27 | +import org.eclipse.edc.connector.dataplane.spi.iam.PublicEndpointGeneratorService; |
| 28 | +import org.eclipse.edc.runtime.metamodel.annotation.Inject; |
| 29 | +import org.eclipse.edc.spi.system.ServiceExtension; |
| 30 | +import org.eclipse.edc.spi.system.ServiceExtensionContext; |
| 31 | +import org.eclipse.edc.web.spi.WebService; |
| 32 | +import org.eclipse.edc.web.spi.configuration.PortMapping; |
| 33 | +import org.eclipse.edc.web.spi.configuration.PortMappingRegistry; |
| 34 | + |
| 35 | +import static jakarta.ws.rs.core.MediaType.WILDCARD; |
| 36 | +import static jakarta.ws.rs.core.Response.Status.FORBIDDEN; |
| 37 | +import static jakarta.ws.rs.core.Response.Status.UNAUTHORIZED; |
| 38 | +import static java.util.Collections.emptyMap; |
| 39 | +import static org.eclipse.edc.util.io.Ports.getFreePort; |
| 40 | + |
| 41 | +/** |
| 42 | + * Extension that provides a dummy proxy that always return a hardcoded successful response when the token validation |
| 43 | + * succeeds. |
| 44 | + */ |
| 45 | +public class HttpProxyDataPlaneExtension implements ServiceExtension { |
| 46 | + |
| 47 | + private static final String API_CONTEXT = "proxy"; |
| 48 | + |
| 49 | + @Inject |
| 50 | + private DataPlaneAuthorizationService authorizationService; |
| 51 | + @Inject |
| 52 | + private PublicEndpointGeneratorService generatorService; |
| 53 | + @Inject |
| 54 | + private PortMappingRegistry portMappingRegistry; |
| 55 | + @Inject |
| 56 | + private WebService webService; |
| 57 | + |
| 58 | + @Override |
| 59 | + public void initialize(ServiceExtensionContext context) { |
| 60 | + var portMapping = new PortMapping(API_CONTEXT, getFreePort(), "/proxy"); |
| 61 | + portMappingRegistry.register(portMapping); |
| 62 | + |
| 63 | + var proxyUrl = "http://localhost:%d%s".formatted(portMapping.port(), portMapping.path()); |
| 64 | + generatorService.addGeneratorFunction("HttpData", address -> Endpoint.url(proxyUrl)); |
| 65 | + |
| 66 | + webService.registerResource(API_CONTEXT, new Controller(authorizationService)); |
| 67 | + } |
| 68 | + |
| 69 | + @Path("{any:.*}") |
| 70 | + @Consumes(WILDCARD) |
| 71 | + @Produces(WILDCARD) |
| 72 | + public static class Controller { |
| 73 | + |
| 74 | + private final DataPlaneAuthorizationService authorizationService; |
| 75 | + |
| 76 | + Controller(DataPlaneAuthorizationService authorizationService) { |
| 77 | + this.authorizationService = authorizationService; |
| 78 | + } |
| 79 | + |
| 80 | + @GET |
| 81 | + public Response get(@Context ContainerRequestContext requestContext) { |
| 82 | + var token = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); |
| 83 | + if (token == null) { |
| 84 | + return Response.status(UNAUTHORIZED).build(); |
| 85 | + } |
| 86 | + |
| 87 | + var sourceDataAddress = authorizationService.authorize(token, emptyMap()); |
| 88 | + if (sourceDataAddress.failed()) { |
| 89 | + return Response.status(FORBIDDEN).build(); |
| 90 | + } |
| 91 | + |
| 92 | + return Response.ok("data").build(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments