From 100fc5be09a02177abf8dc77fe078ddb2eb4c0fa Mon Sep 17 00:00:00 2001 From: Raigor Date: Wed, 17 Jul 2024 18:41:25 +0800 Subject: [PATCH] Remove DialectDataSourceChecker in pipeline module (#32152) * Remove DialectDataSourceChecker in pipeline module * Remove DialectDataSourceChecker in pipeline module --- ...tgreSQLDatabaseEnvironmentCheckerTest.java | 19 +-- .../core/checker/DataSourceCheckEngine.java | 8 +- .../checker/DialectDataSourceChecker.java | 44 ------- .../datasource/MySQLDataSourceChecker.java | 108 ----------------- ...line.core.checker.DialectDataSourceChecker | 18 --- .../MySQLDataSourceCheckerTest.java | 109 ------------------ .../OpenGaussDataSourceChecker.java | 70 ----------- ...line.core.checker.DialectDataSourceChecker | 18 --- .../PostgreSQLDataSourceChecker.java | 72 ------------ ...line.core.checker.DialectDataSourceChecker | 18 --- .../h2/checker/H2DataSourceChecker.java | 41 ------- ...line.core.checker.DialectDataSourceChecker | 18 --- 12 files changed, 15 insertions(+), 528 deletions(-) rename kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceCheckerTest.java => infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentCheckerTest.java (76%) delete mode 100644 kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DialectDataSourceChecker.java delete mode 100644 kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceChecker.java delete mode 100644 kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker delete mode 100644 kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceCheckerTest.java delete mode 100644 kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java delete mode 100644 kernel/data-pipeline/dialect/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker delete mode 100644 kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java delete mode 100644 kernel/data-pipeline/dialect/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker delete mode 100644 test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/fixture/h2/checker/H2DataSourceChecker.java delete mode 100644 test/it/pipeline/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker diff --git a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceCheckerTest.java b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentCheckerTest.java similarity index 76% rename from kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceCheckerTest.java rename to infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentCheckerTest.java index 17471ac564385..96ba13810a522 100644 --- a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceCheckerTest.java +++ b/infra/database/type/postgresql/src/test/java/org/apache/shardingsphere/infra/database/postgresql/checker/PostgreSQLDatabaseEnvironmentCheckerTest.java @@ -15,9 +15,10 @@ * limitations under the License. */ -package org.apache.shardingsphere.data.pipeline.postgresql.check.datasource; +package org.apache.shardingsphere.infra.database.postgresql.checker; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException; +import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType; +import org.apache.shardingsphere.infra.database.core.exception.MissingRequiredPrivilegeException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -38,7 +39,7 @@ import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class PostgreSQLDataSourceCheckerTest { +class PostgreSQLDatabaseEnvironmentCheckerTest { @Mock private DataSource dataSource; @@ -67,28 +68,28 @@ void setUp() throws SQLException { @Test void assertCheckRolReplication() throws SQLException { - PostgreSQLDataSourceChecker dataSourceChecker = new PostgreSQLDataSourceChecker(); + PostgreSQLDatabaseEnvironmentChecker dataSourceChecker = new PostgreSQLDatabaseEnvironmentChecker(); when(resultSet.getString("rolreplication")).thenReturn("t"); when(resultSet.getString("rolsuper")).thenReturn("f"); - dataSourceChecker.checkPrivilege(dataSource); + dataSourceChecker.checkPrivilege(dataSource, PrivilegeCheckType.PIPELINE); verify(resultSet, atLeastOnce()).getString("rolsuper"); } @Test void assertCheckRolSuper() throws SQLException { - PostgreSQLDataSourceChecker dataSourceChecker = new PostgreSQLDataSourceChecker(); + PostgreSQLDatabaseEnvironmentChecker dataSourceChecker = new PostgreSQLDatabaseEnvironmentChecker(); when(resultSet.getString("rolsuper")).thenReturn("t"); when(resultSet.getString("rolreplication")).thenReturn("f"); - dataSourceChecker.checkPrivilege(dataSource); + dataSourceChecker.checkPrivilege(dataSource, PrivilegeCheckType.PIPELINE); verify(resultSet, atLeastOnce()).getString("rolreplication"); } @Test void assertCheckNoPrivilege() throws SQLException { - PostgreSQLDataSourceChecker dataSourceChecker = new PostgreSQLDataSourceChecker(); + PostgreSQLDatabaseEnvironmentChecker dataSourceChecker = new PostgreSQLDatabaseEnvironmentChecker(); when(resultSet.getString("rolsuper")).thenReturn("f"); when(resultSet.getString("rolreplication")).thenReturn("f"); - assertThrows(PrepareJobWithoutEnoughPrivilegeException.class, () -> dataSourceChecker.checkPrivilege(dataSource)); + assertThrows(MissingRequiredPrivilegeException.class, () -> dataSourceChecker.checkPrivilege(dataSource, PrivilegeCheckType.PIPELINE)); verify(resultSet, atLeastOnce()).getString("rolreplication"); } } diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DataSourceCheckEngine.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DataSourceCheckEngine.java index 55a55d100588d..890626556692b 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DataSourceCheckEngine.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DataSourceCheckEngine.java @@ -20,6 +20,8 @@ import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithTargetTableNotEmptyException; import org.apache.shardingsphere.data.pipeline.core.importer.ImporterConfiguration; import org.apache.shardingsphere.data.pipeline.core.sqlbuilder.sql.PipelinePrepareSQLBuilder; +import org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker; +import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType; import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; @@ -38,12 +40,12 @@ */ public final class DataSourceCheckEngine { - private final DialectDataSourceChecker checker; + private final DialectDatabaseEnvironmentChecker checker; private final PipelinePrepareSQLBuilder sqlBuilder; public DataSourceCheckEngine(final DatabaseType databaseType) { - checker = DatabaseTypedSPILoader.findService(DialectDataSourceChecker.class, databaseType).orElse(null); + checker = DatabaseTypedSPILoader.findService(DialectDatabaseEnvironmentChecker.class, databaseType).orElse(null); sqlBuilder = new PipelinePrepareSQLBuilder(databaseType); } @@ -73,7 +75,7 @@ public void checkSourceDataSources(final Collection dataSources) { if (null == checker) { return; } - dataSources.forEach(checker::checkPrivilege); + dataSources.forEach(each -> checker.checkPrivilege(each, PrivilegeCheckType.PIPELINE)); dataSources.forEach(checker::checkVariable); } diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DialectDataSourceChecker.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DialectDataSourceChecker.java deleted file mode 100644 index 946f6b53e712f..0000000000000 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/DialectDataSourceChecker.java +++ /dev/null @@ -1,44 +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.shardingsphere.data.pipeline.core.checker; - -import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI; -import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; - -import javax.sql.DataSource; - -/** - * Dialect data source checker. - */ -@SingletonSPI -public interface DialectDataSourceChecker extends DatabaseTypedSPI { - - /** - * Check user privileges. - * - * @param dataSource data source to be checked - */ - void checkPrivilege(DataSource dataSource); - - /** - * Check variables. - * - * @param dataSource data source to be checked - */ - void checkVariable(DataSource dataSource); -} diff --git a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceChecker.java b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceChecker.java deleted file mode 100644 index acf445a48adcf..0000000000000 --- a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceChecker.java +++ /dev/null @@ -1,108 +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.shardingsphere.data.pipeline.mysql.check.datasource; - -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithInvalidSourceDataSourceException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException; -import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -/** - * Data source checker for MySQL. - */ -public final class MySQLDataSourceChecker implements DialectDataSourceChecker { - - private static final String SHOW_GRANTS_SQL = "SHOW GRANTS"; - - // BINLOG MONITOR is a synonym for REPLICATION CLIENT for MariaDB - private static final String[][] REQUIRED_PRIVILEGES = {{"ALL PRIVILEGES", "ON *.*"}, {"REPLICATION SLAVE", "REPLICATION CLIENT", "ON *.*"}, {"REPLICATION SLAVE", "BINLOG MONITOR", "ON *.*"}}; - - private static final Map REQUIRED_VARIABLES = new HashMap<>(3, 1F); - - private static final String SHOW_VARIABLES_SQL; - - static { - REQUIRED_VARIABLES.put("LOG_BIN", "ON"); - REQUIRED_VARIABLES.put("BINLOG_FORMAT", "ROW"); - // It does not exist in all versions of MySQL - REQUIRED_VARIABLES.put("BINLOG_ROW_IMAGE", "FULL"); - SHOW_VARIABLES_SQL = String.format("SHOW VARIABLES WHERE Variable_name IN (%s)", REQUIRED_VARIABLES.keySet().stream().map(each -> "?").collect(Collectors.joining(","))); - } - - @Override - public void checkPrivilege(final DataSource dataSource) { - try ( - Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SHOW_GRANTS_SQL); - ResultSet resultSet = preparedStatement.executeQuery()) { - while (resultSet.next()) { - String privilege = resultSet.getString(1).toUpperCase(); - if (matchPrivileges(privilege)) { - return; - } - } - } catch (final SQLException ex) { - throw new PrepareJobWithCheckPrivilegeFailedException(ex); - } - throw new PrepareJobWithoutEnoughPrivilegeException(Arrays.asList("REPLICATION SLAVE", "REPLICATION CLIENT")); - } - - private boolean matchPrivileges(final String privilege) { - return Arrays.stream(REQUIRED_PRIVILEGES).anyMatch(each -> Arrays.stream(each).allMatch(privilege::contains)); - } - - @Override - public void checkVariable(final DataSource dataSource) { - try ( - Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SHOW_VARIABLES_SQL)) { - int parameterIndex = 1; - for (Entry entry : REQUIRED_VARIABLES.entrySet()) { - preparedStatement.setString(parameterIndex++, entry.getKey()); - } - try (ResultSet resultSet = preparedStatement.executeQuery()) { - while (resultSet.next()) { - String key = resultSet.getString(1).toUpperCase(); - String expectedValue = REQUIRED_VARIABLES.get(key); - String actualValue = resultSet.getString(2); - ShardingSpherePreconditions.checkState(expectedValue.equalsIgnoreCase(actualValue), - () -> new PrepareJobWithInvalidSourceDataSourceException(key, expectedValue, actualValue)); - } - } - } catch (final SQLException ex) { - throw new PrepareJobWithCheckPrivilegeFailedException(ex); - } - } - - @Override - public String getDatabaseType() { - return "MySQL"; - } -} diff --git a/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker b/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker deleted file mode 100644 index 0e0c299b17a44..0000000000000 --- a/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker +++ /dev/null @@ -1,18 +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. -# - -org.apache.shardingsphere.data.pipeline.mysql.check.datasource.MySQLDataSourceChecker diff --git a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceCheckerTest.java b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceCheckerTest.java deleted file mode 100644 index 513e1462bff15..0000000000000 --- a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/check/datasource/MySQLDataSourceCheckerTest.java +++ /dev/null @@ -1,109 +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.shardingsphere.data.pipeline.mysql.check.datasource; - -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithInvalidSourceDataSourceException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import javax.sql.DataSource; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@ExtendWith(MockitoExtension.class) -class MySQLDataSourceCheckerTest { - - @Mock - private PreparedStatement preparedStatement; - - @Mock - private ResultSet resultSet; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private DataSource dataSource; - - @BeforeEach - void setUp() throws SQLException { - when(dataSource.getConnection().prepareStatement(anyString())).thenReturn(preparedStatement); - when(preparedStatement.executeQuery()).thenReturn(resultSet); - } - - @Test - void assertCheckPrivilegeWithParticularSuccess() throws SQLException { - when(resultSet.next()).thenReturn(true); - when(resultSet.getString(1)).thenReturn("GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%'@'%'"); - new MySQLDataSourceChecker().checkPrivilege(dataSource); - verify(preparedStatement).executeQuery(); - } - - @Test - void assertCheckPrivilegeWithAllSuccess() throws SQLException { - when(resultSet.next()).thenReturn(true); - when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES CLIENT ON *.* TO '%'@'%'"); - new MySQLDataSourceChecker().checkPrivilege(dataSource); - verify(preparedStatement).executeQuery(); - } - - @Test - void assertCheckPrivilegeLackPrivileges() { - assertThrows(PrepareJobWithoutEnoughPrivilegeException.class, () -> new MySQLDataSourceChecker().checkPrivilege(dataSource)); - } - - @Test - void assertCheckPrivilegeFailure() throws SQLException { - when(resultSet.next()).thenThrow(new SQLException("")); - assertThrows(PrepareJobWithCheckPrivilegeFailedException.class, () -> new MySQLDataSourceChecker().checkPrivilege(dataSource)); - } - - @Test - void assertCheckVariableSuccess() throws SQLException { - when(resultSet.next()).thenReturn(true, true, true, false); - when(resultSet.getString(1)).thenReturn("LOG_BIN", "BINLOG_FORMAT", "BINLOG_ROW_IMAGE"); - when(resultSet.getString(2)).thenReturn("ON", "ROW", "FULL"); - assertDoesNotThrow(() -> new MySQLDataSourceChecker().checkVariable(dataSource)); - verify(preparedStatement, times(1)).executeQuery(); - } - - @Test - void assertCheckVariableWithWrongVariable() throws SQLException { - when(resultSet.next()).thenReturn(true, true, false); - when(resultSet.getString(1)).thenReturn("BINLOG_FORMAT", "LOG_BIN"); - when(resultSet.getString(2)).thenReturn("ROW", "OFF"); - assertThrows(PrepareJobWithInvalidSourceDataSourceException.class, () -> new MySQLDataSourceChecker().checkVariable(dataSource)); - } - - @Test - void assertCheckVariableFailure() throws SQLException { - when(resultSet.next()).thenThrow(new SQLException("")); - assertThrows(PrepareJobWithCheckPrivilegeFailedException.class, () -> new MySQLDataSourceChecker().checkVariable(dataSource)); - } -} diff --git a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java deleted file mode 100644 index c098fe3c58036..0000000000000 --- a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/check/datasource/OpenGaussDataSourceChecker.java +++ /dev/null @@ -1,70 +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.shardingsphere.data.pipeline.opengauss.check.datasource; - -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException; -import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Collections; - -/** - * Data source checker of openGauss. - */ -public final class OpenGaussDataSourceChecker implements DialectDataSourceChecker { - - private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles WHERE rolname = ?"; - - @Override - public void checkPrivilege(final DataSource dataSource) { - try ( - Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SHOW_GRANTS_SQL)) { - DatabaseMetaData metaData = connection.getMetaData(); - preparedStatement.setString(1, metaData.getUserName()); - try (ResultSet resultSet = preparedStatement.executeQuery()) { - String username = metaData.getUserName(); - ShardingSpherePreconditions.checkState(resultSet.next(), () -> new PrepareJobWithoutUserException(username)); - String isSuperRole = resultSet.getString("rolsuper"); - String isReplicationRole = resultSet.getString("rolreplication"); - String isSystemAdminRole = resultSet.getString("rolsystemadmin"); - ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) || "t".equalsIgnoreCase(isReplicationRole) || "t".equalsIgnoreCase(isSystemAdminRole), - () -> new PrepareJobWithoutEnoughPrivilegeException(Collections.singleton("REPLICATION"))); - } - } catch (final SQLException ex) { - throw new PrepareJobWithCheckPrivilegeFailedException(ex); - } - } - - @Override - public void checkVariable(final DataSource dataSource) { - } - - @Override - public String getDatabaseType() { - return "openGauss"; - } -} diff --git a/kernel/data-pipeline/dialect/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker b/kernel/data-pipeline/dialect/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker deleted file mode 100644 index ace6b9edc7a5f..0000000000000 --- a/kernel/data-pipeline/dialect/opengauss/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker +++ /dev/null @@ -1,18 +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. -# - -org.apache.shardingsphere.data.pipeline.opengauss.check.datasource.OpenGaussDataSourceChecker diff --git a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java deleted file mode 100644 index b51f9ba14cc26..0000000000000 --- a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/check/datasource/PostgreSQLDataSourceChecker.java +++ /dev/null @@ -1,72 +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.shardingsphere.data.pipeline.postgresql.check.datasource; - -import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithCheckPrivilegeFailedException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutEnoughPrivilegeException; -import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithoutUserException; -import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; - -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.DatabaseMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Collections; - -/** - * PostgreSQL Data source checker. - */ -@Slf4j -public final class PostgreSQLDataSourceChecker implements DialectDataSourceChecker { - - private static final String SHOW_GRANTS_SQL = "SELECT * FROM pg_roles WHERE rolname = ?"; - - @Override - public void checkPrivilege(final DataSource dataSource) { - try ( - Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(SHOW_GRANTS_SQL)) { - DatabaseMetaData metaData = connection.getMetaData(); - preparedStatement.setString(1, metaData.getUserName()); - try (ResultSet resultSet = preparedStatement.executeQuery()) { - String username = metaData.getUserName(); - ShardingSpherePreconditions.checkState(resultSet.next(), () -> new PrepareJobWithoutUserException(username)); - String isSuperRole = resultSet.getString("rolsuper"); - String isReplicationRole = resultSet.getString("rolreplication"); - log.info("checkPrivilege: isSuperRole: {}, isReplicationRole: {}", isSuperRole, isReplicationRole); - ShardingSpherePreconditions.checkState("t".equalsIgnoreCase(isSuperRole) || "t".equalsIgnoreCase(isReplicationRole), - () -> new PrepareJobWithoutEnoughPrivilegeException(Collections.singleton("REPLICATION"))); - } - } catch (final SQLException ex) { - throw new PrepareJobWithCheckPrivilegeFailedException(ex); - } - } - - @Override - public void checkVariable(final DataSource dataSource) { - } - - @Override - public String getDatabaseType() { - return "PostgreSQL"; - } -} diff --git a/kernel/data-pipeline/dialect/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker b/kernel/data-pipeline/dialect/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker deleted file mode 100644 index 9125b0d20f9e0..0000000000000 --- a/kernel/data-pipeline/dialect/postgresql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker +++ /dev/null @@ -1,18 +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. -# - -org.apache.shardingsphere.data.pipeline.postgresql.check.datasource.PostgreSQLDataSourceChecker diff --git a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/fixture/h2/checker/H2DataSourceChecker.java b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/fixture/h2/checker/H2DataSourceChecker.java deleted file mode 100644 index a6b4aab6dbd82..0000000000000 --- a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/fixture/h2/checker/H2DataSourceChecker.java +++ /dev/null @@ -1,41 +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.shardingsphere.test.it.data.pipeline.core.fixture.h2.checker; - -import org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker; - -import javax.sql.DataSource; - -/** - * Data source checker for H2. - */ -public final class H2DataSourceChecker implements DialectDataSourceChecker { - - @Override - public void checkPrivilege(final DataSource dataSource) { - } - - @Override - public void checkVariable(final DataSource dataSource) { - } - - @Override - public String getDatabaseType() { - return "H2"; - } -} diff --git a/test/it/pipeline/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker b/test/it/pipeline/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker deleted file mode 100644 index 373a49fdbf46b..0000000000000 --- a/test/it/pipeline/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.core.checker.DialectDataSourceChecker +++ /dev/null @@ -1,18 +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. -# - -org.apache.shardingsphere.test.it.data.pipeline.core.fixture.h2.checker.H2DataSourceChecker