Skip to content

Commit 8edfb2b

Browse files
Balazs Nemethmp911de
authored andcommitted
DATAREDIS-480 - Add support for all lettuce SSL options.
LettuceConnectionFactory now supports verifyPeer and startTls options for Redis Standalone usage. Original pull request: spring-projects#180. CLA: 166820160311101157 (Balázs Németh)
1 parent 93bad42 commit 8edfb2b

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public class LettuceConnectionFactory implements InitializingBean, DisposableBea
9999
private ClusterCommandExecutor clusterCommandExecutor;
100100
private ClientResources clientResources;
101101
private boolean useSsl;
102+
private boolean verifyPeer = true;
103+
private boolean startTls;
102104

103105
/**
104106
* Constructs a new <code>LettuceConnectionFactory</code> instance with default settings.
@@ -327,6 +329,43 @@ public boolean isUseSsl() {
327329
return useSsl;
328330
}
329331

332+
/**
333+
* Sets to use verify certificate validity/hostname check when SSL is used
334+
*
335+
* @param verifyPeer {@literal false} not to verify hostname.
336+
*/
337+
public void setVerifyPeer(boolean verifyPeer) {
338+
this.verifyPeer = verifyPeer;
339+
}
340+
341+
/**
342+
* Returns whether to verify certificate validity/hostname check when SSL is used
343+
*
344+
* @return
345+
*/
346+
public boolean isVerifyPeer() {
347+
return verifyPeer;
348+
}
349+
350+
/**
351+
* Returns whether to issue a StartTLS
352+
*
353+
* @return
354+
*/
355+
public boolean isStartTls() {
356+
return startTls;
357+
}
358+
359+
360+
/**
361+
* Sets to issue StartTLS
362+
*
363+
* @param startTls {@literal true} to issue StartTLS
364+
*/
365+
public void setStartTls(boolean startTls) {
366+
this.startTls = startTls;
367+
}
368+
330369
/**
331370
* Indicates if validation of the native Lettuce connection is enabled
332371
*
@@ -556,6 +595,8 @@ private AbstractRedisClient createRedisClient() {
556595
builder.withPassword(password);
557596
}
558597
builder.withSsl(useSsl);
598+
builder.withVerifyPeer(verifyPeer);
599+
builder.withStartTls(startTls);
559600
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);
560601
if (clientResources != null) {
561602
return RedisClient.create(clientResources, builder.build());

src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactoryUnitTests.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,46 @@ public void sslShouldBeSetCorrectlyOnClient() {
150150
assertThat(connectionFactory.isUseSsl(), is(true));
151151
}
152152

153+
/**
154+
* @see DATAREDIS-480
155+
*/
156+
@Test
157+
public void verifyPeerOptionShouldBeSetCorrectlyOnClient() {
158+
159+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
160+
connectionFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
161+
connectionFactory.setVerifyPeer(false);
162+
connectionFactory.afterPropertiesSet();
163+
ConnectionFactoryTracker.add(connectionFactory);
164+
165+
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
166+
assertThat(client, instanceOf(RedisClient.class));
167+
168+
RedisURI redisUri = (RedisURI) getField(client, "redisURI");
169+
170+
assertThat(redisUri.isVerifyPeer(), is(false));
171+
assertThat(connectionFactory.isVerifyPeer(), is(false));
172+
}
173+
174+
/**
175+
* @see DATAREDIS-480
176+
*/
177+
@Test
178+
public void startTLSOptionShouldBeSetCorrectlyOnClient() {
179+
180+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
181+
connectionFactory.setClientResources(LettuceTestClientResources.getSharedClientResources());
182+
connectionFactory.setStartTls(true);
183+
connectionFactory.afterPropertiesSet();
184+
ConnectionFactoryTracker.add(connectionFactory);
185+
186+
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
187+
assertThat(client, instanceOf(RedisClient.class));
188+
189+
RedisURI redisUri = (RedisURI) getField(client, "redisURI");
190+
191+
assertThat(redisUri.isStartTls(), is(true));
192+
assertThat(connectionFactory.isStartTls(), is(true));
193+
}
194+
153195
}

0 commit comments

Comments
 (0)