Skip to content

Commit

Permalink
Enable tls (#21)
Browse files Browse the repository at this point in the history
* tls support enabled

* add tls example configuration
  • Loading branch information
kortemik authored Jun 9, 2023
1 parent e21e48d commit b43b712
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/main/java/com/teragrep/jla_01/RlpLogbackAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ Reliable Event Logging Protocol (RELP) Logback plugin

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
Expand All @@ -30,6 +32,10 @@ Reliable Event Logging Protocol (RELP) Logback plugin
import com.teragrep.rlp_01.RelpBatch;
import ch.qos.logback.core.AppenderBase;
import com.teragrep.rlp_01.RelpConnection;
import com.teragrep.rlp_01.SSLContextFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

public class RlpLogbackAppender<E> extends AppenderBase<E> {

Expand Down Expand Up @@ -59,6 +65,13 @@ public class RlpLogbackAppender<E> extends AppenderBase<E> {
private long reconnectIfNoMessagesInterval = 150000;
private long lastMessageSent = 0;

// tls
private boolean useTLS = false;
private String keystorePath = "";
private String keystorePassword = "";
private String tlsProtocol = "";


public void setEncoder(LayoutWrappingEncoder encoder) {
this.encoder = encoder;
}
Expand Down Expand Up @@ -121,6 +134,23 @@ public void setReconnectIfNoMessagesInterval(int interval) {
this.reconnectIfNoMessagesInterval = interval;
}

// tls
public void setUseTLS(boolean on) {
this.useTLS = on;
}

public void setKeystorePath(String keystorePath) {
this.keystorePath = keystorePath;
}

public void setKeystorePassword(String keystorePassword) {
this.keystorePassword = keystorePassword;
}

public void setTlsProtocol(String tlsProtocol) {
this.tlsProtocol = tlsProtocol;
}

private void connect() {
if (System.getenv("JLA01_DEBUG") != null) {
System.out.println("RlpLogbackAppender.connect>");
Expand Down Expand Up @@ -178,7 +208,29 @@ public void start() {
return;

// initialize events sender
this.sender = new RelpConnection();
if (useTLS) {
Supplier<SSLEngine> sslEngineSupplier = new Supplier<SSLEngine>() {
private final SSLContext sslContext;
{
try {
sslContext = SSLContextFactory.authenticatedContext(keystorePath, keystorePassword, tlsProtocol);
} catch (GeneralSecurityException | IOException e) {
throw new RuntimeException(e);
}
}

@Override
public SSLEngine get() {
return sslContext.createSSLEngine();
}
};

this.sender = new RelpConnection(sslEngineSupplier);
}
else {
this.sender = new RelpConnection();
}


this.sender.setConnectionTimeout(connectionTimeout);
this.sender.setReadTimeout(this.readTimeout);
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/logback.example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<readTimeout>15000</readTimeout>
<keepAlive>true</keepAlive>
<reconnectIfNoMessagesInterval>150000</reconnectIfNoMessagesInterval>
<!-- tls settings -->
<useTLS>false</useTLS>
<keystorePath>/path/to/keystore-client.jks</keystorePath>
<keystorePassword>changeit</keystorePassword>
<tlsProtocol>TLSv1.3</tlsProtocol>
<encoder>
<pattern>%-4relative %X{requestId} [%thread] %-5level %logger{35} - %msg</pattern>
</encoder>
Expand Down

0 comments on commit b43b712

Please sign in to comment.