-
Notifications
You must be signed in to change notification settings - Fork 42
SQL-2920: Add configuration properties to support GSSAPI #367
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package com.mongodb.jdbc; | ||
|
||
import static com.mongodb.AuthenticationMechanism.GSSAPI; | ||
import static com.mongodb.AuthenticationMechanism.MONGODB_OIDC; | ||
import static com.mongodb.AuthenticationMechanism.MONGODB_X509; | ||
|
||
|
@@ -184,28 +185,60 @@ private MongoClientSettings createMongoClientSettings( | |
if (credential != null) { | ||
AuthenticationMechanism authMechanism = credential.getAuthenticationMechanism(); | ||
|
||
if (authMechanism != null && authMechanism.equals(MONGODB_OIDC)) { | ||
// Handle OIDC authentication | ||
OidcCallback oidcCallback = new JdbcOidcCallback(this.logger); | ||
credential = | ||
MongoCredential.createOidcCredential( | ||
connectionProperties.getConnectionString().getUsername()) | ||
.withMechanismProperty( | ||
MongoCredential.OIDC_HUMAN_CALLBACK_KEY, oidcCallback); | ||
settingsBuilder.credential(credential); | ||
} else if (authMechanism != null && authMechanism.equals(MONGODB_X509)) { | ||
String pemPath = connectionProperties.getX509PemPath(); | ||
if (pemPath == null || pemPath.isEmpty()) { | ||
pemPath = System.getenv(MONGODB_JDBC_X509_CLIENT_CERT_PATH); | ||
} | ||
if (pemPath == null || pemPath.isEmpty()) { | ||
throw new IllegalStateException( | ||
"PEM file path is required for X.509 authentication but was not provided."); | ||
} | ||
if (authMechanism != null) { | ||
if (authMechanism.equals(MONGODB_OIDC)) { | ||
// Handle OIDC authentication | ||
OidcCallback oidcCallback = new JdbcOidcCallback(this.logger); | ||
credential = | ||
MongoCredential.createOidcCredential( | ||
connectionProperties | ||
.getConnectionString() | ||
.getUsername()) | ||
.withMechanismProperty( | ||
MongoCredential.OIDC_HUMAN_CALLBACK_KEY, oidcCallback); | ||
settingsBuilder.credential(credential); | ||
} else if (authMechanism.equals(GSSAPI)) { | ||
|
||
String jaasPath = connectionProperties.getJaasConfigPath(); | ||
if (jaasPath != null && !jaasPath.isEmpty()) { | ||
System.setProperty("java.security.auth.login.config", jaasPath); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conf changes will affect more than our driver. It will affect everything currently running JVM.
|
||
logger.log(Level.INFO, "Using custom JAAS config: " + jaasPath); | ||
} else { | ||
String existingConfig = | ||
System.getProperty("java.security.auth.login.config"); | ||
if (existingConfig != null) { | ||
logger.log( | ||
Level.INFO, | ||
"Using JAAS config from system property: " + existingConfig); | ||
} else { | ||
logger.log( | ||
Level.INFO, | ||
"No JAAS config specified. Relying on classpath or JVM defaults."); | ||
} | ||
} | ||
|
||
String gssNative = connectionProperties.getGssNativeMode(); | ||
if (gssNative != null && !gssNative.isEmpty()) { | ||
System.setProperty("sun.security.jgss.native", gssNative); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we want to look at using a local conf to make sure we don't impact other drivers when using Tableau for example.
|
||
logger.log( | ||
Level.INFO, "Set sun.security.jgss.native = " + gssNative.trim()); | ||
} | ||
|
||
X509Authentication x509Authentication = new X509Authentication(logger); | ||
x509Authentication.configureX509Authentication( | ||
settingsBuilder, pemPath, this.x509Passphrase); | ||
settingsBuilder.credential(credential); | ||
} else if (authMechanism.equals(MONGODB_X509)) { | ||
String pemPath = connectionProperties.getX509PemPath(); | ||
if (pemPath == null || pemPath.isEmpty()) { | ||
pemPath = System.getenv(MONGODB_JDBC_X509_CLIENT_CERT_PATH); | ||
} | ||
if (pemPath == null || pemPath.isEmpty()) { | ||
throw new IllegalStateException( | ||
"PEM file path is required for X.509 authentication but was not provided."); | ||
} | ||
|
||
X509Authentication x509Authentication = new X509Authentication(logger); | ||
x509Authentication.configureX509Authentication( | ||
settingsBuilder, pemPath, this.x509Passphrase); | ||
} | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -100,7 +100,9 @@ public enum MongoJDBCProperty { | |||||
LOG_DIR("logdir"), | ||||||
EXT_JSON_MODE("extjsonmode"), | ||||||
X509_PEM_PATH("x509pempath"), | ||||||
DISABLE_CLIENT_CACHE("disableclientcache"); | ||||||
DISABLE_CLIENT_CACHE("disableclientcache"), | ||||||
JAAS_CONFIG_PATH("jaasconfigpath"), | ||||||
GSS_NATIVE_MODE("gssnativemode"); | ||||||
|
||||||
private final String propertyName; | ||||||
|
||||||
|
@@ -486,6 +488,19 @@ private MongoConnection createConnection( | |||||
} | ||||||
} | ||||||
|
||||||
String gssNativeModeVal = info.getProperty(GSS_NATIVE_MODE.getPropertyName()); | ||||||
if (gssNativeModeVal != null) { | ||||||
gssNativeModeVal = gssNativeModeVal.trim().toLowerCase(); | ||||||
if (!"true".equals(gssNativeModeVal) && !"false".equals(gssNativeModeVal)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to be less restrictive.
Suggested change
|
||||||
throw new SQLException( | ||||||
"Invalid " | ||||||
+ GSS_NATIVE_MODE.getPropertyName() | ||||||
+ " property value: " | ||||||
+ gssNativeModeVal | ||||||
+ ". Valid values are: 'true', 'false'."); | ||||||
} | ||||||
} | ||||||
|
||||||
MongoConnectionProperties mongoConnectionProperties = | ||||||
new MongoConnectionProperties( | ||||||
cs, | ||||||
|
@@ -494,7 +509,9 @@ private MongoConnection createConnection( | |||||
logDir, | ||||||
clientInfo, | ||||||
extJsonMode, | ||||||
info.getProperty(X509_PEM_PATH.getPropertyName())); | ||||||
info.getProperty(X509_PEM_PATH.getPropertyName()), | ||||||
info.getProperty(JAAS_CONFIG_PATH.getPropertyName()), | ||||||
gssNativeModeVal); | ||||||
|
||||||
String disableCacheVal = | ||||||
info.getProperty(DISABLE_CLIENT_CACHE.getPropertyName(), "false").toLowerCase(); | ||||||
|
@@ -676,8 +693,10 @@ public static MongoConnectionConfig getConnectionSettings(String url, Properties | |||||
if (password == null && user != null) { | ||||||
if (result.authMechanism == null | ||||||
|| (!result.authMechanism.equals(MONGODB_X509) | ||||||
&& !result.authMechanism.equals(MONGODB_OIDC))) { | ||||||
// password is null, but user is not, we must prompt for the password. | ||||||
&& !result.authMechanism.equals(MONGODB_OIDC) | ||||||
&& !result.authMechanism.equals(GSSAPI))) { | ||||||
// Password is null, but user is provided. If the authentication mechanism | ||||||
// does not support password-less login, then the password must be prompted for. | ||||||
mandatoryConnectionProperties.add(new DriverPropertyInfo(PASSWORD, null)); | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.