Skip to content

Commit 2949561

Browse files
committed
Polish "Add support for Couchbase's role-based access"
Closes gh-16389
1 parent 3386856 commit 2949561

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ public DefaultCouchbaseEnvironment couchbaseEnvironment() {
5959
public Cluster couchbaseCluster() {
6060
CouchbaseCluster couchbaseCluster = CouchbaseCluster
6161
.create(couchbaseEnvironment(), determineBootstrapHosts());
62-
if (this.properties.getUsername().isEmpty()
63-
|| this.properties.getPassword().isEmpty()) {
64-
return couchbaseCluster;
62+
if (isRoleBasedAccessControlEnabled()) {
63+
return couchbaseCluster.authenticate(this.properties.getUsername(),
64+
this.properties.getPassword());
6565
}
66-
return couchbaseCluster.authenticate(this.properties.getUsername(),
67-
this.properties.getPassword());
66+
return couchbaseCluster;
6867
}
6968

7069
/**
@@ -86,12 +85,16 @@ public ClusterInfo couchbaseClusterInfo() {
8685
@Bean
8786
@Primary
8887
public Bucket couchbaseClient() {
89-
if (this.properties.getUsername().isEmpty()
90-
|| this.properties.getPassword().isEmpty()) {
91-
return couchbaseCluster().openBucket(this.properties.getBucket().getName(),
92-
this.properties.getBucket().getPassword());
88+
if (isRoleBasedAccessControlEnabled()) {
89+
return couchbaseCluster().openBucket(this.properties.getBucket().getName());
9390
}
94-
return couchbaseCluster().openBucket(this.properties.getBucket().getName());
91+
return couchbaseCluster().openBucket(this.properties.getBucket().getName(),
92+
this.properties.getBucket().getPassword());
93+
}
94+
95+
private boolean isRoleBasedAccessControlEnabled() {
96+
return this.properties.getUsername() != null
97+
&& this.properties.getPassword() != null;
9598
}
9699

97100
/**

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

Lines changed: 18 additions & 17 deletions
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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.boot.autoconfigure.couchbase;
1718

1819
import java.time.Duration;
@@ -37,19 +38,19 @@ public class CouchbaseProperties {
3738
*/
3839
private List<String> bootstrapHosts;
3940

40-
private final Bucket bucket = new Bucket();
41-
42-
private final Env env = new Env();
43-
4441
/**
45-
* Password of the cluster on RBA(role base access).
42+
* Cluster username when using role based access.
4643
*/
47-
private String password = "";
44+
private String username;
4845

4946
/**
50-
* Username of the cluster on RBA(role base access).
47+
* Cluster password when using role based access.
5148
*/
52-
private String username = "";
49+
private String password;
50+
51+
private final Bucket bucket = new Bucket();
52+
53+
private final Env env = new Env();
5354

5455
public List<String> getBootstrapHosts() {
5556
return this.bootstrapHosts;
@@ -59,12 +60,12 @@ public void setBootstrapHosts(List<String> bootstrapHosts) {
5960
this.bootstrapHosts = bootstrapHosts;
6061
}
6162

62-
public Bucket getBucket() {
63-
return this.bucket;
63+
public String getUsername() {
64+
return this.username;
6465
}
6566

66-
public Env getEnv() {
67-
return this.env;
67+
public void setUsername(String username) {
68+
this.username = username;
6869
}
6970

7071
public String getPassword() {
@@ -75,12 +76,12 @@ public void setPassword(String password) {
7576
this.password = password;
7677
}
7778

78-
public String getUsername() {
79-
return this.username;
79+
public Bucket getBucket() {
80+
return this.bucket;
8081
}
8182

82-
public void setUsername(String username) {
83-
this.username = username;
83+
public Env getEnv() {
84+
return this.env;
8485
}
8586

8687
public static class Bucket {

0 commit comments

Comments
 (0)