openinx commented on a change in pull request #163: HBASE-21995 Add a 
coprocessor to set HDFS ACL for hbase granted user
URL: https://github.com/apache/hbase/pull/163#discussion_r295102461
 
 

 ##########
 File path: 
hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/SnapshotScannerHDFSAclController.java
 ##########
 @@ -0,0 +1,667 @@
+/**
+ * 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.hadoop.hbase.security.access;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.CellUtil;
+import org.apache.hadoop.hbase.HBaseInterfaceAudience;
+import org.apache.hadoop.hbase.NamespaceDescriptor;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.TableNotFoundException;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
+import org.apache.hadoop.hbase.client.Delete;
+import org.apache.hadoop.hbase.client.Get;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.Result;
+import org.apache.hadoop.hbase.client.ResultScanner;
+import org.apache.hadoop.hbase.client.Scan;
+import org.apache.hadoop.hbase.client.SnapshotDescription;
+import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
+import org.apache.hadoop.hbase.coprocessor.CoreCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.HasMasterServices;
+import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.MasterObserver;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.master.MasterServices;
+import org.apache.hadoop.hbase.security.User;
+import org.apache.hadoop.hbase.security.UserProvider;
+import org.apache.hadoop.hbase.security.access.Permission.Action;
+import 
org.apache.hadoop.hbase.security.access.SnapshotScannerHDFSAclHelper.PathHelper;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Set HDFS ACLs to hFiles to make HBase granted users have permission to scan 
snapshot
+ * <p>
+ * To use this feature, please mask sure HDFS config:
+ * <ul>
+ * <li>dfs.permissions.enabled = true</li>
+ * <li>fs.permissions.umask-mode = 027 (or smaller umask than 027)</li>
+ * </ul>
+ * </p>
+ * <p>
+ * The implementation of this feature is as followings:
+ * <ul>
+ * <li>For common directories such as 'data' and 'archive', set other 
permission to '--x' to make
+ * everyone have the permission to access the directory.</li>
+ * <li>For namespace or table directories such as 'data/ns/table', 
'archive/ns/table' and
+ * '.hbase-snapshot/snapshotName', set user 'r-x' access acl and 'r-x' default 
acl when following
+ * operations happen:
+ * <ul>
+ * <li>grant user with global, namespace or table permission;</li>
+ * <li>revoke user from global, namespace or table;</li>
+ * <li>snapshot table;</li>
+ * <li>truncate table;</li>
+ * </ul>
+ * </li>
+ * <li>Note: Because snapshots are at table level, so this feature just 
considers users with global,
+ * namespace or table permissions, ignores users with table CF or cell 
permissions.</li>
+ * </ul>
+ * </p>
+ */
+@CoreCoprocessor
+@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
+public class SnapshotScannerHDFSAclController implements MasterCoprocessor, 
MasterObserver {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SnapshotScannerHDFSAclController.class);
+
+  private SnapshotScannerHDFSAclHelper hdfsAclHelper = null;
+  private PathHelper pathHelper = null;
+  private FileSystem fs = null;
+  private volatile boolean initialized = false;
+  /** Provider for mapping principal names to Users */
+  private UserProvider userProvider;
+
+  @Override
+  public Optional<MasterObserver> getMasterObserver() {
+    return Optional.of(this);
+  }
+
+  @Override
+  public void 
preMasterInitialization(ObserverContext<MasterCoprocessorEnvironment> c)
+      throws IOException {
+    if (c.getEnvironment().getConfiguration()
+        .getBoolean(SnapshotScannerHDFSAclHelper.USER_SCAN_SNAPSHOT_ENABLE, 
false)) {
+      MasterCoprocessorEnvironment mEnv = c.getEnvironment();
+      if (!(mEnv instanceof HasMasterServices)) {
+        throw new IOException("Does not implement HMasterServices");
+      }
+      MasterServices masterServices = ((HasMasterServices) 
mEnv).getMasterServices();
+      hdfsAclHelper = new 
SnapshotScannerHDFSAclHelper(masterServices.getConfiguration(),
+          masterServices.getConnection());
+      pathHelper = hdfsAclHelper.getPathHelper();
+      fs = pathHelper.getFileSystem();
+      hdfsAclHelper.setCommonDirectoryPermission();
+      initialized = true;
+      userProvider = 
UserProvider.instantiate(c.getEnvironment().getConfiguration());
+    } else {
+      LOG.warn(
+        "Load SnapshotScannerHDFSAclController but 
hbase.user.scan.snapshot.enable is false");
+    }
+  }
+
+  @Override
+  public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> c) 
throws IOException {
+    if (checkInitialized()) {
+      try (Admin admin = c.getEnvironment().getConnection().getAdmin()) {
+        if (admin.tableExists(PermissionStorage.ACL_TABLE_NAME)) {
+          // Check if hbase acl table has 'm' CF, if not, add 'm' CF
+          TableDescriptor tableDescriptor = 
admin.getDescriptor(PermissionStorage.ACL_TABLE_NAME);
+          boolean containHdfsAclFamily =
+              
Arrays.stream(tableDescriptor.getColumnFamilies()).anyMatch(family -> Bytes
+                  .equals(family.getName(), 
SnapshotScannerHDFSAclStorage.HDFS_ACL_FAMILY));
+          if (!containHdfsAclFamily) {
+            TableDescriptorBuilder builder = 
TableDescriptorBuilder.newBuilder(tableDescriptor)
+                .setColumnFamily(ColumnFamilyDescriptorBuilder
+                    
.newBuilder(SnapshotScannerHDFSAclStorage.HDFS_ACL_FAMILY).build());
+            admin.modifyTable(builder.build());
+          }
+        } else {
+          LOG.error(
 
 Review comment:
   Can just throw a TableNotFoundException with the current log error message  
? no need an extra LOG#error  I think. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to