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

Remove mongodb.seeds and mongodb.credentials config properties #15263

Merged
merged 1 commit into from
Dec 1, 2022
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 @@ -13,10 +13,6 @@
*/
package io.trino.plugin.mongodb;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import io.airlift.configuration.Config;
import io.airlift.configuration.ConfigSecuritySensitive;
import io.airlift.configuration.DefunctConfig;
Expand All @@ -29,24 +25,14 @@
import javax.validation.constraints.Pattern;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.mongodb.MongoCredential.createCredential;

@DefunctConfig({"mongodb.connection-per-host", "mongodb.socket-keep-alive"})
@DefunctConfig({"mongodb.connection-per-host", "mongodb.socket-keep-alive", "mongodb.seeds", "mongodb.credentials"})
public class MongoClientConfig
{
private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
private static final Splitter PORT_SPLITTER = Splitter.on(':').trimResults().omitEmptyStrings();

private String schemaCollection = "_schema";
private boolean caseInsensitiveNameMatching;
private Optional<String> connectionUrl = Optional.empty();
private List<ServerAddress> seeds = ImmutableList.of();
private List<MongoCredential> credentials = ImmutableList.of();
private String connectionUrl;

private int minConnectionsPerHost;
private int connectionsPerHost = 100;
Expand All @@ -68,16 +54,6 @@ public class MongoClientConfig
private String requiredReplicaSetName;
private String implicitRowFieldPrefix = "_pos";

@AssertTrue(message = "Exactly one of these 'mongodb.seed' or 'mongodb.connection-url' must be specified")
public boolean isConnectionPropertyValid()
{
if (seeds.isEmpty() && connectionUrl.isEmpty()) {
return false;
}

return seeds.isEmpty() || connectionUrl.isEmpty();
}

@AssertTrue(message = "'mongodb.tls.keystore-path', 'mongodb.tls.keystore-password', 'mongodb.tls.truststore-path' and 'mongodb.tls.truststore-password' must be empty when TLS is disabled")
public boolean isValidTlsConfig()
{
Expand Down Expand Up @@ -113,7 +89,7 @@ public MongoClientConfig setCaseInsensitiveNameMatching(boolean caseInsensitiveN
}

@NotNull
public Optional<@Pattern(message = "Invalid connection URL. Expected mongodb:// or mongodb+srv://", regexp = "^mongodb(\\+srv)?://.*") String> getConnectionUrl()
public @Pattern(message = "Invalid connection URL. Expected mongodb:// or mongodb+srv://", regexp = "^mongodb(\\+srv)?://.*") String getConnectionUrl()
{
return connectionUrl;
}
Expand All @@ -122,82 +98,10 @@ public MongoClientConfig setCaseInsensitiveNameMatching(boolean caseInsensitiveN
@ConfigSecuritySensitive
public MongoClientConfig setConnectionUrl(String connectionUrl)
{
this.connectionUrl = Optional.ofNullable(connectionUrl);
return this;
}

@NotNull
@Deprecated
public List<ServerAddress> getSeeds()
{
return seeds;
}

@Config("mongodb.seeds")
@Deprecated
public MongoClientConfig setSeeds(String commaSeparatedList)
{
this.seeds = buildSeeds(SPLITTER.split(commaSeparatedList));
return this;
}

public MongoClientConfig setSeeds(String... seeds)
{
this.seeds = buildSeeds(Arrays.asList(seeds));
this.connectionUrl = connectionUrl;
return this;
}

@NotNull
@Deprecated
public List<MongoCredential> getCredentials()
{
return credentials;
}

@Config("mongodb.credentials")
@ConfigSecuritySensitive
@Deprecated
public MongoClientConfig setCredentials(String credentials)
{
this.credentials = buildCredentials(SPLITTER.split(credentials));
return this;
}

private List<ServerAddress> buildSeeds(Iterable<String> hostPorts)
{
ImmutableList.Builder<ServerAddress> builder = ImmutableList.builder();
for (String hostPort : hostPorts) {
List<String> values = PORT_SPLITTER.splitToList(hostPort);
checkArgument(values.size() == 1 || values.size() == 2, "Invalid ServerAddress format. Requires host[:port]");
if (values.size() == 1) {
builder.add(new ServerAddress(values.get(0)));
}
else {
builder.add(new ServerAddress(values.get(0), Integer.parseInt(values.get(1))));
}
}
return builder.build();
}

private List<MongoCredential> buildCredentials(Iterable<String> userPasses)
{
ImmutableList.Builder<MongoCredential> builder = ImmutableList.builder();
for (String userPassDatabase : userPasses) {
int lastIndex = userPassDatabase.lastIndexOf('@');
checkArgument(lastIndex > 0, "Invalid Credential format. Requires user:password@database");
String userPass = userPassDatabase.substring(0, lastIndex);
String database = userPassDatabase.substring(lastIndex + 1);

int firstIndex = userPass.indexOf(':');
checkArgument(firstIndex > 0, "Invalid Credential format. Requires user:password@database");
String user = userPass.substring(0, firstIndex);
String password = userPass.substring(firstIndex + 1);

builder.add(createCredential(user, database, password.toCharArray()));
}
return builder.build();
}

@Min(0)
public int getMinConnectionsPerHost()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,7 @@ public static MongoSession createMongoSession(TypeManager typeManager, MongoClie
});
}

if (config.getConnectionUrl().isPresent()) {
options.applyConnectionString(new ConnectionString(config.getConnectionUrl().get()));
}
else {
options.applyToClusterSettings(builder -> builder.hosts(config.getSeeds()));
if (!config.getCredentials().isEmpty()) {
options.credential(config.getCredentials().get(0));
}
}
options.applyConnectionString(new ConnectionString(config.getConnectionUrl()));

MongoClient client = MongoClients.create(options.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.plugin.mongodb;

import com.google.common.collect.ImmutableMap;
import com.mongodb.MongoCredential;
import io.airlift.configuration.ConfigurationFactory;
import org.testng.annotations.Test;

Expand All @@ -38,8 +37,6 @@ public void testDefaults()
.setConnectionUrl(null)
.setSchemaCollection("_schema")
.setCaseInsensitiveNameMatching(false)
.setSeeds("")
.setCredentials("")
.setMinConnectionsPerHost(0)
.setConnectionsPerHost(100)
.setMaxWaitTime(120_000)
Expand Down Expand Up @@ -68,9 +65,7 @@ public void testExplicitPropertyMappings()
Map<String, String> properties = ImmutableMap.<String, String>builder()
.put("mongodb.schema-collection", "_my_schema")
.put("mongodb.case-insensitive-name-matching", "true")
.put("mongodb.seeds", "")
.put("mongodb.connection-url", "mongodb://router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/")
.put("mongodb.credentials", "username:password@collection")
.put("mongodb.min-connections-per-host", "1")
.put("mongodb.connections-per-host", "99")
.put("mongodb.max-wait-time", "120001")
Expand All @@ -95,9 +90,7 @@ public void testExplicitPropertyMappings()
MongoClientConfig expected = new MongoClientConfig()
.setSchemaCollection("_my_schema")
.setCaseInsensitiveNameMatching(true)
.setSeeds("")
.setConnectionUrl("mongodb://router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/")
.setCredentials("username:password@collection")
.setMinConnectionsPerHost(1)
.setConnectionsPerHost(99)
.setMaxWaitTime(120_001)
Expand All @@ -117,9 +110,7 @@ public void testExplicitPropertyMappings()

assertEquals(config.getSchemaCollection(), expected.getSchemaCollection());
assertEquals(config.isCaseInsensitiveNameMatching(), expected.isCaseInsensitiveNameMatching());
assertEquals(config.getSeeds(), expected.getSeeds());
assertEquals(config.getConnectionUrl(), expected.getConnectionUrl());
assertEquals(config.getCredentials(), expected.getCredentials());
assertEquals(config.getMinConnectionsPerHost(), expected.getMinConnectionsPerHost());
assertEquals(config.getConnectionsPerHost(), expected.getConnectionsPerHost());
assertEquals(config.getMaxWaitTime(), expected.getMaxWaitTime());
Expand All @@ -138,17 +129,6 @@ public void testExplicitPropertyMappings()
assertEquals(config.getImplicitRowFieldPrefix(), expected.getImplicitRowFieldPrefix());
}

@Test
public void testSpecialCharacterCredential()
{
MongoClientConfig config = new MongoClientConfig()
.setCredentials("username:P@ss:w0rd@database");

MongoCredential credential = config.getCredentials().get(0);
MongoCredential expected = MongoCredential.createCredential("username", "database", "P@ss:w0rd".toCharArray());
assertEquals(credential, expected);
}

@Test
public void testValidation()
throws Exception
Expand Down