You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ha...@apache.org on 2015/04/21 16:43:27 UTC

directory-kerby git commit: DIRKRB-224 Enhance kadmin to support delete principal. Contributed by Jiajia

Repository: directory-kerby
Updated Branches:
  refs/heads/master d4367fa28 -> a53cdda57


DIRKRB-224 Enhance kadmin to support delete principal. Contributed by Jiajia


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

Branch: refs/heads/master
Commit: a53cdda57f8cf32a91f0f4bb49794eaf85eaeeee
Parents: d4367fa
Author: Lin <li...@foxmail.com>
Authored: Tue Apr 21 22:43:01 2015 +0800
Committer: Lin <li...@foxmail.com>
Committed: Tue Apr 21 22:43:01 2015 +0800

----------------------------------------------------------------------
 .../identitybackend/JsonIdentityBackend.java    |   6 +-
 .../kerby/kerberos/tool/kadmin/Kadmin.java      |   5 +-
 .../executor/DeletePrincipalExecutor.java       | 105 +++++++++++++++++++
 3 files changed, 114 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a53cdda5/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
----------------------------------------------------------------------
diff --git a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
index 7db5ffd..5b76ee5 100644
--- a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
+++ b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
@@ -147,7 +147,11 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
     @Override
     protected void doDeleteIdentity(String principalName) {
         checkAndLoad();
-        ids.remove(principalName);
+        if (ids.containsKey(principalName)) {
+            ids.remove(principalName);
+        } else {
+            throw new RuntimeException("Principal does not exist.");
+        }
         idsToFile(ids);
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a53cdda5/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/Kadmin.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/Kadmin.java b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/Kadmin.java
index c77b76e..33737b8 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/Kadmin.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/Kadmin.java
@@ -22,6 +22,7 @@ package org.apache.kerby.kerberos.tool.kadmin;
 import org.apache.kerby.config.Conf;
 import org.apache.kerby.kerberos.kerb.server.KdcConfig;
 import org.apache.kerby.kerberos.tool.kadmin.executor.AddPrincipalExecutor;
+import org.apache.kerby.kerberos.tool.kadmin.executor.DeletePrincipalExecutor;
 import org.apache.kerby.kerberos.tool.kadmin.executor.KadminCommandExecutor;
 import org.apache.kerby.kerberos.tool.kadmin.executor.KeytabAddExecutor;
 
@@ -82,8 +83,10 @@ public class Kadmin {
         } else if (command.startsWith("ktadd") ||
                 command.startsWith("xst")) {
             executor = new KeytabAddExecutor(backendConfig);
+        } else if (command.startsWith("delete_principal") ||
+                command.startsWith("delprinc")) {
+            executor = new DeletePrincipalExecutor(backendConfig);
         }
-
         if (executor == null) {
             System.out.println("Unknown request \"" + command + "\". Type \"?\" for a request list.");
             return;

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a53cdda5/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/executor/DeletePrincipalExecutor.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/executor/DeletePrincipalExecutor.java b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/executor/DeletePrincipalExecutor.java
new file mode 100644
index 0000000..c363e82
--- /dev/null
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/executor/DeletePrincipalExecutor.java
@@ -0,0 +1,105 @@
+/**
+ *  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.kerby.kerberos.tool.kadmin.executor;
+
+import org.apache.kerby.config.Config;
+import org.apache.kerby.kerberos.kerb.identity.backend.IdentityBackend;
+import org.apache.kerby.kerberos.tool.kadmin.tool.KadminTool;
+
+import java.io.Console;
+import java.util.Scanner;
+
+public class DeletePrincipalExecutor implements KadminCommandExecutor{
+
+    private static final String USAGE = "Usage: delete_principal [options] principal\n" +
+            "This command prompts for deletion, unless the -force option is given.\n" +
+            "\toptions are:\n" +
+            "\t\t[-force]" + " no prompts for deletion.";
+
+    private Config backendConfig;
+    private Boolean force = false;
+
+    public DeletePrincipalExecutor(Config backendConfig) {
+        this.backendConfig = backendConfig;
+    }
+
+    @Override
+    public void execute(String input) {
+        String[] commands = input.split(" ");
+        if (commands.length < 2) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        parseOptions(commands);
+        String principal = commands[commands.length - 1];
+
+        if (force) {
+            deletePrincipal(principal);
+        } else {
+            String reply;
+            Console console = System.console();
+            String prompt = "Are you sure want to delete the principal? (yes/no, YES/NO, y/n, Y/N) ";
+            if (console == null) {
+                System.out.println("Couldn't get Console instance, " +
+                    "maybe you're running this from within an IDE. " +
+                    "Use scanner to read password.");
+                Scanner scanner = new Scanner(System.in);
+                reply = getReply(scanner, prompt);
+            } else {
+                reply = getReply(console, prompt);
+            }
+            if (reply.equals("yes") || reply.equals("YES") || reply.equals("y") || reply.equals("Y")) {
+                deletePrincipal(principal);
+            } else if (reply.equals("no") || reply.equals("NO") || reply.equals("n") || reply.equals("N")) {
+                System.out.println("Pincipal \"" + principal + "\"  not deleted." );
+            } else {
+                System.err.println("Unknow request, fail to delete the principal.");
+            }
+        }
+    }
+
+    private String getReply(Scanner scanner, String prompt) {
+        System.out.println(prompt);
+        return scanner.nextLine().trim();
+    }
+
+    private String getReply(Console console, String prompt) {
+        console.printf(prompt);
+        String line = console.readLine();
+        return line;
+    }
+
+    private void parseOptions(String[] commands) {
+        if (commands[1].equals("-force")) {
+            force = true;
+        }
+    }
+
+    private void deletePrincipal(String principal) {
+        IdentityBackend backend = KadminTool.getBackend(backendConfig);
+        try {
+            backend.deleteIdentity(principal);
+            System.out.println("Principal \"" + principal + "\" deleted.");
+        } catch (Exception e) {
+            System.err.println("Principal \"" + principal + "\" fail to delete." + e.getMessage());
+        }
+    }
+}