Skip to content

Commit b43b712

Browse files
authored
Enable tls (#21)
* tls support enabled * add tls example configuration
1 parent e21e48d commit b43b712

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/main/java/com/teragrep/jla_01/RlpLogbackAppender.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ Reliable Event Logging Protocol (RELP) Logback plugin
1919

2020
import java.io.IOException;
2121
import java.nio.charset.StandardCharsets;
22+
import java.security.GeneralSecurityException;
2223
import java.util.UUID;
2324
import java.util.concurrent.TimeoutException;
25+
import java.util.function.Supplier;
2426

2527
import ch.qos.logback.classic.spi.ILoggingEvent;
2628
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
@@ -30,6 +32,10 @@ Reliable Event Logging Protocol (RELP) Logback plugin
3032
import com.teragrep.rlp_01.RelpBatch;
3133
import ch.qos.logback.core.AppenderBase;
3234
import com.teragrep.rlp_01.RelpConnection;
35+
import com.teragrep.rlp_01.SSLContextFactory;
36+
37+
import javax.net.ssl.SSLContext;
38+
import javax.net.ssl.SSLEngine;
3339

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

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

68+
// tls
69+
private boolean useTLS = false;
70+
private String keystorePath = "";
71+
private String keystorePassword = "";
72+
private String tlsProtocol = "";
73+
74+
6275
public void setEncoder(LayoutWrappingEncoder encoder) {
6376
this.encoder = encoder;
6477
}
@@ -121,6 +134,23 @@ public void setReconnectIfNoMessagesInterval(int interval) {
121134
this.reconnectIfNoMessagesInterval = interval;
122135
}
123136

137+
// tls
138+
public void setUseTLS(boolean on) {
139+
this.useTLS = on;
140+
}
141+
142+
public void setKeystorePath(String keystorePath) {
143+
this.keystorePath = keystorePath;
144+
}
145+
146+
public void setKeystorePassword(String keystorePassword) {
147+
this.keystorePassword = keystorePassword;
148+
}
149+
150+
public void setTlsProtocol(String tlsProtocol) {
151+
this.tlsProtocol = tlsProtocol;
152+
}
153+
124154
private void connect() {
125155
if (System.getenv("JLA01_DEBUG") != null) {
126156
System.out.println("RlpLogbackAppender.connect>");
@@ -178,7 +208,29 @@ public void start() {
178208
return;
179209

180210
// initialize events sender
181-
this.sender = new RelpConnection();
211+
if (useTLS) {
212+
Supplier<SSLEngine> sslEngineSupplier = new Supplier<SSLEngine>() {
213+
private final SSLContext sslContext;
214+
{
215+
try {
216+
sslContext = SSLContextFactory.authenticatedContext(keystorePath, keystorePassword, tlsProtocol);
217+
} catch (GeneralSecurityException | IOException e) {
218+
throw new RuntimeException(e);
219+
}
220+
}
221+
222+
@Override
223+
public SSLEngine get() {
224+
return sslContext.createSSLEngine();
225+
}
226+
};
227+
228+
this.sender = new RelpConnection(sslEngineSupplier);
229+
}
230+
else {
231+
this.sender = new RelpConnection();
232+
}
233+
182234

183235
this.sender.setConnectionTimeout(connectionTimeout);
184236
this.sender.setReadTimeout(this.readTimeout);

src/main/resources/logback.example.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
<readTimeout>15000</readTimeout>
1313
<keepAlive>true</keepAlive>
1414
<reconnectIfNoMessagesInterval>150000</reconnectIfNoMessagesInterval>
15+
<!-- tls settings -->
16+
<useTLS>false</useTLS>
17+
<keystorePath>/path/to/keystore-client.jks</keystorePath>
18+
<keystorePassword>changeit</keystorePassword>
19+
<tlsProtocol>TLSv1.3</tlsProtocol>
1520
<encoder>
1621
<pattern>%-4relative %X{requestId} [%thread] %-5level %logger{35} - %msg</pattern>
1722
</encoder>

0 commit comments

Comments
 (0)