Skip to content

Commit 0f0f1f6

Browse files
add simple token auth
1 parent 39fa86e commit 0f0f1f6

7 files changed

Lines changed: 60 additions & 60 deletions

File tree

Requests/ControlPlane Management/Download Data from Public API.bru

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
meta {
22
name: Download Data from Public API
33
type: http
4-
seq: 8
4+
seq: 9
55
}
66

77
get {
8-
url: {{CONSUMER_DP}}/api/proxy/flows/{{flowId}}
8+
url: {{CONSUMER_DP}}/api/proxy/flows/{{flowId}}/data
99
body: none
1010
auth: none
1111
}
1212

1313
headers {
14-
Authorization: {{AUTHORIZATION}}
14+
~Authorization: {{accessToken}}
1515
}
1616

1717
script:post-response {

Requests/ControlPlane Management/Get open dataflow by ID.bru

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
meta {
22
name: Get open dataflow by ID
33
type: http
4-
seq: 9
4+
seq: 8
55
}
66

77
get {
8-
url: {{CONSUMER_DP}}/api/proxy/flows
8+
url: {{CONSUMER_DP}}/api/proxy/flows/{{flowId}}
99
body: json
1010
auth: none
1111
}
@@ -21,12 +21,12 @@ body:text {
2121

2222
script:post-response {
2323
const body = res.getBody()
24-
const map = new Map(Object.entries(body));
25-
// just use the first entry in the map
26-
const [flowId, address] = map.entries().next().value;
2724

28-
expect(flowId).not.to.be.undefined
29-
bru.setVar("flowId",flowId)
25+
const token = body.endpointProperties.find(obj => obj.name === "access_token");
26+
expect(token).not.to.be.undefined
27+
console.log(token.value)
28+
bru.setVar("accessToken", token.value)
29+
3030

3131

3232
}

Requests/ControlPlane Management/Get open dataflows.bru

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ script:post-response {
2626
const [flowId, address] = map.entries().next().value;
2727

2828
expect(flowId).not.to.be.undefined
29-
bru.setVar("flowId",flowId)
29+
bru.setVar("flowId", flowId)
30+
console.log(flowId)
3031

3132

33+
const token = address.endpointProperties.find(obj => obj.name === "access_token");
34+
expect(token).not.to.be.undefined
35+
console.log(token.value)
36+
bru.setVar("accessToken", token.value)
37+
3238
}
3339

3440
settings {

launchers/dataplane/src/main/java/org/eclipse/edc/mvd/dataplane/SignalingDataPlaneRuntimeExtension.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.jetbrains.annotations.NotNull;
4141

4242
import java.net.URI;
43+
import java.util.List;
4344

4445
import static java.util.Collections.emptyList;
4546

@@ -115,7 +116,7 @@ public void initialize(ServiceExtensionContext context) {
115116

116117
webService.registerResource(ApiContext.SIGNALING, dataplane.controller());
117118
webService.registerResource(ApiContext.SIGNALING, dataplane.registrationController());
118-
webService.registerResource(APICONTEXT_PUBLIC, new DataPlanePublicApiController(httpClient, monitor));
119+
webService.registerResource(APICONTEXT_PUBLIC, new DataPlanePublicApiController(httpClient, monitor, "foobar"));
119120
webService.registerResource(APICONTEXT_PROXY, new ConsumerProxyController(dataFetcher));
120121
}
121122

@@ -135,7 +136,9 @@ private Result<DataFlow> prepare(DataFlow dataFlow) {
135136
private @NotNull Result<DataFlow> startDataFlow(DataFlow dataFlow) {
136137
switch (dataFlow.getTransferType()) {
137138
case "NonFinite-PULL", "Finite-PULL", "HttpData-PULL" -> {
138-
var dataAddress = new DataAddress(dataFlow.getTransferType(), "http", publicApiConfig.dataSourceEndpoint(hostname.get()), emptyList());
139+
var dataAddress = new DataAddress(dataFlow.getTransferType(), "http",
140+
publicApiConfig.dataSourceEndpoint(hostname.get()),
141+
List.of(new DataAddress.EndpointProperty("authorization", "access_token", "foobar")));
139142
dataFlow.setDataAddress(dataAddress);
140143
return Result.success(dataFlow);
141144
}

launchers/dataplane/src/main/java/org/eclipse/edc/mvd/dataplane/data/DataPlanePublicApiController.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import jakarta.ws.rs.GET;
1818
import jakarta.ws.rs.Path;
1919
import jakarta.ws.rs.PathParam;
20+
import jakarta.ws.rs.core.Context;
21+
import jakarta.ws.rs.core.HttpHeaders;
22+
import jakarta.ws.rs.core.Response;
2023
import okhttp3.Request;
2124
import org.eclipse.edc.http.spi.EdcHttpClient;
2225
import org.eclipse.edc.spi.monitor.Monitor;
@@ -30,32 +33,48 @@ public class DataPlanePublicApiController {
3033

3134
private final EdcHttpClient client;
3235
private final Monitor monitor;
36+
private final String expectedAuthHeader;
3337

34-
public DataPlanePublicApiController(EdcHttpClient client, Monitor monitor) {
38+
public DataPlanePublicApiController(EdcHttpClient client, Monitor monitor, String expectedAuthHeader) {
3539
this.client = client;
3640
this.monitor = monitor;
41+
this.expectedAuthHeader = expectedAuthHeader;
3742
}
3843

3944
@GET
4045
@Path("/source")
41-
public String dataSource() {
42-
return downloadJsonFromUrl("https://jsonplaceholder.typicode.com/todos");
46+
public Response dataSource(@Context HttpHeaders headers) {
47+
if (!isAuthorized(headers)) {
48+
return Response.status(Response.Status.UNAUTHORIZED).build();
49+
}
50+
return Response.ok(downloadJsonFromUrl("https://jsonplaceholder.typicode.com/todos")).build();
4351
}
4452

4553

4654

4755
@GET
4856
@Path("/source/{resource}")
49-
public String dataSource(@PathParam("resource") String resource) {
57+
public Response dataSource(@PathParam("resource") String resource, @Context HttpHeaders headers) {
58+
if (!isAuthorized(headers)) {
59+
return Response.status(Response.Status.UNAUTHORIZED).build();
60+
}
5061
var formatted = "https://jsonplaceholder.typicode.com/%s".formatted(resource);
51-
return downloadJsonFromUrl(formatted);
62+
return Response.ok(downloadJsonFromUrl(formatted)).build();
5263
}
5364

5465
@GET
5566
@Path("/source/{resource}/{id}")
56-
public String dataSource(@PathParam("resource") String resource, @PathParam("id") String id) {
67+
public Response dataSource(@PathParam("resource") String resource, @PathParam("id") String id, @Context HttpHeaders headers) {
68+
if (!isAuthorized(headers)) {
69+
return Response.status(Response.Status.UNAUTHORIZED).build();
70+
}
5771
var formatted = "https://jsonplaceholder.typicode.com/%s/%s".formatted(resource, id);
58-
return downloadJsonFromUrl(formatted);
72+
return Response.ok(downloadJsonFromUrl(formatted)).build();
73+
}
74+
75+
private boolean isAuthorized(HttpHeaders headers) {
76+
var authHeader = headers.getHeaderString(HttpHeaders.AUTHORIZATION);
77+
return expectedAuthHeader.equals(authHeader);
5978
}
6079

6180
private @NotNull String downloadJsonFromUrl(String formatted) {

launchers/dataplane/src/main/java/org/eclipse/edc/mvd/dataplane/signaling/ConsumerDataHandler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ public Result<String> downloadData(String flowId) {
5858
return Result.failure(new IllegalArgumentException("No data flow found for id %s".formatted(flowId)));
5959
}
6060
var sourceUri = URI.create(dataAddress.endpoint());
61-
var request = new Request.Builder().url(sourceUri.toString()).get().build();
61+
var token = dataAddress.endpointProperties().stream().filter(ep -> ep.name().equals("access_token"))
62+
.findFirst()
63+
.map(DataAddress.EndpointProperty::value)
64+
.orElseThrow(() -> new IllegalArgumentException("No access token found in data address properties"));
65+
var request = new Request.Builder()
66+
.addHeader("Authorization", token)
67+
.url(sourceUri.toString())
68+
.get()
69+
.build();
6270
try (var response = httpClient.execute(request)) {
6371
if (response.isSuccessful()) {
6472
var body = response.body().string();

tests/end2end/src/test/java/org/eclipse/edc/demo/tests/transfer/TransferEndToEndTest.java

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,6 @@ private static RequestSpecification baseRequest() {
6666
@DisplayName("Tests a successful End-to-End contract negotiation and data transfer")
6767
@Test
6868
void transferData_hasPermission_shouldTransferData() {
69-
// System.out.println("Waiting for Provider dataplane to come online");
70-
// // wait until provider's dataplane is available
71-
// await().atMost(TEST_TIMEOUT_DURATION)
72-
// .pollDelay(TEST_POLL_DELAY)
73-
// .untilAsserted(() -> {
74-
// var jp = baseRequest()
75-
// .get(PROVIDER_MANAGEMENT_URL + "/api/mgmt/v4/dataplanes")
76-
// .then()
77-
// .statusCode(200)
78-
// .log().ifValidationFails()
79-
// .extract().body().jsonPath();
80-
//
81-
// var state = jp.getString("state");
82-
// assertThat(state).isEqualTo("[REGISTERED]");
83-
// });
84-
//
85-
// System.out.println("Provider dataplane is online, fetching catalog");
86-
8769
var catalogRequestBody = Json.createObjectBuilder()
8870
.add("@context", Json.createObjectBuilder().add("edc", "https://w3id.org/edc/connector/management/v2"))
8971
.add("@type", "CatalogRequest")
@@ -191,15 +173,15 @@ void transferData_hasPermission_shouldTransferData() {
191173
.extract().body().jsonPath();
192174

193175
endpoint.set(jp.getString("endpoint"));
194-
// token.set(jp.getString("authorization"));
176+
token.set(jp.get("endpointProperties.find { it.name == 'access_token' }.value"));
195177

196178
assertThat(endpoint.get()).isNotNull().endsWith("/api/public/data/source");
197-
// assertThat(token.get()).isNotNull();
179+
assertThat(token.get()).isNotNull();
198180
});
199181

200182
//download exemplary JSON data from public endpoint
201183
var response = given()
202-
// .header("Authorization", token.get())
184+
.header("Authorization", token.get())
203185
.get(CONSUMER_PROXY_URL + "/flows/%s/data".formatted(transferProcessId))
204186
.then()
205187
.log().ifError()
@@ -212,24 +194,6 @@ void transferData_hasPermission_shouldTransferData() {
212194
@DisplayName("Tests a failing End-to-End contract negotiation because of an unfulfilled policy")
213195
@Test
214196
void transferData_doesNotHavePermission_shouldTerminate() {
215-
System.out.println("Waiting for Provider dataplane to come online");
216-
// wait until provider's dataplane is available
217-
await().atMost(TEST_TIMEOUT_DURATION)
218-
.pollDelay(TEST_POLL_DELAY)
219-
.untilAsserted(() -> {
220-
var jp = baseRequest()
221-
.get(PROVIDER_MANAGEMENT_URL + "/api/mgmt/v4/dataplanes")
222-
.then()
223-
.statusCode(200)
224-
.log().ifValidationFails()
225-
.extract().body().jsonPath();
226-
227-
var state = jp.getString("state");
228-
assertThat(state).isEqualTo("[REGISTERED]");
229-
});
230-
231-
System.out.println("Provider dataplane is online, fetching catalog");
232-
233197
var catalogRequestBody = Json.createObjectBuilder()
234198
.add("@context", Json.createObjectBuilder().add("edc", "https://w3id.org/edc/connector/management/v2"))
235199
.add("@type", "CatalogRequest")

0 commit comments

Comments
 (0)