Skip to content

Commit 03e22f0

Browse files
committed
ALLOWED_HOSTS validation, 1 minute machine timeout
1 parent 330d3b1 commit 03e22f0

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

driver-core/src/main/com/mongodb/internal/connection/OidcAuthenticator.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public final class OidcAuthenticator extends SaslAuthenticator {
8585
private static final List<String> ALLOWS_USERNAME = Arrays.asList(
8686
AZURE_ENVIRONMENT);
8787

88-
private static final Duration CALLBACK_TIMEOUT = Duration.ofMinutes(5);
88+
private static final Duration CALLBACK_TIMEOUT = Duration.ofMinutes(1);
89+
private static final Duration HUMAN_CALLBACK_TIMEOUT = Duration.ofMinutes(5);
8990

9091
public static final String OIDC_TOKEN_FILE = "OIDC_TOKEN_FILE";
9192

@@ -112,6 +113,10 @@ public OidcAuthenticator(final MongoCredentialWithCache credential,
112113
}
113114
}
114115

116+
private Duration getCallbackTimeout() {
117+
return isHumanCallback() ? HUMAN_CALLBACK_TIMEOUT : CALLBACK_TIMEOUT;
118+
}
119+
115120
@Override
116121
public String getMechanismName() {
117122
return MONGODB_OIDC.getMechanismName();
@@ -306,7 +311,7 @@ private byte[] evaluate(final byte[] challenge) {
306311
// Invoke Callback using cached Refresh Token
307312
fallbackState = FallbackState.PHASE_2_REFRESH_CALLBACK_TOKEN;
308313
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
309-
CALLBACK_TIMEOUT, cachedIdpInfo, cachedRefreshToken, userName));
314+
getCallbackTimeout(), cachedIdpInfo, cachedRefreshToken, userName));
310315
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(cachedIdpInfo, result);
311316
} else {
312317
// cache is empty
@@ -315,7 +320,7 @@ private byte[] evaluate(final byte[] challenge) {
315320
// no principal request
316321
fallbackState = FallbackState.PHASE_3B_CALLBACK_TOKEN;
317322
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
318-
CALLBACK_TIMEOUT, userName));
323+
getCallbackTimeout(), userName));
319324
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(null, result);
320325
if (result.getRefreshToken() != null) {
321326
throw new MongoConfigurationException(
@@ -345,7 +350,7 @@ private byte[] evaluate(final byte[] challenge) {
345350
// there is no cached refresh token
346351
fallbackState = FallbackState.PHASE_3B_CALLBACK_TOKEN;
347352
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
348-
CALLBACK_TIMEOUT, idpInfo, null, userName));
353+
getCallbackTimeout(), idpInfo, null, userName));
349354
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(idpInfo, result);
350355
}
351356
}
@@ -606,6 +611,11 @@ public static void validateBeforeUse(final MongoCredential credential) {
606611
Object environmentName = credential.getMechanismProperty(ENVIRONMENT_KEY, null);
607612
Object machineCallback = credential.getMechanismProperty(OIDC_CALLBACK_KEY, null);
608613
Object humanCallback = credential.getMechanismProperty(OIDC_HUMAN_CALLBACK_KEY, null);
614+
boolean allowedHostsIsSet = credential.getMechanismProperty(ALLOWED_HOSTS_KEY, null) != null;
615+
if (humanCallback == null && allowedHostsIsSet) {
616+
throw new IllegalArgumentException(ALLOWED_HOSTS_KEY + " must be specified only when "
617+
+ OIDC_HUMAN_CALLBACK_KEY + " is specified");
618+
}
609619
if (environmentName == null) {
610620
// callback
611621
if (machineCallback == null && humanCallback == null) {

driver-core/src/test/resources/unified-test-format/auth/mongodb-oidc-no-retry.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
{
66
"minServerVersion": "7.0",
77
"auth": true,
8-
"authMechanism": "MONGODB-OIDC"
8+
"authMechanism": "MONGODB-OIDC",
9+
"serverless": "forbid"
910
}
1011
],
1112
"createEntities": [

driver-sync/src/test/functional/com/mongodb/internal/connection/OidcAuthenticationProseTests.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ private void assumeTestEnvironment() {
9696
}
9797

9898
protected static String getOidcUri() {
99-
return getenv("MONGODB_URI_SINGLE");
99+
return assertNotNull(getenv("MONGODB_URI_SINGLE"));
100100
}
101101

102102
private static String getOidcUriMulti() {
103-
return getenv("MONGODB_URI_MULTI");
103+
return assertNotNull(getenv("MONGODB_URI_MULTI"));
104104
}
105105

106106
private static String getOidcEnv() {
107-
return getenv("OIDC_ENV");
107+
return assertNotNull(getenv("OIDC_ENV"));
108108
}
109109

110110
private static void assumeAzure() {
@@ -179,13 +179,13 @@ public void test1p2CallbackCalledOnceForMultipleConnections() {
179179

180180
@Test
181181
public void test2p1ValidCallbackInputs() {
182-
Duration expectedSeconds = Duration.ofMinutes(5);
182+
Duration expectedTimeoutDuration = Duration.ofMinutes(1);
183183

184184
TestCallback callback1 = createCallback();
185185
// #. Verify that the request callback was called with the appropriate
186186
// inputs, including the timeout parameter if possible.
187187
OidcCallback callback2 = (context) -> {
188-
assertEquals(expectedSeconds, context.getTimeout());
188+
assertEquals(expectedTimeoutDuration, context.getTimeout());
189189
return callback1.onRequest(context);
190190
};
191191
MongoClientSettings clientSettings = createSettings(callback2);
@@ -232,6 +232,28 @@ public void test2p4InvalidClientConfigurationWithCallback() {
232232
() -> performFind(settings));
233233
}
234234

235+
@Test
236+
public void test2p5InvalidAllowedHosts() {
237+
//String uri = getOidcUri() + "&authMechanismProperties=ENVIRONMENT:" + getOidcEnv();
238+
String uri = "mongodb://localhost/?authMechanism=MONGODB-OIDC&&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:123";
239+
ConnectionString cs = new ConnectionString(uri);
240+
MongoCredential credential = assertNotNull(cs.getCredential())
241+
.withMechanismProperty("ALLOWED_HOSTS", Collections.emptyList());
242+
MongoClientSettings settings = MongoClientSettings.builder()
243+
.applicationName(appName)
244+
.applyConnectionString(cs)
245+
.retryReads(false)
246+
.credential(credential)
247+
.build();
248+
assertCause(IllegalArgumentException.class,
249+
"ALLOWED_HOSTS must not be specified only when OIDC_HUMAN_CALLBACK is specified",
250+
() -> {
251+
try (MongoClient mongoClient = createMongoClient(settings)) {
252+
performFind(mongoClient);
253+
}
254+
});
255+
}
256+
235257
@Test
236258
public void test3p1AuthFailsWithCachedToken() throws ExecutionException, InterruptedException, NoSuchFieldException, IllegalAccessException {
237259
TestCallback callbackWrapped = createCallback();

0 commit comments

Comments
 (0)