directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject [23/50] [abbrv] directory-kerby git commit: DIRKRB-368 Fix findbugs Problems for kerby-tool module. Contributed by Yaning
Date Thu, 30 Jul 2015 06:24:30 GMT
DIRKRB-368 Fix findbugs Problems for kerby-tool module. Contributed by Yaning


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

Branch: refs/heads/pkinit-support
Commit: b2bf00d40e3004b13ee06e718af90e99426449e8
Parents: a6afb9a
Author: Lin <linchen7@foxmail.com>
Authored: Sun Jul 19 00:20:48 2015 +0800
Committer: Lin <linchen7@foxmail.com>
Committed: Sun Jul 19 00:20:48 2015 +0800

----------------------------------------------------------------------
 .../kerby/kerberos/tool/kinit/KinitTool.java    |  3 +-
 .../kerby/kerberos/tool/klist/KlistTool.java    |  8 ++++++
 .../kerby/kerberos/tool/token/TokenCache.java   | 30 ++++++++++++++------
 .../kerby/kerberos/tool/kadmin/KadminTool.java  |  6 ++--
 .../kerby/kerberos/tool/kadmin/ToolUtil.java    |  2 +-
 .../kadmin/command/AddPrincipalCommand.java     |  2 +-
 .../kadmin/command/ChangePasswordCommand.java   |  2 +-
 .../kadmin/command/DeletePrincipalCommand.java  |  2 +-
 .../kadmin/command/ListPrincipalCommand.java    | 13 ++++-----
 .../kadmin/command/ModifyPrincipalCommand.java  |  2 ++
 10 files changed, 45 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/kinit/KinitTool.java
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/kinit/KinitTool.java
b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/kinit/KinitTool.java
index c512296..932ed94 100644
--- a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/kinit/KinitTool.java
+++ b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/kinit/KinitTool.java
@@ -88,7 +88,7 @@ public class KinitTool {
                     + "maybe you're running this from within an IDE. "
                     + "Use scanner to read password.");
             System.out.println("Password for " + principal + ":");
-            try (Scanner scanner = new Scanner(System.in)) {
+            try (Scanner scanner = new Scanner(System.in, "UTF-8")) {
                 return scanner.nextLine().trim();
             }
         }
@@ -159,6 +159,7 @@ public class KinitTool {
                 kto = KinitOption.fromName(opt);
                 if (kto == KinitOption.NONE) {
                     error = "Invalid option:" + opt;
+                    System.err.println(error);
                     break;
                 }
             } else {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java
b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java
index 9359450..14d8a12 100644
--- a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java
+++ b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistTool.java
@@ -90,6 +90,14 @@ public class KlistTool {
             } catch (IOException e) {
                 System.err.println("Failed to open CredentialCache from file: " + fileName);
                 e.printStackTrace();
+            } finally {
+                try {
+                    if (cis != null) {
+                        cis.close();
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
 
         }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/token/TokenCache.java
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/token/TokenCache.java
b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/token/TokenCache.java
index e88085b..1235b8f 100644
--- a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/token/TokenCache.java
+++ b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/token/TokenCache.java
@@ -20,19 +20,23 @@
 package org.apache.kerby.kerberos.tool.token;
 
 
+import org.apache.commons.io.output.FileWriterWithEncoding;
+import org.apache.kerby.kerberos.kerb.KrbException;
+
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.Writer;
+import java.nio.charset.Charset;
 
 public class TokenCache {
     private static final String DEFAULT_TOKEN_CACHE_PATH = ".tokenauth";
     private static final String TOKEN_CACHE_FILE = ".tokenauth.token";
 
     public static String readToken(String tokenCacheFile) {
-        File cacheFile = null;
+        File cacheFile;
 
         if (tokenCacheFile != null && !tokenCacheFile.isEmpty()) {
             cacheFile = new File(tokenCacheFile);
@@ -48,14 +52,15 @@ public class TokenCache {
 
         String token = null;
         try {
-            BufferedReader reader = new BufferedReader(new FileReader(cacheFile));
+            BufferedReader reader = new BufferedReader(
+                    new InputStreamReader(new FileInputStream(cacheFile), Charset.forName("UTF-8")));
             String line = reader.readLine();
             reader.close();
             if (line != null) {
                 token = line;
             }
         } catch (IOException ex) { //NOPMD
-            System.out.println(ex);
+            ex.printStackTrace();
         }
 
         return token;
@@ -65,18 +70,25 @@ public class TokenCache {
         File cacheFile = getDefaultTokenCache();
 
         try {
-            Writer writer = new FileWriter(cacheFile);
-            writer.write(token.toString());
+            Writer writer = new FileWriterWithEncoding(cacheFile, Charset.forName("UTF-8"));
+            writer.write(token);
+            writer.flush();
             writer.close();
             // sets read-write permissions to owner only
             cacheFile.setReadable(false, false);
             cacheFile.setReadable(true, true);
-            cacheFile.setWritable(true, true);
+            if (!cacheFile.setWritable(true, true)) {
+                throw new KrbException("Cache file is not readable.");
+            }
         } catch (IOException ioe) {
             // if case of any error we just delete the cache, if user-only
             // write permissions are not properly set a security exception
             // is thrown and the file will be deleted.
-            cacheFile.delete();
+            if (cacheFile.delete()) {
+                System.err.println("Cache file is deleted.");
+            }
+        } catch (KrbException e) {
+            e.printStackTrace();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
index 5c85e0f..63ec1b3 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/KadminTool.java
@@ -156,11 +156,11 @@ public class KadminTool {
 
         System.out.print(PROMPT + ": ");
 
-        try (Scanner scanner = new Scanner(System.in)) {
+        try (Scanner scanner = new Scanner(System.in, "UTF-8")) {
             String input = scanner.nextLine();
 
-            boolean quit = input.equals("quit") || input.equals("exit") || input.equals("q");
-            while (!quit) {
+            while (!(input.equals("quit") || input.equals("exit")
+                    || input.equals("q"))) {
                 execute(kadmin, input);
                 System.out.print(PROMPT + ": ");
                 input = scanner.nextLine();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/ToolUtil.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/ToolUtil.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/ToolUtil.java
index 27668f3..943fdfc 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/ToolUtil.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/ToolUtil.java
@@ -77,7 +77,7 @@ public class ToolUtil {
     }
 
     public static String getReplay(String prompt) {
-        Scanner scanner = new Scanner(System.in);
+        Scanner scanner = new Scanner(System.in, "UTF-8");
         System.out.println(prompt);
         return scanner.nextLine().trim();
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/AddPrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/AddPrincipalCommand.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/AddPrincipalCommand.java
index 7c3aa6a..6814d2d 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/AddPrincipalCommand.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/AddPrincipalCommand.java
@@ -102,7 +102,7 @@ public class AddPrincipalCommand extends KadminCommand {
             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);
+            Scanner scanner = new Scanner(System.in, "UTF-8");
             passwordOnce = getPassword(scanner,
                     "Enter password for principal \"" + principal + "\":");
             passwordTwice = getPassword(scanner,

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ChangePasswordCommand.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ChangePasswordCommand.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ChangePasswordCommand.java
index 25a7c41..e271ca8 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ChangePasswordCommand.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ChangePasswordCommand.java
@@ -98,7 +98,7 @@ public class ChangePasswordCommand extends KadminCommand {
             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);
+            Scanner scanner = new Scanner(System.in, "UTF-8");
             passwordOnce = getPassword(scanner,
                     "Please enter new password  \"" + principal + "\":");
             passwordTwice = getPassword(scanner,

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/DeletePrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/DeletePrincipalCommand.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/DeletePrincipalCommand.java
index 77d8126..af2c0e2 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/DeletePrincipalCommand.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/DeletePrincipalCommand.java
@@ -59,7 +59,7 @@ public class DeletePrincipalCommand extends KadminCommand {
                 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);
+                Scanner scanner = new Scanner(System.in, "UTF-8");
                 reply = getReply(scanner, prompt);
             } else {
                 reply = getReply(console, prompt);

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ListPrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ListPrincipalCommand.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ListPrincipalCommand.java
index 46390de..aa72ff1 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ListPrincipalCommand.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ListPrincipalCommand.java
@@ -33,20 +33,17 @@ public class ListPrincipalCommand extends KadminCommand {
     @Override
     public void execute(String input) {
         String[] commands = input.split(" ");
-        List<String> principalNames = null;
-
 
         if (commands.length == 1) {
             try {
-                principalNames = getKadmin().getPrincipals();
+                List<String> principalNames = getKadmin().getPrincipals();
+                System.out.println("Principals are listed:");
+                for (String principalName : principalNames) {
+                    System.out.println(principalName);
+                }
             } catch (KrbException e) {
                 System.err.print("Fail to list principal!" + e.getMessage());
             }
         }
-        System.out.println("Principals are listed:");
-
-        for (String principalName : principalNames) {
-            System.out.println(principalName);
-        }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/b2bf00d4/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ModifyPrincipalCommand.java
----------------------------------------------------------------------
diff --git a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ModifyPrincipalCommand.java
b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ModifyPrincipalCommand.java
index 9dbf4a6..63415ab 100644
--- a/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ModifyPrincipalCommand.java
+++ b/kerby-tool/kdc-tool/src/main/java/org/apache/kerby/kerberos/tool/kadmin/command/ModifyPrincipalCommand.java
@@ -41,6 +41,7 @@ public class ModifyPrincipalCommand extends KadminCommand {
 
     public ModifyPrincipalCommand(Kadmin kadmin) {
         super(kadmin);
+        this.kOptions = new KOptions();
     }
 
     @Override
@@ -71,6 +72,7 @@ public class ModifyPrincipalCommand extends KadminCommand {
                 kOption = KadminOption.fromName(opt);
                 if (kOption == KadminOption.NONE) {
                     error = "Invalid option:" + opt;
+                    System.err.println(error);
                     break;
                 }
             } else {


Mime
View raw message