How to execute N1QL queries against a SASL password protected bucket

Hi,

I’m trying to make the Drupal 8 couchbase integration support SASL password protected buckets.

When I try to run an N1QL query against a SASL password protected bucket:

  if ($this->password !== NULL) {
    $params['creds'] = json_encode((object) ["user" => '', "pass" => '']);
  }
  return $this->bucket->_n1ql($query, $params, $json_array);

But this code seems not to work, it chockes because it thinks these parameters are for the query itself.

The error I get is:

CouchbaseException: Unrecognized parameter in request: 0 in CouchbaseBucket->_n1ql() (line 312 of [CouchbaseNative]/CouchbaseBucket.class.php).

This issue points to the need of appending the password to every query:

https://issues.couchbase.com/browse/MB-13272

The error I get when the bucket is SASL protected is:

CouchbaseException: Keyspace not found keyspace couch - cause: No bucket named couch in CouchbaseBucket->_n1ql() (line 312 of [CouchbaseNative]/CouchbaseBucket.class.php).

Thanks!

Looks like the way to go is something like this:

public function queryN1QL(\CouchbaseN1qlQuery $query, $params = NULL, $json_array = FALSE) {
    $this->enableN1ql();
    try {
      $query->options["creds"] = [(object) ["user" => "couch", "pass" => "couch"]];
      return $this->bucket->query($query, $params, $json_array);
    }
    catch (\Exception $e) {
      [...]
    }
  }

I say something because I was not able to make this work, I am getting the deadly:

creds has to be of type array of { user, pass }

That seems to have been reported here:

https://issues.couchbase.com/browse/MB-16964

Related posts:

Any hint on how to do this on the PHP SDK?

Thanks!

Yes, this is correct way to pass credentials

$cluster = new CouchbaseCluster("http://localhost:8091");
$bucket = $cluster->openBucket();

$query = CouchbaseN1qlQuery::fromString('SELECT * FROM `travel-sample` limit 10');
$query->options['creds'] = array(
    array('user' => 'local:default', 'pass' => ''),
    array('user' => 'local:travel-sample', 'pass' => 'secret')
);
$res = $bucket->query($query);
var_dump($res);

But because of the bug in N1QL engine (which still in 4.5-beta binary), it does not work. The bug has been fixed with https://github.com/couchbase/query/commit/c1d2ddbd322c088cd3d04e58751020cf7d10f7af and everything will work in GA.

1 Like