-
Notifications
You must be signed in to change notification settings - Fork 5.5k
chore(deps): Upgrading mongodb driver to mongodb driver sync #27685
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
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 |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ public class MongoClientConfig | |
| private static final Splitter TAG_SPLITTER = Splitter.on(':').trimResults().omitEmptyStrings(); | ||
| private String schemaCollection = "_schema"; | ||
| private List<ServerAddress> seeds = ImmutableList.of(); | ||
| private List<MongoCredential> credentials = ImmutableList.of(); | ||
| private MongoCredential credential; | ||
|
|
||
| private int minConnectionsPerHost; | ||
| private int connectionsPerHost = 100; | ||
|
|
@@ -181,23 +181,16 @@ public MongoClientConfig setSeeds(String... seeds) | |
| return this; | ||
| } | ||
|
|
||
| @NotNull | ||
| @Size(min = 0) | ||
| public List<MongoCredential> getCredentials() | ||
| public Optional<MongoCredential> getCredentials() | ||
| { | ||
| return credentials; | ||
| return Optional.ofNullable(credential); | ||
| } | ||
|
|
||
| @Config("mongodb.credentials") | ||
| public MongoClientConfig setCredentials(String credentials) | ||
| { | ||
| this.credentials = buildCredentials(SPLITTER.split(credentials)); | ||
| return this; | ||
| } | ||
|
|
||
| public MongoClientConfig setCredentials(String... credentials) | ||
| @ConfigSecuritySensitive | ||
| public MongoClientConfig setCredentials(String credential) | ||
| { | ||
| this.credentials = buildCredentials(Arrays.asList(credentials)); | ||
| this.credential = credential == null ? null : buildCredential(credential); | ||
|
Member
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. Connection works fine even with a null credential? Or this should throw an error?
Contributor
Author
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. Yes, connections work fine with null credentials. This is intentional and correct behavior because MongoDB supports unauthenticated connections. No exception will throw if authentication is disabled. if authentication is enabled and doesn't provide any credentials, the query will fail with authentication error like this |
||
| return this; | ||
| } | ||
|
|
||
|
|
@@ -217,23 +210,21 @@ private List<ServerAddress> buildSeeds(Iterable<String> hostPorts) | |
| return builder.build(); | ||
| } | ||
|
|
||
| private List<MongoCredential> buildCredentials(Iterable<String> userPasses) | ||
| private MongoCredential buildCredential(String userPass) | ||
| { | ||
| ImmutableList.Builder<MongoCredential> builder = ImmutableList.builder(); | ||
| for (String userPass : userPasses) { | ||
| int atPos = userPass.lastIndexOf('@'); | ||
| checkArgument(atPos > 0, "Invalid credential format. Required user:password@collection"); | ||
| String userAndPassword = userPass.substring(0, atPos); | ||
| String collection = userPass.substring(atPos + 1); | ||
| checkArgument(!userPass.contains(","), "Multiple credentials are not supported. Configure a single credential in the format user:password@authenticationDatabase"); | ||
|
|
||
| int colonPos = userAndPassword.indexOf(':'); | ||
| checkArgument(colonPos > 0, "Invalid credential format. Required user:password@collection"); | ||
| String user = userAndPassword.substring(0, colonPos); | ||
| String password = userAndPassword.substring(colonPos + 1); | ||
| int atPos = userPass.lastIndexOf('@'); | ||
| checkArgument(atPos > 0, "Invalid credential format. Required user:password@authenticationDatabase"); | ||
| String userAndPassword = userPass.substring(0, atPos); | ||
| String authenticationDatabase = userPass.substring(atPos + 1); | ||
|
|
||
| builder.add(createCredential(user, collection, password.toCharArray())); | ||
| } | ||
| return builder.build(); | ||
| int colonPos = userAndPassword.indexOf(':'); | ||
| checkArgument(colonPos > 0, "Invalid credential format. Required user:password@authenticationDatabase"); | ||
| String user = userAndPassword.substring(0, colonPos); | ||
| String password = userAndPassword.substring(colonPos + 1); | ||
|
|
||
| return createCredential(user, authenticationDatabase, password.toCharArray()); | ||
| } | ||
|
|
||
| @Min(0) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,16 @@ | |
| import com.google.inject.Module; | ||
| import com.google.inject.Provides; | ||
| import com.google.inject.Scopes; | ||
| import com.mongodb.MongoClient; | ||
| import com.mongodb.MongoClientOptions; | ||
| import com.mongodb.ConnectionString; | ||
| import com.mongodb.MongoClientSettings; | ||
| import com.mongodb.ReadPreference; | ||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.client.MongoClients; | ||
| import jakarta.inject.Singleton; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static com.facebook.airlift.configuration.ConfigBinder.configBinder; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
|
|
@@ -46,38 +52,67 @@ public static MongoSession createMongoSession(TypeManager typeManager, MongoClie | |
| { | ||
| requireNonNull(config, "config is null"); | ||
|
|
||
| MongoClientOptions.Builder options = MongoClientOptions.builder() | ||
| .connectionsPerHost(config.getConnectionsPerHost()) | ||
| .connectTimeout(config.getConnectionTimeout()) | ||
| .socketTimeout(config.getSocketTimeout()) | ||
| .socketKeepAlive(config.getSocketKeepAlive()) | ||
|
Member
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.
Contributor
Author
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. Yes. The
|
||
| .maxWaitTime(config.getMaxWaitTime()) | ||
| .minConnectionsPerHost(config.getMinConnectionsPerHost()) | ||
| .writeConcern(config.getWriteConcern().getWriteConcern()); | ||
| MongoClientSettings.Builder settingsBuilder = MongoClientSettings.builder(); | ||
|
|
||
| String connectionString = buildConnectionString(config); | ||
| settingsBuilder.applyConnectionString(new ConnectionString(connectionString)); | ||
|
|
||
| config.getCredentials().ifPresent(settingsBuilder::credential); | ||
|
|
||
| settingsBuilder.applyToConnectionPoolSettings(builder -> builder | ||
| .maxSize(config.getConnectionsPerHost()) | ||
| .minSize(config.getMinConnectionsPerHost()) | ||
| .maxWaitTime(config.getMaxWaitTime(), TimeUnit.MILLISECONDS)); | ||
|
|
||
| settingsBuilder.applyToSocketSettings(builder -> builder | ||
| .connectTimeout(config.getConnectionTimeout(), TimeUnit.MILLISECONDS) | ||
| .readTimeout(config.getSocketTimeout(), TimeUnit.MILLISECONDS)); | ||
|
|
||
| settingsBuilder.writeConcern(config.getWriteConcern().getWriteConcern()); | ||
|
|
||
| settingsBuilder.readPreference(configureReadPreference(config)); | ||
|
|
||
| if (config.getRequiredReplicaSetName() != null) { | ||
| options.requiredReplicaSetName(config.getRequiredReplicaSetName()); | ||
| settingsBuilder.applyToClusterSettings(builder -> | ||
| builder.requiredReplicaSetName(config.getRequiredReplicaSetName())); | ||
| } | ||
|
|
||
| configureReadPreference(options, config); | ||
| configureSsl(options, config); | ||
| configureSsl(settingsBuilder, config); | ||
|
|
||
| MongoClient client = new MongoClient(config.getSeeds(), config.getCredentials(), options.build()); | ||
| MongoClient client = MongoClients.create(settingsBuilder.build()); | ||
|
|
||
| return new MongoSession(typeManager, client, config); | ||
| } | ||
|
|
||
| private static void configureReadPreference(MongoClientOptions.Builder options, MongoClientConfig config) | ||
| private static String buildConnectionString(MongoClientConfig config) | ||
| { | ||
| StringBuilder connectionString = new StringBuilder("mongodb://"); | ||
|
|
||
| connectionString.append(config.getSeeds().stream() | ||
| .map(addr -> addr.getHost() + ":" + addr.getPort()) | ||
| .collect(Collectors.joining(","))); | ||
|
|
||
| config.getCredentials().ifPresent(credential -> | ||
| connectionString.append("/") | ||
| .append(credential.getSource())); | ||
|
|
||
| // Enable replica set discovery when replica set name is configured or multiple seeds are provided | ||
| if (config.getRequiredReplicaSetName() != null || config.getSeeds().size() > 1) { | ||
| connectionString.append("?directConnection=false"); | ||
| } | ||
| return connectionString.toString(); | ||
| } | ||
| private static ReadPreference configureReadPreference(MongoClientConfig config) | ||
| { | ||
| if (config.getReadPreferenceTags().isEmpty()) { | ||
| options.readPreference(config.getReadPreference().getReadPreference()); | ||
| return config.getReadPreference().getReadPreference(); | ||
| } | ||
| else { | ||
| options.readPreference(config.getReadPreference().getReadPreferenceWithTags(config.getReadPreferenceTags())); | ||
| return config.getReadPreference().getReadPreferenceWithTags(config.getReadPreferenceTags()); | ||
| } | ||
| } | ||
|
|
||
| private static void configureSsl(MongoClientOptions.Builder options, MongoClientConfig config) | ||
| private static void configureSsl(MongoClientSettings.Builder settings, MongoClientConfig config) | ||
| { | ||
| if (config.isTlsEnabled()) { | ||
| SslContextProvider sslContextProvider = new SslContextProvider( | ||
|
|
@@ -88,8 +123,9 @@ private static void configureSsl(MongoClientOptions.Builder options, MongoClient | |
|
|
||
| sslContextProvider.buildSslContext() | ||
| .ifPresent(sslContext -> { | ||
| options.sslContext(sslContext); | ||
| options.sslEnabled(true); | ||
| settings.applyToSslSettings(builder -> builder | ||
| .enabled(true) | ||
| .context(sslContext)); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
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.
I think, we can annotate this with @ConfigSecuritySensitive
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.
@ConfigSecuritySensitive added