001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.security.access; 019 020import static org.apache.hadoop.fs.permission.AclEntryScope.ACCESS; 021import static org.apache.hadoop.fs.permission.AclEntryScope.DEFAULT; 022import static org.apache.hadoop.fs.permission.AclEntryType.GROUP; 023import static org.apache.hadoop.fs.permission.AclEntryType.USER; 024import static org.apache.hadoop.fs.permission.FsAction.READ_EXECUTE; 025 026import java.io.Closeable; 027import java.io.FileNotFoundException; 028import java.io.IOException; 029import java.util.ArrayList; 030import java.util.Collections; 031import java.util.HashSet; 032import java.util.List; 033import java.util.Set; 034import java.util.concurrent.CompletableFuture; 035import java.util.concurrent.ExecutionException; 036import java.util.concurrent.ExecutorService; 037import java.util.concurrent.Executors; 038import java.util.stream.Collectors; 039import org.apache.hadoop.conf.Configuration; 040import org.apache.hadoop.fs.FileStatus; 041import org.apache.hadoop.fs.FileSystem; 042import org.apache.hadoop.fs.Path; 043import org.apache.hadoop.fs.permission.AclEntry; 044import org.apache.hadoop.fs.permission.AclEntryScope; 045import org.apache.hadoop.fs.permission.FsPermission; 046import org.apache.hadoop.hbase.AuthUtil; 047import org.apache.hadoop.hbase.HConstants; 048import org.apache.hadoop.hbase.NamespaceDescriptor; 049import org.apache.hadoop.hbase.TableName; 050import org.apache.hadoop.hbase.client.Admin; 051import org.apache.hadoop.hbase.client.Connection; 052import org.apache.hadoop.hbase.client.SnapshotDescription; 053import org.apache.hadoop.hbase.client.TableDescriptor; 054import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 055import org.apache.hadoop.hbase.mob.MobUtils; 056import org.apache.hadoop.hbase.util.Bytes; 057import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 058import org.apache.yetus.audience.InterfaceAudience; 059import org.slf4j.Logger; 060import org.slf4j.LoggerFactory; 061 062import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 063import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 064import org.apache.hbase.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder; 065 066/** 067 * A helper to modify or remove HBase granted user default and access HDFS ACLs over hFiles. 068 */ 069@InterfaceAudience.Private 070public class SnapshotScannerHDFSAclHelper implements Closeable { 071 private static final Logger LOG = LoggerFactory.getLogger(SnapshotScannerHDFSAclHelper.class); 072 073 public static final String ACL_SYNC_TO_HDFS_ENABLE = "hbase.acl.sync.to.hdfs.enable"; 074 public static final String ACL_SYNC_TO_HDFS_THREAD_NUMBER = 075 "hbase.acl.sync.to.hdfs.thread.number"; 076 // The tmp directory to restore snapshot, it can not be a sub directory of HBase root dir 077 public static final String SNAPSHOT_RESTORE_TMP_DIR = "hbase.snapshot.restore.tmp.dir"; 078 public static final String SNAPSHOT_RESTORE_TMP_DIR_DEFAULT = 079 "/hbase/.tmpdir-to-restore-snapshot"; 080 // The default permission of the common directories if the feature is enabled. 081 public static final String COMMON_DIRECTORY_PERMISSION = 082 "hbase.acl.sync.to.hdfs.common.directory.permission"; 083 // The secure HBase permission is 700, 751 means all others have execute access and the mask is 084 // set to read-execute to make the extended access ACL entries can work. Be cautious to set 085 // this value. 086 public static final String COMMON_DIRECTORY_PERMISSION_DEFAULT = "751"; 087 // The default permission of the snapshot restore directories if the feature is enabled. 088 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION = 089 "hbase.acl.sync.to.hdfs.restore.directory.permission"; 090 // 753 means all others have write-execute access. 091 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION_DEFAULT = "753"; 092 093 private Admin admin; 094 private final Configuration conf; 095 private final Connection conn; 096 private FileSystem fs; 097 private PathHelper pathHelper; 098 private ExecutorService pool; 099 100 public SnapshotScannerHDFSAclHelper(Configuration configuration, Connection connection) 101 throws IOException { 102 this.conf = configuration; 103 this.conn = connection; 104 this.pathHelper = new PathHelper(conf); 105 this.fs = pathHelper.getFileSystem(); 106 this.pool = Executors.newFixedThreadPool(conf.getInt(ACL_SYNC_TO_HDFS_THREAD_NUMBER, 10), 107 new ThreadFactoryBuilder().setNameFormat("hdfs-acl-thread-%d").setDaemon(true).build()); 108 this.admin = connection.getAdmin(); 109 } 110 111 @Override 112 public void close() { 113 if (pool != null) { 114 pool.shutdown(); 115 } 116 admin.close(); 117 } 118 119 public void setCommonDirectoryPermission() throws IOException { 120 // Set public directory permission to 751 to make all users have access permission. 121 // And we also need the access permission of the parent of HBase root directory, but 122 // it's not set here, because the owner of HBase root directory may don't own permission 123 // to change it's parent permission to 751. 124 // The {root/.tmp} and {root/.tmp/data} directories are created to make global user HDFS 125 // ACLs can be inherited. 126 List<Path> paths = Lists.newArrayList(pathHelper.getRootDir(), pathHelper.getMobDir(), 127 pathHelper.getTmpDir(), pathHelper.getArchiveDir()); 128 paths.addAll(getGlobalRootPaths()); 129 for (Path path : paths) { 130 createDirIfNotExist(path); 131 fs.setPermission(path, new FsPermission( 132 conf.get(COMMON_DIRECTORY_PERMISSION, COMMON_DIRECTORY_PERMISSION_DEFAULT))); 133 } 134 // create snapshot restore directory 135 Path restoreDir = 136 new Path(conf.get(SNAPSHOT_RESTORE_TMP_DIR, SNAPSHOT_RESTORE_TMP_DIR_DEFAULT)); 137 createDirIfNotExist(restoreDir); 138 fs.setPermission(restoreDir, new FsPermission(conf.get(SNAPSHOT_RESTORE_DIRECTORY_PERMISSION, 139 SNAPSHOT_RESTORE_DIRECTORY_PERMISSION_DEFAULT))); 140 } 141 142 /** 143 * Set acl when grant user permission 144 * @param userPermission the user and permission 145 * @param skipNamespaces the namespace set to skip set acl because already set 146 * @param skipTables the table set to skip set acl because already set 147 * @return false if an error occurred, otherwise true 148 */ 149 public boolean grantAcl(UserPermission userPermission, Set<String> skipNamespaces, 150 Set<TableName> skipTables) { 151 try { 152 long start = EnvironmentEdgeManager.currentTime(); 153 handleGrantOrRevokeAcl(userPermission, HDFSAclOperation.OperationType.MODIFY, skipNamespaces, 154 skipTables); 155 LOG.info("Set HDFS acl when grant {}, skipNamespaces: {}, skipTables: {}, cost {} ms", 156 userPermission, skipNamespaces, skipTables, EnvironmentEdgeManager.currentTime() - start); 157 return true; 158 } catch (Exception e) { 159 LOG.error("Set HDFS acl error when grant: {}, skipNamespaces: {}, skipTables: {}", 160 userPermission, skipNamespaces, skipTables, e); 161 return false; 162 } 163 } 164 165 /** 166 * Remove acl when grant or revoke user permission 167 * @param userPermission the user and permission 168 * @param skipNamespaces the namespace set to skip remove acl 169 * @param skipTables the table set to skip remove acl 170 * @return false if an error occurred, otherwise true 171 */ 172 public boolean revokeAcl(UserPermission userPermission, Set<String> skipNamespaces, 173 Set<TableName> skipTables) { 174 try { 175 long start = EnvironmentEdgeManager.currentTime(); 176 handleGrantOrRevokeAcl(userPermission, HDFSAclOperation.OperationType.REMOVE, skipNamespaces, 177 skipTables); 178 LOG.info("Set HDFS acl when revoke {}, skipNamespaces: {}, skipTables: {}, cost {} ms", 179 userPermission, skipNamespaces, skipTables, EnvironmentEdgeManager.currentTime() - start); 180 return true; 181 } catch (Exception e) { 182 LOG.error("Set HDFS acl error when revoke: {}, skipNamespaces: {}, skipTables: {}", 183 userPermission, skipNamespaces, skipTables, e); 184 return false; 185 } 186 } 187 188 /** 189 * Set acl when take a snapshot 190 * @param snapshot the snapshot desc 191 * @return false if an error occurred, otherwise true 192 */ 193 public boolean snapshotAcl(SnapshotDescription snapshot) { 194 try { 195 long start = EnvironmentEdgeManager.currentTime(); 196 TableName tableName = snapshot.getTableName(); 197 // global user permission can be inherited from default acl automatically 198 Set<String> userSet = getUsersWithTableReadAction(tableName, true, false); 199 if (userSet.size() > 0) { 200 Path path = pathHelper.getSnapshotDir(snapshot.getName()); 201 handleHDFSAcl(new HDFSAclOperation(fs, path, userSet, HDFSAclOperation.OperationType.MODIFY, 202 true, HDFSAclOperation.AclType.DEFAULT_ADN_ACCESS)).get(); 203 } 204 LOG.info("Set HDFS acl when snapshot {}, cost {} ms", snapshot.getName(), 205 EnvironmentEdgeManager.currentTime() - start); 206 return true; 207 } catch (Exception e) { 208 LOG.error("Set HDFS acl error when snapshot {}", snapshot, e); 209 return false; 210 } 211 } 212 213 /** 214 * Remove table access acl from namespace dir when delete table 215 * @param tableName the table 216 * @param removeUsers the users whose access acl will be removed 217 * @return false if an error occurred, otherwise true 218 */ 219 public boolean removeNamespaceAccessAcl(TableName tableName, Set<String> removeUsers, 220 String operation) { 221 try { 222 long start = EnvironmentEdgeManager.currentTime(); 223 if (removeUsers.size() > 0) { 224 handleNamespaceAccessAcl(tableName.getNamespaceAsString(), removeUsers, 225 HDFSAclOperation.OperationType.REMOVE); 226 } 227 LOG.info("Remove HDFS acl when {} table {}, cost {} ms", operation, tableName, 228 EnvironmentEdgeManager.currentTime() - start); 229 return true; 230 } catch (Exception e) { 231 LOG.error("Remove HDFS acl error when {} table {}", operation, tableName, e); 232 return false; 233 } 234 } 235 236 /** 237 * Remove default acl from namespace archive dir when delete namespace 238 * @param namespace the namespace 239 * @param removeUsers the users whose default acl will be removed 240 * @return false if an error occurred, otherwise true 241 */ 242 public boolean removeNamespaceDefaultAcl(String namespace, Set<String> removeUsers) { 243 try { 244 long start = EnvironmentEdgeManager.currentTime(); 245 Path archiveNsDir = pathHelper.getArchiveNsDir(namespace); 246 HDFSAclOperation operation = new HDFSAclOperation(fs, archiveNsDir, removeUsers, 247 HDFSAclOperation.OperationType.REMOVE, false, HDFSAclOperation.AclType.DEFAULT); 248 operation.handleAcl(); 249 LOG.info("Remove HDFS acl when delete namespace {}, cost {} ms", namespace, 250 EnvironmentEdgeManager.currentTime() - start); 251 return true; 252 } catch (Exception e) { 253 LOG.error("Remove HDFS acl error when delete namespace {}", namespace, e); 254 return false; 255 } 256 } 257 258 /** 259 * Remove default acl from table archive dir when delete table 260 * @param tableName the table name 261 * @param removeUsers the users whose default acl will be removed 262 * @return false if an error occurred, otherwise true 263 */ 264 public boolean removeTableDefaultAcl(TableName tableName, Set<String> removeUsers) { 265 try { 266 long start = EnvironmentEdgeManager.currentTime(); 267 Path archiveTableDir = pathHelper.getArchiveTableDir(tableName); 268 HDFSAclOperation operation = new HDFSAclOperation(fs, archiveTableDir, removeUsers, 269 HDFSAclOperation.OperationType.REMOVE, false, HDFSAclOperation.AclType.DEFAULT); 270 operation.handleAcl(); 271 LOG.info("Remove HDFS acl when delete table {}, cost {} ms", tableName, 272 EnvironmentEdgeManager.currentTime() - start); 273 return true; 274 } catch (Exception e) { 275 LOG.error("Remove HDFS acl error when delete table {}", tableName, e); 276 return false; 277 } 278 } 279 280 /** 281 * Add table user acls 282 * @param tableName the table 283 * @param users the table users with READ permission 284 * @return false if an error occurred, otherwise true 285 */ 286 public boolean addTableAcl(TableName tableName, Set<String> users, String operation) { 287 try { 288 long start = EnvironmentEdgeManager.currentTime(); 289 if (users.size() > 0) { 290 HDFSAclOperation.OperationType operationType = HDFSAclOperation.OperationType.MODIFY; 291 handleNamespaceAccessAcl(tableName.getNamespaceAsString(), users, operationType); 292 handleTableAcl(Sets.newHashSet(tableName), users, new HashSet<>(0), new HashSet<>(0), 293 operationType); 294 } 295 LOG.info("Set HDFS acl when {} table {}, cost {} ms", operation, tableName, 296 EnvironmentEdgeManager.currentTime() - start); 297 return true; 298 } catch (Exception e) { 299 LOG.error("Set HDFS acl error when {} table {}", operation, tableName, e); 300 return false; 301 } 302 } 303 304 /** 305 * Remove table acls when modify table 306 * @param tableName the table 307 * @param users the table users with READ permission 308 * @return false if an error occurred, otherwise true 309 */ 310 public boolean removeTableAcl(TableName tableName, Set<String> users) { 311 try { 312 long start = EnvironmentEdgeManager.currentTime(); 313 if (users.size() > 0) { 314 handleTableAcl(Sets.newHashSet(tableName), users, new HashSet<>(0), new HashSet<>(0), 315 HDFSAclOperation.OperationType.REMOVE); 316 } 317 LOG.info("Set HDFS acl when create or modify table {}, cost {} ms", tableName, 318 EnvironmentEdgeManager.currentTime() - start); 319 return true; 320 } catch (Exception e) { 321 LOG.error("Set HDFS acl error when create or modify table {}", tableName, e); 322 return false; 323 } 324 } 325 326 private void handleGrantOrRevokeAcl(UserPermission userPermission, 327 HDFSAclOperation.OperationType operationType, Set<String> skipNamespaces, 328 Set<TableName> skipTables) throws ExecutionException, InterruptedException, IOException { 329 Set<String> users = Sets.newHashSet(userPermission.getUser()); 330 switch (userPermission.getAccessScope()) { 331 case GLOBAL: 332 handleGlobalAcl(users, skipNamespaces, skipTables, operationType); 333 break; 334 case NAMESPACE: 335 NamespacePermission namespacePermission = 336 (NamespacePermission) userPermission.getPermission(); 337 handleNamespaceAcl(Sets.newHashSet(namespacePermission.getNamespace()), users, 338 skipNamespaces, skipTables, operationType); 339 break; 340 case TABLE: 341 TablePermission tablePermission = (TablePermission) userPermission.getPermission(); 342 handleNamespaceAccessAcl(tablePermission.getNamespace(), users, operationType); 343 handleTableAcl(Sets.newHashSet(tablePermission.getTableName()), users, skipNamespaces, 344 skipTables, operationType); 345 break; 346 default: 347 throw new IllegalArgumentException( 348 "Illegal user permission scope " + userPermission.getAccessScope()); 349 } 350 } 351 352 private void handleGlobalAcl(Set<String> users, Set<String> skipNamespaces, 353 Set<TableName> skipTables, HDFSAclOperation.OperationType operationType) 354 throws ExecutionException, InterruptedException, IOException { 355 // handle global root directories HDFS acls 356 List<HDFSAclOperation> hdfsAclOperations = 357 getGlobalRootPaths().stream().map(path -> new HDFSAclOperation(fs, path, users, operationType, 358 false, HDFSAclOperation.AclType.DEFAULT_ADN_ACCESS)).collect(Collectors.toList()); 359 handleHDFSAclParallel(hdfsAclOperations).get(); 360 // handle namespace HDFS acls 361 handleNamespaceAcl(Sets.newHashSet(admin.listNamespaces()), users, skipNamespaces, skipTables, 362 operationType); 363 } 364 365 private void handleNamespaceAcl(Set<String> namespaces, Set<String> users, 366 Set<String> skipNamespaces, Set<TableName> skipTables, 367 HDFSAclOperation.OperationType operationType) 368 throws ExecutionException, InterruptedException, IOException { 369 namespaces.removeAll(skipNamespaces); 370 namespaces.remove(NamespaceDescriptor.SYSTEM_NAMESPACE_NAME_STR); 371 // handle namespace root directories HDFS acls 372 List<HDFSAclOperation> hdfsAclOperations = new ArrayList<>(); 373 Set<String> skipTableNamespaces = 374 skipTables.stream().map(TableName::getNamespaceAsString).collect(Collectors.toSet()); 375 for (String ns : namespaces) { 376 /** 377 * When op is REMOVE, remove the DEFAULT namespace ACL while keep the ACCESS for skipTables, 378 * otherwise remove both the DEFAULT + ACCESS ACLs. When op is MODIFY, just operate the 379 * DEFAULT + ACCESS ACLs. 380 */ 381 HDFSAclOperation.OperationType op = operationType; 382 HDFSAclOperation.AclType aclType = HDFSAclOperation.AclType.DEFAULT_ADN_ACCESS; 383 if ( 384 operationType == HDFSAclOperation.OperationType.REMOVE && skipTableNamespaces.contains(ns) 385 ) { 386 // remove namespace directories default HDFS acls for skip tables 387 op = HDFSAclOperation.OperationType.REMOVE; 388 aclType = HDFSAclOperation.AclType.DEFAULT; 389 } 390 for (Path path : getNamespaceRootPaths(ns)) { 391 hdfsAclOperations.add(new HDFSAclOperation(fs, path, users, op, false, aclType)); 392 } 393 } 394 handleHDFSAclParallel(hdfsAclOperations).get(); 395 // handle table directories HDFS acls 396 Set<TableName> tables = new HashSet<>(); 397 for (String namespace : namespaces) { 398 tables.addAll(admin.listTableDescriptorsByNamespace(Bytes.toBytes(namespace)).stream() 399 .filter(this::isAclSyncToHdfsEnabled).map(TableDescriptor::getTableName) 400 .collect(Collectors.toSet())); 401 } 402 handleTableAcl(tables, users, skipNamespaces, skipTables, operationType); 403 } 404 405 private void handleTableAcl(Set<TableName> tableNames, Set<String> users, 406 Set<String> skipNamespaces, Set<TableName> skipTables, 407 HDFSAclOperation.OperationType operationType) 408 throws ExecutionException, InterruptedException, IOException { 409 Set<TableName> filterTableNames = new HashSet<>(); 410 for (TableName tableName : tableNames) { 411 if ( 412 !skipTables.contains(tableName) 413 && !skipNamespaces.contains(tableName.getNamespaceAsString()) 414 ) { 415 filterTableNames.add(tableName); 416 } 417 } 418 List<CompletableFuture<Void>> futures = new ArrayList<>(); 419 // handle table HDFS acls 420 for (TableName tableName : filterTableNames) { 421 List<HDFSAclOperation> hdfsAclOperations = getTableRootPaths(tableName, true).stream() 422 .map(path -> new HDFSAclOperation(fs, path, users, operationType, true, 423 HDFSAclOperation.AclType.DEFAULT_ADN_ACCESS)) 424 .collect(Collectors.toList()); 425 CompletableFuture<Void> future = handleHDFSAclSequential(hdfsAclOperations); 426 futures.add(future); 427 } 428 CompletableFuture<Void> future = 429 CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); 430 future.get(); 431 } 432 433 private void handleNamespaceAccessAcl(String namespace, Set<String> users, 434 HDFSAclOperation.OperationType operationType) throws ExecutionException, InterruptedException { 435 // handle namespace access HDFS acls 436 List<HDFSAclOperation> hdfsAclOperations = 437 getNamespaceRootPaths(namespace).stream().map(path -> new HDFSAclOperation(fs, path, users, 438 operationType, false, HDFSAclOperation.AclType.ACCESS)).collect(Collectors.toList()); 439 CompletableFuture<Void> future = handleHDFSAclParallel(hdfsAclOperations); 440 future.get(); 441 } 442 443 void createTableDirectories(TableName tableName) throws IOException { 444 List<Path> paths = getTableRootPaths(tableName, false); 445 for (Path path : paths) { 446 createDirIfNotExist(path); 447 } 448 } 449 450 /** 451 * return paths that user will global permission will visit 452 * @return the path list 453 */ 454 List<Path> getGlobalRootPaths() { 455 return Lists.newArrayList(pathHelper.getTmpDataDir(), pathHelper.getDataDir(), 456 pathHelper.getMobDataDir(), pathHelper.getArchiveDataDir(), pathHelper.getSnapshotRootDir()); 457 } 458 459 /** 460 * return paths that user will namespace permission will visit 461 * @param namespace the namespace 462 * @return the path list 463 */ 464 List<Path> getNamespaceRootPaths(String namespace) { 465 return Lists.newArrayList(pathHelper.getTmpNsDir(namespace), pathHelper.getDataNsDir(namespace), 466 pathHelper.getMobDataNsDir(namespace), pathHelper.getArchiveNsDir(namespace)); 467 } 468 469 /** 470 * return paths that user will table permission will visit 471 * @param tableName the table 472 * @param includeSnapshotPath true if return table snapshots paths, otherwise false 473 * @return the path list 474 * @throws IOException if an error occurred 475 */ 476 List<Path> getTableRootPaths(TableName tableName, boolean includeSnapshotPath) 477 throws IOException { 478 List<Path> paths = Lists.newArrayList(pathHelper.getDataTableDir(tableName), 479 pathHelper.getMobTableDir(tableName), pathHelper.getArchiveTableDir(tableName)); 480 if (includeSnapshotPath) { 481 paths.addAll(getTableSnapshotPaths(tableName)); 482 } 483 return paths; 484 } 485 486 private List<Path> getTableSnapshotPaths(TableName tableName) throws IOException { 487 return admin.listSnapshots().stream() 488 .filter(snapDesc -> snapDesc.getTableName().equals(tableName)) 489 .map(snapshotDescription -> pathHelper.getSnapshotDir(snapshotDescription.getName())) 490 .collect(Collectors.toList()); 491 } 492 493 /** 494 * Return users with global read permission 495 * @return users with global read permission 496 * @throws IOException if an error occurred 497 */ 498 private Set<String> getUsersWithGlobalReadAction() throws IOException { 499 return getUsersWithReadAction(PermissionStorage.getGlobalPermissions(conn)); 500 } 501 502 /** 503 * Return users with namespace read permission 504 * @param namespace the namespace 505 * @param includeGlobal true if include users with global read action 506 * @return users with namespace read permission 507 * @throws IOException if an error occurred 508 */ 509 Set<String> getUsersWithNamespaceReadAction(String namespace, boolean includeGlobal) 510 throws IOException { 511 Set<String> users = 512 getUsersWithReadAction(PermissionStorage.getNamespacePermissions(conn, namespace)); 513 if (includeGlobal) { 514 users.addAll(getUsersWithGlobalReadAction()); 515 } 516 return users; 517 } 518 519 /** 520 * Return users with table read permission 521 * @param tableName the table 522 * @param includeNamespace true if include users with namespace read action 523 * @param includeGlobal true if include users with global read action 524 * @return users with table read permission 525 * @throws IOException if an error occurred 526 */ 527 Set<String> getUsersWithTableReadAction(TableName tableName, boolean includeNamespace, 528 boolean includeGlobal) throws IOException { 529 Set<String> users = 530 getUsersWithReadAction(PermissionStorage.getTablePermissions(conn, tableName)); 531 if (includeNamespace) { 532 users 533 .addAll(getUsersWithNamespaceReadAction(tableName.getNamespaceAsString(), includeGlobal)); 534 } 535 return users; 536 } 537 538 private Set<String> getUsersWithReadAction(List<UserPermission> perms) { 539 return perms.stream().filter(this::checkUserPermission).map(UserPermission::getUser) 540 .collect(Collectors.toSet()); 541 } 542 543 private boolean checkUserPermission(UserPermission userPermission) { 544 boolean result = containReadAction(userPermission); 545 if (result && userPermission.getPermission() instanceof TablePermission) { 546 result = isNotFamilyOrQualifierPermission((TablePermission) userPermission.getPermission()); 547 } 548 return result; 549 } 550 551 boolean containReadAction(UserPermission userPermission) { 552 return userPermission.getPermission().implies(Permission.Action.READ); 553 } 554 555 boolean isNotFamilyOrQualifierPermission(TablePermission tablePermission) { 556 return !tablePermission.hasFamily() && !tablePermission.hasQualifier(); 557 } 558 559 public static boolean isAclSyncToHdfsEnabled(Configuration conf) { 560 String[] masterCoprocessors = conf.getStrings(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY); 561 Set<String> masterCoprocessorSet = new HashSet<>(); 562 if (masterCoprocessors != null) { 563 Collections.addAll(masterCoprocessorSet, masterCoprocessors); 564 } 565 return conf.getBoolean(SnapshotScannerHDFSAclHelper.ACL_SYNC_TO_HDFS_ENABLE, false) 566 && masterCoprocessorSet.contains(SnapshotScannerHDFSAclController.class.getName()) 567 && masterCoprocessorSet.contains(AccessController.class.getName()); 568 } 569 570 boolean isAclSyncToHdfsEnabled(TableDescriptor tableDescriptor) { 571 return tableDescriptor == null 572 ? false 573 : Boolean.valueOf(tableDescriptor.getValue(ACL_SYNC_TO_HDFS_ENABLE)); 574 } 575 576 PathHelper getPathHelper() { 577 return pathHelper; 578 } 579 580 private CompletableFuture<Void> handleHDFSAcl(HDFSAclOperation acl) { 581 return CompletableFuture.supplyAsync(() -> { 582 List<HDFSAclOperation> childAclOperations = new ArrayList<>(); 583 try { 584 acl.handleAcl(); 585 childAclOperations = acl.getChildAclOperations(); 586 } catch (FileNotFoundException e) { 587 // Skip handle acl if file not found 588 } catch (IOException e) { 589 LOG.error("Set HDFS acl error for path {}", acl.path, e); 590 } 591 return childAclOperations; 592 }, pool).thenComposeAsync(this::handleHDFSAclParallel, pool); 593 } 594 595 private CompletableFuture<Void> handleHDFSAclSequential(List<HDFSAclOperation> operations) { 596 return CompletableFuture.supplyAsync(() -> { 597 try { 598 for (HDFSAclOperation hdfsAclOperation : operations) { 599 handleHDFSAcl(hdfsAclOperation).get(); 600 } 601 } catch (InterruptedException | ExecutionException e) { 602 LOG.error("Set HDFS acl error", e); 603 } 604 return null; 605 }, pool); 606 } 607 608 private CompletableFuture<Void> handleHDFSAclParallel(List<HDFSAclOperation> operations) { 609 List<CompletableFuture<Void>> futures = 610 operations.stream().map(this::handleHDFSAcl).collect(Collectors.toList()); 611 return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); 612 } 613 614 private static AclEntry aclEntry(AclEntryScope scope, String name) { 615 return new AclEntry.Builder().setScope(scope) 616 .setType(AuthUtil.isGroupPrincipal(name) ? GROUP : USER).setName(name) 617 .setPermission(READ_EXECUTE).build(); 618 } 619 620 void createDirIfNotExist(Path path) throws IOException { 621 if (!fs.exists(path)) { 622 fs.mkdirs(path); 623 } 624 } 625 626 void deleteEmptyDir(Path path) throws IOException { 627 if (fs.exists(path) && fs.listStatus(path).length == 0) { 628 fs.delete(path, false); 629 } 630 } 631 632 /** 633 * Inner class used to describe modify or remove what type of acl entries(ACCESS, DEFAULT, 634 * ACCESS_AND_DEFAULT) for files or directories(and child files). 635 */ 636 private static class HDFSAclOperation { 637 enum OperationType { 638 MODIFY, 639 REMOVE 640 } 641 642 enum AclType { 643 ACCESS, 644 DEFAULT, 645 DEFAULT_ADN_ACCESS 646 } 647 648 private interface Operation { 649 void apply(FileSystem fs, Path path, List<AclEntry> aclList) throws IOException; 650 } 651 652 private FileSystem fs; 653 private Path path; 654 private Operation operation; 655 private boolean recursive; 656 private AclType aclType; 657 private List<AclEntry> defaultAndAccessAclEntries; 658 private List<AclEntry> accessAclEntries; 659 private List<AclEntry> defaultAclEntries; 660 661 HDFSAclOperation(FileSystem fs, Path path, Set<String> users, OperationType operationType, 662 boolean recursive, AclType aclType) { 663 this.fs = fs; 664 this.path = path; 665 this.defaultAndAccessAclEntries = getAclEntries(AclType.DEFAULT_ADN_ACCESS, users); 666 this.accessAclEntries = getAclEntries(AclType.ACCESS, users); 667 this.defaultAclEntries = getAclEntries(AclType.DEFAULT, users); 668 if (operationType == OperationType.MODIFY) { 669 operation = FileSystem::modifyAclEntries; 670 } else if (operationType == OperationType.REMOVE) { 671 operation = FileSystem::removeAclEntries; 672 } else { 673 throw new IllegalArgumentException("Illegal HDFS acl operation type: " + operationType); 674 } 675 this.recursive = recursive; 676 this.aclType = aclType; 677 } 678 679 HDFSAclOperation(Path path, HDFSAclOperation parent) { 680 this.fs = parent.fs; 681 this.path = path; 682 this.defaultAndAccessAclEntries = parent.defaultAndAccessAclEntries; 683 this.accessAclEntries = parent.accessAclEntries; 684 this.defaultAclEntries = parent.defaultAclEntries; 685 this.operation = parent.operation; 686 this.recursive = parent.recursive; 687 this.aclType = parent.aclType; 688 } 689 690 List<HDFSAclOperation> getChildAclOperations() throws IOException { 691 List<HDFSAclOperation> hdfsAclOperations = new ArrayList<>(); 692 if (recursive && fs.isDirectory(path)) { 693 FileStatus[] fileStatuses = fs.listStatus(path); 694 for (FileStatus fileStatus : fileStatuses) { 695 hdfsAclOperations.add(new HDFSAclOperation(fileStatus.getPath(), this)); 696 } 697 } 698 return hdfsAclOperations; 699 } 700 701 void handleAcl() throws IOException { 702 if (fs.exists(path)) { 703 if (fs.isDirectory(path)) { 704 switch (aclType) { 705 case ACCESS: 706 operation.apply(fs, path, accessAclEntries); 707 break; 708 case DEFAULT: 709 operation.apply(fs, path, defaultAclEntries); 710 break; 711 case DEFAULT_ADN_ACCESS: 712 operation.apply(fs, path, defaultAndAccessAclEntries); 713 break; 714 default: 715 throw new IllegalArgumentException("Illegal HDFS acl type: " + aclType); 716 } 717 } else { 718 operation.apply(fs, path, accessAclEntries); 719 } 720 } 721 } 722 723 private List<AclEntry> getAclEntries(AclType aclType, Set<String> users) { 724 List<AclEntry> aclEntries = new ArrayList<>(); 725 switch (aclType) { 726 case ACCESS: 727 for (String user : users) { 728 aclEntries.add(aclEntry(ACCESS, user)); 729 } 730 break; 731 case DEFAULT: 732 for (String user : users) { 733 aclEntries.add(aclEntry(DEFAULT, user)); 734 } 735 break; 736 case DEFAULT_ADN_ACCESS: 737 for (String user : users) { 738 aclEntries.add(aclEntry(ACCESS, user)); 739 aclEntries.add(aclEntry(DEFAULT, user)); 740 } 741 break; 742 default: 743 throw new IllegalArgumentException("Illegal HDFS acl type: " + aclType); 744 } 745 return aclEntries; 746 } 747 } 748 749 static final class PathHelper { 750 Configuration conf; 751 Path rootDir; 752 Path tmpDataDir; 753 Path dataDir; 754 Path mobDataDir; 755 Path archiveDataDir; 756 Path snapshotDir; 757 758 PathHelper(Configuration conf) { 759 this.conf = conf; 760 rootDir = new Path(conf.get(HConstants.HBASE_DIR)); 761 tmpDataDir = 762 new Path(new Path(rootDir, HConstants.HBASE_TEMP_DIRECTORY), HConstants.BASE_NAMESPACE_DIR); 763 dataDir = new Path(rootDir, HConstants.BASE_NAMESPACE_DIR); 764 mobDataDir = new Path(MobUtils.getMobHome(rootDir), HConstants.BASE_NAMESPACE_DIR); 765 archiveDataDir = new Path(new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY), 766 HConstants.BASE_NAMESPACE_DIR); 767 snapshotDir = new Path(rootDir, HConstants.SNAPSHOT_DIR_NAME); 768 } 769 770 Path getRootDir() { 771 return rootDir; 772 } 773 774 Path getDataDir() { 775 return dataDir; 776 } 777 778 Path getMobDir() { 779 return mobDataDir.getParent(); 780 } 781 782 Path getMobDataDir() { 783 return mobDataDir; 784 } 785 786 Path getTmpDir() { 787 return new Path(rootDir, HConstants.HBASE_TEMP_DIRECTORY); 788 } 789 790 Path getTmpDataDir() { 791 return tmpDataDir; 792 } 793 794 Path getArchiveDir() { 795 return new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY); 796 } 797 798 Path getArchiveDataDir() { 799 return archiveDataDir; 800 } 801 802 Path getDataNsDir(String namespace) { 803 return new Path(dataDir, namespace); 804 } 805 806 Path getMobDataNsDir(String namespace) { 807 return new Path(mobDataDir, namespace); 808 } 809 810 Path getDataTableDir(TableName tableName) { 811 return new Path(getDataNsDir(tableName.getNamespaceAsString()), 812 tableName.getQualifierAsString()); 813 } 814 815 Path getMobTableDir(TableName tableName) { 816 return new Path(getMobDataNsDir(tableName.getNamespaceAsString()), 817 tableName.getQualifierAsString()); 818 } 819 820 Path getArchiveNsDir(String namespace) { 821 return new Path(archiveDataDir, namespace); 822 } 823 824 Path getArchiveTableDir(TableName tableName) { 825 return new Path(getArchiveNsDir(tableName.getNamespaceAsString()), 826 tableName.getQualifierAsString()); 827 } 828 829 Path getTmpNsDir(String namespace) { 830 return new Path(tmpDataDir, namespace); 831 } 832 833 Path getTmpTableDir(TableName tableName) { 834 return new Path(getTmpNsDir(tableName.getNamespaceAsString()), 835 tableName.getQualifierAsString()); 836 } 837 838 Path getSnapshotRootDir() { 839 return snapshotDir; 840 } 841 842 Path getSnapshotDir(String snapshot) { 843 return new Path(snapshotDir, snapshot); 844 } 845 846 FileSystem getFileSystem() throws IOException { 847 return rootDir.getFileSystem(conf); 848 } 849 } 850}