2828import java .io .FileNotFoundException ;
2929import java .io .IOException ;
3030import java .util .ArrayList ;
31+ import java .util .Collections ;
3132import java .util .HashSet ;
3233import java .util .List ;
3334import java .util .Map ;
5253import org .apache .hadoop .hbase .client .Connection ;
5354import org .apache .hadoop .hbase .client .SnapshotDescription ;
5455import org .apache .hadoop .hbase .client .TableDescriptor ;
56+ import org .apache .hadoop .hbase .coprocessor .CoprocessorHost ;
5557import org .apache .hadoop .hbase .mob .MobUtils ;
5658import org .apache .hadoop .hbase .util .Bytes ;
5759import org .apache .yetus .audience .InterfaceAudience ;
7072public class SnapshotScannerHDFSAclHelper implements Closeable {
7173 private static final Logger LOG = LoggerFactory .getLogger (SnapshotScannerHDFSAclHelper .class );
7274
73- public static final String USER_SCAN_SNAPSHOT_ENABLE = "hbase.user.scan.snapshot .enable" ;
74- public static final String USER_SCAN_SNAPSHOT_THREAD_NUMBER =
75- "hbase.user.scan.snapshot .thread.number" ;
75+ public static final String ACL_SYNC_TO_HDFS_ENABLE = "hbase.acl.sync.to.hdfs .enable" ;
76+ public static final String ACL_SYNC_TO_HDFS_THREAD_NUMBER =
77+ "hbase.acl.sync.to.hdfs .thread.number" ;
7678 // The tmp directory to restore snapshot, it can not be a sub directory of HBase root dir
7779 public static final String SNAPSHOT_RESTORE_TMP_DIR = "hbase.snapshot.restore.tmp.dir" ;
7880 public static final String SNAPSHOT_RESTORE_TMP_DIR_DEFAULT =
7981 "/hbase/.tmpdir-to-restore-snapshot" ;
8082 // The default permission of the common directories if the feature is enabled.
8183 public static final String COMMON_DIRECTORY_PERMISSION =
82- "hbase.user.scan.snapshot .common.directory.permission" ;
84+ "hbase.acl.sync.to.hdfs .common.directory.permission" ;
8385 // The secure HBase permission is 700, 751 means all others have execute access and the mask is
8486 // set to read-execute to make the extended access ACL entries can work. Be cautious to set
8587 // this value.
8688 public static final String COMMON_DIRECTORY_PERMISSION_DEFAULT = "751" ;
8789 // The default permission of the snapshot restore directories if the feature is enabled.
8890 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION =
89- "hbase.user.scan.snapshot .restore.directory.permission" ;
91+ "hbase.acl.sync.to.hdfs .restore.directory.permission" ;
9092 // 753 means all others have write-execute access.
9193 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION_DEFAULT = "753" ;
9294
@@ -101,7 +103,7 @@ public SnapshotScannerHDFSAclHelper(Configuration configuration, Connection conn
101103 this .conf = configuration ;
102104 this .pathHelper = new PathHelper (conf );
103105 this .fs = pathHelper .getFileSystem ();
104- this .pool = Executors .newFixedThreadPool (conf .getInt (USER_SCAN_SNAPSHOT_THREAD_NUMBER , 10 ),
106+ this .pool = Executors .newFixedThreadPool (conf .getInt (ACL_SYNC_TO_HDFS_THREAD_NUMBER , 10 ),
105107 new ThreadFactoryBuilder ().setNameFormat ("hdfs-acl-thread-%d" ).setDaemon (true ).build ());
106108 this .admin = connection .getAdmin ();
107109 }
@@ -233,6 +235,50 @@ public boolean removeNamespaceAccessAcl(TableName tableName, Set<String> removeU
233235 }
234236 }
235237
238+ /**
239+ * Remove default acl from namespace archive dir when delete namespace
240+ * @param namespace the namespace
241+ * @param removeUsers the users whose default acl will be removed
242+ * @return false if an error occurred, otherwise true
243+ */
244+ public boolean removeNamespaceDefaultAcl (String namespace , Set <String > removeUsers ) {
245+ try {
246+ long start = System .currentTimeMillis ();
247+ Path archiveNsDir = pathHelper .getArchiveNsDir (namespace );
248+ HDFSAclOperation operation = new HDFSAclOperation (fs , archiveNsDir , removeUsers ,
249+ HDFSAclOperation .OperationType .REMOVE , false , HDFSAclOperation .AclType .DEFAULT );
250+ operation .handleAcl ();
251+ LOG .info ("Remove HDFS acl when delete namespace {}, cost {} ms" , namespace ,
252+ System .currentTimeMillis () - start );
253+ return true ;
254+ } catch (Exception e ) {
255+ LOG .error ("Remove HDFS acl error when delete namespace {}" , namespace , e );
256+ return false ;
257+ }
258+ }
259+
260+ /**
261+ * Remove default acl from table archive dir when delete table
262+ * @param tableName the table name
263+ * @param removeUsers the users whose default acl will be removed
264+ * @return false if an error occurred, otherwise true
265+ */
266+ public boolean removeTableDefaultAcl (TableName tableName , Set <String > removeUsers ) {
267+ try {
268+ long start = System .currentTimeMillis ();
269+ Path archiveTableDir = pathHelper .getArchiveTableDir (tableName );
270+ HDFSAclOperation operation = new HDFSAclOperation (fs , archiveTableDir , removeUsers ,
271+ HDFSAclOperation .OperationType .REMOVE , false , HDFSAclOperation .AclType .DEFAULT );
272+ operation .handleAcl ();
273+ LOG .info ("Remove HDFS acl when delete table {}, cost {} ms" , tableName ,
274+ System .currentTimeMillis () - start );
275+ return true ;
276+ } catch (Exception e ) {
277+ LOG .error ("Remove HDFS acl error when delete table {}" , tableName , e );
278+ return false ;
279+ }
280+ }
281+
236282 /**
237283 * Add table user acls
238284 * @param tableName the table
@@ -352,7 +398,7 @@ private void handleNamespaceAcl(Set<String> namespaces, Set<String> users,
352398 Set <TableName > tables = new HashSet <>();
353399 for (String namespace : namespaces ) {
354400 tables .addAll (admin .listTableDescriptorsByNamespace (Bytes .toBytes (namespace )).stream ()
355- .filter (this ::isTableUserScanSnapshotEnabled ).map (TableDescriptor ::getTableName )
401+ .filter (this ::isAclSyncToHdfsEnabled ).map (TableDescriptor ::getTableName )
356402 .collect (Collectors .toSet ()));
357403 }
358404 handleTableAcl (tables , users , skipNamespaces , skipTables , operationType );
@@ -406,7 +452,7 @@ void createTableDirectories(TableName tableName) throws IOException {
406452 * return paths that user will global permission will visit
407453 * @return the path list
408454 */
409- private List <Path > getGlobalRootPaths () {
455+ List <Path > getGlobalRootPaths () {
410456 return Lists .newArrayList (pathHelper .getTmpDataDir (), pathHelper .getDataDir (),
411457 pathHelper .getMobDataDir (), pathHelper .getArchiveDataDir (), pathHelper .getSnapshotRootDir ());
412458 }
@@ -514,9 +560,20 @@ boolean isNotFamilyOrQualifierPermission(TablePermission tablePermission) {
514560 return !tablePermission .hasFamily () && !tablePermission .hasQualifier ();
515561 }
516562
517- boolean isTableUserScanSnapshotEnabled (TableDescriptor tableDescriptor ) {
563+ public static boolean isAclSyncToHdfsEnabled (Configuration conf ) {
564+ String [] masterCoprocessors = conf .getStrings (CoprocessorHost .MASTER_COPROCESSOR_CONF_KEY );
565+ Set <String > masterCoprocessorSet = new HashSet <>();
566+ if (masterCoprocessors != null ) {
567+ Collections .addAll (masterCoprocessorSet , masterCoprocessors );
568+ }
569+ return conf .getBoolean (SnapshotScannerHDFSAclHelper .ACL_SYNC_TO_HDFS_ENABLE , false )
570+ && masterCoprocessorSet .contains (SnapshotScannerHDFSAclController .class .getName ())
571+ && masterCoprocessorSet .contains (AccessController .class .getName ());
572+ }
573+
574+ boolean isAclSyncToHdfsEnabled (TableDescriptor tableDescriptor ) {
518575 return tableDescriptor == null ? false
519- : Boolean .valueOf (tableDescriptor .getValue (USER_SCAN_SNAPSHOT_ENABLE ));
576+ : Boolean .valueOf (tableDescriptor .getValue (ACL_SYNC_TO_HDFS_ENABLE ));
520577 }
521578
522579 PathHelper getPathHelper () {
0 commit comments