Skip to content
Open
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 @@ -436,7 +436,10 @@ public static String buildConnectionString(
StringBuilder sb = new StringBuilder(scheme).append("://");

if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
sb.append(encodeValue(username)).append(":").append(encodeValue(password)).append("@");
sb.append(encodeCredential(username))
.append(":")
.append(encodeCredential(password))
.append("@");
}

sb.append(checkNotNull(hosts));
Expand All @@ -448,6 +451,15 @@ public static String buildConnectionString(
return sb.toString();
}

// MongoDB connection strings require spaces in credentials to be percent-encoded as
// "%20" rather than "+". URLEncoder#encode (used by encodeValue) encodes spaces as "+",
// which is only safe for `application/x-www-form-urlencoded` content, not URI user info.
// Apply this correction here, specifically for credentials, rather than in encodeValue
// itself, since encodeValue is a generic percent-encoding utility used elsewhere.
private static String encodeCredential(String value) {
return encodeValue(value).replace("+", "%20");
}

// Checks if given exception is caused by change stream cursor issues, including
// network connection failures, sharded cluster changes, or invalidate events.
// See: https://www.mongodb.com/docs/manual/changeStreams/ for more details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,55 @@
/** IT tests for {@link MongoDBSource}. */
@Timeout(value = 300, unit = TimeUnit.SECONDS)
class MongoDBParallelSourceITCase extends MongoDBSourceTestBase {
private static final String SPACE_PASSWORD_USER = "flinkuser_space_password";
private static final String SPACE_PASSWORD = "password with spaces";
private static final String SPACE_USERNAME_AND_PASSWORD_USER = "flink user with spaces";

private static final int USE_POST_LOWWATERMARK_HOOK = 1;
private static final int USE_PRE_HIGHWATERMARK_HOOK = 2;
private static final int USE_POST_HIGHWATERMARK_HOOK = 3;

private static final StreamExecutionEnvironment env =
StreamExecutionEnvironment.getExecutionEnvironment();

@Test
void testPasswordContainingSpaces() throws Exception {
createMongoUser(SPACE_PASSWORD_USER, SPACE_PASSWORD);
testMongoDBParallelSource(
1,
FailoverType.NONE,
FailoverPhase.NEVER,
new String[] {"customers"},
false,
Collections.emptyMap(),
SPACE_PASSWORD_USER,
SPACE_PASSWORD);
}

@Test
void testUsernameAndPasswordContainingSpaces() throws Exception {
createMongoUser(SPACE_USERNAME_AND_PASSWORD_USER, SPACE_PASSWORD);
testMongoDBParallelSource(
1,
FailoverType.NONE,
FailoverPhase.NEVER,
new String[] {"customers"},
false,
Collections.emptyMap(),
SPACE_USERNAME_AND_PASSWORD_USER,
SPACE_PASSWORD);
}

private static void createMongoUser(String username, String password) {
MONGO_CONTAINER.executeCommand(
"db = db.getSiblingDB('admin');"
+ "db.createUser({user: '"
+ username
+ "', pwd: '"
+ password
+ "', roles: [{role: 'flinkrole', db: 'admin'}]});");
}

@Test
void testReadSingleCollectionWithSingleParallelism() throws Exception {
testMongoDBParallelSource(
Expand Down Expand Up @@ -583,7 +625,27 @@ private void testMongoDBParallelSource(
boolean skipSnapshotBackfill,
Map<String, String> extraOptions)
throws Exception {
testMongoDBParallelSource(
parallelism,
failoverType,
failoverPhase,
captureCustomerCollections,
skipSnapshotBackfill,
extraOptions,
FLINK_USER,
FLINK_USER_PASSWORD);
}

private void testMongoDBParallelSource(
int parallelism,
FailoverType failoverType,
FailoverPhase failoverPhase,
String[] captureCustomerCollections,
boolean skipSnapshotBackfill,
Map<String, String> extraOptions,
String username,
String password)
throws Exception {
String customerDatabase = MONGO_CONTAINER.executeCommandFileInSeparateDatabase("customer");

StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
Expand Down Expand Up @@ -613,8 +675,8 @@ private void testMongoDBParallelSource(
+ " %s"
+ ")",
MONGO_CONTAINER.getHostAndPort(),
FLINK_USER,
FLINK_USER_PASSWORD,
username,
password,
customerDatabase,
getCollectionNameRegex(customerDatabase, captureCustomerCollections),
skipSnapshotBackfill,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.cdc.connectors.mongodb.source.utils;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/** Tests for {@link MongoUtils}. */
class MongoUtilsTest {

@Test
void shouldEncodeSpacesInCredentialsAsPercent20() {
String connectionString =
MongoUtils.buildConnectionString(
"my user", "my pass", "mongodb", "localhost:27017", null);

assertThat(connectionString).isEqualTo("mongodb://my%20user:my%20pass@localhost:27017");
}

@Test
void shouldHandleTildeAndAsteriskInCredentials() {
String connectionString =
MongoUtils.buildConnectionString(
"user~*", "pass~*", "mongodb", "localhost:27017", null);

assertThat(connectionString).isEqualTo("mongodb://user%7E*:pass%7E*@localhost:27017");
}

@Test
void shouldBuildConnectionStringWithoutCredentials() {
String connectionString =
MongoUtils.buildConnectionString(null, null, "mongodb", "localhost:27017", null);

assertThat(connectionString).isEqualTo("mongodb://localhost:27017");
}

@Test
void shouldAppendConnectionOptions() {
String connectionString =
MongoUtils.buildConnectionString(
"user", "pass", "mongodb", "localhost:27017", "replicaSet=rs0");

assertThat(connectionString)
.isEqualTo("mongodb://user:pass@localhost:27017/?replicaSet=rs0");
}
}