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 ;
5354import org .apache .hadoop .hbase .client .Connection ;
5455import org .apache .hadoop .hbase .client .SnapshotDescription ;
5556import org .apache .hadoop .hbase .client .TableDescriptor ;
57+ import org .apache .hadoop .hbase .coprocessor .CoprocessorHost ;
5658import org .apache .hadoop .hbase .mob .MobUtils ;
5759import org .apache .hadoop .hbase .util .Bytes ;
5860import org .apache .yetus .audience .InterfaceAudience ;
7173public class SnapshotScannerHDFSAclHelper implements Closeable {
7274 private static final Logger LOG = LoggerFactory .getLogger (SnapshotScannerHDFSAclHelper .class );
7375
74- public static final String USER_SCAN_SNAPSHOT_ENABLE = "hbase.user.scan.snapshot .enable" ;
75- public static final String USER_SCAN_SNAPSHOT_THREAD_NUMBER =
76- "hbase.user.scan.snapshot .thread.number" ;
76+ public static final String ACL_SYNC_TO_HDFS_ENABLE = "hbase.acl.sync.to.hdfs .enable" ;
77+ public static final String ACL_SYNC_TO_HDFS_THREAD_NUMBER =
78+ "hbase.acl.sync.to.hdfs .thread.number" ;
7779 // The tmp directory to restore snapshot, it can not be a sub directory of HBase root dir
7880 public static final String SNAPSHOT_RESTORE_TMP_DIR = "hbase.snapshot.restore.tmp.dir" ;
7981 public static final String SNAPSHOT_RESTORE_TMP_DIR_DEFAULT =
8082 "/hbase/.tmpdir-to-restore-snapshot" ;
8183 // The default permission of the common directories if the feature is enabled.
8284 public static final String COMMON_DIRECTORY_PERMISSION =
83- "hbase.user.scan.snapshot .common.directory.permission" ;
85+ "hbase.acl.sync.to.hdfs .common.directory.permission" ;
8486 // The secure HBase permission is 700, 751 means all others have execute access and the mask is
8587 // set to read-execute to make the extended access ACL entries can work. Be cautious to set
8688 // this value.
8789 public static final String COMMON_DIRECTORY_PERMISSION_DEFAULT = "751" ;
8890 // The default permission of the snapshot restore directories if the feature is enabled.
8991 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION =
90- "hbase.user.scan.snapshot .restore.directory.permission" ;
92+ "hbase.acl.sync.to.hdfs .restore.directory.permission" ;
9193 // 753 means all others have write-execute access.
9294 public static final String SNAPSHOT_RESTORE_DIRECTORY_PERMISSION_DEFAULT = "753" ;
9395
@@ -102,7 +104,7 @@ public SnapshotScannerHDFSAclHelper(Configuration configuration, Connection conn
102104 this .conf = configuration ;
103105 this .pathHelper = new PathHelper (conf );
104106 this .fs = pathHelper .getFileSystem ();
105- this .pool = Executors .newFixedThreadPool (conf .getInt (USER_SCAN_SNAPSHOT_THREAD_NUMBER , 10 ),
107+ this .pool = Executors .newFixedThreadPool (conf .getInt (ACL_SYNC_TO_HDFS_THREAD_NUMBER , 10 ),
106108 new ThreadFactoryBuilder ().setNameFormat ("hdfs-acl-thread-%d" ).setDaemon (true ).build ());
107109 this .admin = connection .getAdmin ();
108110 }
@@ -230,6 +232,50 @@ public boolean removeNamespaceAccessAcl(TableName tableName, Set<String> removeU
230232 }
231233 }
232234
235+ /**
236+ * Remove default acl from namespace archive dir when delete namespace
237+ * @param namespace the namespace
238+ * @param removeUsers the users whose default acl will be removed
239+ * @return false if an error occurred, otherwise true
240+ */
241+ public boolean removeNamespaceDefaultAcl (String namespace , Set <String > removeUsers ) {
242+ try {
243+ long start = System .currentTimeMillis ();
244+ Path archiveNsDir = pathHelper .getArchiveNsDir (namespace );
245+ HDFSAclOperation operation = new HDFSAclOperation (fs , archiveNsDir , removeUsers ,
246+ HDFSAclOperation .OperationType .REMOVE , false , HDFSAclOperation .AclType .DEFAULT );
247+ operation .handleAcl ();
248+ LOG .info ("Remove HDFS acl when delete namespace {}, cost {} ms" , namespace ,
249+ System .currentTimeMillis () - start );
250+ return true ;
251+ } catch (Exception e ) {
252+ LOG .error ("Remove HDFS acl error when delete namespace {}" , namespace , e );
253+ return false ;
254+ }
255+ }
256+
257+ /**
258+ * Remove default acl from table archive dir when delete table
259+ * @param tableName the table name
260+ * @param removeUsers the users whose default acl will be removed
261+ * @return false if an error occurred, otherwise true
262+ */
263+ public boolean removeTableDefaultAcl (TableName tableName , Set <String > removeUsers ) {
264+ try {
265+ long start = System .currentTimeMillis ();
266+ Path archiveTableDir = pathHelper .getArchiveTableDir (tableName );
267+ HDFSAclOperation operation = new HDFSAclOperation (fs , archiveTableDir , removeUsers ,
268+ HDFSAclOperation .OperationType .REMOVE , false , HDFSAclOperation .AclType .DEFAULT );
269+ operation .handleAcl ();
270+ LOG .info ("Remove HDFS acl when delete table {}, cost {} ms" , tableName ,
271+ System .currentTimeMillis () - start );
272+ return true ;
273+ } catch (Exception e ) {
274+ LOG .error ("Remove HDFS acl error when delete table {}" , tableName , e );
275+ return false ;
276+ }
277+ }
278+
233279 /**
234280 * Add table user acls
235281 * @param tableName the table
@@ -349,7 +395,7 @@ private void handleNamespaceAcl(Set<String> namespaces, Set<String> users,
349395 Set <TableName > tables = new HashSet <>();
350396 for (String namespace : namespaces ) {
351397 tables .addAll (admin .listTableDescriptorsByNamespace (Bytes .toBytes (namespace )).stream ()
352- .filter (this ::isTableUserScanSnapshotEnabled ).map (TableDescriptor ::getTableName )
398+ .filter (this ::isAclSyncToHdfsEnabled ).map (TableDescriptor ::getTableName )
353399 .collect (Collectors .toSet ()));
354400 }
355401 handleTableAcl (tables , users , skipNamespaces , skipTables , operationType );
@@ -403,7 +449,7 @@ void createTableDirectories(TableName tableName) throws IOException {
403449 * return paths that user will global permission will visit
404450 * @return the path list
405451 */
406- private List <Path > getGlobalRootPaths () {
452+ List <Path > getGlobalRootPaths () {
407453 return Lists .newArrayList (pathHelper .getTmpDataDir (), pathHelper .getDataDir (),
408454 pathHelper .getMobDataDir (), pathHelper .getArchiveDataDir (), pathHelper .getSnapshotRootDir ());
409455 }
@@ -511,9 +557,20 @@ boolean isNotFamilyOrQualifierPermission(TablePermission tablePermission) {
511557 return !tablePermission .hasFamily () && !tablePermission .hasQualifier ();
512558 }
513559
514- boolean isTableUserScanSnapshotEnabled (TableDescriptor tableDescriptor ) {
560+ public static boolean isAclSyncToHdfsEnabled (Configuration conf ) {
561+ String [] masterCoprocessors = conf .getStrings (CoprocessorHost .MASTER_COPROCESSOR_CONF_KEY );
562+ Set <String > masterCoprocessorSet = new HashSet <>();
563+ if (masterCoprocessors != null ) {
564+ Collections .addAll (masterCoprocessorSet , masterCoprocessors );
565+ }
566+ return conf .getBoolean (SnapshotScannerHDFSAclHelper .ACL_SYNC_TO_HDFS_ENABLE , false )
567+ && masterCoprocessorSet .contains (SnapshotScannerHDFSAclController .class .getName ())
568+ && masterCoprocessorSet .contains (AccessController .class .getName ());
569+ }
570+
571+ boolean isAclSyncToHdfsEnabled (TableDescriptor tableDescriptor ) {
515572 return tableDescriptor == null ? false
516- : Boolean .valueOf (tableDescriptor .getValue (USER_SCAN_SNAPSHOT_ENABLE ));
573+ : Boolean .valueOf (tableDescriptor .getValue (ACL_SYNC_TO_HDFS_ENABLE ));
517574 }
518575
519576 PathHelper getPathHelper () {
0 commit comments