コンテンツへスキップ

CouchDBのユーザー管理

タグ:

ユーザーの管理方法

CouchDBのユーザー管理についてはオフィシャルドキュメントの以下に記載があります。

ユーザーを管理するには以下の二つがありますが、iniファイルに記載する方法は管理用アカウントのためにありますので、ほとんどの場合は _users データベースで管理します。

  • iniファイルに記載する方法
  • _usersデータベースに管理する方法
# craete user
# https://docs.couchdb.org/en/latest/intro/security.html?highlight=_users#creating-a-new-user
curl -X PUT http://127.0.0.1:5984/_users/org.couchdb.user:username \
     -H "Accept: application/json" \
     -H "Content-Type: application/json" \
     -d '{"name": "username", "password": "passssword", "roles": [], "type": "user"}'

# login user
curl -vX POST $HOST/_session -H "Content-Type:application/x-www-form-urlencoded" -d "name=username&password=password"

# check user _id
curl -X GET http://localhost:5984/_users/org.couchdb.user:username

# update user password
# https://docs.couchdb.org/en/latest/intro/security.html?highlight=_users#password-changing
curl -X PUT http://127.0.0.1:5984/_users/org.couchdb.user:username \
     -H "Accept: application/json" \
     -H "Content-Type: application/json" \
     -H "If-Match: 1-e0ebfb84005b920488fc7a8cc5470cc0" \
     -d '{"name":"username", "roles":[], "type":"user", "password":"password"}'

認証方法

https://docs.couchdb.org/en/latest/api/server/authn.html

認証方法は以下の4種類の方が用意されています。なおドキュメントにも記載されていますが経路はSSLで暗号化されていることが前提です。(暗号化されていなくても動作しますが認証情報が漏れてしまうため、推奨されません)

  • Basic Authentication
  • Cookie Authentication
  • Proxy Authentication
  • JWT Authentication

Basic Authentication

登録済みのユーザーとパスワードで認証を行います。最も簡単な方法ですがアクセスするタイミングで毎回送信する必要があります。

Cookie Authentication

登録済みのユーザーとパスワードで認証を行います。

Proxy Authentication

決められたヘッダ文字列を送信する事で認証を行います。この方法は事前にユーザーを登録する必要はありません。

JWT Authentication

未調査

CouchDBのアクセスコントロールについて

「公開データベースへは読み取りのみを許可する」といった場合、Apache2やnginx等のリバースプロキシ経由でのみアクセス可能に設定し、特定のメソッド(例えばGET)のみを許可する事で、読み取り専用のデータベースとする事が可能です。

別の方法としては、デザインドキュメントに validate_doc_update という関数を追加することで妥当性のチェックを行う事が可能です。

function(newDoc, oldDoc, userCtx, secObj) {
    if(userCtx.name === \"USERNAME\") {
        return;
    }
    throw({forbidden: \"READ ONLY\"});
}