Skip to content

Commit

Permalink
Merge pull request #107 from mkollenstart/main
Browse files Browse the repository at this point in the history
feat: Configurable HTTP Authorization header
  • Loading branch information
jimmarino authored Nov 14, 2024
2 parents e72efd4 + faafb6a commit 91998ef
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.17.2, Apache-2.0 AN
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.17.2, Apache-2.0, approved, #13671
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jakarta-jsonp/2.17.2, Apache-2.0, approved, #14161
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.2, Apache-2.0, approved, #14162
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, CC-BY-2.5, approved, #15220
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, Apache-2.0 and CC-BY-2.5, approved, #15220
maven/mavencentral/com.google.errorprone/error_prone_annotations/2.7.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.guava/failureaccess/1.0.1, Apache-2.0, approved, CQ22654
maven/mavencentral/com.google.guava/guava/31.0.1-jre, Apache-2.0, approved, clearlydefined
Expand Down
1 change: 1 addition & 0 deletions config/tck/sample.tck.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dataspacetck.debug=true
dataspacetck.dsp.local.connector=false
dataspacetck.dsp.connector.agent.id=CONNECTOR_UNDER_TEST
dataspacetck.dsp.connector.http.url=http://localhost:8282/api/v1/dsp/
dataspacetck.dsp.connector.http.headers.authorization="{\"region\": \"any\", \"audience\": \"any\", \"clientId\":\"any\"}"
dataspacetck.dsp.connector.negotiation.initiate.url=http://localhost:8687/tck/negotiations/requests
dataspacetck.dsp.default.wait=10000000

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

package org.eclipse.dataspacetck.dsp.system.api.http;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.eclipse.dataspacetck.dsp.system.api.metadata.DspTestingWorkaround;

import java.io.IOException;

Expand All @@ -31,6 +31,16 @@
* Utility methods for HTTP requests.
*/
public class HttpFunctions {
private static Interceptor authorizationInterceptor = chain -> chain.proceed(chain.request());

public static void registerAuthorizationInterceptor(String authorizationHeader) {
authorizationInterceptor = chain -> {
var request = chain.request();
var authenticatedRequest = request.newBuilder()
.header("Authorization", authorizationHeader).build();
return chain.proceed(authenticatedRequest);
};
}

public static Response postJson(String url, Object message) {
return postJson(url, message, false);
Expand All @@ -43,14 +53,12 @@ public static Response postJson(String url, Object message, boolean expectError)
public static Response postJson(String url, Object message, boolean expectError, boolean plain) {
var serialized = plain ? serializePlainJson(message) : serialize(message);
var requestBody = RequestBody.create(serialized, MediaType.get("application/json"));
@DspTestingWorkaround("Remove header claims: region, audience, clientId")
var httpRequest = new Request.Builder()
.url(url)
.header("Authorization", "{\"region\": \"any\", \"audience\": \"any\", \"clientId\":\"any\"}")
.post(requestBody)
.build();

var httpClient = new OkHttpClient.Builder().build();
var httpClient = new OkHttpClient.Builder().addInterceptor(authorizationInterceptor).build();
try {
var response = httpClient.newCall(httpRequest).execute();
if (404 == response.code()) {
Expand All @@ -69,11 +77,10 @@ public static Response postJson(String url, Object message, boolean expectError,
public static Response getJson(String url) {
var httpRequest = new Request.Builder()
.url(url)
.header("Authorization", "{\"region\": \"any\", \"audience\": \"any\", \"clientId\":\"any\"}")
.get()
.build();

var httpClient = new OkHttpClient.Builder().build();
var httpClient = new OkHttpClient.Builder().addInterceptor(authorizationInterceptor).build();
try {
var response = httpClient.newCall(httpRequest).execute();
if (404 == response.code()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.dataspacetck.core.spi.system.SystemLauncher;
import org.eclipse.dataspacetck.dsp.system.api.connector.Connector;
import org.eclipse.dataspacetck.dsp.system.api.connector.Consumer;
import org.eclipse.dataspacetck.dsp.system.api.http.HttpFunctions;
import org.eclipse.dataspacetck.dsp.system.api.mock.ConsumerNegotiationMock;
import org.eclipse.dataspacetck.dsp.system.api.mock.ProviderNegotiationMock;
import org.eclipse.dataspacetck.dsp.system.api.pipeline.ConsumerNegotiationPipeline;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class DspSystemLauncher implements SystemLauncher {
private static final String LOCAL_CONNECTOR_CONFIG = TCK_PREFIX + ".dsp.local.connector";
private static final String CONNECTOR_AGENT_ID_CONFIG = TCK_PREFIX + ".dsp.connector.agent.id";
private static final String CONNECTOR_BASE_URL_CONFIG = TCK_PREFIX + ".dsp.connector.http.url";
private static final String CONNECTOR_BASE_AUTHORIZATION_HEADER_CONFIG = TCK_PREFIX + ".dsp.connector.http.header.authorization";
private static final String CONNECTOR_INITIATE_URL_CONFIG = TCK_PREFIX + ".dsp.connector.negotiation.initiate.url";
private static final String THREAD_POOL_CONFIG = TCK_PREFIX + ".dsp.thread.pool";
private static final String DEFAULT_WAIT_CONFIG = TCK_PREFIX + ".dsp.default.wait";
Expand All @@ -64,6 +66,7 @@ public class DspSystemLauncher implements SystemLauncher {
private ExecutorService executor;
private String connectorUnderTestId = "ANONYMOUS";
private String baseConnectorUrl;
private String baseAuthorizationHeader;
private String connectorInitiateUrl;
private boolean useLocalConnector;
private long waitTime = DEFAULT_WAIT_SECONDS;
Expand All @@ -88,6 +91,10 @@ public void start(SystemConfiguration configuration) {
if (baseConnectorUrl == null) {
throw new RuntimeException("Required configuration not set: " + CONNECTOR_BASE_URL_CONFIG);
}
baseAuthorizationHeader = configuration.getPropertyAsString(CONNECTOR_BASE_AUTHORIZATION_HEADER_CONFIG, null);
if (baseAuthorizationHeader != null) {
HttpFunctions.registerAuthorizationInterceptor(baseAuthorizationHeader);
}
connectorInitiateUrl = configuration.getPropertyAsString(CONNECTOR_INITIATE_URL_CONFIG, null);
if (connectorInitiateUrl == null) {
throw new RuntimeException("Required configuration not set: " + CONNECTOR_INITIATE_URL_CONFIG);
Expand Down

0 comments on commit 91998ef

Please sign in to comment.