Checking Admin credentials before opening a bucket


I am looking for some way in JAVA SDK, so that I can check the admin credentials before opening a Bucket. Now When ever I open a bucket then only I come to know that my admin credentials are not matching. But I want to know prior to that. Is there any way out in SDK ? Or do I need to always rely on going one step ahead and try to open a bucket ?



Hi Panda,

As a basic pre-flight check, you could try getting the cluster info. This will fail with InvalidPasswordException if the credentials are not valid:

CouchbaseCluster cluster = CouchbaseCluster.create("localhost");
ClusterInfo info = cluster.clusterManager(username, password).info();
// Didn't throw exception. User exists and password is valid.

Because Couchbase Server 5.0 and later use Role-Based Access Control, it’s possible for a user to have access to the cluster info but not have permission to open a bucket.

You can see what roles a user has by calling clusterManager.getUser(AuthDomain authDomain, String username), assuming the cluster manager was created with credentials for a user who has permission to view user info. That might be overkill for your use case though.

Caveat: getUser doesn’t appear to to work with the built-in “Administrator” user, but it should work with external users and any users you add in the web console.



Hi Panda,

That said, if you know the bucket name and user credentials early in your application’s startup process, I’d recommend trying to open the bucket.

If it succeeds, you don’t need to close the bucket. Modern versions of the Java client will cache bucket instances, so unless you explicitly close the bucket, subsequent calls to openBucket will return the same instance.