knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lmc...@apache.org
Subject [2/3] knox git commit: KNOX-509 add logging and better error handling
Date Sat, 11 Apr 2015 00:03:37 GMT
KNOX-509 add logging and better error handling

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

Branch: refs/heads/master
Commit: 95136c51b1425f66cb226c3b74fafca77e71af97
Parents: 79e707a
Author: Larry McCay <lmccay@hortonworks.com>
Authored: Fri Apr 10 12:49:10 2015 -0400
Committer: Larry McCay <lmccay@hortonworks.com>
Committed: Fri Apr 10 12:49:10 2015 -0400

----------------------------------------------------------------------
 .../service/knoxsso/KnoxSSOMessages.java        | 37 +++++++++++++++++
 .../gateway/service/knoxsso/WebSSOResource.java | 42 +++++++-------------
 2 files changed, 52 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/95136c51/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/KnoxSSOMessages.java
----------------------------------------------------------------------
diff --git a/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/KnoxSSOMessages.java
b/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/KnoxSSOMessages.java
new file mode 100644
index 0000000..cb8b137
--- /dev/null
+++ b/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/KnoxSSOMessages.java
@@ -0,0 +1,37 @@
+/**
+ * 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.hadoop.gateway.service.knoxsso;
+
+import org.apache.hadoop.gateway.i18n.messages.Message;
+import org.apache.hadoop.gateway.i18n.messages.MessageLevel;
+import org.apache.hadoop.gateway.i18n.messages.Messages;
+
+@Messages(logger="org.apache.hadoop.gateway.service.knoxsso")
+public interface KnoxSSOMessages {
+  @Message( level = MessageLevel.INFO, text = "About to redirect to original URL: {0}")
+  void aboutToRedirectToOriginal(String original);
+
+  @Message( level = MessageLevel.DEBUG, text = "Adding the following JWT token as a cookie:
{0}")
+  void addingJWTCookie(String token);
+
+  @Message( level = MessageLevel.INFO, text = "Unable to find cookie with name: {0}")
+  void cookieNotFound(String name);
+
+  @Message( level = MessageLevel.ERROR, text = "Unable to properly send needed HTTP status
code: {0}, {1}")
+  void unableToCloseOutputStream(String message, String string);
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/knox/blob/95136c51/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/WebSSOResource.java
----------------------------------------------------------------------
diff --git a/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/WebSSOResource.java
b/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/WebSSOResource.java
index f1e4c50..84b74e7 100644
--- a/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/WebSSOResource.java
+++ b/gateway-service-knoxsso/src/main/java/org/apache/hadoop/gateway/service/knoxsso/WebSSOResource.java
@@ -32,7 +32,9 @@ import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.WebApplicationException;
 
+import org.apache.hadoop.gateway.i18n.messages.MessagesFactory;
 import org.apache.hadoop.gateway.services.GatewayServices;
 import org.apache.hadoop.gateway.services.security.token.JWTokenAuthority;
 import org.apache.hadoop.gateway.services.security.token.impl.JWT;
@@ -42,6 +44,8 @@ import static javax.ws.rs.core.MediaType.APPLICATION_XML;
 
 @Path( "/knoxsso/websso" )
 public class WebSSOResource {
+  private static KnoxSSOMessages log = MessagesFactory.get( KnoxSSOMessages.class );
+
   @Context 
   private HttpServletRequest request;
 
@@ -78,54 +82,37 @@ public class WebSSOResource {
     JWT token = ts.issueToken(p, "RS256");
     
     addJWTHadoopCookie(original, token);
-    addHadoopCookie(original, p);
     
     if (removeOriginalUrlCookie) {
       removeOriginalUrlCookie(response);
     }
     
-    System.out.println( new Date() + "about to redirect to original: " + original );
+    log.aboutToRedirectToOriginal(original);
     response.setStatus(statusCode);
     response.setHeader("Location", original);
     try {
       response.getOutputStream().close();
     } catch (IOException e) {
-      e.printStackTrace();
+      log.unableToCloseOutputStream(e.getMessage(), e.getStackTrace().toString());
     }
     return null;
   }
 
   public void addJWTHadoopCookie(String original, JWT token) {
-    System.out.println( "adding JWT cookie: " + token.toString() );
+    log.addingJWTCookie(token.toString());
     Cookie c = new Cookie("hadoop-jwt",  token.toString());
     c.setPath("/");
     try {
       String domain = getDomainName(original);
       c.setDomain(domain);
-      // c.setHttpOnly(false);
-      // c.setSecure(false);
+      c.setHttpOnly(true);
+      c.setSecure(true);
+      c.setMaxAge(120);
+      response.addCookie(c);
     }
     catch(Exception e) {
-      e.printStackTrace();
+      throw new WebApplicationException("Unable to add JWT cookie to response.");
     }
-    c.setMaxAge(120);
-    response.addCookie(c);
-  }
-
-  public void addHadoopCookie(String original, Principal p) {
-    System.out.println( "adding cookie with username: " + p.getName() );
-    Cookie c = new Cookie("hadoop-auth", p.getName());
-    c.setPath("/");
-    try {
-      String domain = getDomainName(original);
-      System.out.println("Setting domain on cookie: " + domain);
-      c.setDomain(domain);
-    }
-    catch(Exception e) {
-      e.printStackTrace();
-    }
-    c.setMaxAge(120);
-    response.addCookie(c);
   }
 
   private void removeOriginalUrlCookie(HttpServletResponse response) {
@@ -145,12 +132,13 @@ public class WebSSOResource {
     Cookie[] cookies = request.getCookies();
     String value = null;
     for(Cookie cookie : cookies){
-      System.out.println( "cookie name: " + cookie.getName());
-      System.out.println( "cookie value: " + cookie.getValue());
       if(name.equals(cookie.getName())){
           value = cookie.getValue();
       }
     }
+    if (value == null) {
+      log.cookieNotFound(name);
+    }
     return value;
   }
 }


Mime
View raw message