diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterAndRegionObserver.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterAndRegionObserver.java index 03bcb782b6e..afc6ecfd9c3 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterAndRegionObserver.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterAndRegionObserver.java @@ -372,6 +372,16 @@ public abstract class BaseMasterAndRegionObserver extends BaseRegionObserver throws IOException { } + @Override + public void preListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + } + + @Override + public void postListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + } + @Override public void preCloneSnapshot(final ObserverContext ctx, final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterObserver.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterObserver.java index dbe99f51f49..3f0ffc21b0c 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterObserver.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/BaseMasterObserver.java @@ -365,6 +365,16 @@ public class BaseMasterObserver implements MasterObserver { throws IOException { } + @Override + public void preListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + } + + @Override + public void postListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + } + @Override public void preCloneSnapshot(final ObserverContext ctx, final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/MasterObserver.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/MasterObserver.java index 3086cf60dd4..1e002109587 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/MasterObserver.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/MasterObserver.java @@ -597,6 +597,26 @@ public interface MasterObserver extends Coprocessor { final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) throws IOException; + /** + * Called before listSnapshots request has been processed. + * It can't bypass the default action, e.g., ctx.bypass() won't have effect. + * @param ctx the environment to interact with the framework and master + * @param snapshot the SnapshotDescriptor of the snapshot to list + * @throws IOException + */ + void preListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException; + + /** + * Called after listSnapshots request has been processed. + * It can't bypass the default action, e.g., ctx.bypass() won't have effect. + * @param ctx the environment to interact with the framework and master + * @param snapshot the SnapshotDescriptor of the snapshot to list + * @throws IOException + */ + void postListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException; + /** * Called before a snapshot is cloned. * Called as part of restoreSnapshot RPC call. diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java index 293d9d5d3b0..31a5dabcfa1 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java @@ -727,6 +727,26 @@ public class MasterCoprocessorHost }); } + public void preListSnapshot(final SnapshotDescription snapshot) throws IOException { + execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() { + @Override + public void call(MasterObserver observer, ObserverContext ctx) + throws IOException { + observer.preListSnapshot(ctx, snapshot); + } + }); + } + + public void postListSnapshot(final SnapshotDescription snapshot) throws IOException { + execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() { + @Override + public void call(MasterObserver observer, ObserverContext ctx) + throws IOException { + observer.postListSnapshot(ctx, snapshot); + } + }); + } + public void preCloneSnapshot(final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) throws IOException { execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/snapshot/SnapshotManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/snapshot/SnapshotManager.java index 5099cc5d67e..65c7670cac0 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/snapshot/SnapshotManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/snapshot/SnapshotManager.java @@ -31,19 +31,19 @@ import java.util.concurrent.ThreadPoolExecutor; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.hadoop.hbase.classification.InterfaceAudience; -import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; -import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.HBaseInterfaceAudience; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.HTableDescriptor; -import org.apache.hadoop.hbase.Stoppable; import org.apache.hadoop.hbase.MetaTableAccessor; +import org.apache.hadoop.hbase.Stoppable; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; import org.apache.hadoop.hbase.client.TableState; import org.apache.hadoop.hbase.errorhandling.ForeignException; import org.apache.hadoop.hbase.executor.ExecutorService; @@ -65,7 +65,7 @@ import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair; import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ProcedureDescription; import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription; import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription.Type; -import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos; +import org.apache.hadoop.hbase.security.AccessDeniedException; import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils; import org.apache.hadoop.hbase.snapshot.HBaseSnapshotException; @@ -214,6 +214,7 @@ public class SnapshotManager extends MasterProcedureManager implements Stoppable // ignore all the snapshots in progress FileStatus[] snapshots = fs.listStatus(snapshotDir, new SnapshotDescriptionUtils.CompletedSnaphotDirectoriesFilter(fs)); + MasterCoprocessorHost cpHost = master.getMasterCoprocessorHost(); // loop through all the completed snapshots for (FileStatus snapshot : snapshots) { Path info = new Path(snapshot.getPath(), SnapshotDescriptionUtils.SNAPSHOTINFO_FILE); @@ -226,7 +227,22 @@ public class SnapshotManager extends MasterProcedureManager implements Stoppable try { in = fs.open(info); SnapshotDescription desc = SnapshotDescription.parseFrom(in); + if (cpHost != null) { + try { + cpHost.preListSnapshot(desc); + } catch (AccessDeniedException e) { + LOG.warn("Current user does not have access to " + desc.getName() + " snapshot. " + + "Either you should be owner of this snapshot or admin user."); + // Skip this and try for next snapshot + continue; + } + } snapshotDescs.add(desc); + + // call coproc post hook + if (cpHost != null) { + cpHost.postListSnapshot(desc); + } } catch (IOException e) { LOG.warn("Found a corrupted snapshot " + snapshot.getPath(), e); } finally { diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java index 455408ad4be..1c6a34147f8 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/access/AccessController.java @@ -1103,6 +1103,16 @@ public class AccessController extends BaseMasterAndRegionObserver Permission.Action.ADMIN); } + @Override + public void preListSnapshot(ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + if (SnapshotDescriptionUtils.isSnapshotOwner(snapshot, getActiveUser())) { + // list it, if user is the owner of snapshot + } else { + requirePermission("listSnapshot", Action.ADMIN); + } + } + @Override public void preCloneSnapshot(final ObserverContext ctx, final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestMasterObserver.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestMasterObserver.java index 4a5fa633018..8fc4471142b 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestMasterObserver.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestMasterObserver.java @@ -122,6 +122,8 @@ public class TestMasterObserver { private boolean stopCalled; private boolean preSnapshotCalled; private boolean postSnapshotCalled; + private boolean preListSnapshotCalled; + private boolean postListSnapshotCalled; private boolean preCloneSnapshotCalled; private boolean postCloneSnapshotCalled; private boolean preRestoreSnapshotCalled; @@ -192,6 +194,8 @@ public class TestMasterObserver { postBalanceSwitchCalled = false; preSnapshotCalled = false; postSnapshotCalled = false; + preListSnapshotCalled = false; + postListSnapshotCalled = false; preCloneSnapshotCalled = false; postCloneSnapshotCalled = false; preRestoreSnapshotCalled = false; @@ -705,6 +709,22 @@ public class TestMasterObserver { return preSnapshotCalled && postSnapshotCalled; } + @Override + public void preListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + preListSnapshotCalled = true; + } + + @Override + public void postListSnapshot(final ObserverContext ctx, + final SnapshotDescription snapshot) throws IOException { + postListSnapshotCalled = true; + } + + public boolean wasListSnapshotCalled() { + return preListSnapshotCalled && postListSnapshotCalled; + } + @Override public void preCloneSnapshot(final ObserverContext ctx, final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) @@ -1315,6 +1335,11 @@ public class TestMasterObserver { assertTrue("Coprocessor should have been called on snapshot", cp.wasSnapshotCalled()); + //Test list operation + admin.listSnapshots(); + assertTrue("Coprocessor should have been called on snapshot list", + cp.wasListSnapshotCalled()); + // Test clone operation admin.cloneSnapshot(TEST_SNAPSHOT, TEST_CLONE); assertTrue("Coprocessor should have been called on snapshot clone",