This is an automated email from the ASF dual-hosted git repository. jackietien pushed a commit to branch AuthEnhance in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit e034ae304d89b9546eb4238b5a76c7ccc6e116e8 Author: JackieTien97 <[email protected]> AuthorDate: Wed Sep 17 16:45:34 2025 +0800 finish template related --- .../iotdb/db/it/auth/IoTDBTemplateAuthIT.java | 558 +++++++++++++++++++++ .../queryengine/plan/analyze/AnalyzeVisitor.java | 21 - .../config/executor/ClusterConfigTaskExecutor.java | 30 +- .../template/ShowNodesInSchemaTemplateTask.java | 47 +- .../metadata/template/ShowPathSetTemplateTask.java | 20 +- .../security/TreeAccessCheckVisitor.java | 4 +- ...atement.java => AbstractShowExactTemplate.java} | 21 +- .../ShowNodesInSchemaTemplateStatement.java | 24 +- .../template/ShowPathSetTemplateStatement.java | 24 +- .../template/ShowSchemaTemplateStatement.java | 11 + 10 files changed, 634 insertions(+), 126 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBTemplateAuthIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBTemplateAuthIT.java new file mode 100644 index 00000000000..5244287ef7a --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/auth/IoTDBTemplateAuthIT.java @@ -0,0 +1,558 @@ +/* + * 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.iotdb.db.it.auth; + +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.ClusterIT; +import org.apache.iotdb.itbase.category.LocalStandaloneIT; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.CHILD_NODES; +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.PATHS; +import static org.apache.iotdb.commons.schema.column.ColumnHeaderConstant.TEMPLATE_NAME; +import static org.apache.iotdb.db.it.utils.TestUtils.assertNonQueryTestFail; +import static org.apache.iotdb.db.it.utils.TestUtils.executeNonQuery; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +@RunWith(IoTDBTestRunner.class) +@Category({LocalStandaloneIT.class, ClusterIT.class}) +public class IoTDBTemplateAuthIT { + @BeforeClass + public static void setUp() throws Exception { + EnvFactory.getEnv().getConfig().getCommonConfig().setEnforceStrongPassword(false); + EnvFactory.getEnv().initClusterEnvironment(); + } + + @AfterClass + public static void tearDown() throws Exception { + EnvFactory.getEnv().cleanClusterEnvironment(); + } + + @Test + public void manageDataBaseTest() { + + try (Connection adminCon = EnvFactory.getEnv().getConnection(); + Statement adminStmt = adminCon.createStatement()) { + adminStmt.execute("create device template t1 (temperature1 FLOAT, status1 BOOLEAN)"); + adminStmt.execute("create device template t2 (temperature2 FLOAT, status2 BOOLEAN)"); + adminStmt.execute("create device template t3 (temperature3 FLOAT, status3 BOOLEAN)"); + adminStmt.execute("create user tytyty1 'tytytyty'"); + adminStmt.execute("create user tytyty2 'tytytyty'"); + adminStmt.execute("create user tytyty3 'tytytyty'"); + + assertNonQueryTestFail( + "create device template t4 (temperature4 FLOAT, status4 BOOLEAN)", + "803: No permissions for this operation, please add privilege SYSTEM", + "tytyty1", + "tytytyty"); + + Set<String> retSet = new HashSet<>(Arrays.asList("t1", "t2", "t3")); + + try (ResultSet resultSet = adminStmt.executeQuery("show device templates")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(TEMPLATE_NAME); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + + adminStmt.execute("create database root.sg1"); + adminStmt.execute("create database root.sg2"); + adminStmt.execute("create database root.sg3"); + adminStmt.execute("set device template t1 to root.sg1.d1"); + + adminStmt.execute("set device template t2 to root.sg2"); + + assertNonQueryTestFail( + "set device template t1 to root.sg3", + "803: No permissions for this operation, please add privilege SYSTEM", + "tytyty1", + "tytytyty"); + + adminStmt.execute("set device template t1 to root.sg3"); + adminStmt.execute("create timeseries using device template on root.sg1.d1"); + adminStmt.execute("create timeseries using device template on root.sg3"); + + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + + adminStmt.execute("grant read_schema on root.sg1.d2.** to user tytyty1"); + + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + + adminStmt.execute("grant read_schema on root.sg1.d1.** to user tytyty1"); + retSet = Collections.singleton("t1"); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(TEMPLATE_NAME); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + adminStmt.execute("grant read_schema on root.sg1.** to user tytyty2"); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(TEMPLATE_NAME); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + adminStmt.execute("grant read_schema on root.** to user tytyty3"); + retSet = new HashSet<>(Arrays.asList("t1", "t2")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(TEMPLATE_NAME); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + // show nodes in device template + retSet = new HashSet<>(Arrays.asList("temperature1", "status1")); + try (ResultSet resultSet = adminStmt.executeQuery("show nodes in device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("temperature2", "status2")); + try (ResultSet resultSet = adminStmt.executeQuery("show nodes in device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t2")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t2")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("temperature3", "status3")); + try (ResultSet resultSet = adminStmt.executeQuery("show nodes in device template t3")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t3")) { + while (resultSet.next()) { + fail("should see nothing"); + } + } + + // show paths set device template + retSet = new HashSet<>(Arrays.asList("root.sg1.d1", "root.sg3")); + try (ResultSet resultSet = adminStmt.executeQuery("show paths set device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + retSet = new HashSet<>(Arrays.asList("root.sg1.d1")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("root.sg2")); + try (ResultSet resultSet = adminStmt.executeQuery("show paths set device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t2")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t2")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + try (ResultSet resultSet = adminStmt.executeQuery("show paths set device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t3")) { + while (resultSet.next()) { + fail("should see nothing"); + } + } + + // show paths using device template + retSet = new HashSet<>(Arrays.asList("root.sg1.d1", "root.sg3")); + try (ResultSet resultSet = adminStmt.executeQuery("show paths using device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty3", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths using device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + retSet = new HashSet<>(Arrays.asList("root.sg1.d1")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths using device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty2", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths using device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + // alter template + assertNonQueryTestFail( + "alter device template t1 add (speed FLOAT)", + "803: No permissions for this operation, please add privilege SYSTEM", + "tytyty1", + "tytytyty"); + + adminStmt.execute("grant SYSTEM on root.** to user tytyty1"); + executeNonQuery("alter device template t1 add (speed FLOAT)", "tytyty1", "tytytyty"); + + retSet = new HashSet<>(Arrays.asList("t1", "t2", "t3")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show device templates")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(TEMPLATE_NAME); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("temperature1", "status1", "speed")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + retSet = new HashSet<>(Arrays.asList("temperature2", "status2")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("temperature3", "status3")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show nodes in device template t3")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(CHILD_NODES); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + retSet = new HashSet<>(Arrays.asList("root.sg1.d1", "root.sg3")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + retSet = new HashSet<>(Arrays.asList("root.sg2")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t2")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths set device template t3")) { + while (resultSet.next()) { + fail("should see nothing."); + } + } + + retSet = new HashSet<>(Arrays.asList("root.sg1.d1")); + try (Connection userCon = EnvFactory.getEnv().getConnection("tytyty1", "tytytyty"); + Statement userStmt = userCon.createStatement(); + ResultSet resultSet = userStmt.executeQuery("show paths using device template t1")) { + int cnt = 0; + while (resultSet.next()) { + String ans = resultSet.getString(PATHS); + assertTrue(ans, retSet.contains(ans)); + cnt++; + } + assertEquals(retSet.size(), cnt); + } + + } catch (SQLException e) { + fail(e.getMessage()); + } + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java index b12f8408230..d5558374cbf 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java @@ -139,8 +139,6 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.Activate import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.BatchActivateTemplateStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.CreateSchemaTemplateStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.SetSchemaTemplateStatement; -import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.ShowNodesInSchemaTemplateStatement; -import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.ShowPathSetTemplateStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.template.ShowPathsUsingTemplateStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.view.CreateLogicalViewStatement; import org.apache.iotdb.db.queryengine.plan.statement.metadata.view.ShowLogicalViewStatement; @@ -3590,16 +3588,6 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext> return analysis; } - @Override - public Analysis visitShowNodesInSchemaTemplate( - ShowNodesInSchemaTemplateStatement showNodesInSchemaTemplateStatement, - MPPQueryContext context) { - Analysis analysis = new Analysis(); - analysis.setRealStatement(showNodesInSchemaTemplateStatement); - analysis.setRespDatasetHeader(DatasetHeaderFactory.getShowNodesInSchemaTemplateHeader()); - return analysis; - } - @Override public Analysis visitSetSchemaTemplate( SetSchemaTemplateStatement setSchemaTemplateStatement, MPPQueryContext context) { @@ -3609,15 +3597,6 @@ public class AnalyzeVisitor extends StatementVisitor<Analysis, MPPQueryContext> return analysis; } - @Override - public Analysis visitShowPathSetTemplate( - ShowPathSetTemplateStatement showPathSetTemplateStatement, MPPQueryContext context) { - Analysis analysis = new Analysis(); - analysis.setRealStatement(showPathSetTemplateStatement); - analysis.setRespDatasetHeader(DatasetHeaderFactory.getShowPathSetTemplateHeader()); - return analysis; - } - @Override public Analysis visitActivateTemplate( ActivateTemplateStatement activateTemplateStatement, MPPQueryContext context) { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java index 4032c331c66..21045e84263 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/executor/ClusterConfigTaskExecutor.java @@ -363,6 +363,7 @@ import java.util.function.Predicate; import java.util.stream.Collectors; import static org.apache.iotdb.commons.conf.IoTDBConstant.MAX_DATABASE_NAME_LENGTH; +import static org.apache.iotdb.commons.conf.IoTDBConstant.MULTI_LEVEL_PATH_WILDCARD; import static org.apache.iotdb.commons.schema.SchemaConstant.ALL_MATCH_SCOPE; import static org.apache.iotdb.commons.schema.SchemaConstant.ALL_RESULT_NODES; import static org.apache.iotdb.db.protocol.client.ConfigNodeClient.MSG_RECONNECTION_FAIL; @@ -1763,10 +1764,15 @@ public class ClusterConfigTaskExecutor implements IConfigTaskExecutor { final ShowSchemaTemplateStatement showSchemaTemplateStatement) { final SettableFuture<ConfigTaskResult> future = SettableFuture.create(); try { + List<Template> templateList; // Send request to some API server - final List<Template> templateList = - ClusterTemplateManager.getInstance() - .getAllRelatedTemplates(showSchemaTemplateStatement.getAuthorityScope()); + if (showSchemaTemplateStatement.isCamSeeAll()) { + templateList = ClusterTemplateManager.getInstance().getAllTemplates(); + } else { + templateList = + ClusterTemplateManager.getInstance() + .getAllRelatedTemplates(showSchemaTemplateStatement.getAuthorityScope()); + } // build TSBlock ShowSchemaTemplateTask.buildTSBlock(templateList, future); } catch (final Exception e) { @@ -1782,7 +1788,14 @@ public class ClusterConfigTaskExecutor implements IConfigTaskExecutor { final String req = showNodesInSchemaTemplateStatement.getTemplateName(); try { // Send request to some API server - final Template template = ClusterTemplateManager.getInstance().getTemplate(req); + Template template = ClusterTemplateManager.getInstance().getTemplate(req); + if (!showNodesInSchemaTemplateStatement.isCamSeeAll() + && template != null + && ClusterTemplateManager.getInstance() + .getPathsSetTemplate(req, showNodesInSchemaTemplateStatement.getAuthorityScope()) + .isEmpty()) { + template = null; + } // Build TSBlock ShowNodesInSchemaTemplateTask.buildTSBlock(template, future); } catch (final Exception e) { @@ -1824,6 +1837,15 @@ public class ClusterConfigTaskExecutor implements IConfigTaskExecutor { showPathSetTemplateStatement.getTemplateName(), showPathSetTemplateStatement.getAuthorityScope()); // Build TSBlock + if (listPath != null) { + listPath.removeIf( + path -> + !showPathSetTemplateStatement.isCamSeeAll() + && showPathSetTemplateStatement + .getAuthorityScope() + .getOverlappedPathPatterns(path.concatNode(MULTI_LEVEL_PATH_WILDCARD)) + .isEmpty()); + } ShowPathSetTemplateTask.buildTSBlock(listPath, future); } catch (final Exception e) { future.setException(e); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowNodesInSchemaTemplateTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowNodesInSchemaTemplateTask.java index 2ab8fcbd1cb..9d833e890e5 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowNodesInSchemaTemplateTask.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowNodesInSchemaTemplateTask.java @@ -63,33 +63,28 @@ public class ShowNodesInSchemaTemplateTask implements IConfigTask { .map(ColumnHeader::getColumnType) .collect(Collectors.toList()); TsBlockBuilder builder = new TsBlockBuilder(outputDataTypes); - try { - if (template != null) { - // template.get - for (Map.Entry<String, IMeasurementSchema> entry : template.getSchemaMap().entrySet()) { - String keyName = entry.getKey(); - IMeasurementSchema measurementSchema = entry.getValue(); - builder.getTimeColumnBuilder().writeLong(0L); - builder.getColumnBuilder(0).writeBinary(new Binary(keyName, TSFileConfig.STRING_CHARSET)); - builder - .getColumnBuilder(1) - .writeBinary( - new Binary(measurementSchema.getType().name(), TSFileConfig.STRING_CHARSET)); - builder - .getColumnBuilder(2) - .writeBinary( - new Binary( - measurementSchema.getEncodingType().name(), TSFileConfig.STRING_CHARSET)); - builder - .getColumnBuilder(3) - .writeBinary( - new Binary( - measurementSchema.getCompressor().name(), TSFileConfig.STRING_CHARSET)); - builder.declarePosition(); - } + if (template != null) { + // template.get + for (Map.Entry<String, IMeasurementSchema> entry : template.getSchemaMap().entrySet()) { + String keyName = entry.getKey(); + IMeasurementSchema measurementSchema = entry.getValue(); + builder.getTimeColumnBuilder().writeLong(0L); + builder.getColumnBuilder(0).writeBinary(new Binary(keyName, TSFileConfig.STRING_CHARSET)); + builder + .getColumnBuilder(1) + .writeBinary( + new Binary(measurementSchema.getType().name(), TSFileConfig.STRING_CHARSET)); + builder + .getColumnBuilder(2) + .writeBinary( + new Binary( + measurementSchema.getEncodingType().name(), TSFileConfig.STRING_CHARSET)); + builder + .getColumnBuilder(3) + .writeBinary( + new Binary(measurementSchema.getCompressor().name(), TSFileConfig.STRING_CHARSET)); + builder.declarePosition(); } - } catch (Exception e) { - e.printStackTrace(); } DatasetHeader datasetHeader = DatasetHeaderFactory.getShowNodesInSchemaTemplateHeader(); future.set(new ConfigTaskResult(TSStatusCode.SUCCESS_STATUS, builder.build(), datasetHeader)); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowPathSetTemplateTask.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowPathSetTemplateTask.java index 51094d7586c..ab08a47805f 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowPathSetTemplateTask.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/metadata/template/ShowPathSetTemplateTask.java @@ -61,19 +61,15 @@ public class ShowPathSetTemplateTask implements IConfigTask { .map(ColumnHeader::getColumnType) .collect(Collectors.toList()); TsBlockBuilder builder = new TsBlockBuilder(outputDataTypes); - try { - if (listPath != null) { - // template.get - for (PartialPath path : listPath) { - builder.getTimeColumnBuilder().writeLong(0L); - builder - .getColumnBuilder(0) - .writeBinary(new Binary(path.getFullPath(), TSFileConfig.STRING_CHARSET)); - builder.declarePosition(); - } + if (listPath != null) { + // template.get + for (PartialPath path : listPath) { + builder.getTimeColumnBuilder().writeLong(0L); + builder + .getColumnBuilder(0) + .writeBinary(new Binary(path.getFullPath(), TSFileConfig.STRING_CHARSET)); + builder.declarePosition(); } - } catch (Exception e) { - e.printStackTrace(); } DatasetHeader datasetHeader = DatasetHeaderFactory.getShowPathSetTemplateHeader(); future.set(new ConfigTaskResult(TSStatusCode.SUCCESS_STATUS, builder.build(), datasetHeader)); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/security/TreeAccessCheckVisitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/security/TreeAccessCheckVisitor.java index 8bb0c7e2075..229f18f5eb5 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/security/TreeAccessCheckVisitor.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/security/TreeAccessCheckVisitor.java @@ -202,11 +202,13 @@ public class TreeAccessCheckVisitor extends StatementVisitor<TSStatus, TreeAcces } private TSStatus checkTemplateShowRelated( - AuthorityInformationStatement statement, TreeAccessCheckContext context) { + ShowSchemaTemplateStatement statement, TreeAccessCheckContext context) { // own SYSTEM can see all, otherwise can only see PATHS that user has READ_SCHEMA auth if (!AuthorityChecker.checkSystemPermission(context.userName, PrivilegeType.SYSTEM)) { + statement.setCamSeeAll(false); return visitAuthorityInformation(statement, context); } else { + statement.setCamSeeAll(true); return SUCCEED; } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/AbstractShowExactTemplate.java similarity index 62% copy from iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java copy to iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/AbstractShowExactTemplate.java index be4a7ef1af5..91281cbaf71 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/AbstractShowExactTemplate.java @@ -19,19 +19,14 @@ package org.apache.iotdb.db.queryengine.plan.statement.metadata.template; -import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; -import org.apache.iotdb.db.queryengine.plan.statement.IConfigStatement; import org.apache.iotdb.db.queryengine.plan.statement.StatementType; -import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor; -import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowStatement; - -public class ShowPathSetTemplateStatement extends ShowStatement implements IConfigStatement { +public class AbstractShowExactTemplate extends ShowSchemaTemplateStatement { private String templateName; - public ShowPathSetTemplateStatement(String templateName) { + public AbstractShowExactTemplate(String templateName, StatementType statementType) { super(); - statementType = StatementType.SHOW_PATH_SET_SCHEMA_TEMPLATE; + this.statementType = statementType; this.templateName = templateName; } @@ -42,14 +37,4 @@ public class ShowPathSetTemplateStatement extends ShowStatement implements IConf public void setTemplateName(String templateName) { this.templateName = templateName; } - - @Override - public <R, C> R accept(StatementVisitor<R, C> visitor, C context) { - return visitor.visitShowPathSetTemplate(this, context); - } - - @Override - public QueryType getQueryType() { - return QueryType.READ; - } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowNodesInSchemaTemplateStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowNodesInSchemaTemplateStatement.java index 04b4c92a6e8..6f8065ce3f4 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowNodesInSchemaTemplateStatement.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowNodesInSchemaTemplateStatement.java @@ -19,37 +19,17 @@ package org.apache.iotdb.db.queryengine.plan.statement.metadata.template; -import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; -import org.apache.iotdb.db.queryengine.plan.statement.IConfigStatement; import org.apache.iotdb.db.queryengine.plan.statement.StatementType; import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor; -import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowStatement; -public class ShowNodesInSchemaTemplateStatement extends ShowStatement implements IConfigStatement { - - private String templateName; +public class ShowNodesInSchemaTemplateStatement extends AbstractShowExactTemplate { public ShowNodesInSchemaTemplateStatement(String templateName) { - super(); - statementType = StatementType.SHOW_NODES_IN_SCHEMA_TEMPLATE; - this.templateName = templateName; + super(templateName, StatementType.SHOW_NODES_IN_SCHEMA_TEMPLATE); } @Override public <R, C> R accept(StatementVisitor<R, C> visitor, C context) { return visitor.visitShowNodesInSchemaTemplate(this, context); } - - public String getTemplateName() { - return templateName; - } - - public void setTemplateName(String templateName) { - this.templateName = templateName; - } - - @Override - public QueryType getQueryType() { - return QueryType.READ; - } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java index be4a7ef1af5..767357b11ae 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowPathSetTemplateStatement.java @@ -19,37 +19,17 @@ package org.apache.iotdb.db.queryengine.plan.statement.metadata.template; -import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; -import org.apache.iotdb.db.queryengine.plan.statement.IConfigStatement; import org.apache.iotdb.db.queryengine.plan.statement.StatementType; import org.apache.iotdb.db.queryengine.plan.statement.StatementVisitor; -import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowStatement; -public class ShowPathSetTemplateStatement extends ShowStatement implements IConfigStatement { - - private String templateName; +public class ShowPathSetTemplateStatement extends AbstractShowExactTemplate { public ShowPathSetTemplateStatement(String templateName) { - super(); - statementType = StatementType.SHOW_PATH_SET_SCHEMA_TEMPLATE; - this.templateName = templateName; - } - - public String getTemplateName() { - return templateName; - } - - public void setTemplateName(String templateName) { - this.templateName = templateName; + super(templateName, StatementType.SHOW_PATH_SET_SCHEMA_TEMPLATE); } @Override public <R, C> R accept(StatementVisitor<R, C> visitor, C context) { return visitor.visitShowPathSetTemplate(this, context); } - - @Override - public QueryType getQueryType() { - return QueryType.READ; - } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowSchemaTemplateStatement.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowSchemaTemplateStatement.java index c31689aca2b..e9b3a97f7e5 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowSchemaTemplateStatement.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/statement/metadata/template/ShowSchemaTemplateStatement.java @@ -27,6 +27,9 @@ import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowStatement; public class ShowSchemaTemplateStatement extends ShowStatement implements IConfigStatement { + // own SYSTEM can see all + private boolean camSeeAll = true; + public ShowSchemaTemplateStatement() { super(); statementType = StatementType.SHOW_SCHEMA_TEMPLATE; @@ -41,4 +44,12 @@ public class ShowSchemaTemplateStatement extends ShowStatement implements IConfi public QueryType getQueryType() { return QueryType.READ; } + + public void setCamSeeAll(boolean camSeeAll) { + this.camSeeAll = camSeeAll; + } + + public boolean isCamSeeAll() { + return camSeeAll; + } }
