Skip to content

Commit

Permalink
Encode MongoDB/DocumentDB username and password while constructing co…
Browse files Browse the repository at this point in the history
…nnection string (#4423)

Signed-off-by: Dinu John <86094133+dinujoh@users.noreply.github.com>
  • Loading branch information
dinujoh committed Apr 16, 2024
1 parent a5bdcf2 commit 11b18cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.opensearch.dataprepper.plugins.truststore.TrustStoreProvider;

import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class MongoDBConnection {
Expand All @@ -33,9 +35,23 @@ public static MongoClient getMongoClient(final MongoDBSourceConfig sourceConfig)
return MongoClients.create(settingBuilder.build());
}

private static String encodeString(final String input) {
return URLEncoder.encode(input, StandardCharsets.UTF_8);
}

private static String getConnectionString(final MongoDBSourceConfig sourceConfig) {
final String username = sourceConfig.getCredentialsConfig().getUsername();
final String password = sourceConfig.getCredentialsConfig().getPassword();
final String username;
try {
username = encodeString(sourceConfig.getCredentialsConfig().getUsername());
} catch (final Exception e) {
throw new RuntimeException("Unsupported characters in username.");
}
final String password;
try {
password = encodeString(sourceConfig.getCredentialsConfig().getPassword());
} catch (final Exception e) {
throw new RuntimeException("Unsupported characters in password.");
}
final String hostname = sourceConfig.getHostname();
final int port = sourceConfig.getPort();
final String tls = sourceConfig.getTls().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class MongoDBConnectionTest {
@BeforeEach
void setUp() {
when(mongoDBSourceConfig.getCredentialsConfig()).thenReturn(credentialsConfig);
when(credentialsConfig.getUsername()).thenReturn(UUID.randomUUID().toString());
when(credentialsConfig.getPassword()).thenReturn(UUID.randomUUID().toString());
when(credentialsConfig.getUsername()).thenReturn("\uD800\uD800" + UUID.randomUUID());
when(credentialsConfig.getPassword()).thenReturn("aЯ ⾀sd?q=%%l€0£.lo" + UUID.randomUUID());
when(mongoDBSourceConfig.getHostname()).thenReturn(UUID.randomUUID().toString());
when(mongoDBSourceConfig.getPort()).thenReturn(getRandomInteger());
when(mongoDBSourceConfig.getTls()).thenReturn(getRandomBoolean());
Expand Down

0 comments on commit 11b18cd

Please sign in to comment.