From 4bdc24a66b7825035443a17e192982bbde115e69 Mon Sep 17 00:00:00 2001 From: RaigorJiang Date: Thu, 18 Jul 2024 18:15:34 +0800 Subject: [PATCH] Check privilege when register or alter storage unit --- .../core/advice/SQLParseCountAdviceTest.java | 2 +- ...ava => StorageUnitsValidateException.java} | 8 ++--- .../DataSourcePoolPropertiesValidator.java | 31 ++++++++++++++++--- ...DataSourcePoolPropertiesValidatorTest.java | 2 +- .../core/checker/PrivilegeCheckType.java | 2 +- .../resource/AlterStorageUnitExecutor.java | 5 +-- .../resource/RegisterStorageUnitExecutor.java | 3 +- ...tSQLDataSourcePoolPropertiesValidator.java | 19 ++++++++++-- .../AlterStorageUnitExecutorTest.java | 5 +-- .../RegisterStorageUnitExecutorTest.java | 5 +-- .../transaction/util/AutoCommitUtilsTest.java | 9 ++---- .../engine/src/main/antlr4/imports/Keyword.g4 | 4 +++ .../src/main/antlr4/imports/RDLStatement.g4 | 16 ++++++++-- .../kernel/KernelDistSQLStatementVisitor.java | 17 ++++++++-- .../unit/type/AlterStorageUnitStatement.java | 2 ++ .../type/RegisterStorageUnitStatement.java | 2 ++ 16 files changed, 100 insertions(+), 32 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/{StorageUnitsConnectException.java => StorageUnitsValidateException.java} (82%) diff --git a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java index 661025e35435c..da5395622a963 100644 --- a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java +++ b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdviceTest.java @@ -102,7 +102,7 @@ void assertParseRQL() { @Test void assertParseRDL() { - assertParse(new RegisterStorageUnitStatement(false, Collections.emptyList()), "RDL=1"); + assertParse(new RegisterStorageUnitStatement(false, Collections.emptyList(), Collections.emptySet()), "RDL=1"); } @Test diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java similarity index 82% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java index 95cdd19514e92..80ccacc50ab9b 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsConnectException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsValidateException.java @@ -24,14 +24,14 @@ import java.util.stream.Collectors; /** - * Storage units connect exception. + * Storage units validate exception. */ -public final class StorageUnitsConnectException extends ResourceDefinitionException { +public final class StorageUnitsValidateException extends ResourceDefinitionException { private static final long serialVersionUID = 1824912697040264268L; - public StorageUnitsConnectException(final Map causes) { - super(XOpenSQLState.CONNECTION_EXCEPTION, 10, "Storage units can not connect, error messages are: %s.", causes.entrySet().stream().map(entry -> String.format( + public StorageUnitsValidateException(final Map causes) { + super(XOpenSQLState.CONNECTION_EXCEPTION, 10, "Storage units validate error, messages are: %s.", causes.entrySet().stream().map(entry -> String.format( "Storage unit name: '%s', error message is: %s", entry.getKey(), entry.getValue().getMessage())).collect(Collectors.joining(System.lineSeparator()))); } } diff --git a/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java b/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java index 370fb39ef01f1..c2ae0a408fcde 100644 --- a/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java +++ b/infra/data-source-pool/core/src/main/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidator.java @@ -19,6 +19,11 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +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.database.core.type.DatabaseTypeFactory; import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; @@ -27,9 +32,11 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; /** * Data source pool properties validator. @@ -41,14 +48,15 @@ public final class DataSourcePoolPropertiesValidator { * Validate data source pool properties map. * * @param propsMap data source pool properties map + * @param expectedPrivileges excepted privileges * @return data source name and exception map */ - public static Map validate(final Map propsMap) { + public static Map validate(final Map propsMap, final Collection expectedPrivileges) { Map result = new LinkedHashMap<>(propsMap.size(), 1F); for (Entry entry : propsMap.entrySet()) { try { validateProperties(entry.getKey(), entry.getValue()); - validateConnection(entry.getKey(), entry.getValue()); + validateConnection(entry.getKey(), entry.getValue(), expectedPrivileges); } catch (final InvalidDataSourcePoolPropertiesException ex) { result.put(entry.getKey(), ex); } @@ -64,11 +72,16 @@ private static void validateProperties(final String dataSourceName, final DataSo } } - private static void validateConnection(final String dataSourceName, final DataSourcePoolProperties props) throws InvalidDataSourcePoolPropertiesException { + private static void validateConnection(final String dataSourceName, final DataSourcePoolProperties props, + final Collection expectedPrivileges) throws InvalidDataSourcePoolPropertiesException { DataSource dataSource = null; try { dataSource = DataSourcePoolCreator.create(props); - checkFailFast(dataSource); + if (expectedPrivileges.isEmpty() || expectedPrivileges.contains(PrivilegeCheckType.NONE)) { + checkFailFast(dataSource); + return; + } + checkPrivileges(dataSource, props, expectedPrivileges); // CHECKSTYLE:OFF } catch (final SQLException | RuntimeException ex) { // CHECKSTYLE:ON @@ -87,4 +100,14 @@ private static void checkFailFast(final DataSource dataSource) throws SQLExcepti // CHECKSTYLE:ON } } + + private static void checkPrivileges(final DataSource dataSource, final DataSourcePoolProperties props, final Collection expectedPrivileges) { + DatabaseType databaseType = DatabaseTypeFactory.get((String) props.getConnectionPropertySynonyms().getStandardProperties().get("url")); + Optional checker = DatabaseTypedSPILoader.findService(DialectDatabaseEnvironmentChecker.class, databaseType); + if (checker.isPresent()) { + for (PrivilegeCheckType each : expectedPrivileges) { + checker.get().checkPrivilege(dataSource, each); + } + } + } } diff --git a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java index 548c50901f895..cdbaae15bffc2 100644 --- a/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java +++ b/infra/data-source-pool/core/src/test/java/org/apache/shardingsphere/infra/datasource/pool/props/validator/DataSourcePoolPropertiesValidatorTest.java @@ -37,6 +37,6 @@ static void setUp() throws ClassNotFoundException { @Test void assertValidate() { assertTrue(DataSourcePoolPropertiesValidator.validate( - Collections.singletonMap("name", new DataSourcePoolProperties(HikariDataSource.class.getName(), Collections.singletonMap("jdbcUrl", "jdbc:mock")))).isEmpty()); + Collections.singletonMap("name", new DataSourcePoolProperties(HikariDataSource.class.getName(), Collections.singletonMap("jdbcUrl", "jdbc:mock"))), Collections.emptySet()).isEmpty()); } } diff --git a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java index f670118d7cb19..8ce96c4b46868 100644 --- a/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java +++ b/infra/database/core/src/main/java/org/apache/shardingsphere/infra/database/core/checker/PrivilegeCheckType.java @@ -22,5 +22,5 @@ */ public enum PrivilegeCheckType { - PIPELINE, SELECT, XA + NONE, PIPELINE, SELECT, XA } diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java index 82791ab55c842..e2d93881af999 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment; import org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter; import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement; +import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType; import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties; import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl; import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser; @@ -35,8 +36,8 @@ import org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException; import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.AlterStorageUnitConnectionInfoException; import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.DuplicateStorageUnitException; -import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsOperateException; import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.MissingRequiredStorageUnitsException; +import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsOperateException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -64,7 +65,7 @@ public final class AlterStorageUnitExecutor implements DistSQLUpdateExecutor propsMap = DataSourceSegmentsConverter.convert(database.getProtocolType(), sqlStatement.getStorageUnits()); - validateHandler.validate(propsMap); + validateHandler.validate(propsMap, sqlStatement.getExpectedPrivileges().stream().map(each -> PrivilegeCheckType.valueOf(each.toUpperCase())).collect(Collectors.toSet())); try { MetaDataContexts originalMetaDataContexts = contextManager.getMetaDataContexts(); contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().alterStorageUnits(database.getName(), propsMap); diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java index a0e15507fa1aa..f2bc21fa4ece1 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java @@ -25,6 +25,7 @@ import org.apache.shardingsphere.distsql.segment.DataSourceSegment; import org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter; import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement; +import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException; @@ -66,7 +67,7 @@ public void executeUpdate(final RegisterStorageUnitStatement sqlStatement, final if (propsMap.isEmpty()) { return; } - validateHandler.validate(propsMap); + validateHandler.validate(propsMap, sqlStatement.getExpectedPrivileges().stream().map(each -> PrivilegeCheckType.valueOf(each.toUpperCase())).collect(Collectors.toSet())); try { MetaDataContexts originalMetaDataContexts = contextManager.getMetaDataContexts(); contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().registerStorageUnits(database.getName(), propsMap); diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java index f9778b3b0d48c..5ec0180e80a4e 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/validate/DistSQLDataSourcePoolPropertiesValidator.java @@ -17,11 +17,14 @@ package org.apache.shardingsphere.distsql.handler.validate; +import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.datasource.pool.props.validator.DataSourcePoolPropertiesValidator; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; -import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsConnectException; +import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.StorageUnitsValidateException; +import java.util.Collection; +import java.util.Collections; import java.util.Map; /** @@ -35,7 +38,17 @@ public final class DistSQLDataSourcePoolPropertiesValidator { * @param propsMap data source pool properties map */ public void validate(final Map propsMap) { - Map exceptions = DataSourcePoolPropertiesValidator.validate(propsMap); - ShardingSpherePreconditions.checkMustEmpty(exceptions, () -> new StorageUnitsConnectException(exceptions)); + validate(propsMap, Collections.emptySet()); + } + + /** + * Validate data source properties map. + * + * @param propsMap data source pool properties map + * @param expectedPrivileges expected privileges + */ + public void validate(final Map propsMap, final Collection expectedPrivileges) { + Map exceptions = DataSourcePoolPropertiesValidator.validate(propsMap, expectedPrivileges); + ShardingSpherePreconditions.checkMustEmpty(exceptions, () -> new StorageUnitsValidateException(exceptions)); } } diff --git a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java index 1d2a5f9c46c68..c9c47ae891da8 100644 --- a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java +++ b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutorTest.java @@ -103,13 +103,14 @@ private ContextManager mockContextManager(final MetaDataContexts metaDataContext } private AlterStorageUnitStatement createAlterStorageUnitStatement(final String resourceName) { - return new AlterStorageUnitStatement(Collections.singleton(new URLBasedDataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0", "root", "", new Properties()))); + return new AlterStorageUnitStatement(Collections.singleton(new URLBasedDataSourceSegment(resourceName, "jdbc:mysql://127.0.0.1:3306/ds_0", "root", "", new Properties())), + Collections.emptySet()); } private AlterStorageUnitStatement createAlterStorageUnitStatementWithDuplicateStorageUnitNames() { return new AlterStorageUnitStatement(Arrays.asList( new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()), - new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties()))); + new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())), Collections.emptySet()); } private ConnectionProperties mockConnectionProperties(final String catalog) { diff --git a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java index fffdbfe5a3f52..e06efe08be850 100644 --- a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java +++ b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutorTest.java @@ -91,12 +91,13 @@ void assertExecuteUpdateWithDuplicateStorageUnitNamesWithDataSourceContainedRule } private RegisterStorageUnitStatement createRegisterStorageUnitStatement() { - return new RegisterStorageUnitStatement(false, Collections.singleton(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/test0", "root", "", new Properties()))); + return new RegisterStorageUnitStatement(false, Collections.singleton(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/test0", "root", "", new Properties())), + Collections.emptySet()); } private RegisterStorageUnitStatement createRegisterStorageUnitStatementWithDuplicateStorageUnitNames() { return new RegisterStorageUnitStatement(false, Arrays.asList( new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()), - new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties()))); + new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())), Collections.emptySet()); } } diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java index 5a53315e76581..249c5ec0efc8f 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/util/AutoCommitUtilsTest.java @@ -19,18 +19,15 @@ import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement; import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; -import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment; import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement; -import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; import org.apache.shardingsphere.sql.parser.statement.mysql.ddl.MySQLCreateTableStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLInsertStatement; import org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement; import org.junit.jupiter.api.Test; -import java.util.LinkedList; - import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; class AutoCommitUtilsTest { @@ -38,7 +35,7 @@ class AutoCommitUtilsTest { void assertNeedOpenTransactionForSelectStatement() { SelectStatement selectStatement = new MySQLSelectStatement(); assertFalse(AutoCommitUtils.needOpenTransaction(selectStatement)); - selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 5, new IdentifierValue("foo")))); + selectStatement.setFrom(mock(SimpleTableSegment.class)); assertTrue(AutoCommitUtils.needOpenTransaction(selectStatement)); } @@ -50,6 +47,6 @@ void assertNeedOpenTransactionForDDLOrDMLStatement() { @Test void assertNeedOpenTransactionForOtherStatement() { - assertFalse(AutoCommitUtils.needOpenTransaction(new RegisterStorageUnitStatement(false, new LinkedList<>()))); + assertFalse(AutoCommitUtils.needOpenTransaction(mock(RegisterStorageUnitStatement.class))); } } diff --git a/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4 b/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4 index e2bdb512195b1..6f096e6417f40 100644 --- a/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4 +++ b/parser/distsql/engine/src/main/antlr4/imports/Keyword.g4 @@ -346,3 +346,7 @@ ALGORITHM FORCE : F O R C E ; + +CHECK_PRIVILEGES + : C H E C K UL_ P R I V I L E G E S + ; diff --git a/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4 b/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4 index 418dd843b7c64..5dfb4f7a0bd71 100644 --- a/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4 +++ b/parser/distsql/engine/src/main/antlr4/imports/RDLStatement.g4 @@ -20,17 +20,21 @@ grammar RDLStatement; import BaseRule; registerStorageUnit - : REGISTER STORAGE UNIT ifNotExists? storageUnitDefinition (COMMA_ storageUnitDefinition)* + : REGISTER STORAGE UNIT ifNotExists? storageUnitsDefinition (COMMA_ checkPrivileges)? ; alterStorageUnit - : ALTER STORAGE UNIT storageUnitDefinition (COMMA_ storageUnitDefinition)* + : ALTER STORAGE UNIT storageUnitsDefinition (COMMA_ checkPrivileges)? ; unregisterStorageUnit : UNREGISTER STORAGE UNIT ifExists? storageUnitName (COMMA_ storageUnitName)* ignoreTables? ; +storageUnitsDefinition + : storageUnitDefinition (COMMA_ storageUnitDefinition)* + ; + storageUnitDefinition : storageUnitName LP_ (simpleSource | urlSource) COMMA_ USER EQ_ user (COMMA_ PASSWORD EQ_ password)? (COMMA_ propertiesDefinition)? RP_ ; @@ -80,3 +84,11 @@ ifExists ifNotExists : IF NOT EXISTS ; + +checkPrivileges + : CHECK_PRIVILEGES EQ_ privilegeType (COMMA_ privilegeType)* + ; + +privilegeType + : IDENTIFIER_ + ; diff --git a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java index 83dde189cd16a..fa7113f6acc92 100644 --- a/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java +++ b/parser/distsql/engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlgorithmDefinitionContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterComputeNodeContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.AlterStorageUnitContext; +import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.CheckPrivilegesContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ConvertYamlConfigurationContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DatabaseNameContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DisableComputeNodeContext; @@ -58,6 +59,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowStorageUnitsContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ShowTableMetadataContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitDefinitionContext; +import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.StorageUnitsDefinitionContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlabelComputeNodeContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnlockClusterContext; import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.UnregisterStorageUnitContext; @@ -83,8 +85,8 @@ import org.apache.shardingsphere.distsql.statement.ral.updatable.LockClusterStatement; import org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshDatabaseMetaDataStatement; import org.apache.shardingsphere.distsql.statement.ral.updatable.RefreshTableMetaDataStatement; -import org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement; import org.apache.shardingsphere.distsql.statement.ral.updatable.SetComputeNodeStateStatement; +import org.apache.shardingsphere.distsql.statement.ral.updatable.SetDistVariableStatement; import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlabelComputeNodeStatement; import org.apache.shardingsphere.distsql.statement.ral.updatable.UnlockClusterStatement; import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.AlterStorageUnitStatement; @@ -100,6 +102,7 @@ import org.apache.shardingsphere.sql.parser.statement.core.value.literal.impl.StringLiteralValue; import java.util.Collection; +import java.util.Collections; import java.util.Objects; import java.util.Properties; import java.util.stream.Collectors; @@ -111,12 +114,20 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB @Override public ASTNode visitRegisterStorageUnit(final RegisterStorageUnitContext ctx) { - return new RegisterStorageUnitStatement(null != ctx.ifNotExists(), ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList())); + return new RegisterStorageUnitStatement(null != ctx.ifNotExists(), getStorageUnits(ctx.storageUnitsDefinition()), getExpectedPrivileges(ctx.checkPrivileges())); } @Override public ASTNode visitAlterStorageUnit(final AlterStorageUnitContext ctx) { - return new AlterStorageUnitStatement(ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList())); + return new AlterStorageUnitStatement(getStorageUnits(ctx.storageUnitsDefinition()), getExpectedPrivileges(ctx.checkPrivileges())); + } + + private Collection getStorageUnits(final StorageUnitsDefinitionContext ctx) { + return ctx.storageUnitDefinition().stream().map(each -> (DataSourceSegment) visit(each)).collect(Collectors.toList()); + } + + private Collection getExpectedPrivileges(final CheckPrivilegesContext ctx) { + return null == ctx ? Collections.emptySet() : ctx.privilegeType().stream().map(this::getIdentifierValue).collect(Collectors.toSet()); } @Override diff --git a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java index 7a26c867c4d26..0bf3cb2f5066f 100644 --- a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java +++ b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/AlterStorageUnitStatement.java @@ -32,4 +32,6 @@ public final class AlterStorageUnitStatement extends StorageUnitDefinitionStatement { private final Collection storageUnits; + + private final Collection expectedPrivileges; } diff --git a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java index 84795ad9340e2..90418b0d6009c 100644 --- a/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java +++ b/parser/distsql/statement/src/main/java/org/apache/shardingsphere/distsql/statement/rdl/resource/unit/type/RegisterStorageUnitStatement.java @@ -34,4 +34,6 @@ public final class RegisterStorageUnitStatement extends StorageUnitDefinitionSta private final boolean ifNotExists; private final Collection storageUnits; + + private final Collection expectedPrivileges; }