Skip to content

Commit 9b73d65

Browse files
committed
Merge pull request #16389 from enesacikoglu
* pr/16389: Polish "Add support for Couchbase's role-based access" Add support for Couchbase's role-based access
2 parents f8eb230 + 2949561 commit 9b73d65

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public DefaultCouchbaseEnvironment couchbaseEnvironment() {
5757
@Bean
5858
@Primary
5959
public Cluster couchbaseCluster() {
60-
return CouchbaseCluster.create(couchbaseEnvironment(), determineBootstrapHosts());
60+
CouchbaseCluster couchbaseCluster = CouchbaseCluster
61+
.create(couchbaseEnvironment(), determineBootstrapHosts());
62+
if (isRoleBasedAccessControlEnabled()) {
63+
return couchbaseCluster.authenticate(this.properties.getUsername(),
64+
this.properties.getPassword());
65+
}
66+
return couchbaseCluster;
6167
}
6268

6369
/**
@@ -79,10 +85,18 @@ public ClusterInfo couchbaseClusterInfo() {
7985
@Bean
8086
@Primary
8187
public Bucket couchbaseClient() {
88+
if (isRoleBasedAccessControlEnabled()) {
89+
return couchbaseCluster().openBucket(this.properties.getBucket().getName());
90+
}
8291
return couchbaseCluster().openBucket(this.properties.getBucket().getName(),
8392
this.properties.getBucket().getPassword());
8493
}
8594

95+
private boolean isRoleBasedAccessControlEnabled() {
96+
return this.properties.getUsername() != null
97+
&& this.properties.getPassword() != null;
98+
}
99+
86100
/**
87101
* Initialize an environment builder based on the specified settings.
88102
* @param properties the couchbase properties to use

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -38,6 +38,16 @@ public class CouchbaseProperties {
3838
*/
3939
private List<String> bootstrapHosts;
4040

41+
/**
42+
* Cluster username when using role based access.
43+
*/
44+
private String username;
45+
46+
/**
47+
* Cluster password when using role based access.
48+
*/
49+
private String password;
50+
4151
private final Bucket bucket = new Bucket();
4252

4353
private final Env env = new Env();
@@ -50,6 +60,22 @@ public void setBootstrapHosts(List<String> bootstrapHosts) {
5060
this.bootstrapHosts = bootstrapHosts;
5161
}
5262

63+
public String getUsername() {
64+
return this.username;
65+
}
66+
67+
public void setUsername(String username) {
68+
this.username = username;
69+
}
70+
71+
public String getPassword() {
72+
return this.password;
73+
}
74+
75+
public void setPassword(String password) {
76+
this.password = password;
77+
}
78+
5379
public Bucket getBucket() {
5480
return this.bucket;
5581
}

0 commit comments

Comments
 (0)