Skip to content

Commit 109fd6f

Browse files
committed
Add SSL service connection support for MongoDB
See gh-41137
1 parent 0ccf1b8 commit 109fd6f

File tree

12 files changed

+167
-55
lines changed

12 files changed

+167
-55
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -18,6 +18,7 @@
1818

1919
import com.mongodb.client.MongoClient;
2020

21+
import org.springframework.beans.factory.ObjectProvider;
2122
import org.springframework.boot.autoconfigure.AutoConfiguration;
2223
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -27,6 +28,7 @@
2728
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
2829
import org.springframework.boot.autoconfigure.mongo.PropertiesMongoConnectionDetails;
2930
import org.springframework.boot.context.properties.EnableConfigurationProperties;
31+
import org.springframework.boot.ssl.SslBundles;
3032
import org.springframework.context.annotation.Bean;
3133
import org.springframework.context.annotation.Import;
3234
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -59,8 +61,9 @@ public class MongoDataAutoConfiguration {
5961

6062
@Bean
6163
@ConditionalOnMissingBean(MongoConnectionDetails.class)
62-
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties) {
63-
return new PropertiesMongoConnectionDetails(properties);
64+
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties,
65+
ObjectProvider<SslBundles> sslBundles) {
66+
return new PropertiesMongoConnectionDetails(properties, sslBundles.getIfAvailable());
6467
}
6568

6669
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -48,8 +48,9 @@ public class MongoAutoConfiguration {
4848

4949
@Bean
5050
@ConditionalOnMissingBean(MongoConnectionDetails.class)
51-
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties) {
52-
return new PropertiesMongoConnectionDetails(properties);
51+
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties,
52+
ObjectProvider<SslBundles> sslBundles) {
53+
return new PropertiesMongoConnectionDetails(properties, sslBundles.getIfAvailable());
5354
}
5455

5556
@Bean
@@ -70,9 +71,9 @@ MongoClientSettings mongoClientSettings() {
7071

7172
@Bean
7273
StandardMongoClientSettingsBuilderCustomizer standardMongoSettingsCustomizer(MongoProperties properties,
73-
MongoConnectionDetails connectionDetails, ObjectProvider<SslBundles> sslBundles) {
74-
return new StandardMongoClientSettingsBuilderCustomizer(connectionDetails.getConnectionString(),
75-
properties.getUuidRepresentation(), properties.getSsl(), sslBundles.getIfAvailable());
74+
MongoConnectionDetails connectionDetails) {
75+
return new StandardMongoClientSettingsBuilderCustomizer(connectionDetails,
76+
properties.getUuidRepresentation());
7677
}
7778

7879
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoConnectionDetails.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -19,6 +19,7 @@
1919
import com.mongodb.ConnectionString;
2020

2121
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
22+
import org.springframework.boot.ssl.SslBundle;
2223

2324
/**
2425
* Details required to establish a connection to a MongoDB service.
@@ -36,6 +37,15 @@ public interface MongoConnectionDetails extends ConnectionDetails {
3637
*/
3738
ConnectionString getConnectionString();
3839

40+
/**
41+
* SSL bundle to use.
42+
* @return the SSL bundle to use
43+
* @since 3.5.0
44+
*/
45+
default SslBundle getSslBundle() {
46+
return null;
47+
}
48+
3949
/**
4050
* GridFS configuration.
4151
* @return the GridFS configuration or {@code null}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfiguration.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -53,8 +53,9 @@ public class MongoReactiveAutoConfiguration {
5353

5454
@Bean
5555
@ConditionalOnMissingBean(MongoConnectionDetails.class)
56-
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties) {
57-
return new PropertiesMongoConnectionDetails(properties);
56+
PropertiesMongoConnectionDetails mongoConnectionDetails(MongoProperties properties,
57+
ObjectProvider<SslBundles> sslBundles) {
58+
return new PropertiesMongoConnectionDetails(properties, sslBundles.getIfAvailable());
5859
}
5960

6061
@Bean
@@ -77,9 +78,9 @@ MongoClientSettings mongoClientSettings() {
7778

7879
@Bean
7980
StandardMongoClientSettingsBuilderCustomizer standardMongoSettingsCustomizer(MongoProperties properties,
80-
MongoConnectionDetails connectionDetails, ObjectProvider<SslBundles> sslBundles) {
81-
return new StandardMongoClientSettingsBuilderCustomizer(connectionDetails.getConnectionString(),
82-
properties.getUuidRepresentation(), properties.getSsl(), sslBundles.getIfAvailable());
81+
MongoConnectionDetails connectionDetails) {
82+
return new StandardMongoClientSettingsBuilderCustomizer(connectionDetails,
83+
properties.getUuidRepresentation());
8384
}
8485

8586
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/PropertiesMongoConnectionDetails.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -23,6 +23,10 @@
2323

2424
import com.mongodb.ConnectionString;
2525

26+
import org.springframework.boot.autoconfigure.mongo.MongoProperties.Ssl;
27+
import org.springframework.boot.ssl.SslBundle;
28+
import org.springframework.boot.ssl.SslBundles;
29+
import org.springframework.util.Assert;
2630
import org.springframework.util.StringUtils;
2731

2832
/**
@@ -38,8 +42,11 @@ public class PropertiesMongoConnectionDetails implements MongoConnectionDetails
3842

3943
private final MongoProperties properties;
4044

41-
public PropertiesMongoConnectionDetails(MongoProperties properties) {
45+
private final SslBundles sslBundles;
46+
47+
public PropertiesMongoConnectionDetails(MongoProperties properties, SslBundles sslBundles) {
4248
this.properties = properties;
49+
this.sslBundles = sslBundles;
4350
}
4451

4552
@Override
@@ -90,6 +97,19 @@ public GridFs getGridFs() {
9097
PropertiesMongoConnectionDetails.this.properties.getGridfs().getBucket());
9198
}
9299

100+
@Override
101+
public SslBundle getSslBundle() {
102+
Ssl ssl = this.properties.getSsl();
103+
if (!ssl.isEnabled()) {
104+
return null;
105+
}
106+
if (StringUtils.hasLength(ssl.getBundle())) {
107+
Assert.notNull(this.sslBundles, "SSL bundle name has been set but no SSL bundles found in context");
108+
return this.sslBundles.getBundle(ssl.getBundle());
109+
}
110+
return SslBundle.systemDefault();
111+
}
112+
93113
private List<String> getOptions() {
94114
List<String> options = new ArrayList<>();
95115
if (StringUtils.hasText(this.properties.getReplicaSetName())) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/StandardMongoClientSettingsBuilderCustomizer.java

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -41,26 +41,55 @@ public class StandardMongoClientSettingsBuilderCustomizer implements MongoClient
4141

4242
private final UuidRepresentation uuidRepresentation;
4343

44+
private final MongoConnectionDetails connectionDetails;
45+
4446
private final MongoProperties.Ssl ssl;
4547

4648
private final SslBundles sslBundles;
4749

4850
private int order = 0;
4951

52+
/**
53+
* Create a new instance.
54+
* @param connectionString the connection string
55+
* @param uuidRepresentation the uuid representation
56+
* @param ssl the ssl properties
57+
* @param sslBundles the ssl bundles
58+
* @deprecated since 3.5.0 for removal in 3.7.0 in favor of
59+
* {@link #StandardMongoClientSettingsBuilderCustomizer(MongoConnectionDetails, UuidRepresentation)}
60+
*/
61+
@Deprecated(forRemoval = true, since = "3.5.0")
5062
public StandardMongoClientSettingsBuilderCustomizer(ConnectionString connectionString,
5163
UuidRepresentation uuidRepresentation, MongoProperties.Ssl ssl, SslBundles sslBundles) {
64+
this.connectionDetails = null;
5265
this.connectionString = connectionString;
5366
this.uuidRepresentation = uuidRepresentation;
5467
this.ssl = ssl;
5568
this.sslBundles = sslBundles;
5669
}
5770

71+
public StandardMongoClientSettingsBuilderCustomizer(MongoConnectionDetails connectionDetails,
72+
UuidRepresentation uuidRepresentation) {
73+
this.connectionString = null;
74+
this.ssl = null;
75+
this.sslBundles = null;
76+
this.connectionDetails = connectionDetails;
77+
this.uuidRepresentation = uuidRepresentation;
78+
}
79+
5880
@Override
5981
public void customize(MongoClientSettings.Builder settingsBuilder) {
6082
settingsBuilder.uuidRepresentation(this.uuidRepresentation);
61-
settingsBuilder.applyConnectionString(this.connectionString);
62-
if (this.ssl.isEnabled()) {
63-
settingsBuilder.applyToSslSettings(this::configureSsl);
83+
if (this.connectionDetails != null) {
84+
settingsBuilder.applyConnectionString(this.connectionDetails.getConnectionString());
85+
settingsBuilder.applyToSslSettings(this::configureSslIfNeeded);
86+
}
87+
else {
88+
settingsBuilder.uuidRepresentation(this.uuidRepresentation);
89+
settingsBuilder.applyConnectionString(this.connectionString);
90+
if (this.ssl.isEnabled()) {
91+
settingsBuilder.applyToSslSettings(this::configureSsl);
92+
}
6493
}
6594
}
6695

@@ -73,6 +102,15 @@ private void configureSsl(SslSettings.Builder settings) {
73102
}
74103
}
75104

105+
private void configureSslIfNeeded(SslSettings.Builder settings) {
106+
SslBundle sslBundle = this.connectionDetails.getSslBundle();
107+
if (sslBundle != null) {
108+
settings.enabled(true);
109+
Assert.state(!sslBundle.getOptions().isSpecified(), "SSL options cannot be specified with MongoDB");
110+
settings.context(sslBundle.createSslContext());
111+
}
112+
}
113+
76114
@Override
77115
public int getOrder() {
78116
return this.order;

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfigurationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -80,7 +80,7 @@ void configuresSslWhenEnabled() {
8080
this.contextRunner.withPropertyValues("spring.data.mongodb.ssl.enabled=true").run((context) -> {
8181
SslSettings sslSettings = getSettings(context).getSslSettings();
8282
assertThat(sslSettings.isEnabled()).isTrue();
83-
assertThat(sslSettings.getContext()).isNull();
83+
assertThat(sslSettings.getContext()).isNotNull();
8484
});
8585
}
8686

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -88,7 +88,7 @@ void configuresSslWhenEnabled() {
8888
this.contextRunner.withPropertyValues("spring.data.mongodb.ssl.enabled=true").run((context) -> {
8989
SslSettings sslSettings = getSettings(context).getSslSettings();
9090
assertThat(sslSettings.isEnabled()).isTrue();
91-
assertThat(sslSettings.getContext()).isNull();
91+
assertThat(sslSettings.getContext()).isNotNull();
9292
});
9393
}
9494

0 commit comments

Comments
 (0)