Skip to content

Commit 710e77d

Browse files
misaavedodrotbohm
authored andcommitted
DATAMONGO-764 - Added SSL support to Mongo configuration options.
Added support for allowing Mongo clients to use secure SSL connections by introducing the "ssl" property in MongoOptionsFactoryBean that will enable the use of the configured SSLSocketFactory to create SSLSockets. If no custom SSLSocketFactory is configured SSLSocketFactory#getDefault() will be used. We introduce this configuration in a new version of spring-mongo-1.4.xsd. Applied Mike Saavedra's pull request (spring-projects#75) with the above mentioned extensions. Original pull request: spring-projects#83.
1 parent 9c99661 commit 710e77d

File tree

8 files changed

+805
-64
lines changed

8 files changed

+805
-64
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MongoParsingUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 the original author or authors.
2+
* Copyright 2011-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @author Mark Pollack
3535
* @author Oliver Gierke
36+
* @author Thomas Darimont
3637
*/
3738
abstract class MongoParsingUtils {
3839

@@ -79,6 +80,8 @@ static boolean parseMongoOptions(Element element, BeanDefinitionBuilder mongoBui
7980
setPropertyValue(optionsDefBuilder, optionsElement, "write-timeout", "writeTimeout");
8081
setPropertyValue(optionsDefBuilder, optionsElement, "write-fsync", "writeFsync");
8182
setPropertyValue(optionsDefBuilder, optionsElement, "slave-ok", "slaveOk");
83+
setPropertyValue(optionsDefBuilder, optionsElement, "ssl", "ssl");
84+
setPropertyReference(optionsDefBuilder, optionsElement, "ssl-socket-factory-ref", "sslSocketFactory");
8285

8386
mongoBuilder.addPropertyValue("mongoOptions", optionsDefBuilder.getBeanDefinition());
8487
return true;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2011 the original author or authors.
2+
* Copyright 2010-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,129 +15,146 @@
1515
*/
1616
package org.springframework.data.mongodb.core;
1717

18-
import com.mongodb.MongoOptions;
18+
import javax.net.ssl.SSLSocketFactory;
1919

2020
import org.springframework.beans.factory.FactoryBean;
2121
import org.springframework.beans.factory.InitializingBean;
2222

23+
import com.mongodb.MongoOptions;
24+
2325
/**
24-
* A factory bean for construction of a MongoOptions instance
26+
* A factory bean for construction of a {@link MongoOptions} instance.
2527
*
2628
* @author Graeme Rocher
27-
* @Author Mark Pollack
29+
* @author Mark Pollack
30+
* @author Mike Saavedra
31+
* @author Thomas Darimont
2832
*/
2933
public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, InitializingBean {
3034

31-
private static final MongoOptions MONGO_OPTIONS = new MongoOptions();
35+
private final MongoOptions MONGO_OPTIONS = new MongoOptions();
36+
3237
/**
33-
* number of connections allowed per host will block if run out
38+
* The number of connections allowed per host will block if run out.
3439
*/
3540
private int connectionsPerHost = MONGO_OPTIONS.connectionsPerHost;
3641

3742
/**
38-
* multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is 10, and
39-
* threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an exception will
40-
* be throw
43+
* A multiplier for connectionsPerHost for # of threads that can block a connection.
44+
* <p>
45+
* If connectionsPerHost is {@literal 10}, and threadsAllowedToBlockForConnectionMultiplier is {@literal 5}, then
46+
* {@literal 50} threads can block. If more threads try to block an exception will be thrown.
4147
*/
4248
private int threadsAllowedToBlockForConnectionMultiplier = MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier;
4349

4450
/**
45-
* max wait time of a blocking thread for a connection
51+
* Max wait time of a blocking thread for a connection.
4652
*/
4753
private int maxWaitTime = MONGO_OPTIONS.maxWaitTime;
4854

4955
/**
50-
* connect timeout in milliseconds. 0 is default and infinite
56+
* Connect timeout in milliseconds. {@literal 0} is default and means infinite time.
5157
*/
5258
private int connectTimeout = MONGO_OPTIONS.connectTimeout;
5359

5460
/**
55-
* socket timeout. 0 is default and infinite
61+
* The socket timeout. {@literal 0} is default and means infinite time.
5662
*/
5763
private int socketTimeout = MONGO_OPTIONS.socketTimeout;
5864

5965
/**
60-
* This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE).
61-
*
62-
* defaults to false
66+
* This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE). This defaults to {@literal false}.
6367
*/
6468
public boolean socketKeepAlive = MONGO_OPTIONS.socketKeepAlive;
6569

6670
/**
67-
* this controls whether or not on a connect, the system retries automatically
71+
* This controls whether or not the system retries automatically on a failed connect. This defaults to
72+
* {@literal false}.
6873
*/
6974
private boolean autoConnectRetry = MONGO_OPTIONS.autoConnectRetry;
7075

7176
private long maxAutoConnectRetryTime = MONGO_OPTIONS.maxAutoConnectRetryTime;
7277

7378
/**
74-
* This specifies the number of servers to wait for on the write operation, and exception raising behavior.
75-
*
76-
* Defaults to 0.
79+
* This specifies the number of servers to wait for on the write operation, and exception raising behavior. This
80+
* defaults to {@literal 0}.
7781
*/
7882
private int writeNumber;
7983

8084
/**
81-
* This controls timeout for write operations in milliseconds.
82-
*
83-
* Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait.
85+
* This controls timeout for write operations in milliseconds. This defaults to {@literal 0} (indefinite). Greater
86+
* than zero is number of milliseconds to wait.
8487
*/
8588
private int writeTimeout;
8689

8790
/**
88-
* This controls whether or not to fsync.
89-
*
90-
* Defaults to false.
91+
* This controls whether or not to fsync. This defaults to {@literal false}.
9192
*/
9293
private boolean writeFsync;
9394

9495
/**
95-
* Specifies if the driver is allowed to read from secondaries or slaves.
96-
*
97-
* Defaults to false
96+
* Specifies if the driver is allowed to read from secondaries or slaves. This defaults to {@literal false}.
9897
*/
99-
@SuppressWarnings("deprecation")
100-
private boolean slaveOk = MONGO_OPTIONS.slaveOk;
98+
@SuppressWarnings("deprecation") private boolean slaveOk = MONGO_OPTIONS.slaveOk;
10199

102100
/**
103-
* number of connections allowed per host will block if run out
101+
* This controls SSL support via SSLSocketFactory. This defaults to {@literal false}.
102+
*/
103+
private boolean ssl;
104+
105+
/**
106+
* Specifies the {@link SSLSocketFactory} to use. This defaults to {@link SSLSocketFactory#getDefault()}
107+
*/
108+
private SSLSocketFactory sslSocketFactory;
109+
110+
/**
111+
* The maximum number of connections allowed per host until we will block.
112+
*
113+
* @param connectionsPerHost
104114
*/
105115
public void setConnectionsPerHost(int connectionsPerHost) {
106116
this.connectionsPerHost = connectionsPerHost;
107117
}
108118

109119
/**
110-
* multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is 10, and
111-
* threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more than that and an exception will
112-
* be throw
120+
* A multiplier for connectionsPerHost for # of threads that can block a connection.
121+
*
122+
* @see #threadsAllowedToBlockForConnectionMultiplier
123+
* @param threadsAllowedToBlockForConnectionMultiplier
113124
*/
114125
public void setThreadsAllowedToBlockForConnectionMultiplier(int threadsAllowedToBlockForConnectionMultiplier) {
115126
this.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
116127
}
117128

118129
/**
119-
* max wait time of a blocking thread for a connection
130+
* Max wait time of a blocking thread for a connection.
131+
*
132+
* @param maxWaitTime
120133
*/
121134
public void setMaxWaitTime(int maxWaitTime) {
122135
this.maxWaitTime = maxWaitTime;
123136
}
124137

125138
/**
126-
* connect timeout in milliseconds. 0 is default and infinite
139+
* The connect timeout in milliseconds. {@literal 0} is default and infinite
140+
*
141+
* @param connectTimeout
127142
*/
128143
public void setConnectTimeout(int connectTimeout) {
129144
this.connectTimeout = connectTimeout;
130145
}
131146

132147
/**
133-
* socket timeout. 0 is default and infinite
148+
* The socket timeout. {@literal 0} is default and infinite.
149+
*
150+
* @param socketTimeout
134151
*/
135152
public void setSocketTimeout(int socketTimeout) {
136153
this.socketTimeout = socketTimeout;
137154
}
138155

139156
/**
140-
* This controls whether or not to have socket keep alive
157+
* This controls whether or not to have socket keep alive.
141158
*
142159
* @param socketKeepAlive
143160
*/
@@ -147,12 +164,12 @@ public void setSocketKeepAlive(boolean socketKeepAlive) {
147164

148165
/**
149166
* This specifies the number of servers to wait for on the write operation, and exception raising behavior. The 'w'
150-
* option to the getlasterror command. Defaults to 0.
167+
* option to the getlasterror command. Defaults to {@literal 0}.
151168
* <ul>
152-
* <li>-1 = don't even report network errors</li>
153-
* <li>0 = default, don't call getLastError by default</li>
154-
* <li>1 = basic, call getLastError, but don't wait for slaves</li>
155-
* <li>2+= wait for slaves</li>
169+
* <li>{@literal -1} = don't even report network errors</li>
170+
* <li>{@literal 0} = default, don't call getLastError by default</li>
171+
* <li>{@literal 1} = basic, call getLastError, but don't wait for slaves</li>
172+
* <li>{@literal 2} += wait for slaves</li>
156173
* </ul>
157174
*
158175
* @param writeNumber the number of servers to wait for on the write operation, and exception raising behavior.
@@ -164,31 +181,33 @@ public void setWriteNumber(int writeNumber) {
164181
/**
165182
* This controls timeout for write operations in milliseconds. The 'wtimeout' option to the getlasterror command.
166183
*
167-
* @param writeTimeout Defaults to 0 (indefinite). Greater than zero is number of milliseconds to wait.
184+
* @param writeTimeout Defaults to {@literal 0} (indefinite). Greater than zero is number of milliseconds to wait.
168185
*/
169186
public void setWriteTimeout(int writeTimeout) {
170187
this.writeTimeout = writeTimeout;
171188
}
172189

173190
/**
174-
* This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to false.
191+
* This controls whether or not to fsync. The 'fsync' option to the getlasterror command. Defaults to {@literal false}
175192
*
176-
* @param writeFsync to fsync on write (true), otherwise false.
193+
* @param writeFsync to fsync on <code>write (true)<code>, otherwise {@literal false}.
177194
*/
178195
public void setWriteFsync(boolean writeFsync) {
179196
this.writeFsync = writeFsync;
180197
}
181198

182199
/**
183-
* this controls whether or not on a connect, the system retries automatically
200+
* Controls whether or not the system retries automatically, on a failed connect.
201+
*
202+
* @param autoConnectRetry
184203
*/
185204
public void setAutoConnectRetry(boolean autoConnectRetry) {
186205
this.autoConnectRetry = autoConnectRetry;
187206
}
188207

189208
/**
190-
* The maximum amount of time in millisecons to spend retrying to open connection to the same server. Default is 0,
191-
* which means to use the default 15s if autoConnectRetry is on.
209+
* The maximum amount of time in millisecons to spend retrying to open connection to the same server. This defaults to
210+
* {@literal 0}, which means to use the default {@literal 15s} if {@link #autoConnectRetry} is on.
192211
*
193212
* @param maxAutoConnectRetryTime the maxAutoConnectRetryTime to set
194213
*/
@@ -197,16 +216,37 @@ public void setMaxAutoConnectRetryTime(long maxAutoConnectRetryTime) {
197216
}
198217

199218
/**
200-
* Specifies if the driver is allowed to read from secondaries or slaves. Defaults to false.
219+
* Specifies if the driver is allowed to read from secondaries or slaves. This defaults to {@literal false}.
201220
*
202221
* @param slaveOk true if the driver should read from secondaries or slaves.
203222
*/
204223
public void setSlaveOk(boolean slaveOk) {
205224
this.slaveOk = slaveOk;
206225
}
207226

227+
/**
228+
* Specifies if the driver should use an SSL connection to Mongo. This defaults to {@literal false}.
229+
*
230+
* @param ssl true if the driver should use an SSL connection.
231+
*/
232+
public void setSsl(boolean ssl) {
233+
this.ssl = ssl;
234+
}
235+
236+
/**
237+
* Specifies the SSLSocketFactory to use for creating SSL connections to Mongo.
238+
*
239+
* @param sslSocketFactory the sslSocketFactory to use.
240+
*/
241+
public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
242+
243+
setSsl(sslSocketFactory != null);
244+
this.sslSocketFactory = sslSocketFactory;
245+
}
246+
208247
@SuppressWarnings("deprecation")
209248
public void afterPropertiesSet() {
249+
210250
MONGO_OPTIONS.connectionsPerHost = connectionsPerHost;
211251
MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
212252
MONGO_OPTIONS.maxWaitTime = maxWaitTime;
@@ -219,6 +259,9 @@ public void afterPropertiesSet() {
219259
MONGO_OPTIONS.w = writeNumber;
220260
MONGO_OPTIONS.wtimeout = writeTimeout;
221261
MONGO_OPTIONS.fsync = writeFsync;
262+
if (ssl) {
263+
MONGO_OPTIONS.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault());
264+
}
222265
}
223266

224267
public MongoOptions getObject() {
@@ -232,5 +275,4 @@ public Class<?> getObjectType() {
232275
public boolean isSingleton() {
233276
return true;
234277
}
235-
236278
}

spring-data-mongodb/src/main/resources/META-INF/spring.schemas

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/sprin
22
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd=org/springframework/data/mongodb/config/spring-mongo-1.1.xsd
33
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd=org/springframework/data/mongodb/config/spring-mongo-1.2.xsd
44
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.3.xsd=org/springframework/data/mongodb/config/spring-mongo-1.3.xsd
5-
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.3.xsd
5+
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.4.xsd=org/springframework/data/mongodb/config/spring-mongo-1.4.xsd
6+
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.4.xsd

0 commit comments

Comments
 (0)