Skip to content

Commit ee7f370

Browse files
authored
Allow PolarisServerManager implementations to define custom client headers (#2510)
`PolarisServerManager` is a plugin point for downstream builds to define runtime env. for tests under `integration-tests`. This change allows more flexibility for test runtime environments by allowing injecting extra headers into test clients.
1 parent d11b9e4 commit ee7f370

File tree

8 files changed

+40
-30
lines changed

8 files changed

+40
-30
lines changed

integration-tests/src/main/java/org/apache/polaris/service/it/env/IcebergHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static RESTCatalog restCatalog(
3939
.put(CatalogProperties.URI, endpoints.catalogApiEndpoint().toString())
4040
.put(OAuth2Properties.TOKEN, authToken)
4141
.put("warehouse", catalog)
42-
.put("header." + endpoints.realmHeaderName(), endpoints.realmId())
42+
.putAll(endpoints.extraHeaders("header."))
4343
.putAll(extraProperties);
4444

4545
restCatalog.initialize("polaris", propertiesBuilder.buildKeepingLast());

integration-tests/src/main/java/org/apache/polaris/service/it/env/PolarisApiEndpoints.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.io.Serializable;
2222
import java.net.URI;
23+
import java.util.Map;
24+
import java.util.stream.Collectors;
2325

2426
/**
2527
* This class contains the most fundamental information for accessing Polaris APIs, such as the base
@@ -30,12 +32,16 @@ public final class PolarisApiEndpoints implements Serializable {
3032

3133
private final URI baseUri;
3234
private final String realmId;
33-
private final String realmHeaderName;
35+
private final Map<String, String> headers;
3436

3537
public PolarisApiEndpoints(URI baseUri, String realmId, String realmHeaderName) {
38+
this(baseUri, realmId, Map.of(realmHeaderName, realmId));
39+
}
40+
41+
public PolarisApiEndpoints(URI baseUri, String realmId, Map<String, String> headers) {
3642
this.baseUri = baseUri;
3743
this.realmId = realmId;
38-
this.realmHeaderName = realmHeaderName;
44+
this.headers = headers;
3945
}
4046

4147
public URI catalogApiEndpoint() {
@@ -50,7 +56,13 @@ public String realmId() {
5056
return realmId;
5157
}
5258

53-
public String realmHeaderName() {
54-
return realmHeaderName;
59+
public Map<String, String> extraHeaders() {
60+
return headers;
61+
}
62+
63+
public Map<String, String> extraHeaders(String keyPrefix) {
64+
return headers.entrySet().stream()
65+
.map(e -> Map.entry(keyPrefix + e.getKey(), e.getValue()))
66+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
5567
}
5668
}

integration-tests/src/main/java/org/apache/polaris/service/it/env/PolarisRestApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public class PolarisRestApi extends RestApi {
3939
}
4040

4141
protected Map<String, String> defaultHeaders() {
42-
Map<String, String> headers = new HashMap<>();
43-
headers.put(endpoints.realmHeaderName(), endpoints.realmId());
42+
Map<String, String> headers = new HashMap<>(endpoints.extraHeaders());
4443
if (authToken != null) {
4544
headers.put("Authorization", "Bearer " + authToken);
4645
}

integration-tests/src/main/java/org/apache/polaris/service/it/env/Server.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.polaris.service.it.env;
2020

2121
import java.net.URI;
22+
import java.util.Map;
2223

2324
/**
2425
* This is a holder for access information to a particular Polaris Server. Test cases may use only
@@ -37,6 +38,10 @@ default String realmHeaderName() {
3738
return DEFAULT_REALM_HEADER;
3839
}
3940

41+
default Map<String, String> headers() {
42+
return Map.of(realmHeaderName(), realmId());
43+
}
44+
4045
/**
4146
* The base URI to all Polaris APIs (e.g. the common base of the Iceberg REST API endpoints and
4247
* Polaris Management API endpoints).

integration-tests/src/main/java/org/apache/polaris/service/it/ext/PolarisIntegrationTestExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static class Env implements AutoCloseable {
9292
private Env(Server server) {
9393
this.server = server;
9494
this.endpoints =
95-
new PolarisApiEndpoints(server.baseUri(), server.realmId(), server.realmHeaderName());
95+
new PolarisApiEndpoints(server.baseUri(), server.realmId(), server.headers());
9696
}
9797

9898
PolarisApiEndpoints endpoints() {

integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisApplicationIntegrationTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,12 @@ private static RESTSessionCatalog newSessionCatalog(String catalog) {
259259
RESTSessionCatalog sessionCatalog = new RESTSessionCatalog();
260260
sessionCatalog.initialize(
261261
"polaris_catalog_test",
262-
Map.of(
263-
"uri",
264-
endpoints.catalogApiEndpoint().toString(),
265-
OAuth2Properties.TOKEN,
266-
authToken,
267-
"warehouse",
268-
catalog,
269-
"header." + endpoints.realmHeaderName(),
270-
realm));
262+
ImmutableMap.<String, String>builder()
263+
.put("uri", endpoints.catalogApiEndpoint().toString())
264+
.put(OAuth2Properties.TOKEN, authToken)
265+
.put("warehouse", catalog)
266+
.putAll(endpoints.extraHeaders("header."))
267+
.build());
271268
return sessionCatalog;
272269
}
273270

@@ -590,15 +587,12 @@ public void testWarehouseNotSpecified() throws IOException {
590587
() ->
591588
sessionCatalog.initialize(
592589
"polaris_catalog_test",
593-
Map.of(
594-
"uri",
595-
endpoints.catalogApiEndpoint().toString(),
596-
OAuth2Properties.TOKEN,
597-
authToken,
598-
"warehouse",
599-
emptyEnvironmentVariable,
600-
"header." + endpoints.realmHeaderName(),
601-
realm)))
590+
ImmutableMap.<String, String>builder()
591+
.put("uri", endpoints.catalogApiEndpoint().toString())
592+
.put(OAuth2Properties.TOKEN, authToken)
593+
.put("warehouse", emptyEnvironmentVariable)
594+
.putAll(endpoints.extraHeaders("header."))
595+
.build()))
602596
.isInstanceOf(BadRequestException.class)
603597
.hasMessage("Malformed request: Please specify a warehouse");
604598
}

runtime/service/src/intTest/java/org/apache/polaris/service/it/RestCatalogMinIOSpecialIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private RESTCatalog createCatalog(
198198
org.apache.iceberg.CatalogProperties.URI, endpoints.catalogApiEndpoint().toString())
199199
.put(OAuth2Properties.TOKEN, authToken)
200200
.put("warehouse", catalogName)
201-
.put("header." + endpoints.realmHeaderName(), endpoints.realmId())
201+
.putAll(endpoints.extraHeaders("header."))
202202
.put("header.X-Iceberg-Access-Delegation", "vended-credentials");
203203

204204
restCatalog.initialize("polaris", propertiesBuilder.buildKeepingLast());

runtime/service/src/test/java/org/apache/polaris/service/it/ApplicationIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testIcebergRestApiRefreshExpiredToken(
7575
String path = endpoints.catalogApiEndpoint() + "/v1/oauth/tokens";
7676
try (RESTClient client =
7777
HTTPClient.builder(Map.of())
78-
.withHeader(endpoints.realmHeaderName(), endpoints.realmId())
78+
.withHeaders(endpoints.extraHeaders())
7979
.uri(path)
8080
.withAuthSession(AuthSession.EMPTY)
8181
.build()) {
@@ -106,7 +106,7 @@ public void testIcebergRestApiRefreshValidToken(
106106
String path = endpoints.catalogApiEndpoint() + "/v1/oauth/tokens";
107107
try (RESTClient client =
108108
HTTPClient.builder(Map.of())
109-
.withHeader(endpoints.realmHeaderName(), endpoints.realmId())
109+
.withHeaders(endpoints.extraHeaders())
110110
.uri(path)
111111
.withAuthSession(AuthSession.EMPTY)
112112
.build()) {
@@ -147,7 +147,7 @@ public void testIcebergRestApiInvalidToken(
147147
String path = endpoints.catalogApiEndpoint() + "/v1/oauth/tokens";
148148
try (RESTClient client =
149149
HTTPClient.builder(Map.of())
150-
.withHeader(endpoints.realmHeaderName(), endpoints.realmId())
150+
.withHeaders(endpoints.extraHeaders())
151151
.uri(path)
152152
.withAuthSession(AuthSession.EMPTY)
153153
.build()) {

0 commit comments

Comments
 (0)