|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.pipe.event; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TSStatus; |
| 23 | +import org.apache.iotdb.commons.audit.IAuditEntity; |
| 24 | +import org.apache.iotdb.commons.auth.entity.PrivilegeType; |
| 25 | +import org.apache.iotdb.commons.conf.IoTDBConstant; |
| 26 | +import org.apache.iotdb.commons.exception.auth.AccessDeniedException; |
| 27 | +import org.apache.iotdb.commons.path.PartialPath; |
| 28 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.IoTDBTreePattern; |
| 29 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.TablePattern; |
| 30 | +import org.apache.iotdb.commons.utils.FileUtils; |
| 31 | +import org.apache.iotdb.db.auth.AuthorityChecker; |
| 32 | +import org.apache.iotdb.db.pipe.event.common.tsfile.PipeTsFileInsertionEvent; |
| 33 | +import org.apache.iotdb.db.queryengine.plan.relational.metadata.QualifiedObjectName; |
| 34 | +import org.apache.iotdb.db.queryengine.plan.relational.security.AccessControl; |
| 35 | +import org.apache.iotdb.db.queryengine.plan.relational.security.TreeAccessCheckContext; |
| 36 | +import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RelationalAuthorStatement; |
| 37 | +import org.apache.iotdb.db.queryengine.plan.statement.Statement; |
| 38 | +import org.apache.iotdb.db.storageengine.dataregion.compaction.utils.CompactionTestFileWriter; |
| 39 | +import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource; |
| 40 | +import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResourceStatus; |
| 41 | +import org.apache.iotdb.db.storageengine.dataregion.tsfile.generator.TsFileNameGenerator; |
| 42 | +import org.apache.iotdb.db.storageengine.dataregion.tsfile.timeindex.ArrayDeviceTimeIndex; |
| 43 | +import org.apache.iotdb.db.storageengine.dataregion.tsfile.timeindex.ITimeIndex; |
| 44 | +import org.apache.iotdb.db.utils.constant.TestConstant; |
| 45 | + |
| 46 | +import org.apache.tsfile.file.metadata.IDeviceID; |
| 47 | +import org.apache.tsfile.file.metadata.enums.CompressionType; |
| 48 | +import org.apache.tsfile.file.metadata.enums.TSEncoding; |
| 49 | +import org.apache.tsfile.read.common.TimeRange; |
| 50 | +import org.junit.Assert; |
| 51 | +import org.junit.Test; |
| 52 | + |
| 53 | +import java.io.File; |
| 54 | +import java.util.Arrays; |
| 55 | +import java.util.Collection; |
| 56 | +import java.util.Collections; |
| 57 | +import java.util.List; |
| 58 | +import java.util.function.Supplier; |
| 59 | +import java.util.stream.Collectors; |
| 60 | +import java.util.stream.IntStream; |
| 61 | + |
| 62 | +import static org.apache.iotdb.commons.pipe.datastructure.pattern.TreePattern.buildUnionPattern; |
| 63 | +import static org.apache.iotdb.db.auth.AuthorityChecker.NO_PERMISSION_PROMOTION; |
| 64 | + |
| 65 | +public class PipeTsFileInsertionEventTest { |
| 66 | + @Test |
| 67 | + public void testAuthCheck() throws Exception { |
| 68 | + final AccessControl oldControl = AuthorityChecker.getAccessControl(); |
| 69 | + final File file = |
| 70 | + new File( |
| 71 | + TsFileNameGenerator.generateNewTsFilePath( |
| 72 | + TestConstant.BASE_OUTPUT_PATH + IoTDBConstant.SEQUENCE_FOLDER_NAME, 1, 1, 1, 1)); |
| 73 | + try { |
| 74 | + AuthorityChecker.setAccessControl(new TestAccessControl()); |
| 75 | + |
| 76 | + final TsFileResource resource = new TsFileResource(file); |
| 77 | + try (CompactionTestFileWriter writer = new CompactionTestFileWriter(resource)) { |
| 78 | + writer.startChunkGroup("d1"); |
| 79 | + writer.generateSimpleAlignedSeriesToCurrentDevice( |
| 80 | + Arrays.asList("s1", "s2"), |
| 81 | + new TimeRange[][][] { |
| 82 | + new TimeRange[][] {new TimeRange[] {new TimeRange(10, 12), new TimeRange(3, 12)}} |
| 83 | + }, |
| 84 | + TSEncoding.PLAIN, |
| 85 | + CompressionType.LZ4); |
| 86 | + writer.endChunkGroup(); |
| 87 | + writer.endFile(); |
| 88 | + } |
| 89 | + resource.setStatus(TsFileResourceStatus.NORMAL); |
| 90 | + |
| 91 | + final ITimeIndex timeIndex = new ArrayDeviceTimeIndex(); |
| 92 | + final IDeviceID deviceID = IDeviceID.Factory.DEFAULT_FACTORY.create("root.db.d1"); |
| 93 | + timeIndex.putStartTime(deviceID, 0); |
| 94 | + timeIndex.putEndTime(deviceID, 1); |
| 95 | + resource.setTimeIndex(timeIndex); |
| 96 | + |
| 97 | + final PipeTsFileInsertionEvent tableEvent = |
| 98 | + new PipeTsFileInsertionEvent( |
| 99 | + true, |
| 100 | + "db", |
| 101 | + resource, |
| 102 | + null, |
| 103 | + true, |
| 104 | + false, |
| 105 | + false, |
| 106 | + Collections.singleton("table"), |
| 107 | + null, |
| 108 | + 0, |
| 109 | + null, |
| 110 | + buildUnionPattern( |
| 111 | + false, Collections.singletonList(new IoTDBTreePattern(false, null))), |
| 112 | + new TablePattern(true, null, null), |
| 113 | + "0", |
| 114 | + "user", |
| 115 | + "localhost", |
| 116 | + false, |
| 117 | + Long.MIN_VALUE, |
| 118 | + Long.MAX_VALUE); |
| 119 | + Assert.assertThrows(AccessDeniedException.class, tableEvent::throwIfNoPrivilege); |
| 120 | + tableEvent.close(); |
| 121 | + |
| 122 | + final PipeTsFileInsertionEvent treeEvent = |
| 123 | + new PipeTsFileInsertionEvent( |
| 124 | + false, |
| 125 | + "root.db", |
| 126 | + resource, |
| 127 | + null, |
| 128 | + true, |
| 129 | + false, |
| 130 | + false, |
| 131 | + Collections.singleton("table"), |
| 132 | + null, |
| 133 | + 0, |
| 134 | + null, |
| 135 | + buildUnionPattern(true, Collections.singletonList(new IoTDBTreePattern(true, null))), |
| 136 | + new TablePattern(false, null, null), |
| 137 | + "0", |
| 138 | + "user", |
| 139 | + "localhost", |
| 140 | + false, |
| 141 | + Long.MIN_VALUE, |
| 142 | + Long.MAX_VALUE); |
| 143 | + Assert.assertThrows(AccessDeniedException.class, treeEvent::throwIfNoPrivilege); |
| 144 | + |
| 145 | + treeEvent.setTreeSchemaMap(Collections.singletonMap(deviceID, new String[] {"s0", "s1"})); |
| 146 | + Assert.assertThrows(AccessDeniedException.class, treeEvent::throwIfNoPrivilege); |
| 147 | + |
| 148 | + treeEvent.close(); |
| 149 | + } finally { |
| 150 | + AuthorityChecker.setAccessControl(oldControl); |
| 151 | + FileUtils.deleteFileOrDirectory(new File(TestConstant.BASE_OUTPUT_PATH)); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static class TestAccessControl implements AccessControl { |
| 156 | + |
| 157 | + @Override |
| 158 | + public void checkCanCreateDatabase( |
| 159 | + String userName, String databaseName, IAuditEntity auditEntity) {} |
| 160 | + |
| 161 | + @Override |
| 162 | + public void checkCanDropDatabase( |
| 163 | + String userName, String databaseName, IAuditEntity auditEntity) {} |
| 164 | + |
| 165 | + @Override |
| 166 | + public void checkCanAlterDatabase( |
| 167 | + String userName, String databaseName, IAuditEntity auditEntity) {} |
| 168 | + |
| 169 | + @Override |
| 170 | + public void checkCanShowOrUseDatabase( |
| 171 | + String userName, String databaseName, IAuditEntity auditEntity) {} |
| 172 | + |
| 173 | + @Override |
| 174 | + public void checkCanCreateTable( |
| 175 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 176 | + |
| 177 | + @Override |
| 178 | + public void checkCanDropTable( |
| 179 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 180 | + |
| 181 | + @Override |
| 182 | + public void checkCanAlterTable( |
| 183 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 184 | + |
| 185 | + @Override |
| 186 | + public void checkCanInsertIntoTable( |
| 187 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 188 | + |
| 189 | + @Override |
| 190 | + public void checkCanSelectFromTable( |
| 191 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 192 | + |
| 193 | + @Override |
| 194 | + public void checkCanSelectFromDatabase4Pipe( |
| 195 | + String userName, String databaseName, IAuditEntity auditEntity) { |
| 196 | + throw new AccessDeniedException( |
| 197 | + NO_PERMISSION_PROMOTION + PrivilegeType.SELECT + " ON DB:" + databaseName); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public boolean checkCanSelectFromTable4Pipe( |
| 202 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public void checkCanDeleteFromTable( |
| 208 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 209 | + |
| 210 | + @Override |
| 211 | + public void checkCanShowOrDescTable( |
| 212 | + String userName, QualifiedObjectName tableName, IAuditEntity auditEntity) {} |
| 213 | + |
| 214 | + @Override |
| 215 | + public void checkCanCreateViewFromTreePath(PartialPath path, IAuditEntity auditEntity) {} |
| 216 | + |
| 217 | + @Override |
| 218 | + public void checkUserCanRunRelationalAuthorStatement( |
| 219 | + String userName, RelationalAuthorStatement statement, IAuditEntity auditEntity) {} |
| 220 | + |
| 221 | + @Override |
| 222 | + public void checkUserIsAdmin(IAuditEntity auditEntity) {} |
| 223 | + |
| 224 | + @Override |
| 225 | + public void checkUserGlobalSysPrivilege(IAuditEntity auditEntity) {} |
| 226 | + |
| 227 | + @Override |
| 228 | + public boolean hasGlobalPrivilege(IAuditEntity auditEntity, PrivilegeType privilegeType) { |
| 229 | + return false; |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void checkMissingPrivileges( |
| 234 | + String username, Collection<PrivilegeType> privilegeTypes, IAuditEntity auditEntity) {} |
| 235 | + |
| 236 | + @Override |
| 237 | + public TSStatus checkPermissionBeforeProcess( |
| 238 | + Statement statement, TreeAccessCheckContext context) { |
| 239 | + return null; |
| 240 | + } |
| 241 | + |
| 242 | + @Override |
| 243 | + public TSStatus checkFullPathWriteDataPermission( |
| 244 | + IAuditEntity auditEntity, IDeviceID device, String measurementId) { |
| 245 | + return null; |
| 246 | + } |
| 247 | + |
| 248 | + @Override |
| 249 | + public TSStatus checkCanCreateDatabaseForTree(IAuditEntity entity, PartialPath databaseName) { |
| 250 | + return null; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public TSStatus checkCanAlterTemplate(IAuditEntity entity, Supplier<String> auditObject) { |
| 255 | + return null; |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public TSStatus checkCanAlterView( |
| 260 | + IAuditEntity entity, List<PartialPath> sourcePaths, List<PartialPath> targetPaths) { |
| 261 | + return null; |
| 262 | + } |
| 263 | + |
| 264 | + @Override |
| 265 | + public TSStatus checkSeriesPrivilege4Pipe( |
| 266 | + IAuditEntity context, |
| 267 | + List<? extends PartialPath> checkedPathsSupplier, |
| 268 | + PrivilegeType permission) { |
| 269 | + return AuthorityChecker.getTSStatus( |
| 270 | + IntStream.range(0, checkedPathsSupplier.size()).boxed().collect(Collectors.toList()), |
| 271 | + checkedPathsSupplier, |
| 272 | + permission); |
| 273 | + } |
| 274 | + |
| 275 | + @Override |
| 276 | + public List<Integer> checkSeriesPrivilegeWithIndexes4Pipe( |
| 277 | + IAuditEntity context, |
| 278 | + List<? extends PartialPath> checkedPathsSupplier, |
| 279 | + PrivilegeType permission) { |
| 280 | + return null; |
| 281 | + } |
| 282 | + |
| 283 | + @Override |
| 284 | + public TSStatus allowUserToLogin(String userName) { |
| 285 | + return null; |
| 286 | + } |
| 287 | + } |
| 288 | +} |
0 commit comments