This is an automated email from the ASF dual-hosted git repository.

hapylestat pushed a commit to branch branch-2.7
in repository https://gitbox.apache.org/repos/asf/ambari.git


The following commit(s) were added to refs/heads/branch-2.7 by this push:
     new 5ea6ec2  AMBARI-25353 Seeing an error stack when running an API call 
against Ambari server (santal via dgrinenko) (#3208)
5ea6ec2 is described below

commit 5ea6ec295d040a4b7db19e3f889b073329dcb3fa
Author: Szilárd Antal <san...@cloudera.com>
AuthorDate: Mon Aug 10 05:25:54 2020 +0200

    AMBARI-25353 Seeing an error stack when running an API call against Ambari 
server (santal via dgrinenko) (#3208)
---
 .../ambari/server/configuration/Configuration.java | 16 +++++++++++++
 .../server/controller/AmbariHandlerList.java       |  8 +++++++
 .../server/configuration/ConfigurationTest.java    | 27 ++++++++++++++++++++++
 .../server/controller/AmbariHandlerListTest.java   | 19 ++++++++++++---
 4 files changed, 67 insertions(+), 3 deletions(-)

diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
index 7c76180..1949dc3 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/configuration/Configuration.java
@@ -2644,6 +2644,13 @@ public class Configuration {
   public static final ConfigurationProperty<Integer> 
KERBEROS_SERVER_ACTION_THREADPOOL_SIZE = new ConfigurationProperty<>(
     "server.kerberos.action.threadpool.size", 1);
 
+  /**
+   * A flag to determine whether error stacks appear on the error page
+   */
+  @Markdown(description = "Show or hide the error stacks on the error page")
+  public static final ConfigurationProperty<String> SERVER_SHOW_ERROR_STACKS = 
new ConfigurationProperty<>(
+    "server.show.error.stacks", "false");
+
   private static final Logger LOG = LoggerFactory.getLogger(
     Configuration.class);
 
@@ -6152,4 +6159,13 @@ public class Configuration {
   public int getAlertServiceCorePoolSize() {
     return Integer.parseInt(getProperty(SERVER_SIDE_ALERTS_CORE_POOL_SIZE));
   }
+
+  /**
+   * Determines whether error stacks appear on the error page
+   *
+   * @return true if error stacks appear on the error page (defaults to {@code 
false})
+   */
+  public boolean isServerShowErrorStacks() {
+    return Boolean.parseBoolean(getProperty(SERVER_SHOW_ERROR_STACKS));
+  }
 }
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariHandlerList.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariHandlerList.java
index e3ebc77..663592e 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariHandlerList.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/controller/AmbariHandlerList.java
@@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.ambari.server.api.AmbariPersistFilter;
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.server.security.AmbariViewsSecurityHeaderFilter;
@@ -104,6 +105,9 @@ public class AmbariHandlerList extends HandlerCollection 
implements ViewInstance
   @Inject
   SessionHandlerConfigurer sessionHandlerConfigurer;
 
+  @Inject
+  Configuration configuration;
+
   /**
    * Mapping of view instance entities to handlers.
    */
@@ -251,6 +255,10 @@ public class AmbariHandlerList extends HandlerCollection 
implements ViewInstance
     webAppContext.addFilter(new FilterHolder(springSecurityFilter), "/*", 
AmbariServer.DISPATCHER_TYPES);
     webAppContext.setAllowNullPathInfo(true);
 
+    if (webAppContext.getErrorHandler() != null) {
+      
webAppContext.getErrorHandler().setShowStacks(configuration.isServerShowErrorStacks());
+    }
+
     return webAppContext;
   }
 
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java
index 315a02b..123e441 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java
@@ -967,4 +967,31 @@ public class ConfigurationTest {
       // This is expected
     }
   }
+
+  @Test
+  public void testServerShowErrorStacksEnabled() throws  Exception {
+    // given
+    final Properties ambariProperties = new Properties();
+    
ambariProperties.setProperty(Configuration.SERVER_SHOW_ERROR_STACKS.getKey(), 
"true");
+    final Configuration configuration = new Configuration(ambariProperties);
+
+    // when
+    boolean result = configuration.isServerShowErrorStacks();
+
+    // then
+    Assert.assertTrue(result);
+  }
+
+  @Test
+  public void testServerShowErrorStacksDefault() throws  Exception {
+    // given
+    final Properties ambariProperties = new Properties();
+    final Configuration configuration = new Configuration(ambariProperties);
+
+    // when
+    boolean result = configuration.isServerShowErrorStacks();
+
+    // then
+    Assert.assertEquals(result, 
Boolean.parseBoolean(Configuration.SERVER_SHOW_ERROR_STACKS.getDefaultValue()));
+  }
 }
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariHandlerListTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariHandlerListTest.java
index effdf9d..37e7bb4 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariHandlerListTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariHandlerListTest.java
@@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.ambari.server.api.AmbariPersistFilter;
+import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.orm.entities.ViewEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntity;
 import org.apache.ambari.server.orm.entities.ViewInstanceEntityTest;
@@ -44,6 +45,7 @@ import org.eclipse.jetty.server.Handler;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.SessionIdManager;
+import org.eclipse.jetty.server.handler.ErrorHandler;
 import org.eclipse.jetty.server.session.SessionCache;
 import org.eclipse.jetty.server.session.SessionHandler;
 import org.eclipse.jetty.servlet.FilterHolder;
@@ -64,7 +66,7 @@ public class AmbariHandlerListTest {
   private final SessionIdManager sessionIdManager = 
createNiceMock(SessionIdManager.class);
   private final SessionHandlerConfigurer sessionHandlerConfigurer = 
createNiceMock(SessionHandlerConfigurer.class);
   private final SessionCache sessionCache = createNiceMock(SessionCache.class);
-
+  private final Configuration configuration = 
createNiceMock(Configuration.class);
 
   @Test
   public void testAddViewInstance() throws Exception {
@@ -90,7 +92,16 @@ public class AmbariHandlerListTest {
     handler.addFilter(capture(securityFilterCapture), eq("/*"), 
eq(AmbariServer.DISPATCHER_TYPES));
     handler.setAllowNullPathInfo(true);
 
-    replay(handler, server, sessionHandler);
+    final boolean showErrorStacks = true;
+    expect(configuration.isServerShowErrorStacks()).andReturn(showErrorStacks);
+
+    ErrorHandler errorHandler = createNiceMock(ErrorHandler.class);
+    Capture<Boolean> showStackCapture = EasyMock.newCapture();
+    errorHandler.setShowStacks(EasyMock.captureBoolean(showStackCapture));
+
+    expect(handler.getErrorHandler()).andReturn(errorHandler).times(2);
+
+    replay(handler, server, sessionHandler, configuration, errorHandler);
 
     AmbariHandlerList handlerList = getAmbariHandlerList(handler);
 
@@ -103,8 +114,9 @@ public class AmbariHandlerListTest {
     Assert.assertEquals(ambariViewsSecurityHeaderFilter, 
securityHeaderFilterCapture.getValue().getFilter());
     Assert.assertEquals(persistFilter, 
persistFilterCapture.getValue().getFilter());
     Assert.assertEquals(springSecurityFilter, 
securityFilterCapture.getValue().getFilter());
+    Assert.assertEquals(showErrorStacks, showStackCapture.getValue());
 
-    verify(handler, server, sessionHandler);
+    verify(handler, server, sessionHandler, configuration, errorHandler);
   }
 
   @Test
@@ -185,6 +197,7 @@ public class AmbariHandlerListTest {
     handlerList.springSecurityFilter = springSecurityFilter;
     handlerList.sessionHandler = sessionHandler;
     handlerList.sessionHandlerConfigurer = sessionHandlerConfigurer;
+    handlerList.configuration = configuration;
     return handlerList;
   }
 

Reply via email to