.NET Core Couchbase with multiple buckets

Im using CouchbaseNetClient and DI (v3.5.2) and we were able to create and register for multiple buckets previously but in the current version we cannot authenticate with multiple buckets as below.

services
    .AddCouchbase(opt =>
    {
        opt.ConnectionString = couchbaseSettings.ConnectionString;
        if (couchbaseSettings.UseSsl)
            opt.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;

        opt.UserName = couchbaseSettings.Username;
        opt.Password = couchbaseSettings.Password;
    })
    .AddCouchbaseBucket<ISiteBucketProvider>("site")
    .AddCouchbaseBucket<IProductsBucketProvider>("products");

In previous (v2.0.2) versions we use to manage multiple buckets as below

services
    .AddCouchbase(configuration.GetSection("Couchbase"))
    .AddCouchbaseBucket<IProductsBucketProvider>("products", productsPassword)
.AddCouchbaseBucket<ISiteBucketProvider>("site", sitePassword);

How can we achieve above with latest version of couchbase .net client?

Starting with Couchbase Server 4.5 they began migrating from bucket passwords to cluster-wide RBAC authentication, which is now the standard. When SDK version 3 was released, the minimum supported version was Server 5.5. Therefore, support for bucket passwords was not included in the SDK. Instead, use a single username/password and grant that user access to both buckets.

My Couchbase community version - Couchbase Server Community Edition 7.1.1 build 3175

CouchbaseNetClient - v 3.6.2
Couchbase.Extensions.DependencyInjection - v3.6.2

I tried creating a new user account admin_bucket_user and added application access for both buckets products and site. However, I keep getting DocumentNotFound exception. In the exception details - BucketName is empty as well

If I change the userName to products (we’ve app user in CB Security) then I can access the docs inside products bucket.

This tells me that we must have a userName same as Bucketname. Please advise?

Below is the RBAC response

{
    "id": "admin_bucket_user",
    "domain": "local",
    "roles": [
      {
        "role": "bucket_full_access",
        "bucket_name": "site",
        "origins": [
          {
            "type": "user"
          }
        ]
      },
      {
        "role": "bucket_full_access",
        "bucket_name": "products",
        "origins": [
          {
            "type": "user"
          }
        ]
      }
    ],
    "groups": [],
    "external_groups": [],
    "name": "admin_bucket_user",
    "uuid": "e2edbfea-7f3f-4fb8-8b68-5c7567340dd4",
    "password_change_date": "2024-08-23T09:24:54.000Z"
  }

This is how Im accessing my bucket in generic repository

protected readonly ValueTask<IBucket> _bucket;

public BaseGenericRepository(INamedBucketProvider bucketProvider)
{
    _bucket = bucketProvider.GetBucketAsync();
}

public async Task<List<T>> GetAllAsync(string n1q1)
{
    var bucket = await _bucket;

    var scope = await bucket.ScopeAsync("_default");
    var collection = await scope.CollectionAsync("_default");
}

And my config in DI

services
    .AddCouchbase(opt =>
    {
        opt.ConnectionString = couchbaseSettings.ConnectionString;

        if (couchbaseSettings.UseSsl)
            opt.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;

        opt.WithCredentials(couchbaseSettings.Username, couchbaseSettings.Password);
        opt.WithBuckets(new string [] { "products", "site" });
    })
    .AddCouchbaseBucket<IProductsBucketProvider>("products")
    .AddCouchbaseBucket<ISiteBucketProvider>("site");

DocumentNotFoundError is due to the document not being found in the bucket/scope/collection that it was requested from.

I would suggest using your cluster, user, bucket, scope and collection in the Couchbase .NET Client documentation sample code here - Start Using the .NET SDK | Couchbase Docs . When you have that working, then revisit your
classes ( Services, BaseGenericRepository, BucketProvider etc) to determine why the correct collection is not being used.

In the exception details - BucketName is empty as well

I suspect that is an omission in the error logging. KV operations are only possible on a keyspace (bucket, [scope,collection])

Ive tried multiple times now that below code works as long as bucket and username are same and it doesn’t work if they’re different.

Ive tried to put wrong password and it throws Unauthorized error.

var cluster = await Cluster.ConnectAsync(
        // Update these credentials for your Local Couchbase instance!
        "127.0.0.1",
        "products",
        "passwod");

// get a bucket reference
var bucket = await cluster.BucketAsync("products");

CouchbaseNetClient Version=3.6.2
Couchbase Server Community Edition 7.1.1 build 3175

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.