Skip to content

Commit 325e255

Browse files
robertapasRoberta Pascale
and
Roberta Pascale
authored
Updates to allow configuration of SSLContext for TLS connection (#33)
Co-authored-by: Roberta Pascale <[email protected]>
1 parent 2eef343 commit 325e255

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ target/
88
.settings/
99
.project
1010
.classpath
11-
.idea
11+
.idea
12+
.vscode/

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ therefore its easy for Rabbit client to subscribe to selective combinations eg:
6666
- `KK_TO_RMQ_USERNAME` - default: *admin*
6767
- `KK_TO_RMQ_PASSWORD` - default: *admin*
6868
- `KK_TO_RMQ_USE_TLS` - default: *false*
69+
- `KK_TO_RMQ_KEY_STORE` - default: *empty*
70+
- `KK_TO_RMQ_KEY_STORE_PASS` - default: *empty*
71+
- `KK_TO_RMQ_TRUST_STORE` - default: *empty*
72+
- `KK_TO_RMQ_TRUST_STORE_PASS` - default: *empty*
6973

7074
###### Deprecated OPTION 2: edit Keycloak subsystem of WildFly (Keycloak 16 and older) standalone.xml or standalone-ha.xml:
7175

@@ -78,7 +82,10 @@ therefore its easy for Rabbit client to subscribe to selective combinations eg:
7882
<property name="vhost" value="${env.KK_TO_RMQ_VHOST:}"/>
7983
<property name="exchange" value="${env.KK_TO_RMQ_EXCHANGE:amq.topic}"/>
8084
<property name="use_tls" value="${env.KK_TO_RMQ_USE_TLS:false}"/>
81-
85+
<property name="key_store" value="${env.KK_TO_RMQ_KEY_STORE:}"/>
86+
<property name="key_store_pass" value="${env.KK_TO_RMQ_KEY_STORE_PASS:}"/>
87+
<property name="trust_store" value="${env.KK_TO_RMQ_TRUST_STORE:}"/>
88+
<property name="trust_store_pass" value="${env.KK_TO_RMQ_TRUST_STORE_PASS:}"/>
8289
<property name="username" value="${env.KK_TO_RMQ_USERNAME:guest}"/>
8390
<property name="password" value="${env.KK_TO_RMQ_PASSWORD:guest}"/>
8491
</properties>

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.aznamier.keycloak.event.provider</groupId>
66
<artifactId>keycloak-to-rabbit</artifactId>
77
<packaging>jar</packaging>
8-
<version>3.0.2</version>
8+
<version>3.0.3</version>
99

1010

1111

src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqConfig.java

+43
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public class RabbitMqConfig {
2828
private String vhost;
2929
private Boolean useTls;
3030

31+
// SSL context settings
32+
private String trustStore;
33+
private String trustStorePass;
34+
private String keyStore;
35+
private String keyStorePass;
36+
//
37+
38+
3139
private String exchange;
3240

3341
public static String calculateRoutingKey(AdminEvent adminEvent) {
@@ -92,6 +100,13 @@ public static RabbitMqConfig createFromScope(Scope config) {
92100
cfg.vhost = resolveConfigVar(config, "vhost", "");
93101
cfg.useTls = Boolean.valueOf(resolveConfigVar(config, "use_tls", "false"));
94102

103+
// SSL context settings
104+
cfg.trustStore = resolveConfigVar(config, "trust_store", "");
105+
cfg.trustStorePass = resolveConfigVar(config, "trust_store_pass", "");
106+
cfg.keyStore = resolveConfigVar(config, "key_store", "");
107+
cfg.keyStorePass = resolveConfigVar(config, "key_store_pass", "");
108+
//
109+
95110
cfg.exchange = resolveConfigVar(config, "exchange", "amq.topic");
96111
return cfg;
97112

@@ -154,6 +169,34 @@ public Boolean getUseTls() {
154169
public void setUseTls(Boolean useTls) {
155170
this.useTls = useTls;
156171
}
172+
173+
// setters and getters SSL context setting
174+
public void setTrustStore(String trustStore) {
175+
this.trustStore = trustStore;
176+
}
177+
public void setTrustStorePass(String trustStorePass) {
178+
this.trustStorePass = trustStorePass;
179+
}
180+
public void setKeyStore(String keyStore) {
181+
this.keyStore = keyStore;
182+
}
183+
public void setKeyStorePass(String keyStorePass) {
184+
this.keyStorePass = keyStorePass;
185+
}
186+
public String getTrustStore() {
187+
return trustStore;
188+
}
189+
public String getTrustStorePass() {
190+
return trustStorePass;
191+
}
192+
public String getKeyStore() {
193+
return keyStore;
194+
}
195+
public String getKeytStorePass() {
196+
return keyStorePass;
197+
}
198+
//
199+
157200
public String getExchange() {
158201
return exchange;
159202
}

src/main/java/com/github/aznamier/keycloak/event/provider/RabbitMqEventListenerProviderFactory.java

+44-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import com.rabbitmq.client.ConnectionFactory;
66
import java.io.IOException;
77
import java.util.concurrent.TimeoutException;
8+
//
9+
import java.io.*;
10+
import java.security.*;
11+
import javax.net.ssl.*;
12+
//
813
import org.jboss.logging.Logger;
914
import org.keycloak.Config.Scope;
1015
import org.keycloak.events.EventListenerProvider;
@@ -54,7 +59,45 @@ public void init(Scope config) {
5459

5560
if (cfg.getUseTls()) {
5661
try {
57-
this.connectionFactory.useSslProtocol();
62+
Boolean context = false;
63+
SSLContext c = SSLContext.getInstance("TLSv1.2");
64+
65+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
66+
if (! cfg.getTrustStore().isEmpty()){
67+
char[] trustPassphrase = cfg.getTrustStorePass().toCharArray();
68+
KeyStore tks = KeyStore.getInstance("JKS");
69+
tks.load(new FileInputStream(cfg.getTrustStore()), trustPassphrase);
70+
71+
tmf.init(tks);
72+
73+
c.init(null, tmf.getTrustManagers(), null);
74+
context = true;
75+
}
76+
77+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
78+
if (! cfg.getKeyStore().isEmpty()){
79+
char[] keyPassphrase = cfg.getKeytStorePass().toCharArray();
80+
KeyStore ks = KeyStore.getInstance("PKCS12");
81+
ks.load(new FileInputStream(cfg.getKeyStore()), keyPassphrase);
82+
83+
kmf.init(ks, keyPassphrase);
84+
85+
if (context){
86+
c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
87+
}
88+
else{
89+
c.init(kmf.getKeyManagers(), null, null);
90+
context = true;
91+
}
92+
}
93+
94+
if ( context ){
95+
this.connectionFactory.useSslProtocol(c);
96+
}
97+
else {
98+
this.connectionFactory.useSslProtocol();
99+
}
100+
58101
}
59102
catch (Exception e) {
60103
log.error("Could not use SSL protocol", e);

0 commit comments

Comments
 (0)