Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALLOWED_HOSTS validation, 1 minute machine timeout #1417

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public final class OidcAuthenticator extends SaslAuthenticator {
private static final List<String> ALLOWS_USERNAME = Arrays.asList(
AZURE_ENVIRONMENT);

private static final Duration CALLBACK_TIMEOUT = Duration.ofMinutes(5);
private static final Duration CALLBACK_TIMEOUT = Duration.ofMinutes(1);
private static final Duration HUMAN_CALLBACK_TIMEOUT = Duration.ofMinutes(5);

public static final String OIDC_TOKEN_FILE = "OIDC_TOKEN_FILE";

Expand All @@ -112,6 +113,10 @@ public OidcAuthenticator(final MongoCredentialWithCache credential,
}
}

private Duration getCallbackTimeout() {
return isHumanCallback() ? HUMAN_CALLBACK_TIMEOUT : CALLBACK_TIMEOUT;
}

@Override
public String getMechanismName() {
return MONGODB_OIDC.getMechanismName();
Expand Down Expand Up @@ -306,7 +311,7 @@ private byte[] evaluate(final byte[] challenge) {
// Invoke Callback using cached Refresh Token
fallbackState = FallbackState.PHASE_2_REFRESH_CALLBACK_TOKEN;
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
CALLBACK_TIMEOUT, cachedIdpInfo, cachedRefreshToken, userName));
getCallbackTimeout(), cachedIdpInfo, cachedRefreshToken, userName));
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(cachedIdpInfo, result);
} else {
// cache is empty
Expand All @@ -315,7 +320,7 @@ private byte[] evaluate(final byte[] challenge) {
// no principal request
fallbackState = FallbackState.PHASE_3B_CALLBACK_TOKEN;
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
CALLBACK_TIMEOUT, userName));
getCallbackTimeout(), userName));
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(null, result);
if (result.getRefreshToken() != null) {
throw new MongoConfigurationException(
Expand Down Expand Up @@ -345,7 +350,7 @@ private byte[] evaluate(final byte[] challenge) {
// there is no cached refresh token
fallbackState = FallbackState.PHASE_3B_CALLBACK_TOKEN;
OidcCallbackResult result = requestCallback.onRequest(new OidcCallbackContextImpl(
CALLBACK_TIMEOUT, idpInfo, null, userName));
getCallbackTimeout(), idpInfo, null, userName));
jwt[0] = populateCacheWithCallbackResultAndPrepareJwt(idpInfo, result);
}
}
Expand Down Expand Up @@ -606,6 +611,11 @@ public static void validateBeforeUse(final MongoCredential credential) {
Object environmentName = credential.getMechanismProperty(ENVIRONMENT_KEY, null);
Object machineCallback = credential.getMechanismProperty(OIDC_CALLBACK_KEY, null);
Object humanCallback = credential.getMechanismProperty(OIDC_HUMAN_CALLBACK_KEY, null);
boolean allowedHostsIsSet = credential.getMechanismProperty(ALLOWED_HOSTS_KEY, null) != null;
if (humanCallback == null && allowedHostsIsSet) {
throw new IllegalArgumentException(ALLOWED_HOSTS_KEY + " must be specified only when "
+ OIDC_HUMAN_CALLBACK_KEY + " is specified");
}
if (environmentName == null) {
// callback
if (machineCallback == null && humanCallback == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
{
"minServerVersion": "7.0",
"auth": true,
"authMechanism": "MONGODB-OIDC"
"authMechanism": "MONGODB-OIDC",
"serverless": "forbid"
}
],
"createEntities": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ private void assumeTestEnvironment() {
}

protected static String getOidcUri() {
return getenv("MONGODB_URI_SINGLE");
return assertNotNull(getenv("MONGODB_URI_SINGLE"));
}

private static String getOidcUriMulti() {
return getenv("MONGODB_URI_MULTI");
return assertNotNull(getenv("MONGODB_URI_MULTI"));
}

private static String getOidcEnv() {
return getenv("OIDC_ENV");
return assertNotNull(getenv("OIDC_ENV"));
}

private static void assumeAzure() {
Expand Down Expand Up @@ -179,13 +179,13 @@ public void test1p2CallbackCalledOnceForMultipleConnections() {

@Test
public void test2p1ValidCallbackInputs() {
Duration expectedSeconds = Duration.ofMinutes(5);
Duration expectedTimeoutDuration = Duration.ofMinutes(1);

TestCallback callback1 = createCallback();
// #. Verify that the request callback was called with the appropriate
// inputs, including the timeout parameter if possible.
OidcCallback callback2 = (context) -> {
assertEquals(expectedSeconds, context.getTimeout());
assertEquals(expectedTimeoutDuration, context.getTimeout());
return callback1.onRequest(context);
};
MongoClientSettings clientSettings = createSettings(callback2);
Expand Down