directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject directory-kerby git commit: DIRKRB-573 Implement Rename_Principal in Remote Kadmin. Contributed by Yan.
Date Thu, 26 May 2016 08:57:15 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/kadmin-remote c962f4ab4 -> 946948080


DIRKRB-573 Implement Rename_Principal in Remote Kadmin. Contributed by Yan.


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

Branch: refs/heads/kadmin-remote
Commit: 9469480801b27ce33424b645d1a04a616c25043c
Parents: c962f4a
Author: plusplusjiajia <jiajia.li@intel.com>
Authored: Thu May 26 17:02:34 2016 +0800
Committer: plusplusjiajia <jiajia.li@intel.com>
Committed: Thu May 26 17:02:34 2016 +0800

----------------------------------------------------------------------
 .../admin/server/kadmin/AdminServerHandler.java | 38 +++++++++
 .../kerberos/kerb/admin/RemoteAdminTool.java    | 83 +++++++------------
 .../kerb/admin/kadmin/remote/AdminClient.java   |  5 ++
 .../kerb/admin/kadmin/remote/AdminHandler.java  |  9 +++
 .../admin/kadmin/remote/RemoteKadminImpl.java   |  6 +-
 .../command/RemoteAddPrincipalCommand.java      | 65 +++++++++++++++
 .../kadmin/remote/command/RemoteCommand.java    | 41 ++++++++++
 .../command/RemoteDeletePrincipalCommand.java   | 83 +++++++++++++++++++
 .../command/RemoteRenamePrincipalCommand.java   | 85 ++++++++++++++++++++
 .../remote/request/RenamePrincipalRequest.java  | 75 +++++++++++++++++
 .../kerb/admin/tool/AdminMessageType.java       | 15 ++--
 .../kerb/admin/tool/RenamePrincipalRep.java     | 29 +++++++
 .../kerb/admin/tool/RenamePrincipalReq.java     | 29 +++++++
 13 files changed, 501 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
b/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
index 0252dd0..1032644 100644
--- a/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
+++ b/kerby-kerb/kerb-admin-server/src/main/java/org/apache/kerby/kerberos/kerb/admin/server/kadmin/AdminServerHandler.java
@@ -78,6 +78,10 @@ public class AdminServerHandler {
                 System.out.println("message type: delete principal req");
                 responseMessage = handleDeletePrincipalReq(localKadmin, fieldInfos);
                 break;
+            case RENAME_PRINCIPAL_REQ:
+                System.out.println("message type: rename principal req");
+                responseMessage = handleRenamePrincipalReq(localKadmin, fieldInfos);
+                break;
             default:
                 throw new KrbException("AdminMessageType error, can not handle it.");
         }
@@ -183,4 +187,38 @@ public class AdminServerHandler {
         return responseMessage;
     }
 
+    private ByteBuffer handleRenamePrincipalReq(LocalKadmin localKadmin, XdrFieldInfo[] fieldInfos)
throws IOException {
+        /** message structure: msg_type, para_num(always equals 2), old name, new name*/
+
+        String[] oldPrincipalName = ((String) fieldInfos[2].getValue()).split("@");
+        String[] newPrincipalName = ((String) fieldInfos[3].getValue()).split("@");
+
+        try {
+            localKadmin.renamePrincipal(oldPrincipalName[0], newPrincipalName[0]);
+        } catch (KrbException e) {
+            String error = "the old principal name does not exist, or the new principal name"
+                + " already exists, rename failed.";
+            System.err.println(error);
+            XdrFieldInfo[] xdrFieldInfos = new XdrFieldInfo[3];
+            xdrFieldInfos[0] = new XdrFieldInfo(0, XdrDataType.ENUM, AdminMessageType.RENAME_PRINCIPAL_REP);
+            xdrFieldInfos[1] = new XdrFieldInfo(1, XdrDataType.INTEGER, 1);
+            xdrFieldInfos[2] = new XdrFieldInfo(2, XdrDataType.STRING, error);
+            AdminMessageCode value = new AdminMessageCode(xdrFieldInfos);
+            AdminMessage errorMessage = new RenamePrincipalRep();
+            errorMessage.setMessageBuffer(ByteBuffer.wrap(value.encode()));
+            ByteBuffer response = KadminCode.encodeMessage(errorMessage);
+            return response;
+        }
+
+        String message = "rename " + oldPrincipalName[0] + " to " + newPrincipalName[0];
+        AdminMessage renamePrincipalRep = new RenamePrincipalRep();
+        XdrFieldInfo[] xdrFieldInfos = new XdrFieldInfo[3];
+        xdrFieldInfos[0] = new XdrFieldInfo(0, XdrDataType.ENUM, AdminMessageType.RENAME_PRINCIPAL_REP);
+        xdrFieldInfos[1] = new XdrFieldInfo(1, XdrDataType.INTEGER, 1);
+        xdrFieldInfos[2] = new XdrFieldInfo(2, XdrDataType.STRING, message);
+        AdminMessageCode value = new AdminMessageCode(xdrFieldInfos);
+        renamePrincipalRep.setMessageBuffer(ByteBuffer.wrap(value.encode()));
+        ByteBuffer responseMessage = KadminCode.encodeMessage(renamePrincipalRep);
+        return responseMessage;
+    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/RemoteAdminTool.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/RemoteAdminTool.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/RemoteAdminTool.java
index 0d60e38..fbeda4a 100644
--- a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/RemoteAdminTool.java
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/RemoteAdminTool.java
@@ -22,9 +22,12 @@ package org.apache.kerby.kerberos.kerb.admin;
 import org.apache.kerby.kerberos.kerb.KrbException;
 import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminClient;
 import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminConfig;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.command.RemoteAddPrincipalCommand;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.command.RemoteCommand;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.command.RemoteDeletePrincipalCommand;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.command.RemoteRenamePrincipalCommand;
 import org.apache.kerby.util.OSUtil;
 
-import java.io.Console;
 import java.io.File;
 import java.util.Scanner;
 
@@ -41,11 +44,14 @@ public class RemoteAdminTool {
         ? "bin\\remoteAdmin.cmd" : "sh bin/remoteAdmin.sh")
         + " conf\n";
 
-    private static final String COMMAND = "Usage: add_principal [options] <principal-name>\n"
-        + "delete_principal <principal_name>\n"
-        + "\tExample:\n"
-        + "\t\tadd_principal -pw mypassword alice\n"
-        + "\t\tdelete_principal alice\n";
+    private static final String LEGAL_COMMANDS = "Available commands are: "
+        + "\n"
+        + "add_principal, addprinc\n"
+        + "                         Add principal\n"
+        + "delete_principal, delprinc\n"
+        + "                         Delete principal\n"
+        + "rename_principal, renprinc\n"
+        + "                         Rename principal\n";
 
     public static void main(String[] args) throws Exception {
         AdminClient adminClient;
@@ -90,62 +96,27 @@ public class RemoteAdminTool {
     private static void excute(AdminClient adminClient, String input) throws KrbException
{
         input = input.trim();
         if (input.startsWith("command")) {
-            System.out.println(COMMAND);
+            System.out.println(LEGAL_COMMANDS);
             return;
         }
 
-        String[] temp = input.split("\\s+");
-
-        if (temp[0].startsWith("add_principal")) {
-            String adminRealm = adminClient.getAdminConfig().getAdminRealm();
-            String clientPrincipal = temp[temp.length - 1] + "@" + adminRealm;
-            if (!temp[1].startsWith("-")) {
-                adminClient.requestAddPrincipal(clientPrincipal);
-            } else if (temp[1].startsWith("-nokey")) {
-                adminClient.requestAddPrincipal(clientPrincipal);
-            } else if (temp[1].startsWith("-pw")) {
-                String password = temp[2];
-                adminClient.requestAddPrincipal(clientPrincipal, password);
-            } else {
-                System.out.println("add_principal command format error.\n"
-                + "Please input command for further reference.");
-            }
-
-        } else if (temp[0].startsWith("delete_principal")) {
-            String principal = temp[1] + "@"
-                + adminClient.getAdminConfig().getAdminRealm();
-            String reply;
-            Console console = System.console();
-            String prompt = "Are you sure 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, "UTF-8");
-                reply = getReply(scanner, prompt);
-            } else {
-                reply = getReply(console, prompt);
-            }
-            if (reply.equals("yes") || reply.equals("YES") || reply.equals("y") || reply.equals("Y"))
{
-                adminClient.requestDeletePrincipal(principal);
-            } else if (reply.equals("no") || reply.equals("NO") || reply.equals("n") || reply.equals("N"))
{
-                System.out.println("Principal \"" + principal + "\"  not deleted.");
-            } else {
-                System.err.println("Unknown request, fail to delete the principal.");
-            }
+        RemoteCommand executor = null;
+
+        if (input.startsWith("add_principal")
+            || input.startsWith("addprinc")) {
+            executor = new RemoteAddPrincipalCommand(adminClient);
+        } else if (input.startsWith("delete_principal")
+            || input.startsWith("delprinc")) {
+            executor = new RemoteDeletePrincipalCommand(adminClient);
+        } else if (input.startsWith("rename_principal")
+            || input.startsWith("renprinc")) {
+            executor = new RemoteRenamePrincipalCommand(adminClient);
         } else {
-            System.out.println("remain to be developed...");
+            System.out.println(LEGAL_COMMANDS);
+            return;
         }
+        executor.execute(input);
     }
 
-    private static String getReply(Scanner scanner, String prompt) {
-        System.out.println(prompt);
-        return scanner.nextLine().trim();
-    }
 
-    private static String getReply(Console console, String prompt) {
-        console.printf(prompt);
-        String line = console.readLine();
-        return line;
-    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminClient.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminClient.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminClient.java
index 790196d..f482262 100644
--- a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminClient.java
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminClient.java
@@ -179,4 +179,9 @@ public class AdminClient {
         Kadmin remote = new RemoteKadminImpl(innerClient);
         remote.deletePrincipal(principal);
     }
+
+    public void requestRenamePrincipal(String oldPrincipal, String newPrincipal) throws KrbException
{
+        Kadmin remote = new RemoteKadminImpl(innerClient);
+        remote.renamePrincipal(oldPrincipal, newPrincipal);
+    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminHandler.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminHandler.java
index 6c83242..0a64914 100644
--- a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminHandler.java
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/AdminHandler.java
@@ -101,6 +101,15 @@ public abstract class AdminHandler {
                     + AdminMessageType.DELETE_PRINCIPAL_REP);
                 }
                 break;
+            case RENAME_PRINCIPAL_REP:
+                if (adminRequest.getAdminReq().getAdminMessageType()
+                    == AdminMessageType.RENAME_PRINCIPAL_REQ) {
+                    System.out.println((String) fieldInfos[2].getValue());
+                } else {
+                    throw new KrbException("Response message type error: need "
+                    + AdminMessageType.RENAME_PRINCIPAL_REP);
+                }
+                break;
             default:
                 throw new KrbException("Response message type error: " + type);
         }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/RemoteKadminImpl.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/RemoteKadminImpl.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/RemoteKadminImpl.java
index 1ee6513..915287e 100644
--- a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/RemoteKadminImpl.java
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/RemoteKadminImpl.java
@@ -27,6 +27,7 @@ import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.impl.InternalAdminClie
 import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.request.AddPrincipalRequest;
 import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.request.AdminRequest;
 import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.request.DeletePrincipalRequest;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.request.RenamePrincipalRequest;
 import org.apache.kerby.kerberos.kerb.transport.KrbNetwork;
 import org.apache.kerby.kerberos.kerb.transport.KrbTransport;
 import org.apache.kerby.kerberos.kerb.transport.TransportPair;
@@ -165,7 +166,10 @@ public class RemoteKadminImpl implements Kadmin {
     @Override
     public void renamePrincipal(String oldPrincipalName,
                                 String newPrincipalName) throws KrbException {
-
+        AdminRequest renamePrincipalRequest =  new RenamePrincipalRequest(oldPrincipalName,
newPrincipalName);
+        renamePrincipalRequest.setTransport(transport);
+        AdminHandler adminHandler = new DefaultAdminHandler();
+        adminHandler.handleRequest(renamePrincipalRequest);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteAddPrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteAddPrincipalCommand.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteAddPrincipalCommand.java
new file mode 100644
index 0000000..d3d27a5
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteAddPrincipalCommand.java
@@ -0,0 +1,65 @@
+/**
+ *  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.kerb.admin.kadmin.remote.command;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminClient;
+
+/**
+ * Remote add principal command
+ */
+public class RemoteAddPrincipalCommand extends RemoteCommand {
+
+    public static final String USAGE = "Usage: add_principal [options] <principal-name>\n"
+        + "\toptions are:\n"
+        + "\t\t[-randkey|-nokey]\n"
+        + "\t\t[-pw password]"
+        + "\tExample:\n"
+        + "\t\tadd_principal -pw mypassword alice\n";
+
+    public RemoteAddPrincipalCommand(AdminClient adminClient) {
+        super(adminClient);
+    }
+
+    @Override
+    public void execute(String input) throws KrbException {
+        String[] items = input.split("\\s+");
+
+        if (items.length < 2) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        String adminRealm = adminClient.getAdminConfig().getAdminRealm();
+        String clientPrincipal = items[items.length - 1] + "@" + adminRealm;
+
+        if (!items[1].startsWith("-")) {
+            adminClient.requestAddPrincipal(clientPrincipal);
+        } else if (items[1].startsWith("-nokey")) {
+            adminClient.requestAddPrincipal(clientPrincipal);
+        } else if (items[1].startsWith("-pw")) {
+            String password = items[2];
+            adminClient.requestAddPrincipal(clientPrincipal, password);
+        } else {
+            System.err.println("add_principal command format error.");
+            System.err.println(USAGE);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteCommand.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteCommand.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteCommand.java
new file mode 100644
index 0000000..d5ffe0f
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteCommand.java
@@ -0,0 +1,41 @@
+/**
+ *  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.kerb.admin.kadmin.remote.command;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminClient;
+
+/**
+ * Abstract class of all remote kadmin commands
+ */
+public abstract class RemoteCommand {
+
+    AdminClient adminClient;
+
+    public RemoteCommand(AdminClient adminClient) {
+        this.adminClient = adminClient;
+    }
+
+    /**
+     * Execute the remote kadmin command
+     * @param input String includes commands
+     */
+    public abstract void execute(String input) throws KrbException;
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteDeletePrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteDeletePrincipalCommand.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteDeletePrincipalCommand.java
new file mode 100644
index 0000000..e6368bd
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteDeletePrincipalCommand.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.kerby.kerberos.kerb.admin.kadmin.remote.command;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminClient;
+
+import java.io.Console;
+import java.util.Scanner;
+
+/**
+ * Remote delete principal command
+ */
+public class RemoteDeletePrincipalCommand extends RemoteCommand {
+
+    public static final String USAGE = "Usage: delete_principal <principal-name>\n"
+        + "\tExample:\n"
+        + "\t\tdelete_principal alice\n";
+
+    public RemoteDeletePrincipalCommand(AdminClient adminClient) {
+        super(adminClient);
+    }
+
+    @Override
+    public void execute(String input) throws KrbException {
+        String[] items = input.split("\\s+");
+        if (items.length < 2) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        String principal = items[items.length - 1] + "@"
+            + adminClient.getAdminConfig().getAdminRealm();
+        String reply;
+        Console console = System.console();
+        String prompt = "Are you sure 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, "UTF-8");
+            reply = getReply(scanner, prompt);
+        } else {
+            reply = getReply(console, prompt);
+        }
+        if (reply.equals("yes") || reply.equals("YES") || reply.equals("y") || reply.equals("Y"))
{
+            adminClient.requestDeletePrincipal(principal);
+        } else if (reply.equals("no") || reply.equals("NO") || reply.equals("n") || reply.equals("N"))
{
+            System.out.println("Principal \"" + principal + "\"  not deleted.");
+        } else {
+            System.err.println("Unknown request, fail to delete the principal.");
+            System.err.println(USAGE);
+        }
+    }
+
+    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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteRenamePrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteRenamePrincipalCommand.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteRenamePrincipalCommand.java
new file mode 100644
index 0000000..fd0cd61
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/command/RemoteRenamePrincipalCommand.java
@@ -0,0 +1,85 @@
+/**
+ *  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.kerb.admin.kadmin.remote.command;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.admin.kadmin.remote.AdminClient;
+
+import java.io.Console;
+import java.util.Scanner;
+
+/**
+ * Remote rename principal command
+ */
+public class RemoteRenamePrincipalCommand extends RemoteCommand {
+    public static final String USAGE = "Usage: rename_principal <old_principal_name>"
+        + " <new_principal_name>\n"
+        + "\tExample:\n"
+        + "\t\trename_principal alice bob\n";
+
+    public RemoteRenamePrincipalCommand(AdminClient adminClient) {
+        super(adminClient);
+    }
+
+    @Override
+    public void execute(String input) throws KrbException {
+        String[] items = input.split("\\s+");
+        if (items.length < 3) {
+            System.err.println(USAGE);
+            return;
+        }
+
+        String adminRealm = adminClient.getAdminConfig().getAdminRealm();
+        String oldPrincipalName = items[items.length - 2] + "@" + adminRealm;
+        String newPrincipalName = items[items.length - 1] + "@" + adminRealm;
+
+        String reply;
+        Console console = System.console();
+        String prompt = "Are you sure to rename 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, "UTF-8");
+            reply = getReply(scanner, prompt);
+        } else {
+            reply = getReply(console, prompt);
+        }
+        if (reply.equals("yes") || reply.equals("YES") || reply.equals("y") || reply.equals("Y"))
{
+            adminClient.requestRenamePrincipal(oldPrincipalName, newPrincipalName);
+        } else if (reply.equals("no") || reply.equals("NO") || reply.equals("n") || reply.equals("N"))
{
+            System.out.println("Principal \"" + oldPrincipalName + "\"  not renamed.");
+        } else {
+            System.err.println("Unknown request, fail to rename the principal.");
+            System.err.println(USAGE);
+        }
+    }
+
+    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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/request/RenamePrincipalRequest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/request/RenamePrincipalRequest.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/request/RenamePrincipalRequest.java
new file mode 100644
index 0000000..5473578
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/kadmin/remote/request/RenamePrincipalRequest.java
@@ -0,0 +1,75 @@
+/**
+ *  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.kerb.admin.kadmin.remote.request;
+
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.admin.tool.AdminMessageCode;
+import org.apache.kerby.kerberos.kerb.admin.tool.AdminMessageType;
+import org.apache.kerby.kerberos.kerb.admin.tool.RenamePrincipalReq;
+import org.apache.kerby.xdr.XdrDataType;
+import org.apache.kerby.xdr.XdrFieldInfo;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+/**
+ * RenamePrincipal request.
+ */
+public class RenamePrincipalRequest extends AdminRequest {
+    String newPrincipalName;
+
+    public RenamePrincipalRequest(String oldPrincipalName, String newPrincipalName) {
+        super(oldPrincipalName);
+        this.newPrincipalName = newPrincipalName;
+    }
+
+    @Override
+    public void process() throws KrbException {
+        super.process();
+
+        RenamePrincipalReq renamePrincipalReq = new RenamePrincipalReq();
+
+        /** encode admin message:
+         *  encode type
+         *  encode paranum
+         *  encode old principal name
+         *  encode new principal name
+         */
+        int paramNum = 2;
+        XdrFieldInfo[] xdrFieldInfos = new XdrFieldInfo[paramNum + 2];
+        xdrFieldInfos[0] = new XdrFieldInfo(0, XdrDataType.ENUM, AdminMessageType.RENAME_PRINCIPAL_REQ);
+        xdrFieldInfos[1] = new XdrFieldInfo(1, XdrDataType.INTEGER, paramNum);
+        xdrFieldInfos[2] = new XdrFieldInfo(2, XdrDataType.STRING, getPrincipal());
+        xdrFieldInfos[3] = new XdrFieldInfo(3, XdrDataType.STRING, newPrincipalName);
+
+        AdminMessageCode value = new AdminMessageCode(xdrFieldInfos);
+        byte[] encodeBytes;
+        try {
+            encodeBytes = value.encode();
+        } catch (IOException e) {
+            throw new KrbException("Xdr encode error when generate rename principal request.",
e);
+        }
+        ByteBuffer messageBuffer = ByteBuffer.wrap(encodeBytes);
+        renamePrincipalReq.setMessageBuffer(messageBuffer);
+
+        setAdminReq(renamePrincipalReq);
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/AdminMessageType.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/AdminMessageType.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/AdminMessageType.java
index 52cbc6e..5630d13 100644
--- a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/AdminMessageType.java
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/AdminMessageType.java
@@ -24,10 +24,13 @@ import org.apache.kerby.xdr.EnumType;
 /**
  * Type of Admin Message:
  * NONE(-1)
- * AD_REQ(0) add principal request
- * AD_REP(1) add principal reply
- * DE_REQ(2),
- * DE_REP(3);
+ * ADD_PRINCIPAL_REQ(0) add principal request
+ * ADD_PRINCIPAL_REP(1) add principal reply
+ * DELETE_PRINCIPAL_REQ(2),
+ * DELETE_PRINCIPAL_REP(3);
+ * RENAME_PRINCIPAL_REQ(4),
+ * RENAME_PRINCIPAL_REP(5);
+ *
  */
 
 public enum AdminMessageType implements EnumType {
@@ -35,7 +38,9 @@ public enum AdminMessageType implements EnumType {
     ADD_PRINCIPAL_REQ(0),
     ADD_PRINCIPAL_REP(1),
     DELETE_PRINCIPAL_REQ(2),
-    DELETE_PRINCIPAL_REP(3);
+    DELETE_PRINCIPAL_REP(3),
+    RENAME_PRINCIPAL_REQ(4),
+    RENAME_PRINCIPAL_REP(5);
 
     private int value;
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalRep.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalRep.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalRep.java
new file mode 100644
index 0000000..831b8fe
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalRep.java
@@ -0,0 +1,29 @@
+/**
+ *  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.kerb.admin.tool;
+
+/**
+ * Rename principal reply, to general admin message
+ */
+public class RenamePrincipalRep extends AdminRep {
+    public RenamePrincipalRep() {
+        super(AdminMessageType.RENAME_PRINCIPAL_REP);
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/94694808/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalReq.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalReq.java
b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalReq.java
new file mode 100644
index 0000000..d146b12
--- /dev/null
+++ b/kerby-kerb/kerb-admin/src/main/java/org/apache/kerby/kerberos/kerb/admin/tool/RenamePrincipalReq.java
@@ -0,0 +1,29 @@
+/**
+ *  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.kerb.admin.tool;
+
+/**
+ * Rename principal request, to general admin message
+ */
+public class RenamePrincipalReq extends AdminReq {
+    public RenamePrincipalReq() {
+        super(AdminMessageType.RENAME_PRINCIPAL_REQ);
+    }
+}


Mime
View raw message