diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtils.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtils.java index 318065072af..79f78c036b0 100644 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtils.java +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtils.java @@ -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)); @@ -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. diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/MongoDBParallelSourceITCase.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/MongoDBParallelSourceITCase.java index a3f64ab2063..4e3c7b6af5c 100644 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/MongoDBParallelSourceITCase.java +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/MongoDBParallelSourceITCase.java @@ -69,6 +69,10 @@ /** 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; @@ -76,6 +80,44 @@ class MongoDBParallelSourceITCase extends MongoDBSourceTestBase { 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( @@ -583,7 +625,27 @@ private void testMongoDBParallelSource( boolean skipSnapshotBackfill, Map 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 extraOptions, + String username, + String password) + throws Exception { String customerDatabase = MONGO_CONTAINER.executeCommandFileInSeparateDatabase("customer"); StreamTableEnvironment tEnv = StreamTableEnvironment.create(env); @@ -613,8 +675,8 @@ private void testMongoDBParallelSource( + " %s" + ")", MONGO_CONTAINER.getHostAndPort(), - FLINK_USER, - FLINK_USER_PASSWORD, + username, + password, customerDatabase, getCollectionNameRegex(customerDatabase, captureCustomerCollections), skipSnapshotBackfill, diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtilsTest.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtilsTest.java new file mode 100644 index 00000000000..5217b3d671f --- /dev/null +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtilsTest.java @@ -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"); + } +}