You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by el...@apache.org on 2014/06/12 17:47:44 UTC

[1/2] git commit: ACCUMULO-1376 Shell verifies before dropping user

Repository: accumulo
Updated Branches:
  refs/heads/master 97cac218c -> 3bb70a694


ACCUMULO-1376 Shell verifies before dropping user

Signed-off-by: Josh Elser <el...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/f3d73910
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/f3d73910
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/f3d73910

Branch: refs/heads/master
Commit: f3d739102a8f3f9688c7236e964ee287911bb895
Parents: 97cac21
Author: Vincent Russell <vr...@texeltek.com>
Authored: Thu Jun 12 11:37:04 2014 -0400
Committer: Josh Elser <el...@apache.org>
Committed: Thu Jun 12 11:39:01 2014 -0400

----------------------------------------------------------------------
 .../shell/commands/DropUserCommand.java         | 44 ++++++++++++++++----
 .../org/apache/accumulo/test/ShellServerIT.java |  3 +-
 2 files changed, 39 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/f3d73910/shell/src/main/java/org/apache/accumulo/shell/commands/DropUserCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/accumulo/shell/commands/DropUserCommand.java b/shell/src/main/java/org/apache/accumulo/shell/commands/DropUserCommand.java
index 7de216d..35fa37a 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/commands/DropUserCommand.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/commands/DropUserCommand.java
@@ -16,6 +16,7 @@
  */
 package org.apache.accumulo.shell.commands;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.Set;
 
@@ -23,39 +24,68 @@ import org.apache.accumulo.core.client.AccumuloException;
 import org.apache.accumulo.core.client.AccumuloSecurityException;
 import org.apache.accumulo.core.util.BadArgumentException;
 import org.apache.accumulo.shell.Shell;
-import org.apache.accumulo.shell.Token;
 import org.apache.accumulo.shell.Shell.Command;
+import org.apache.accumulo.shell.Token;
 import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
 
 public class DropUserCommand extends Command {
+  private Option forceOpt;
+
   @Override
   public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException {
     final String user = cl.getArgs()[0];
     if (shellState.getConnector().whoami().equals(user)) {
       throw new BadArgumentException("You cannot delete yourself", fullCommand, fullCommand.indexOf(user));
     }
-    shellState.getConnector().securityOperations().dropLocalUser(user);
-    Shell.log.debug("Deleted user " + user);
+    doDropUser(shellState, user, cl.hasOption(forceOpt.getOpt()));
     return 0;
   }
-  
+
+  private void doDropUser(final Shell shellState, final String user, final boolean force) throws AccumuloException, AccumuloSecurityException {
+    boolean operate = true;
+
+    try {
+      if (!force) {
+        shellState.getReader().flush();
+        String line = shellState.getReader().readLine(getName() + " { " + user + " } (yes|no)? ");
+        operate = line != null && (line.equalsIgnoreCase("y") || line.equalsIgnoreCase("yes"));
+      }
+      if (operate) {
+        shellState.getConnector().securityOperations().dropLocalUser(user);
+        Shell.log.debug("Deleted user " + user);
+      }
+    } catch (IOException e) {
+      throw new AccumuloException(e);
+    }
+  }
+
   @Override
   public String description() {
     return "deletes a user";
   }
-  
+
   @Override
   public String usage() {
     return getName() + " <username>";
   }
-  
+
   @Override
   public void registerCompletion(final Token root, final Map<Command.CompletionSet,Set<String>> completionSet) {
     registerCompletionForUsers(root, completionSet);
   }
-  
+
   @Override
   public int numArgs() {
     return 1;
   }
+
+  @Override
+  public Options getOptions() {
+    forceOpt = new Option("f", "force", false, "force deletion without prompting");
+    final Options opts = super.getOptions();
+    opts.addOption(forceOpt);
+    return opts;
+  }
 }

http://git-wip-us.apache.org/repos/asf/accumulo/blob/f3d73910/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java b/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
index bff8c3a..f980641 100644
--- a/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
+++ b/test/src/test/java/org/apache/accumulo/test/ShellServerIT.java
@@ -417,7 +417,8 @@ public class ShellServerIT extends SimpleMacIT {
     ts.exec("revoke -u xyzzy -t " + MetadataTable.NAME + " Table.WRITE", true);
     ts.exec("revoke -u xyzzy -t " + MetadataTable.NAME + " Table.GOOFY", false);
     ts.exec("revoke -u xyzzy -t " + MetadataTable.NAME + " foo", false);
-    ts.exec("deleteuser xyzzy", true);
+    ts.exec("deleteuser xyzzy", true, "deleteuser { xyzzy } (yes|no)?", true);
+    ts.exec("deleteuser -f xyzzy", true);
     ts.exec("users", true, "xyzzy", false);
   }
 


[2/2] git commit: ACCUMULO-1376 Add a unit test that ensure that the user is prompted w/o the force option when dropping a user

Posted by el...@apache.org.
ACCUMULO-1376 Add a unit test that ensure that the user is prompted w/o the force option when dropping a user


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/3bb70a69
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/3bb70a69
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/3bb70a69

Branch: refs/heads/master
Commit: 3bb70a694103a7290cdebcd4dbbed0935a0b5a56
Parents: f3d7391
Author: Josh Elser <el...@apache.org>
Authored: Thu Jun 12 11:39:15 2014 -0400
Committer: Josh Elser <el...@apache.org>
Committed: Thu Jun 12 11:39:15 2014 -0400

----------------------------------------------------------------------
 .../shell/command/DropUserCommandTest.java      | 83 ++++++++++++++++++++
 1 file changed, 83 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/3bb70a69/shell/src/test/java/org/apache/accumulo/shell/command/DropUserCommandTest.java
----------------------------------------------------------------------
diff --git a/shell/src/test/java/org/apache/accumulo/shell/command/DropUserCommandTest.java b/shell/src/test/java/org/apache/accumulo/shell/command/DropUserCommandTest.java
new file mode 100644
index 0000000..a204075
--- /dev/null
+++ b/shell/src/test/java/org/apache/accumulo/shell/command/DropUserCommandTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.accumulo.shell.command;
+
+import jline.console.ConsoleReader;
+
+import org.apache.accumulo.core.client.Connector;
+import org.apache.accumulo.core.client.admin.SecurityOperations;
+import org.apache.accumulo.shell.Shell;
+import org.apache.accumulo.shell.commands.DropUserCommand;
+import org.apache.commons.cli.CommandLine;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * 
+ */
+public class DropUserCommandTest {
+
+  private DropUserCommand cmd;
+
+  @Before
+  public void setup() {
+    cmd  = new DropUserCommand();
+
+    // Initialize that internal state
+    cmd.getOptions();
+  }
+
+  @Test
+  public void dropUserWithoutForcePrompts() throws Exception {
+    Connector conn = EasyMock.createMock(Connector.class);
+    CommandLine cli = EasyMock.createMock(CommandLine.class);
+    Shell shellState = EasyMock.createMock(Shell.class);
+    ConsoleReader reader = EasyMock.createMock(ConsoleReader.class);
+    SecurityOperations secOps = EasyMock.createMock(SecurityOperations.class);
+
+    EasyMock.expect(shellState.getConnector()).andReturn(conn);
+
+    // The user we want to remove 
+    EasyMock.expect(cli.getArgs()).andReturn(new String[] {"user"});
+
+    // We're the root user
+    EasyMock.expect(conn.whoami()).andReturn("root");
+
+    // Force option was not provided
+    EasyMock.expect(cli.hasOption("f")).andReturn(false);
+    EasyMock.expect(shellState.getReader()).andReturn(reader);
+    reader.flush();
+    EasyMock.expectLastCall().once();
+
+    // Fake a "yes" response
+    EasyMock.expect(shellState.getReader()).andReturn(reader);
+    EasyMock.expect(reader.readLine(EasyMock.anyObject(String.class))).andReturn("yes");
+    EasyMock.expect(shellState.getConnector()).andReturn(conn);
+
+    EasyMock.expect(conn.securityOperations()).andReturn(secOps);
+    secOps.dropLocalUser("user");
+    EasyMock.expectLastCall();
+
+    EasyMock.replay(conn, cli, shellState, reader, secOps);
+    
+    cmd.execute("dropuser foo -f", cli, shellState);
+
+    EasyMock.verify(conn, cli, shellState, reader, secOps);
+  }
+
+}