cloudant / nodejs-cloudant

Cloudant Node.js client library
Apache License 2.0
255 stars 90 forks source link

Replicating databases using IAM Authentication #451

Closed hedphuqz closed 3 years ago

hedphuqz commented 3 years ago

Please read these guidelines before opening an issue.

Bug Description

If i try to run a replication between one DB to another, the replication will fail by default if i am using the IAM plugin, saying that Error: unauthorized to access or create database <SOURCE_DB>

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

Load cloudant and use the IAM plugin for authentication with IBM Cloud. Run .replicate() on any DB

import * as Cloudant from '@cloudant/cloudant'
const cloudant = Cloudant({
      `xxxcloudant.ibm.com`,
    plugins: [{ iamauth: { iamApiKey: `xxxxx` } }],
    })

(async () => {
    const db = await cloudant.use('db1')
    await db.insert({ msg: 'hello' })
    await cloudant.db.replicate('db1', 'db2', { create_target: true }) // will fail because this op does not use API key
})()

2. What you expected to happen

I expected the replication work successfully (OR find some documentation around this issue specifically, https://cloud.ibm.com/docs/Cloudant?topic=Cloudant-authorization returns an empty document)

3. What actually happened

The replication failed saying the client was not authorized.

Instead to solve it i had to do a cloudant.request() and pass in the IAM key this way.

cloudant.request({
      path: '_replicate',
      method: 'POST',
      body: {
        create_target: true,
        source: {
          url: 'source_db',
          iamauth: {
            iamApiKey: 'xxx',
          },
        },
        target: {
          url: 'target_db',
          iamauth: {
            iamApiKey: 'xxx',
          },
        },
      },
    })

Environment details

eiri commented 3 years ago

Auth plugins are not supported for replication, db.replicate requires using api key/password to compose a correct source and target urls. ref.

In general we recommend to use our new node sdk, which does support IAM in replication.

As a work-around, as you've found out, it is possible to use request and pass in replication job document directly.

Thank you for pointing on dead documentation links in README.