Skip to content

Commit 1a8b85d

Browse files
authored
Simplify configuration for Capella (#24)
Take advantage of the Capella CA certificate bundled with Java SDK 3.3.0. Add a note that the simplest way to enable TLS is to treat the `spring.couchbase.bootstrap-hosts` application property as a connection string, and prefix it with "couchbases://". In the commented-out example code, configure the ClusterEnvironment using a lambda. This way, the user is not responsible for shutting down the environment. Disconnect the cluster when the bean is destroyed. It doesn't matter for this example project, but it's a good practice that prevents resource leakage when the Spring webapp is undeployed from a non-embedded app server.
1 parent 212d550 commit 1a8b85d

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/main/java/org/couchbase/quickstart/configs/CouchbaseConfig.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
11
package org.couchbase.quickstart.configs;
22

3-
import com.couchbase.client.core.deps.io.netty.handler.ssl.util.InsecureTrustManagerFactory;
4-
import com.couchbase.client.core.env.IoConfig;
5-
import com.couchbase.client.core.env.SecurityConfig;
6-
import com.couchbase.client.core.error.BucketExistsException;
73
import com.couchbase.client.core.msg.kv.DurabilityLevel;
84
import com.couchbase.client.java.Bucket;
95
import com.couchbase.client.java.Cluster;
106
import com.couchbase.client.java.ClusterOptions;
11-
import com.couchbase.client.java.env.ClusterEnvironment;
127
import com.couchbase.client.java.manager.bucket.BucketSettings;
138
import com.couchbase.client.java.manager.bucket.BucketType;
149
import org.springframework.beans.factory.annotation.Autowired;
1510
import org.springframework.context.annotation.Bean;
1611
import org.springframework.context.annotation.Configuration;
1712

13+
import java.nio.file.Paths;
14+
1815
@Configuration
1916
public class CouchbaseConfig {
2017

2118
@Autowired
2219
private DBProperties dbProp;
2320

2421
/**
25-
* NOTE: To connect with Couchbase CAPELLA please use the commented method bellow as it requires TLS
26-
*/
27-
/* @Bean
28-
public Cluster getCouchbaseCluster(){
29-
ClusterEnvironment env = ClusterEnvironment.builder()
30-
.securityConfig(SecurityConfig.enableTls(true)
31-
.trustManagerFactory(InsecureTrustManagerFactory.INSTANCE))
32-
.ioConfig(IoConfig.enableDnsSrv(true))
33-
.build();
34-
return Cluster.connect(dbProp.getHostName(),
35-
ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword()).environment(env));
36-
}
37-
*/
38-
39-
/**
40-
* NOTE: To connect with Couchbase locally use the methode bellow
22+
* NOTE: If connecting to Couchbase Capella, you must enable TLS.
23+
* <p>
24+
* The simplest way to enable TLS is to edit {@code application.properties}
25+
* and make sure the {@code spring.couchbase.bootstrap-hosts} config property
26+
* starts with "couchbases://" (note the final 's'), like this:
27+
* <pre>
28+
* spring.couchbase.bootstrap-hosts=couchbases://my-cluster.cloud.couchbase.com
29+
* </pre>
30+
* Alternatively, you can enable TLS by writing code to configure the cluster environment;
31+
* see the commented-out code in this method for an example.
4132
*/
42-
@Bean
43-
public Cluster getCouchbaseCluster(){
33+
@Bean(destroyMethod = "disconnect")
34+
public Cluster getCouchbaseCluster() {
4435
return Cluster.connect(dbProp.getHostName(), dbProp.getUsername(), dbProp.getPassword());
36+
37+
// Here is an alternative version that enables TLS by configuring the cluster environment.
38+
/* return Cluster.connect(
39+
dbProp.getHostName(),
40+
ClusterOptions.clusterOptions(dbProp.getUsername(), dbProp.getPassword())
41+
.environment(env -> { // Configure cluster environment properties here
42+
env.securityConfig().enableTls(true);
43+
44+
// If you're connecting to Capella, the SDK already knows which certificates to trust.
45+
// When using TLS with non-Capella clusters, you must tell the SDK which certificates to trust.
46+
env.securityConfig().trustCertificate(
47+
Paths.get("/path/to/trusted-root-certificate.pem")
48+
);
49+
})
50+
);
51+
*/
4552
}
4653

4754
@Bean
48-
public Bucket getCouchbaseBucket(Cluster cluster){
55+
public Bucket getCouchbaseBucket(Cluster cluster) {
4956

5057
// Creates the cluster if it does not exist yet
5158
if (!cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) {

0 commit comments

Comments
 (0)