ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smoha...@apache.org
Subject [17/51] [abbrv] ambari git commit: AMBARI-19075. Cookie management for Ambari LogSearch Integration (oleewere)
Date Fri, 09 Dec 2016 21:57:09 GMT
AMBARI-19075. Cookie management for Ambari LogSearch Integration (oleewere)

Change-Id: I67395f02705d296e7b1b0dced2fffde69d92482d


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

Branch: refs/heads/branch-feature-AMBARI-18901
Commit: a9a05f76f07df992e4216a2c9e27736239bfdb9a
Parents: 8bdb745
Author: oleewere <oleewere@gmail.com>
Authored: Thu Dec 8 00:02:50 2016 +0100
Committer: oleewere <oleewere@gmail.com>
Committed: Thu Dec 8 16:28:11 2016 +0100

----------------------------------------------------------------------
 .../org/apache/ambari/logsearch/LogSearch.java  |  4 +-
 .../web/listener/LogSearchSessionListener.java  | 48 ++++++++++++++++++++
 .../docker/test-config/logsearch/log4j.xml      |  2 +-
 .../controller/logging/LoggingCookieStore.java  | 44 ++++++++++++++++++
 .../logging/LoggingRequestHelperImpl.java       | 42 +++++++++++++++--
 .../0.5.0/properties/logsearch-log4j.xml.j2     |  4 +-
 .../logging/LoggingRequestHelperImplTest.java   |  5 ++
 7 files changed, 141 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
index 2c3f4f5..14f83cf 100644
--- a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
+++ b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/LogSearch.java
@@ -30,6 +30,7 @@ import org.apache.ambari.logsearch.common.ManageStartEndTime;
 import org.apache.ambari.logsearch.common.PropertiesHelper;
 import org.apache.ambari.logsearch.conf.ApplicationConfig;
 import org.apache.ambari.logsearch.util.SSLUtil;
+import org.apache.ambari.logsearch.web.listener.LogSearchSessionListener;
 import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.eclipse.jetty.server.Connector;
@@ -66,7 +67,7 @@ public class LogSearch {
 
   private static final String WEB_RESOURCE_FOLDER = "webapps/app";
   private static final String ROOT_CONTEXT = "/";
-  private static final Integer SESSION_TIMEOUT = 30;
+  private static final Integer SESSION_TIMEOUT = 60 * 30;
 
 
   public static void main(String[] argv) {
@@ -136,6 +137,7 @@ public class LogSearch {
     context.setBaseResource(Resource.newResource(webResourceBase));
     context.setContextPath(ROOT_CONTEXT);
     context.setParentLoaderPriority(true);
+    context.addEventListener(new LogSearchSessionListener());
 
     // Configure Spring
     context.addEventListener(new ContextLoaderListener());

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/web/listener/LogSearchSessionListener.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/web/listener/LogSearchSessionListener.java
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/web/listener/LogSearchSessionListener.java
new file mode 100644
index 0000000..9fa5c80
--- /dev/null
+++ b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/web/listener/LogSearchSessionListener.java
@@ -0,0 +1,48 @@
+/*
+ * 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.ambari.logsearch.web.listener;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
+
+public class LogSearchSessionListener implements HttpSessionListener {
+
+  private Logger LOG = LoggerFactory.getLogger(LogSearchSessionListener.class);
+
+  private int numberOfSessions = 0;
+
+  @Override
+  public void sessionCreated(HttpSessionEvent event) {
+    synchronized (this) {
+      numberOfSessions++;
+    }
+    LOG.debug(String.format("New session is created (Id: %s). Number of sessions: %d", event.getSession().getId(),
numberOfSessions));
+  }
+
+  @Override
+  public void sessionDestroyed(HttpSessionEvent event) {
+    synchronized (this) {
+      numberOfSessions--;
+    }
+    LOG.debug(String.format("Session destroyed (Id: %s). Number of sessions: %d", event.getSession().getId(),
numberOfSessions));
+  }
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-logsearch/docker/test-config/logsearch/log4j.xml
----------------------------------------------------------------------
diff --git a/ambari-logsearch/docker/test-config/logsearch/log4j.xml b/ambari-logsearch/docker/test-config/logsearch/log4j.xml
index b80824b..d0e26ed 100644
--- a/ambari-logsearch/docker/test-config/logsearch/log4j.xml
+++ b/ambari-logsearch/docker/test-config/logsearch/log4j.xml
@@ -25,7 +25,7 @@
     <param name="maxFileSize" value="10MB" />
     <param name="maxBackupIndex" value="10" />
     <layout class="org.apache.log4j.PatternLayout">
-      <param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" />
+      <param name="ConversionPattern" value="%d %-5p [%t] %C{6} (%F:%L) - %m%n" />
     </layout>
   </appender>
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingCookieStore.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingCookieStore.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingCookieStore.java
new file mode 100644
index 0000000..a779068
--- /dev/null
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingCookieStore.java
@@ -0,0 +1,44 @@
+/*
+ * 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.ambari.server.controller.logging;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Package protected singleton for storing Cookie key value pairs for Logging Service.
+ * This has chosen instead of using CookieManager to avoid using system wide Cookie handling
+ */
+class LoggingCookieStore {
+  public static final LoggingCookieStore INSTANCE = new LoggingCookieStore();
+
+  private final Map<String, String> cookiesMap = new HashMap<>();
+
+  private LoggingCookieStore() {
+  }
+
+  public Map<String, String> getCookiesMap() {
+    return cookiesMap;
+  }
+
+  public void addCookie(String cookieName, String cookieValue) {
+    cookiesMap.put(cookieName, cookieValue);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImpl.java
index eab0c04..358c1b7 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImpl.java
@@ -26,6 +26,7 @@ import org.apache.ambari.server.security.encryption.CredentialStoreService;
 import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Config;
 import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang.StringUtils;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.log4j.Logger;
 import org.codehaus.jackson.map.AnnotationIntrospector;
@@ -39,11 +40,14 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringReader;
+import java.net.HttpCookie;
 import java.net.HttpURLConnection;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -76,6 +80,10 @@ public class LoggingRequestHelperImpl implements LoggingRequestHelper {
 
   private static final String PAGE_SIZE_QUERY_PARAMETER_NAME = "pageSize";
 
+  private static final String COOKIE_HEADER = "Cookie";
+
+  private static final String SET_COOKIES_HEADER = "Set-Cookie";
+
   private static final int DEFAULT_LOGSEARCH_CONNECT_TIMEOUT_IN_MILLISECONDS = 5000;
 
   private static final int DEFAULT_LOGSEARCH_READ_TIMEOUT_IN_MILLISECONDS = 5000;
@@ -109,20 +117,20 @@ public class LoggingRequestHelperImpl implements LoggingRequestHelper
{
       // use the Apache builder to create the correct URI
       URI logSearchURI = createLogSearchQueryURI("http", queryParameters);
       LOG.debug("Attempting to connect to LogSearch server at " + logSearchURI);
-
-      HttpURLConnection httpURLConnection  = (HttpURLConnection)logSearchURI.toURL().openConnection();
+      HttpURLConnection httpURLConnection  = (HttpURLConnection) logSearchURI.toURL().openConnection();
       httpURLConnection.setRequestMethod("GET");
       httpURLConnection.setConnectTimeout(DEFAULT_LOGSEARCH_CONNECT_TIMEOUT_IN_MILLISECONDS);
       httpURLConnection.setReadTimeout(DEFAULT_LOGSEARCH_READ_TIMEOUT_IN_MILLISECONDS);
 
+      addCookiesFromCookieStore(httpURLConnection);
 
       setupCredentials(httpURLConnection);
 
       StringBuffer buffer = networkConnection.readQueryResponseFromServer(httpURLConnection);
+      addCookiesToCookieStoreFromResponse(httpURLConnection);
 
       // setup a reader for the JSON response
-      StringReader stringReader =
-        new StringReader(buffer.toString());
+      StringReader stringReader = new StringReader(buffer.toString());
 
       ObjectReader logQueryResponseReader =
         createObjectReader(LogQueryResponse.class);
@@ -137,6 +145,27 @@ public class LoggingRequestHelperImpl implements LoggingRequestHelper
{
     return null;
   }
 
+  private void addCookiesFromCookieStore(HttpURLConnection httpURLConnection) {
+    if (LoggingCookieStore.INSTANCE.getCookiesMap().size() > 0) {
+      List<String> cookiesStrList = new ArrayList<>();
+      for (Map.Entry<String, String> entry : LoggingCookieStore.INSTANCE.getCookiesMap().entrySet())
{
+        cookiesStrList.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
+      }
+      httpURLConnection.setRequestProperty(COOKIE_HEADER, StringUtils.join(cookiesStrList,
"; "));
+    }
+  }
+
+  private void addCookiesToCookieStoreFromResponse(HttpURLConnection httpURLConnection) {
+    Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
+    List<String> cookiesHeader = headerFields.get(SET_COOKIES_HEADER);
+    if (cookiesHeader != null) {
+      for (String cookie : cookiesHeader) {
+        HttpCookie cookie1 = HttpCookie.parse(cookie).get(0);
+        LoggingCookieStore.INSTANCE.addCookie(cookie1.getName(), cookie1.getValue());
+      }
+    }
+  }
+
 
   private void setupCredentials(HttpURLConnection httpURLConnection) {
     final String logSearchAdminUser =
@@ -224,10 +253,14 @@ public class LoggingRequestHelperImpl implements LoggingRequestHelper
{
       HttpURLConnection httpURLConnection = (HttpURLConnection) logLevelQueryURI.toURL().openConnection();
       httpURLConnection.setRequestMethod("GET");
 
+      addCookiesFromCookieStore(httpURLConnection);
+
       setupCredentials(httpURLConnection);
 
       StringBuffer buffer = networkConnection.readQueryResponseFromServer(httpURLConnection);
 
+      addCookiesToCookieStoreFromResponse(httpURLConnection);
+
       // setup a reader for the JSON response
       StringReader stringReader =
         new StringReader(buffer.toString());
@@ -374,6 +407,7 @@ public class LoggingRequestHelperImpl implements LoggingRequestHelper
{
         BufferedReader reader = new BufferedReader(new InputStreamReader(resultStream));
         LOG.debug("Response code from LogSearch Service is = " + httpURLConnection.getResponseCode());
 
+
         String line = reader.readLine();
         StringBuffer buffer = new StringBuffer();
         while (line != null) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/properties/logsearch-log4j.xml.j2
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/properties/logsearch-log4j.xml.j2
b/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/properties/logsearch-log4j.xml.j2
index ce39030..06fdad2 100644
--- a/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/properties/logsearch-log4j.xml.j2
+++ b/ambari-server/src/main/resources/common-services/LOGSEARCH/0.5.0/properties/logsearch-log4j.xml.j2
@@ -25,7 +25,7 @@ limitations under the License.
   </appender>
 
   <appender name="rolling_file" class="org.apache.log4j.RollingFileAppender">
-    <param name="file" value="{{logsearch_log_dir}}/logsearch.err" />
+    <param name="file" value="{{logsearch_log_dir}}/logsearch.log" />
     <param name="Threshold" value="info" />
     <param name="append" value="true" />
     <param name="maxFileSize" value="10MB" />
@@ -74,7 +74,7 @@ limitations under the License.
   </category>
 
   <root>
-    <priority value="warn"/>
+    <priority value="info"/>
     <!-- <appender-ref ref="console" /> -->
     <appender-ref ref="rolling_file" />
     <appender-ref ref="rolling_file_json"/>

http://git-wip-us.apache.org/repos/asf/ambari/blob/a9a05f76/ambari-server/src/test/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImplTest.java
index b839b64..12b5b69 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/logging/LoggingRequestHelperImplTest.java
@@ -122,6 +122,7 @@ public class LoggingRequestHelperImplTest {
 
   @Test
   public void testLogQueryRequestBasic() throws Exception {
+    LoggingCookieStore.INSTANCE.getCookiesMap().clear();
     EasyMockSupport mockSupport =
       new EasyMockSupport();
 
@@ -301,6 +302,7 @@ public class LoggingRequestHelperImplTest {
 
   @Test
   public void testLogLevelRequestBasic() throws Exception {
+    LoggingCookieStore.INSTANCE.getCookiesMap().clear();
     EasyMockSupport mockSupport =
       new EasyMockSupport();
 
@@ -391,6 +393,7 @@ public class LoggingRequestHelperImplTest {
 
   @Test
   public void testLogFileNameRequestBasic() throws Exception {
+    LoggingCookieStore.INSTANCE.getCookiesMap().clear();
     final String expectedComponentName = "hdfs_namenode";
 
     EasyMockSupport mockSupport =
@@ -478,6 +481,7 @@ public class LoggingRequestHelperImplTest {
    */
   @Test
   public void testLogQueryRequestBasicCredentialsNotInConfig() throws Exception {
+    LoggingCookieStore.INSTANCE.getCookiesMap().clear();
     final String expectedClusterName = "my-test-cluster";
 
     EasyMockSupport mockSupport =
@@ -656,6 +660,7 @@ public class LoggingRequestHelperImplTest {
 
   @Test
   public void testCreateLogFileTailURI() throws Exception {
+    LoggingCookieStore.INSTANCE.getCookiesMap().clear();
     final String expectedHostName = "c6401.ambari.apache.org";
     final String expectedPort = "61888";
     final String expectedComponentName = "hdfs_namenode";


Mime
View raw message