From 149b79e6245ea7868e6287c1b620bb60d6513834 Mon Sep 17 00:00:00 2001 From: Luana Date: Mon, 12 Oct 2026 14:19:17 +0300 Subject: [PATCH 1/6] [FLINK-40957][connect/mongodb] Fix MongoDB credential encoding for spaces --- .../mongodb/internal/MongoDBEnvelope.java | 3 ++- .../mongodb/internal/MongoDBEnvelopeTest.java | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java index 27e5da98320..44a47236c60 100644 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java @@ -148,7 +148,8 @@ public class MongoDBEnvelope { public static String encodeValue(String value) { try { - return URLEncoder.encode(value, StandardCharsets.UTF_8.name()); + return URLEncoder.encode(value, StandardCharsets.UTF_8.name()) + .replace("+", "%20"); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java new file mode 100644 index 00000000000..5b751f67ee6 --- /dev/null +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java @@ -0,0 +1,15 @@ +package org.apache.flink.cdc.connectors.mongodb.internal; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class MongoDBEnvelopeTest { + + @Test + void shouldEncodeSpacesAsPercent20() { + assertEquals( + "my%20pass", + MongoDBEnvelope.encodeValue("my pass")); + } +} \ No newline at end of file From 592d1f9b9cd0cf7ea2c402a17031cc00c3adee05 Mon Sep 17 00:00:00 2001 From: Luana Date: Thu, 22 Oct 2026 17:09:55 +0300 Subject: [PATCH 2/6] Add missing license header --- .../mongodb/internal/MongoDBEnvelopeTest.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java index 5b751f67ee6..d5f2563aa31 100644 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java @@ -1,15 +1,31 @@ +/* + * 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.internal; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +/** Tests for {@link MongoDBEnvelope}. */ class MongoDBEnvelopeTest { @Test void shouldEncodeSpacesAsPercent20() { - assertEquals( - "my%20pass", - MongoDBEnvelope.encodeValue("my pass")); + assertThat(MongoDBEnvelope.encodeValue("my pass")).isEqualTo("my%20pass"); } } \ No newline at end of file From 5fcd45b5ca2632d413ba1f2388b9701ef2787e7a Mon Sep 17 00:00:00 2001 From: Luana Date: Mon, 26 Oct 2026 17:45:21 +0200 Subject: [PATCH 3/6] [FLINK-40957][connect/mongodb] Encode spaces in credentials only within buildConnectionString Per review feedback, revert MongoDBEnvelope#encodeValue() to its original, generic percent-encoding behavior, since it is not specific to MongoDB credentials and other callers could be affected by changing it. Move the space ('+' -> '%20') correction into a new private MongoUtils#encodeCredential() helper, applied only to username/password when building the connection string in buildConnectionString(). Replace MongoDBEnvelopeTest (which tested the reverted behavior) with MongoUtilsTest, covering buildConnectionString directly. --- .../mongodb/internal/MongoDBEnvelope.java | 3 +- .../mongodb/source/utils/MongoUtils.java | 14 ++++- .../mongodb/internal/MongoDBEnvelopeTest.java | 31 ----------- .../mongodb/source/utils/MongoUtilsTest.java | 53 +++++++++++++++++++ 4 files changed, 67 insertions(+), 34 deletions(-) delete mode 100644 flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java create mode 100644 flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/source/utils/MongoUtilsTest.java diff --git a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java index 44a47236c60..27e5da98320 100644 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java +++ b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/main/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelope.java @@ -148,8 +148,7 @@ public class MongoDBEnvelope { public static String encodeValue(String value) { try { - return URLEncoder.encode(value, StandardCharsets.UTF_8.name()) - .replace("+", "%20"); + return URLEncoder.encode(value, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } 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/internal/MongoDBEnvelopeTest.java b/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java deleted file mode 100644 index d5f2563aa31..00000000000 --- a/flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mongodb-cdc/src/test/java/org/apache/flink/cdc/connectors/mongodb/internal/MongoDBEnvelopeTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.internal; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** Tests for {@link MongoDBEnvelope}. */ -class MongoDBEnvelopeTest { - - @Test - void shouldEncodeSpacesAsPercent20() { - assertThat(MongoDBEnvelope.encodeValue("my pass")).isEqualTo("my%20pass"); - } -} \ No newline at end of file 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..8618d7043be --- /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,53 @@ +/* + * 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 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"); + } +} From 05153006073ee2aa2dd4ecfff300303356df5fa6 Mon Sep 17 00:00:00 2001 From: Luana Date: Mon, 26 Oct 2026 17:47:16 +0200 Subject: [PATCH 4/6] [FLINK-40957][connect/mongodb] Add ITCase for MongoDB credentials containing spaces Add end-to-end tests to MongoDBParallelSourceITCase verifying that the MongoDB CDC source authenticates and reads data successfully when the username and/or password contain spaces, per review feedback. --- .../source/MongoDBParallelSourceITCase.java | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) 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, From f7e99641db71e88c0d5874ff79b92b64a3ad98b4 Mon Sep 17 00:00:00 2001 From: Luana Date: Fri, 27 Nov 2026 11:45:49 +0200 Subject: [PATCH 5/6] [FLINK-40957][connect/mongodb] Add test for special characters in MongoDB credentials --- .../mongodb/source/utils/MongoUtilsTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) 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 index 8618d7043be..b3b5d1a1889 100644 --- 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 @@ -33,6 +33,16 @@ void shouldEncodeSpacesInCredentialsAsPercent20() { 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 = From 63ad5a47d0bd4d9ae4737674bd6efc8dad89ad7e Mon Sep 17 00:00:00 2001 From: Luana Date: Fri, 27 Nov 2026 11:51:54 +0200 Subject: [PATCH 6/6] [FLINK-40957][connect/mongodb] Fix license check issue --- .../cdc/connectors/mongodb/source/utils/MongoUtilsTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 index b3b5d1a1889..5217b3d671f 100644 --- 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 @@ -39,8 +39,7 @@ void shouldHandleTildeAndAsteriskInCredentials() { MongoUtils.buildConnectionString( "user~*", "pass~*", "mongodb", "localhost:27017", null); - assertThat(connectionString) - .isEqualTo("mongodb://user%7E*:pass%7E*@localhost:27017"); + assertThat(connectionString).isEqualTo("mongodb://user%7E*:pass%7E*@localhost:27017"); } @Test