mnunberg / perl-Couchbase-Client

Perl Client for Couchbase
http://www.couchbase.com
16 stars 11 forks source link

Help : how to pass credentials to the connection #48

Open LFranck opened 6 years ago

LFranck commented 6 years ago

Hi,

On my couchbase plateform, the connection credentials is on the cluster not on the bucket. So how to use this where the password is for bucket :

# Create a new connection
my $cb = Couchbase::Bucket->new("couchbases://anynode/bucket", { password => "secret" });

In the previous version, Couchbase::Client, we used to have :

    my $client = Couchbase::Client->new({
        server => 'localhost:8091',
        username => 'some_user',
        password => 'secret',
        bucket => 'my_bucket'
    });

Can you give me the equivalent?

Thanks LF

annibale-x commented 6 years ago

@LFranck I took a look to this (unofficial) sdk, and the RBAC logic of libcouchbase seems not yet implemented here.

A trick you can do is to: 1) create a new bucket from the gui 2) create a user with the same name of the bucket 3) grant the user to read/write the bucket

Now you can access using the password of the user with the same name of the bucket:

my $cb = Couchbase::Bucket->new("couchbase://server/bucket", { password => "pwd" });

ps: when connected, you can access data in any allowed bucket using N1QL queries. eg:

my $res = $cb->query_slurp(qq{ SELECT * FROM `another_bucket` })->rows ;

Cheers AM

annibale-x commented 6 years ago

@LFranck I published a pull request with the patch for passing the username option to the constructor.

Please refer to the pull request https://github.com/mnunberg/perl-Couchbase-Client/pull/49 or clone/download the patched release from the repo https://github.com/annibale-x/perl-Couchbase-Client

Usage example:

use Couchbase::Bucket ;
my $bucket = Couchbase::Bucket->new(   
    "couchbase://127.0.0.1/bucket-name", 
    { 
        username =>  "myUserName" ,  # <== new option 
        password => 'myPassword'
    }
) ;

Hope this helps.

Cheers AM