This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-12933-2c025594845650564d41b47387c2848f0b892c66 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit 3bdd5fc5d5a4ac3a5315814149dc20cec58ead12 Author: Nikhil kumar <[email protected]> AuthorDate: Thu Nov 26 05:17:14 2026 +0000 [Improve][Connector-V2] Validate Postgres CDC source URL (#12933) Co-authored-by: Nikhil <[email protected]> --- docs/en/connectors/source/PostgreSQL-CDC.md | 2 +- docs/zh/connectors/source/PostgreSQL-CDC.md | 2 +- .../source/PostgresIncrementalSourceFactory.java | 8 +- .../config/PostgresSourceConfigFactoryTest.java | 110 +++++++++++++++++++++ .../seatunnel/cdc/postgres/PostgresCDCIT.java | 29 ++++++ .../src/test/resources/postgrescdc_blank_url.conf | 35 +++++++ 6 files changed, 180 insertions(+), 6 deletions(-) diff --git a/docs/en/connectors/source/PostgreSQL-CDC.md b/docs/en/connectors/source/PostgreSQL-CDC.md index 1c443c249d..c754f89b82 100644 --- a/docs/en/connectors/source/PostgreSQL-CDC.md +++ b/docs/en/connectors/source/PostgreSQL-CDC.md @@ -98,7 +98,7 @@ ALTER TABLE your_table_name REPLICA IDENTITY FULL; | Name | Type | Required | Default | Description [...] |-------------------------------------------|----------|----------|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ [...] -| url | String | Yes | - | The URL of the JDBC connection. Refer to a case: `jdbc:postgresql://localhost:5432/postgres_cdc?loggerLevel=OFF`. [...] +| url | String | Yes | - | The URL of the JDBC connection. The value must not be empty or whitespace-only. Refer to a case: `jdbc:postgresql://localhost:5432/postgres_cdc?loggerLevel=OFF`. [...] | username | String | Yes | - | Name of the database to use when connecting to the database server. [...] | password | String | Yes | - | Password to use when connecting to the database server. [...] | database-names | List | No | - | Database names to monitor. [...] diff --git a/docs/zh/connectors/source/PostgreSQL-CDC.md b/docs/zh/connectors/source/PostgreSQL-CDC.md index 43c5ba2099..3c626598d9 100644 --- a/docs/zh/connectors/source/PostgreSQL-CDC.md +++ b/docs/zh/connectors/source/PostgreSQL-CDC.md @@ -95,7 +95,7 @@ ALTER TABLE your_table_name REPLICA IDENTITY FULL; | 名称 | 类型 | 必需 | 默认 | 描述 [...] |-------------------------------------------|----------|------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [...] -| url | String | 是 | - | JDBC 连接的 URL。参考案例:`jdbc:postgresql://localhost:5432/postgres_cdc?loggerLevel=OFF`。 [...] +| url | String | 是 | - | JDBC 连接的 URL。该值不能为空或仅包含空白字符。参考案例:`jdbc:postgresql://localhost:5432/postgres_cdc?loggerLevel=OFF`。 [...] | username | String | 是 | - | 连接到数据库服务器时使用的数据库名称。 [...] | password | String | 是 | - | 连接到数据库服务器时使用的密码。 [...] | database-names | List | 否 | - | 需要监控的数据库名称。 [...] diff --git a/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/source/PostgresIncrementalSourceFactory.java b/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/source/PostgresIncrementalSourceFactory.java index b80e54d2b3..ad456fe27d 100644 --- a/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/source/PostgresIncrementalSourceFactory.java +++ b/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/main/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/source/PostgresIncrementalSourceFactory.java @@ -46,6 +46,8 @@ import java.util.Collections; import java.util.List; import java.util.Optional; +import static org.apache.seatunnel.api.configuration.util.Conditions.notBlank; + @AutoService(Factory.class) @Slf4j public class PostgresIncrementalSourceFactory extends BaseChangeStreamTableSourceFactory { @@ -61,10 +63,8 @@ public class PostgresIncrementalSourceFactory extends BaseChangeStreamTableSourc @Override public OptionRule optionRule() { return JdbcSourceOptions.getBaseRule() - .required( - JdbcSourceOptions.USERNAME, - JdbcSourceOptions.PASSWORD, - JdbcCommonOptions.URL) + .required(JdbcSourceOptions.USERNAME, JdbcSourceOptions.PASSWORD) + .required(JdbcCommonOptions.URL, notBlank(JdbcCommonOptions.URL)) .exclusive(ConnectorCommonOptions.TABLE_NAMES, ConnectorCommonOptions.TABLE_PATTERN) .optional( JdbcSourceOptions.DATABASE_NAMES, diff --git a/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/config/PostgresSourceConfigFactoryTest.java b/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/config/PostgresSourceConfigFactoryTest.java index 2ee0b54dd8..657222d392 100644 --- a/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/config/PostgresSourceConfigFactoryTest.java +++ b/seatunnel-connectors-v2/connector-cdc/connector-cdc-postgres/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/config/PostgresSourceConfigFactoryTest.java @@ -29,19 +29,129 @@ import org.apache.seatunnel.connectors.cdc.base.option.StartupMode; import org.apache.seatunnel.connectors.cdc.base.option.StopMode; import org.apache.seatunnel.connectors.seatunnel.cdc.postgres.source.PostgresIncrementalSourceFactory; import org.apache.seatunnel.connectors.seatunnel.cdc.postgres.source.PostgresSourceOptions; +import org.apache.seatunnel.connectors.seatunnel.jdbc.config.JdbcCommonOptions; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import java.util.Collections; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.Properties; +import java.util.stream.Stream; /** Tests the PG-base-backed PostgreSQL source config factory behavior that must stay compatible. */ public class PostgresSourceConfigFactoryTest { + private static final String VALID_URL = "jdbc:postgresql://localhost:5432/postgres_cdc"; + + @ParameterizedTest + @MethodSource("nonblankUrls") + public void shouldAcceptNonblankUrlWithoutChangingValue(String key, String value) { + Map<String, Object> config = validFactoryConfig(); + config.remove("url"); + config.put(key, value); + + ReadonlyConfig readonlyConfig = + Assertions.assertDoesNotThrow(() -> validateFactoryConfig(config)); + + // Factory validation must not trim a supplied URL or test connectivity. + Assertions.assertEquals(value, readonlyConfig.get(JdbcCommonOptions.URL)); + } + + private static Stream<Arguments> nonblankUrls() { + return Stream.of("url", "base-url") + .flatMap( + key -> + Stream.of(VALID_URL, " \t" + VALID_URL + " \r\n") + .map(value -> Arguments.of(key, value))); + } + + @ParameterizedTest + @MethodSource("blankUrls") + public void shouldRejectBlankUrl(String key, String value) { + Map<String, Object> config = validFactoryConfig(); + config.remove("url"); + config.put(key, value); + + assertInvalidUrl(config); + } + + private static Stream<Arguments> blankUrls() { + return Stream.of("url", "base-url") + .flatMap( + key -> + Stream.of("", " ", "\t", "\n", " \t\r\n ") + .map(value -> Arguments.of(key, value))); + } + + @Test + public void shouldRejectMissingUrl() { + Map<String, Object> config = validFactoryConfig(); + config.remove("url"); + + assertInvalidUrl(config); + } + + @Test + public void shouldPreferCanonicalUrlOverBlankFallback() { + Map<String, Object> config = validFactoryConfig(); + config.put("base-url", " "); + + ReadonlyConfig readonlyConfig = + Assertions.assertDoesNotThrow(() -> validateFactoryConfig(config)); + + Assertions.assertEquals(VALID_URL, readonlyConfig.get(JdbcCommonOptions.URL)); + } + + @Test + public void shouldRejectBlankCanonicalUrlWithValidFallback() { + Map<String, Object> config = validFactoryConfig(); + config.put("url", " \t "); + config.put("base-url", VALID_URL); + + assertInvalidUrl(config); + } + + @Test + public void shouldAcceptUrlWithTablePattern() { + Map<String, Object> config = validFactoryConfig(); + config.remove("table-names"); + config.put("table-pattern", "postgres_cdc\\.inventory\\..*"); + + Assertions.assertDoesNotThrow(() -> validateFactoryConfig(config)); + } + + private Map<String, Object> validFactoryConfig() { + Map<String, Object> config = new HashMap<>(); + config.put("url", VALID_URL); + config.put("username", "test-user"); + config.put("password", "test-password"); + config.put("table-names", Collections.singletonList("postgres_cdc.inventory.orders")); + return config; + } + + private ReadonlyConfig validateFactoryConfig(Map<String, Object> config) { + ReadonlyConfig readonlyConfig = ReadonlyConfig.fromMap(config); + OptionRule rule = new PostgresIncrementalSourceFactory().optionRule(); + ConfigValidator.validateUnknownKeys(readonlyConfig, rule, "Postgres-CDC"); + ConfigValidator.of(readonlyConfig).validate(rule); + return readonlyConfig; + } + + private void assertInvalidUrl(Map<String, Object> config) { + OptionValidationException error = + Assertions.assertThrows( + OptionValidationException.class, () -> validateFactoryConfig(config)); + + Assertions.assertTrue(error.getMessage().contains("url"), error.getMessage()); + } + @Test public void shouldDeclareStopModeInRuntimeFactoryRule() { Option<?> stopMode = diff --git a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/PostgresCDCIT.java b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/PostgresCDCIT.java index 106125c90e..34992fc57c 100644 --- a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/PostgresCDCIT.java +++ b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/java/org/apache/seatunnel/connectors/seatunnel/cdc/postgres/PostgresCDCIT.java @@ -28,6 +28,7 @@ import org.apache.seatunnel.e2e.common.container.EngineType; import org.apache.seatunnel.e2e.common.container.TestContainer; import org.apache.seatunnel.e2e.common.junit.DisabledOnContainer; import org.apache.seatunnel.e2e.common.junit.TestContainerExtension; +import org.apache.seatunnel.e2e.common.util.ContainerUtil; import org.apache.seatunnel.e2e.common.util.DependencyJar; import org.apache.seatunnel.e2e.common.util.JobIdGenerator; @@ -306,6 +307,34 @@ public class PostgresCDCIT extends TestSuiteBase implements TestResource { return data; } + @TestTemplate + @DisabledOnContainer( + value = {}, + type = {EngineType.SPARK, EngineType.FLINK}, + disabledReason = "The configuration-only --check command is provided by Zeta") + public void testBlankSourceUrlRejectedByConfigCheck(TestContainer container) + throws IOException, InterruptedException { + String configResource = "/postgrescdc_blank_url.conf"; + container.executeExtraCommands( + engineContainer -> + ContainerUtil.copyConnectorJarToContainer( + engineContainer, + configResource, + "seatunnel-connectors-v2", + "connector-", + "seatunnel", + "/tmp/seatunnel")); + String configPath = "/tmp/seatunnel/config/postgrescdc_blank_url.conf"; + container.copyFileToContainer(configResource, configPath); + + Container.ExecResult result = + container.executeBaseCommand(new String[] {"--config", configPath, "--check"}); + String output = result.getStdout() + result.getStderr(); + + Assertions.assertNotEquals(0, result.getExitCode(), output); + Assertions.assertTrue(output.contains("url") && output.contains("blank"), output); + } + @TestTemplate @DisabledOnContainer( value = {}, diff --git a/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/resources/postgrescdc_blank_url.conf b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/resources/postgrescdc_blank_url.conf new file mode 100644 index 0000000000..5ba6c601a3 --- /dev/null +++ b/seatunnel-e2e/seatunnel-connector-v2-e2e/connector-cdc-postgres-e2e/src/test/resources/postgrescdc_blank_url.conf @@ -0,0 +1,35 @@ +# +# 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. +# + +env { + parallelism = 1 + job.mode = "STREAMING" +} + +source { + Postgres-CDC { + username = "postgres" + password = "postgres" + table-names = ["postgres_cdc.inventory.orders"] + url = " \t\r\n " + } +} + +sink { + Console { + } +}
