Skip to content

Commit 6cec368

Browse files
committed
Forcing port to 443 when using MarkLogic cloud authentication
443 is written in stone for now, and thus the UX can be simpler by forcing the port to that value. Also, removed support for defaulting to 443 in DatabaseClientPropertySource. That was intended for ML Cloud users, but the better way to do it is via the DatabaseClientFactory method that's modified in this PR.
1 parent f02e27a commit 6cec368

File tree

9 files changed

+23
-46
lines changed

9 files changed

+23
-46
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClientFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,13 @@ static public DatabaseClient newClient(String host, int port, String basePath, S
13831383
SecurityContext securityContext,
13841384
DatabaseClient.ConnectionType connectionType) {
13851385
RESTServices services = new OkHttpServices();
1386+
// As of 6.1.0, the following optimization is made as it's guaranteed that if the user is connecting to a
1387+
// MarkLogic Cloud instance, then port 443 will be used. Every path for constructing a DatabaseClient goes through
1388+
// this method, ensuring that this optimization will always be applied, and thus freeing the user from having to
1389+
// worry about what port to configure when using MarkLogic Cloud.
1390+
if (securityContext instanceof MarkLogicCloudAuthContext) {
1391+
port = 443;
1392+
}
13861393
services.connect(host, port, basePath, database, securityContext);
13871394

13881395
if (clientConfigurator != null) {

marklogic-client-api/src/main/java/com/marklogic/client/extra/okhttpclient/OkHttpClientBuilderFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public interface OkHttpClientBuilderFactory {
3030

31-
static OkHttpClient.Builder newOkHttpClientBuilder(String host, int port, DatabaseClientFactory.SecurityContext securityContext) {
32-
return OkHttpUtil.newOkHttpClientBuilder(host, port, securityContext);
31+
static OkHttpClient.Builder newOkHttpClientBuilder(String host, DatabaseClientFactory.SecurityContext securityContext) {
32+
return OkHttpUtil.newOkHttpClientBuilder(host, securityContext);
3333
}
3434
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/DatabaseClientPropertySource.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ public DatabaseClientFactory.Bean newClientBean() {
9191
}
9292
});
9393
bean.setSecurityContext(newSecurityContext());
94-
if (bean.getSecurityContext() != null && bean.getSecurityContext().getSSLContext() != null && bean.getPort() == 0) {
95-
bean.setPort(443);
96-
}
9794
return bean;
9895
}
9996

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void connect(String host, int port, String basePath, String database, Sec
197197
this.database = database;
198198
this.baseUri = HttpUrlBuilder.newBaseUrl(host, port, basePath, securityContext.getSSLContext());
199199

200-
OkHttpClient.Builder clientBuilder = OkHttpUtil.newOkHttpClientBuilder(host, port, securityContext);
200+
OkHttpClient.Builder clientBuilder = OkHttpUtil.newOkHttpClientBuilder(host, securityContext);
201201

202202
Properties props = System.getProperties();
203203
if (props.containsKey(OKHTTP_LOGGINGINTERCEPTOR_LEVEL)) {

marklogic-client-api/src/main/java/com/marklogic/client/impl/okhttp/MarkLogicCloudAuthenticationConfigurer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ public class MarkLogicCloudAuthenticationConfigurer implements AuthenticationCon
2929
private final static Logger logger = LoggerFactory.getLogger(MarkLogicCloudAuthenticationConfigurer.class);
3030

3131
private String host;
32-
private int port;
3332

34-
public MarkLogicCloudAuthenticationConfigurer(String host, int port) {
33+
public MarkLogicCloudAuthenticationConfigurer(String host) {
3534
this.host = host;
36-
this.port = port;
3735
}
3836

3937
@Override
@@ -84,10 +82,12 @@ private Response callTokenEndpoint(MarkLogicCloudAuthContext securityContext) {
8482
}
8583

8684
protected HttpUrl buildTokenUrl(MarkLogicCloudAuthContext securityContext) {
85+
// For the near future, it's guaranteed that https and 443 will be required for connecting to MarkLogic Cloud,
86+
// so providing the ability to customize this would be misleading.
8787
return new HttpUrl.Builder()
88-
.scheme(securityContext.getSSLContext() != null ? "https" : "http")
88+
.scheme("https")
8989
.host(host)
90-
.port(port)
90+
.port(443)
9191
.build()
9292
.resolve(securityContext.getTokenEndpoint()).newBuilder().build();
9393
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/okhttp/OkHttpUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class OkHttpUtil {
3636

3737
final private static ConnectionPool connectionPool = new ConnectionPool();
3838

39-
public static OkHttpClient.Builder newOkHttpClientBuilder(String host, int port, DatabaseClientFactory.SecurityContext securityContext) {
39+
public static OkHttpClient.Builder newOkHttpClientBuilder(String host, DatabaseClientFactory.SecurityContext securityContext) {
4040
OkHttpClient.Builder clientBuilder = OkHttpUtil.newClientBuilder();
4141
AuthenticationConfigurer authenticationConfigurer = null;
4242

@@ -53,7 +53,7 @@ public static OkHttpClient.Builder newOkHttpClientBuilder(String host, int port,
5353
} else if (securityContext instanceof DatabaseClientFactory.SAMLAuthContext) {
5454
configureSAMLAuth((DatabaseClientFactory.SAMLAuthContext) securityContext, clientBuilder);
5555
} else if (securityContext instanceof DatabaseClientFactory.MarkLogicCloudAuthContext) {
56-
authenticationConfigurer = new MarkLogicCloudAuthenticationConfigurer(host, port);
56+
authenticationConfigurer = new MarkLogicCloudAuthenticationConfigurer(host);
5757
} else {
5858
throw new IllegalArgumentException("Unsupported security context: " + securityContext.getClass());
5959
}

marklogic-client-api/src/test/java/com/marklogic/client/extra/okhttpclient/OkHttpClientBuilderFactoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ public class OkHttpClientBuilderFactoryTest {
1212
@Test
1313
void smokeTest() {
1414
DatabaseClientFactory.Bean bean = Common.newClientBuilder().buildBean();
15-
OkHttpClient.Builder builder = OkHttpClientBuilderFactory.newOkHttpClientBuilder(
16-
bean.getHost(), bean.getPort(), bean.getSecurityContext());
15+
OkHttpClient.Builder builder = OkHttpClientBuilderFactory.newOkHttpClientBuilder(bean.getHost(), bean.getSecurityContext());
1716
assertNotNull(builder);
1817

1918
OkHttpClient client = builder.build();

marklogic-client-api/src/test/java/com/marklogic/client/impl/okhttp/MarkLogicCloudAuthenticationConfigurerTest.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,11 @@ public class MarkLogicCloudAuthenticationConfigurerTest {
1717

1818
@Test
1919
void buildTokenUrl() throws Exception {
20-
HttpUrl tokenUrl = new MarkLogicCloudAuthenticationConfigurer("somehost", 443).buildTokenUrl(
20+
HttpUrl tokenUrl = new MarkLogicCloudAuthenticationConfigurer("somehost").buildTokenUrl(
2121
new DatabaseClientFactory.MarkLogicCloudAuthContext("doesnt-matter")
2222
.withSSLContext(SSLContext.getDefault(), null)
2323
);
24-
assertEquals("https://somehost/token", tokenUrl.toString(),
25-
"When the port is 443, OkHttp won't include it in the URL");
26-
}
27-
28-
@Test
29-
void buildTokenUrlWithNonStandardPort() throws Exception {
30-
HttpUrl tokenUrl = new MarkLogicCloudAuthenticationConfigurer("somehost", 444).buildTokenUrl(
31-
new DatabaseClientFactory.MarkLogicCloudAuthContext("doesnt-matter")
32-
.withSSLContext(SSLContext.getDefault(), null)
33-
);
34-
assertEquals("https://somehost:444/token", tokenUrl.toString(),
35-
"If the port isn't 443, then OkHttp will include it in the URL");
24+
assertEquals("https://somehost/token", tokenUrl.toString());
3625
}
3726

3827
/**
@@ -41,7 +30,7 @@ void buildTokenUrlWithNonStandardPort() throws Exception {
4130
*/
4231
@Test
4332
void buildTokenUrlWithCustomTokenPath() throws Exception {
44-
HttpUrl tokenUrl = new MarkLogicCloudAuthenticationConfigurer("otherhost", 443).buildTokenUrl(
33+
HttpUrl tokenUrl = new MarkLogicCloudAuthenticationConfigurer("otherhost").buildTokenUrl(
4534
new DatabaseClientFactory.MarkLogicCloudAuthContext("doesnt-matter", "/customToken", "doesnt-matter")
4635
.withSSLContext(SSLContext.getDefault(), null)
4736
);
@@ -50,7 +39,7 @@ void buildTokenUrlWithCustomTokenPath() throws Exception {
5039

5140
@Test
5241
void newFormBody() {
53-
FormBody body = new MarkLogicCloudAuthenticationConfigurer("doesnt-matter", 443)
42+
FormBody body = new MarkLogicCloudAuthenticationConfigurer("doesnt-matter")
5443
.newFormBody(new DatabaseClientFactory.MarkLogicCloudAuthContext("myKey"));
5544
assertEquals("grant_type", body.name(0));
5645
assertEquals("apikey", body.value(0));
@@ -64,7 +53,7 @@ void newFormBody() {
6453
*/
6554
@Test
6655
void newFormBodyWithOverrides() {
67-
FormBody body = new MarkLogicCloudAuthenticationConfigurer("doesnt-matter", 443)
56+
FormBody body = new MarkLogicCloudAuthenticationConfigurer("doesnt-matter")
6857
.newFormBody(new DatabaseClientFactory.MarkLogicCloudAuthContext("myKey", "doesnt-matter", "custom-grant-type"));
6958
assertEquals("grant_type", body.name(0));
7059
assertEquals("custom-grant-type", body.value(0));

marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientBuilderTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,4 @@ void sslProtocolAndTrustManagerAndHostnameVerifier() {
216216
assertEquals(Common.TRUST_ALL_MANAGER, context.getTrustManager());
217217
assertEquals(DatabaseClientFactory.SSLHostnameVerifier.COMMON, context.getSSLHostnameVerifier());
218218
}
219-
220-
@Test
221-
void sslContextWithNoPort() throws Exception {
222-
bean = new DatabaseClientBuilder()
223-
.withSecurityContextType("DIGEST")
224-
.withSSLContext(SSLContext.getDefault())
225-
.buildBean();
226-
227-
assertTrue(bean.getSecurityContext() instanceof DatabaseClientFactory.DigestAuthContext);
228-
assertNotNull(bean.getSecurityContext().getSSLContext());
229-
assertEquals(443, bean.getPort(),
230-
"If an SSLContext is provided with no port, then assume 443, as that's the standard port for HTTPS calls. " +
231-
"That makes life a little simpler for MarkLogic Cloud users as well, as they don't need to worry about " +
232-
"setting the port.");
233-
}
234219
}

0 commit comments

Comments
 (0)