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

jensdeppe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new a6f2baa  GEODE-6443: Log all requests to REST ManagementService (#3225)
a6f2baa is described below

commit a6f2baa8a90c2ffb73778490138a07408e814f55
Author: Jens Deppe <[email protected]>
AuthorDate: Tue Mar 5 08:07:47 2019 -0800

    GEODE-6443: Log all requests to REST ManagementService (#3225)
    
    - This adds a logging filter to the web.xml
---
 geode-assembly/build.gradle                        |  4 +
 .../rest/ManagementRequestLoggingDUnitTest.java    | 89 ++++++++++++++++++++++
 .../internal/rest/ManagementLoggingFilter.java     | 46 +++++++++++
 .../src/main/webapp/WEB-INF/web.xml                | 10 +++
 4 files changed, 149 insertions(+)

diff --git a/geode-assembly/build.gradle b/geode-assembly/build.gradle
index 8e17765..8c4133f 100755
--- a/geode-assembly/build.gradle
+++ b/geode-assembly/build.gradle
@@ -194,6 +194,10 @@ dependencies {
   distributedTestCompile(project(':geode-assembly:geode-assembly-test'))
   distributedTestCompile('org.apache.httpcomponents:httpclient')
   distributedTestCompile('org.springframework:spring-web')
+  distributedTestCompile(project(':geode-management'))
+  distributedTestCompile(project(':geode-web-management'))
+  distributedTestCompile('org.apache.logging.log4j:log4j-core::tests')
+  distributedTestCompile('org.apache.logging.log4j:log4j-core::test-sources')
 
   
distributedTestRuntime(project(':extensions:geode-modules-session-internal')) {
     exclude group: 'org.apache.tomcat'
diff --git 
a/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/rest/ManagementRequestLoggingDUnitTest.java
 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/rest/ManagementRequestLoggingDUnitTest.java
new file mode 100644
index 0000000..d01bbbe
--- /dev/null
+++ 
b/geode-assembly/src/distributedTest/java/org/apache/geode/management/internal/rest/ManagementRequestLoggingDUnitTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.geode.management.internal.rest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+
+import org.apache.geode.cache.configuration.RegionConfig;
+import org.apache.geode.management.api.ClusterManagementResult;
+import org.apache.geode.test.dunit.rules.ClusterStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.rules.GeodeDevRestClient;
+
+public class ManagementRequestLoggingDUnitTest {
+
+  @ClassRule
+  public static ClusterStartupRule cluster = new ClusterStartupRule();
+
+  private static MemberVM locator, server;
+
+  private static GeodeDevRestClient restClient;
+
+  @BeforeClass
+  public static void beforeClass() {
+    locator = cluster.startLocatorVM(0, l -> l.withHttpService());
+    server = cluster.startServerVM(1, locator.getPort());
+    restClient =
+        new GeodeDevRestClient("/geode-management/v2", "localhost", 
locator.getHttpPort(), false);
+  }
+
+  @Test
+  public void checkRequestsAreLogged() throws Exception {
+    locator.invoke(() -> {
+      Logger logger = (Logger) 
LogManager.getLogger(ManagementLoggingFilter.class);
+      logger.addAppender(new ListAppender("ListAppender"));
+    });
+
+    RegionConfig regionConfig = new RegionConfig();
+    regionConfig.setName("customers");
+    regionConfig.setType("REPLICATE");
+    ObjectMapper mapper = new ObjectMapper();
+    String json = mapper.writeValueAsString(regionConfig);
+
+    ClusterManagementResult result =
+        restClient.doPostAndAssert("/regions", json)
+            .hasStatusCode(201)
+            .getClusterManagementResult();
+
+    assertThat(result.isRealizedOnAllOrNone()).isTrue();
+
+    locator.invoke(() -> {
+      Logger logger = (Logger) 
LogManager.getLogger(ManagementLoggingFilter.class);
+      Map<String, Appender> appenders = logger.getAppenders();
+      ListAppender listAppender = (ListAppender) appenders.get("ListAppender");
+      List<LogEvent> logEvents = listAppender.getEvents();
+
+      assertThat(logEvents.size()).as("Expected LogEvents").isEqualTo(1);
+      assertThat(logEvents.get(0).getMessage().getFormattedMessage())
+          .startsWith("Management request:");
+
+      logger.removeAppender(listAppender);
+    });
+  }
+}
diff --git 
a/geode-web-management/src/main/java/org/apache/geode/management/internal/rest/ManagementLoggingFilter.java
 
b/geode-web-management/src/main/java/org/apache/geode/management/internal/rest/ManagementLoggingFilter.java
new file mode 100644
index 0000000..d827266
--- /dev/null
+++ 
b/geode-web-management/src/main/java/org/apache/geode/management/internal/rest/ManagementLoggingFilter.java
@@ -0,0 +1,46 @@
+/*
+ * 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.geode.management.internal.rest;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.web.filter.AbstractRequestLoggingFilter;
+
+public class ManagementLoggingFilter extends AbstractRequestLoggingFilter {
+
+  // Because someone is going to want to disable this.
+  private static final Boolean ENABLE_REQUEST_LOGGING =
+      
Boolean.parseBoolean(System.getProperty("geode.management.request.logging", 
"true"));
+
+  public ManagementLoggingFilter() {
+    super.setIncludeQueryString(true);
+    super.setIncludePayload(true);
+    super.setMaxPayloadLength(1000);
+    super.setAfterMessagePrefix("Management request: [");
+  }
+
+  @Override
+  protected void beforeRequest(HttpServletRequest request, String message) {
+    // No logging here - this would not display the payload
+  }
+
+  @Override
+  protected void afterRequest(HttpServletRequest request, String message) {
+    if (ENABLE_REQUEST_LOGGING) {
+      logger.info(message);
+    }
+  }
+}
diff --git a/geode-web-management/src/main/webapp/WEB-INF/web.xml 
b/geode-web-management/src/main/webapp/WEB-INF/web.xml
index 026b0d1..c7bdd97 100644
--- a/geode-web-management/src/main/webapp/WEB-INF/web.xml
+++ b/geode-web-management/src/main/webapp/WEB-INF/web.xml
@@ -34,6 +34,16 @@
     <url-pattern>/*</url-pattern>
   </filter-mapping>
 
+  <filter>
+    <filter-name>requestLoggingFilter</filter-name>
+    
<filter-class>org.apache.geode.management.internal.rest.ManagementLoggingFilter</filter-class>
+  </filter>
+
+  <filter-mapping>
+    <filter-name>requestLoggingFilter</filter-name>
+    <url-pattern>/*</url-pattern>
+  </filter-mapping>
+
   <servlet>
     <description>
       The Spring DispatcherServlet (FrontController) handling all HTTP 
requests to the Geode Management REST API.

Reply via email to