From 3386856ccf667aee1a1ece2f4d9a60131e69ac63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20A=C3=A7=C4=B1ko=C4=9Flu?= Date: Fri, 29 Mar 2019 18:07:15 +0300 Subject: [PATCH 1/2] Add support for Couchbase's role-based access See gh-16389 --- .../couchbase/CouchbaseConfiguration.java | 17 +++++++++--- .../couchbase/CouchbaseProperties.java | 27 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java index 2cad70668e..4dc458be67 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java @@ -57,7 +57,14 @@ public class CouchbaseConfiguration { @Bean @Primary public Cluster couchbaseCluster() { - return CouchbaseCluster.create(couchbaseEnvironment(), determineBootstrapHosts()); + CouchbaseCluster couchbaseCluster = CouchbaseCluster + .create(couchbaseEnvironment(), determineBootstrapHosts()); + if (this.properties.getUsername().isEmpty() + || this.properties.getPassword().isEmpty()) { + return couchbaseCluster; + } + return couchbaseCluster.authenticate(this.properties.getUsername(), + this.properties.getPassword()); } /** @@ -79,8 +86,12 @@ public class CouchbaseConfiguration { @Bean @Primary public Bucket couchbaseClient() { - return couchbaseCluster().openBucket(this.properties.getBucket().getName(), - this.properties.getBucket().getPassword()); + if (this.properties.getUsername().isEmpty() + || this.properties.getPassword().isEmpty()) { + return couchbaseCluster().openBucket(this.properties.getBucket().getName(), + this.properties.getBucket().getPassword()); + } + return couchbaseCluster().openBucket(this.properties.getBucket().getName()); } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java index 2fbfa8e8c2..5919df7880 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.boot.autoconfigure.couchbase; import java.time.Duration; @@ -42,6 +41,16 @@ public class CouchbaseProperties { private final Env env = new Env(); + /** + * Password of the cluster on RBA(role base access). + */ + private String password = ""; + + /** + * Username of the cluster on RBA(role base access). + */ + private String username = ""; + public List getBootstrapHosts() { return this.bootstrapHosts; } @@ -58,6 +67,22 @@ public class CouchbaseProperties { return this.env; } + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + public static class Bucket { /** From 2949561bf6279279089f6cf19a07b195790abb8a Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 21 May 2019 09:13:09 +0200 Subject: [PATCH 2/2] Polish "Add support for Couchbase's role-based access" Closes gh-16389 --- .../couchbase/CouchbaseConfiguration.java | 23 ++++++----- .../couchbase/CouchbaseProperties.java | 39 ++++++++++--------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java index 4dc458be67..28f14f89dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java @@ -59,12 +59,11 @@ public class CouchbaseConfiguration { public Cluster couchbaseCluster() { CouchbaseCluster couchbaseCluster = CouchbaseCluster .create(couchbaseEnvironment(), determineBootstrapHosts()); - if (this.properties.getUsername().isEmpty() - || this.properties.getPassword().isEmpty()) { - return couchbaseCluster; + if (isRoleBasedAccessControlEnabled()) { + return couchbaseCluster.authenticate(this.properties.getUsername(), + this.properties.getPassword()); } - return couchbaseCluster.authenticate(this.properties.getUsername(), - this.properties.getPassword()); + return couchbaseCluster; } /** @@ -86,12 +85,16 @@ public class CouchbaseConfiguration { @Bean @Primary public Bucket couchbaseClient() { - if (this.properties.getUsername().isEmpty() - || this.properties.getPassword().isEmpty()) { - return couchbaseCluster().openBucket(this.properties.getBucket().getName(), - this.properties.getBucket().getPassword()); + if (isRoleBasedAccessControlEnabled()) { + return couchbaseCluster().openBucket(this.properties.getBucket().getName()); } - return couchbaseCluster().openBucket(this.properties.getBucket().getName()); + return couchbaseCluster().openBucket(this.properties.getBucket().getName(), + this.properties.getBucket().getPassword()); + } + + private boolean isRoleBasedAccessControlEnabled() { + return this.properties.getUsername() != null + && this.properties.getPassword() != null; } /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java index 5919df7880..e5aa85ee29 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.boot.autoconfigure.couchbase; import java.time.Duration; @@ -37,20 +38,20 @@ public class CouchbaseProperties { */ private List bootstrapHosts; + /** + * Cluster username when using role based access. + */ + private String username; + + /** + * Cluster password when using role based access. + */ + private String password; + private final Bucket bucket = new Bucket(); private final Env env = new Env(); - /** - * Password of the cluster on RBA(role base access). - */ - private String password = ""; - - /** - * Username of the cluster on RBA(role base access). - */ - private String username = ""; - public List getBootstrapHosts() { return this.bootstrapHosts; } @@ -59,12 +60,12 @@ public class CouchbaseProperties { this.bootstrapHosts = bootstrapHosts; } - public Bucket getBucket() { - return this.bucket; + public String getUsername() { + return this.username; } - public Env getEnv() { - return this.env; + public void setUsername(String username) { + this.username = username; } public String getPassword() { @@ -75,12 +76,12 @@ public class CouchbaseProperties { this.password = password; } - public String getUsername() { - return this.username; + public Bucket getBucket() { + return this.bucket; } - public void setUsername(String username) { - this.username = username; + public Env getEnv() { + return this.env; } public static class Bucket {