You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by lm...@apache.org on 2017/02/05 20:13:20 UTC

knox git commit: added error message for failed auth for token acquisition

Repository: knox
Updated Branches:
  refs/heads/knoxinit 9032f6416 -> fef119af6


added error message for failed auth for token acquisition 

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

Branch: refs/heads/knoxinit
Commit: fef119af69f8145fefd0ab68d18afbbb5d98728e
Parents: 9032f64
Author: Larry McCay <lm...@hortonworks.com>
Authored: Sun Feb 5 15:13:07 2017 -0500
Committer: Larry McCay <lm...@hortonworks.com>
Committed: Sun Feb 5 15:13:07 2017 -0500

----------------------------------------------------------------------
 .../org/apache/hadoop/gateway/shell/KnoxSh.java | 63 ++++++++++++--------
 1 file changed, 37 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/fef119af/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
index 468b6b7..68fe771 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/KnoxSh.java
@@ -35,6 +35,8 @@ import java.util.Date;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+
+import org.apache.hadoop.gateway.shell.knox.token.Get.Response;
 import org.apache.hadoop.gateway.shell.knox.token.Token;
 import org.apache.hadoop.gateway.util.JsonUtils;
 
@@ -170,33 +172,36 @@ public class KnoxSh {
 
       String username = credentials.get("user").string();
       String pass = credentials.get("pass").string();
-
-      if (gateway == null) {
-        gateway = System.getenv("GATEWAY_HOME");
+      
+      Hadoop session = null;
+      Response response = null;
+      try {
+        session = Hadoop.login(gateway, username, pass);
+  
+        response = Token.get( session ).now();
+        String text = response.getString();
+        Map<String, String> json = JsonUtils.getMapFromJsonString(text);
+  
+        //println "Access Token: " + json.access_token
+        System.out.println("knoxinit successful!");
+        displayTokenDetails(json);
+  
+        File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
+        FileOutputStream fos = new FileOutputStream(tokenfile);
+        fos.write(text.getBytes("UTF-8"));
+  
+        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
+        fos.close();
+  
+        //add owners permission only
+        perms.add(PosixFilePermission.OWNER_READ);
+        perms.add(PosixFilePermission.OWNER_WRITE);
+  
+        Files.setPosixFilePermissions(Paths.get(System.getProperty("user.home") + "/.knoxtokencache"), perms);
+      }
+      catch(HadoopException he) {
+        System.out.println("Failuire to acquire token. Please verify your credentials and Knox URL and try again.");
       }
-
-      Hadoop session = Hadoop.login(gateway, username, pass);
-
-      String text = Token.get( session ).now().getString();
-      Map<String, String> json = JsonUtils.getMapFromJsonString(text);
-
-      //println "Access Token: " + json.access_token
-      System.out.println("knoxinit successful!");
-      displayTokenDetails(json);
-
-      File tokenfile = new File(System.getProperty("user.home"), ".knoxtokencache");
-      FileOutputStream fos = new FileOutputStream(tokenfile);
-      fos.write(text.getBytes("UTF-8"));
-
-      Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
-      fos.close();
-
-      //add owners permission only
-      perms.add(PosixFilePermission.OWNER_READ);
-      perms.add(PosixFilePermission.OWNER_WRITE);
-
-      Files.setPosixFilePermissions(Paths.get(System.getProperty("user.home") + "/.knoxtokencache"), perms);
-
       session.shutdown();
     }
 
@@ -259,6 +264,12 @@ public class KnoxSh {
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     System.out.println("Expires On: " + formatter.format(calendar.getTime()));
+    String targetUrl = json.get("target_url");
+    if (targetUrl != null) {
+      System.out.println("Target URL: " + json.get("target_url"));
+    } else {
+      System.out.println("No specific target URL configured.");
+    }
   }
 
   private String readFile(String file) throws IOException {