You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2017/05/22 16:02:19 UTC

logging-log4j2 git commit: Https support (W.I.P.)

Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-1442 42dcb4588 -> 93ac8ab82


Https support (W.I.P.)


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/93ac8ab8
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/93ac8ab8
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/93ac8ab8

Branch: refs/heads/LOG4J2-1442
Commit: 93ac8ab82037ea77eee5e99cd3e6b26b0ea1ad95
Parents: 42dcb45
Author: Mikael Ståldal <mi...@magine.com>
Authored: Mon May 22 18:02:13 2017 +0200
Committer: Mikael Ståldal <mi...@magine.com>
Committed: Mon May 22 18:02:13 2017 +0200

----------------------------------------------------------------------
 .../log4j/core/appender/HttpAppender.java       | 15 +++++++++-
 .../core/appender/HttpURLConnectionManager.java | 17 +++++++++--
 .../log4j/core/appender/HttpAppenderTest.java   | 31 ++++++++++++++++++--
 3 files changed, 58 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/93ac8ab8/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
index c43c63d..efc9942 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
@@ -32,6 +32,7 @@ import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
 import org.apache.logging.log4j.core.config.plugins.PluginElement;
 import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
+import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
 
 /**
  * Sends log events over HTTP.
@@ -62,10 +63,13 @@ public final class HttpAppender extends AbstractAppender {
         @PluginElement("Headers")
         private Property[] headers;
 
+        @PluginElement("SslConfiguration")
+        private SslConfiguration sslConfiguration;
+
         @Override
         public HttpAppender build() {
             final HttpManager httpManager = new HttpURLConnectionManager(getConfiguration(), getConfiguration().getLoggerContext(),
-                getName(), url, method, connectTimeoutMillis, readTimeoutMillis, headers);
+                getName(), url, method, connectTimeoutMillis, readTimeoutMillis, headers, sslConfiguration);
             return new HttpAppender(getName(), getLayout(), getFilter(), isIgnoreExceptions(), httpManager);
         }
 
@@ -89,6 +93,10 @@ public final class HttpAppender extends AbstractAppender {
             return headers;
         }
 
+        public SslConfiguration getSslConfiguration() {
+            return sslConfiguration;
+        }
+
         public B setUrl(final String url) {
             this.url = url;
             return asBuilder();
@@ -113,6 +121,11 @@ public final class HttpAppender extends AbstractAppender {
             this.headers = headers;
             return asBuilder();
         }
+
+        public B setSslConfiguration(final SslConfiguration sslConfiguration) {
+            this.sslConfiguration = sslConfiguration;
+            return asBuilder();
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/93ac8ab8/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
index d656c90..de9225c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
@@ -26,12 +26,15 @@ import java.net.URL;
 import java.nio.charset.Charset;
 import java.util.Objects;
 
+import javax.net.ssl.HttpsURLConnection;
+
 import org.apache.logging.log4j.core.Layout;
 import org.apache.logging.log4j.core.LogEvent;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.config.ConfigurationException;
 import org.apache.logging.log4j.core.config.Property;
+import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
 import org.apache.logging.log4j.core.util.IOUtils;
 
 public class HttpURLConnectionManager extends HttpManager {
@@ -43,10 +46,13 @@ public class HttpURLConnectionManager extends HttpManager {
     private final int connectTimeoutMillis;
     private final int readTimeoutMillis;
     private final Property[] headers;
+    private final SslConfiguration sslConfiguration;
 
     public HttpURLConnectionManager(final Configuration configuration, LoggerContext loggerContext, final String name,
-                                    final String url, final String method, final int connectTimeoutMillis, final int readTimeoutMillis,
-                                    final Property[] headers) {
+                                    final String url, final String method, final int connectTimeoutMillis,
+                                    final int readTimeoutMillis,
+                                    final Property[] headers,
+                                    SslConfiguration sslConfiguration) {
         super(configuration, loggerContext, name);
         try {
             this.url = new URL(url);
@@ -57,6 +63,10 @@ public class HttpURLConnectionManager extends HttpManager {
         this.connectTimeoutMillis = connectTimeoutMillis;
         this.readTimeoutMillis = readTimeoutMillis;
         this.headers = headers != null ? headers : new Property[0];
+        this.sslConfiguration = sslConfiguration;
+        if (this.sslConfiguration != null && !this.url.getProtocol().equalsIgnoreCase("https")) {
+            throw new ConfigurationException("SSL configuration can only be specified with URL scheme https");
+        }
     }
 
     @Override
@@ -74,6 +84,9 @@ public class HttpURLConnectionManager extends HttpManager {
                 header.getName(),
                 header.isValueNeedsLookup() ? getConfiguration().getStrSubstitutor().replace(event, header.getValue()) : header.getValue());
         }
+        if (sslConfiguration != null) {
+            ((HttpsURLConnection)urlConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
+        }
 
         byte[] msg = layout.toByteArray(event);
         urlConnection.setFixedLengthStreamingMode(msg.length);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/93ac8ab8/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
index c9f9385..9ce31e0 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
@@ -10,6 +10,9 @@ import org.apache.logging.log4j.core.config.Property;
 import org.apache.logging.log4j.core.impl.Log4jLogEvent;
 import org.apache.logging.log4j.core.layout.JsonLayout;
 import org.apache.logging.log4j.core.lookup.JavaLookup;
+import org.apache.logging.log4j.core.net.ssl.KeyStoreConfiguration;
+import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
+import org.apache.logging.log4j.core.net.ssl.TestConstants;
 import org.apache.logging.log4j.junit.LoggerContextRule;
 import org.apache.logging.log4j.message.SimpleMessage;
 import org.apache.logging.log4j.status.StatusData;
@@ -22,7 +25,6 @@ import static org.junit.Assert.*;
 import static com.github.tomakehurst.wiremock.client.WireMock.*;
 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
 
-// TODO test HTTPS
 public class HttpAppenderTest {
 
     private static final String LOG_MESSAGE = "Hello, world!";
@@ -50,7 +52,10 @@ public class HttpAppenderTest {
     public LoggerContextRule ctx = new LoggerContextRule("HttpAppenderTest.xml");
 
     @Rule
-    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
+    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort()
+        .keystorePath(TestConstants.KEYSTORE_FILE)
+        .keystorePassword(TestConstants.KEYSTORE_PWD)
+        .keystoreType(TestConstants.KEYSTORE_TYPE));
 
     @Test
     public void testAppend() throws Exception {
@@ -72,6 +77,28 @@ public class HttpAppenderTest {
     }
 
     @Test
+    public void testAppendHttps() throws Exception {
+        wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
+            .willReturn(SUCCESS_RESPONSE));
+
+        final Appender appender = HttpAppender.newBuilder()
+            .withName("Http")
+            .withLayout(JsonLayout.createDefaultLayout())
+            .setConfiguration(ctx.getConfiguration())
+            .setUrl("https://localhost:"+wireMockRule.httpsPort()+"/test/log4j/")
+            .setSslConfiguration(SslConfiguration.createSSLConfiguration("TLS",
+                KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE, TestConstants.KEYSTORE_PWD, TestConstants.KEYSTORE_TYPE, null),
+                null))
+            .build();
+        appender.append(createLogEvent());
+
+        wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
+            .withHeader("Host", containing("localhost"))
+            .withHeader("Content-Type", containing("application/json"))
+            .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\"")));
+    }
+
+    @Test
     public void testAppendMethodPut() throws Exception {
         wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234"))
             .willReturn(SUCCESS_RESPONSE));


Re: logging-log4j2 git commit: Https support (W.I.P.)

Posted by Gary Gregory <ga...@gmail.com>.
On Mon, May 22, 2017 at 9:02 AM, <mi...@apache.org> wrote:

> Repository: logging-log4j2
> Updated Branches:
>   refs/heads/LOG4J2-1442 42dcb4588 -> 93ac8ab82
>
>
> Https support (W.I.P.)
>
>
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> commit/93ac8ab8
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/93ac8ab8
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/93ac8ab8
>
> Branch: refs/heads/LOG4J2-1442
> Commit: 93ac8ab82037ea77eee5e99cd3e6b26b0ea1ad95
> Parents: 42dcb45
> Author: Mikael Ståldal <mi...@magine.com>
> Authored: Mon May 22 18:02:13 2017 +0200
> Committer: Mikael Ståldal <mi...@magine.com>
> Committed: Mon May 22 18:02:13 2017 +0200
>
> ----------------------------------------------------------------------
>  .../log4j/core/appender/HttpAppender.java       | 15 +++++++++-
>  .../core/appender/HttpURLConnectionManager.java | 17 +++++++++--
>  .../log4j/core/appender/HttpAppenderTest.java   | 31 ++++++++++++++++++--
>  3 files changed, 58 insertions(+), 5 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 93ac8ab8/log4j-core/src/main/java/org/apache/logging/log4j/
> core/appender/HttpAppender.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/HttpAppender.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> appender/HttpAppender.java
> index c43c63d..efc9942 100644
> --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> appender/HttpAppender.java
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> appender/HttpAppender.java
> @@ -32,6 +32,7 @@ import org.apache.logging.log4j.core.config.plugins.
> PluginBuilderAttribute;
>  import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
>  import org.apache.logging.log4j.core.config.plugins.PluginElement;
>  import org.apache.logging.log4j.core.config.plugins.validation.
> constraints.Required;
> +import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
>
>  /**
>   * Sends log events over HTTP.
> @@ -62,10 +63,13 @@ public final class HttpAppender extends
> AbstractAppender {
>          @PluginElement("Headers")
>          private Property[] headers;
>
> +        @PluginElement("SslConfiguration")
> +        private SslConfiguration sslConfiguration;
> +
>          @Override
>          public HttpAppender build() {
>              final HttpManager httpManager = new HttpURLConnectionManager(getConfiguration(),
> getConfiguration().getLoggerContext(),
> -                getName(), url, method, connectTimeoutMillis,
> readTimeoutMillis, headers);
> +                getName(), url, method, connectTimeoutMillis,
> readTimeoutMillis, headers, sslConfiguration);
>              return new HttpAppender(getName(), getLayout(), getFilter(),
> isIgnoreExceptions(), httpManager);
>          }
>
> @@ -89,6 +93,10 @@ public final class HttpAppender extends
> AbstractAppender {
>              return headers;
>          }
>
> +        public SslConfiguration getSslConfiguration() {
> +            return sslConfiguration;
> +        }
> +
>          public B setUrl(final String url) {
>              this.url = url;
>              return asBuilder();
> @@ -113,6 +121,11 @@ public final class HttpAppender extends
> AbstractAppender {
>              this.headers = headers;
>              return asBuilder();
>          }
> +
> +        public B setSslConfiguration(final SslConfiguration
> sslConfiguration) {
> +            this.sslConfiguration = sslConfiguration;
> +            return asBuilder();
> +        }
>      }
>
>      /**
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 93ac8ab8/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/
> HttpURLConnectionManager.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> appender/HttpURLConnectionManager.java b/log4j-core/src/main/java/
> org/apache/logging/log4j/core/appender/HttpURLConnectionManager.java
> index d656c90..de9225c 100644
> --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/
> HttpURLConnectionManager.java
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/
> HttpURLConnectionManager.java
> @@ -26,12 +26,15 @@ import java.net.URL;
>  import java.nio.charset.Charset;
>  import java.util.Objects;
>
> +import javax.net.ssl.HttpsURLConnection;
> +
>  import org.apache.logging.log4j.core.Layout;
>  import org.apache.logging.log4j.core.LogEvent;
>  import org.apache.logging.log4j.core.LoggerContext;
>  import org.apache.logging.log4j.core.config.Configuration;
>  import org.apache.logging.log4j.core.config.ConfigurationException;
>  import org.apache.logging.log4j.core.config.Property;
> +import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
>  import org.apache.logging.log4j.core.util.IOUtils;
>
>  public class HttpURLConnectionManager extends HttpManager {
> @@ -43,10 +46,13 @@ public class HttpURLConnectionManager extends
> HttpManager {
>      private final int connectTimeoutMillis;
>      private final int readTimeoutMillis;
>      private final Property[] headers;
> +    private final SslConfiguration sslConfiguration;
>
>      public HttpURLConnectionManager(final Configuration configuration,
> LoggerContext loggerContext, final String name,
> -                                    final String url, final String
> method, final int connectTimeoutMillis, final int readTimeoutMillis,
> -                                    final Property[] headers) {
> +                                    final String url, final String
> method, final int connectTimeoutMillis,
> +                                    final int readTimeoutMillis,
> +                                    final Property[] headers,
> +                                    SslConfiguration sslConfiguration) {
>          super(configuration, loggerContext, name);
>          try {
>              this.url = new URL(url);
> @@ -57,6 +63,10 @@ public class HttpURLConnectionManager extends
> HttpManager {
>          this.connectTimeoutMillis = connectTimeoutMillis;
>          this.readTimeoutMillis = readTimeoutMillis;
>          this.headers = headers != null ? headers : new Property[0];
> +        this.sslConfiguration = sslConfiguration;
> +        if (this.sslConfiguration != null && !this.url.getProtocol().equalsIgnoreCase("https"))
> {
> +            throw new ConfigurationException("SSL configuration can only
> be specified with URL scheme https");
> +        }
>      }
>
>      @Override
> @@ -74,6 +84,9 @@ public class HttpURLConnectionManager extends
> HttpManager {
>                  header.getName(),
>                  header.isValueNeedsLookup() ? getConfiguration().
> getStrSubstitutor().replace(event, header.getValue()) :
> header.getValue());
>          }
> +        if (sslConfiguration != null) {
> +            ((HttpsURLConnection)urlConnection).setSSLSocketFactory(
> sslConfiguration.getSslSocketFactory());
> +        }
>
>          byte[] msg = layout.toByteArray(event);
>          urlConnection.setFixedLengthStreamingMode(msg.length);
>
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 93ac8ab8/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/
> HttpAppenderTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/HttpAppenderTest.java
> b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> appender/HttpAppenderTest.java
> index c9f9385..9ce31e0 100644
> --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> appender/HttpAppenderTest.java
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> appender/HttpAppenderTest.java
> @@ -10,6 +10,9 @@ import org.apache.logging.log4j.core.config.Property;
>  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
>  import org.apache.logging.log4j.core.layout.JsonLayout;
>  import org.apache.logging.log4j.core.lookup.JavaLookup;
> +import org.apache.logging.log4j.core.net.ssl.KeyStoreConfiguration;
> +import org.apache.logging.log4j.core.net.ssl.SslConfiguration;
> +import org.apache.logging.log4j.core.net.ssl.TestConstants;
>  import org.apache.logging.log4j.junit.LoggerContextRule;
>  import org.apache.logging.log4j.message.SimpleMessage;
>  import org.apache.logging.log4j.status.StatusData;
> @@ -22,7 +25,6 @@ import static org.junit.Assert.*;
>  import static com.github.tomakehurst.wiremock.client.WireMock.*;
>  import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.
> wireMockConfig;
>
> -// TODO test HTTPS
>  public class HttpAppenderTest {
>
>      private static final String LOG_MESSAGE = "Hello, world!";
> @@ -50,7 +52,10 @@ public class HttpAppenderTest {
>      public LoggerContextRule ctx = new LoggerContextRule("
> HttpAppenderTest.xml");
>
>      @Rule
> -    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().
> dynamicPort());
> +    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().
> dynamicPort().dynamicHttpsPort()
> +        .keystorePath(TestConstants.KEYSTORE_FILE)
> +        .keystorePassword(TestConstants.KEYSTORE_PWD)
> +        .keystoreType(TestConstants.KEYSTORE_TYPE));
>
>      @Test
>      public void testAppend() throws Exception {
> @@ -72,6 +77,28 @@ public class HttpAppenderTest {
>      }
>
>      @Test
> +    public void testAppendHttps() throws Exception {
> +        wireMockRule.stubFor(post(urlEqualTo("/test/log4j/"))
> +            .willReturn(SUCCESS_RESPONSE));
> +
> +        final Appender appender = HttpAppender.newBuilder()
> +            .withName("Http")
> +            .withLayout(JsonLayout.createDefaultLayout())
> +            .setConfiguration(ctx.getConfiguration())
> +            .setUrl("https://localhost:"+wireMockRule.httpsPort()+"/
> test/log4j/")
>

Looks like you're missing some spaces there ;-)

I'd like to see SslConfiguration enhanced to allow for self-signed
certificates but I cannot take the time to do that ATM.

Gary


+            .setSslConfiguration(SslConfiguration.
> createSSLConfiguration("TLS",
> +                KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE,
> TestConstants.KEYSTORE_PWD, TestConstants.KEYSTORE_TYPE, null),
> +                null))
> +            .build();
> +        appender.append(createLogEvent());
> +
> +        wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/"))
> +            .withHeader("Host", containing("localhost"))
> +            .withHeader("Content-Type", containing("application/json"))
> +            .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE
> + "\"")));
> +    }
> +
> +    @Test
>      public void testAppendMethodPut() throws Exception {
>          wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234"))
>              .willReturn(SUCCESS_RESPONSE));
>
>


-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<https://www.amazon.com/gp/product/1617290459/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1617290459&linkCode=as2&tag=garygregory-20&linkId=cadb800f39946ec62ea2b1af9fe6a2b8>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1617290459>
JUnit in Action, Second Edition
<https://www.amazon.com/gp/product/1935182021/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182021&linkCode=as2&tag=garygregory-20&linkId=31ecd1f6b6d1eaf8886ac902a24de418%22>

<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182021>
Spring Batch in Action
<https://www.amazon.com/gp/product/1935182951/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1935182951&linkCode=%7B%7BlinkCode%7D%7D&tag=garygregory-20&linkId=%7B%7Blink_id%7D%7D%22%3ESpring+Batch+in+Action>
<http:////ir-na.amazon-adsystem.com/e/ir?t=garygregory-20&l=am2&o=1&a=1935182951>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory