通过_users数据库管理Cloudant访问

时间:2015-10-15 15:06:05

标签: couchdb cloudant

我已经创建了_users数据库以及_security文档,并根据https://cloudant.com/for-developers/faq/auth/正确设置了安全性。

然后我创建了一个角色为“_reader”的用户“test”,但是当我尝试登录Cloudant时,它无法识别用户。

_users数据库包含一个文档:

{
  "_id": "test",
  "_rev": "1-96973497bc9c89989c4beed02e3f0b96",
  "name": "test",
  "roles": [
    "_reader"
  ],
  "type": "user",
  "password": "password"
}

然后我尝试使用用户名“test”和密码“password”查看设计文档,如上所述,但它无效。

在设计文档所在的数据库的安全文档中,我有:

{ "couchdb_auth_only": true, "members": { "names": [ "test" ], "roles": [ "_reader" ] } }

有什么建议吗?

谢谢!

2 个答案:

答案 0 :(得分:5)

没有使用Cloudant对此进行测试,我没有使用加密专家验证此方法的安全性(请执行此操作) ,但希望它可能会指出你正确的方向:

首先,您需要生成salt和password_sha:

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.util.Random;

    public class CouchPassword {

        public static void main(String[] args) throws NoSuchAlgorithmException {

            String password = "123456"; // your password        
            String salt = genSalt();
            password = password + salt;

            MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(password.getBytes());
            byte byteData[] = md.digest();

            System.out.println("Password SHA:   " + byteToHexString(byteData) );
            System.out.println("Generated Salt: " + salt);
        }
        public static String genSalt() {
            Random ranGen = new SecureRandom();
            byte[] salt = new byte[16];
            ranGen.nextBytes(salt);
            return byteToHexString(salt);
        }
        public static String byteToHexString(byte[] b) {    
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
             sb.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
            }
            return sb.toString();
        }
    }
    // There are libraries to help with generating SHAs and Hex Strings
    // but I have choosen not to use those here so the answer is more standalone.

将上述密码字符串设置为所需的值,然后执行。 E.g。

    Password SHA:   316ccb0df3c8bbd8f568347827803682d2c93849
    Generated Salt: bba45713ef54016c8ef4b56b6b147936

创建_user和_security文档(选项1):

   // _user
    {
           "_id"          : "org.couchdb.user:joe",
           "type"         : "user",
           "name"         : "joe",
           "roles"        : ["standarduser"],
           "password_sha" : "316ccb0df3c8bbd8f568347827803682d2c93849",
          "salt"         : "bba45713ef54016c8ef4b56b6b147936"
    }

_users文档地图中的角色到_security文档中定义的角色。

// _security
{
   "couchdb_auth_only": true,
   "members": {
     "names": [],
     "roles": ["standarduser"]
   },
   "admins": {}
 }

来自CouchDB documentation

  • 数据库成员 - 按数据库定义。他们可以从数据库中读取所有类型的文档,除了设计文档外,他们可以将文档写入(和编辑)到数据库。

  • 数据库管理员 - 按数据库定义。他们拥有成员拥有的所有权限以及权限:编写(和编辑)设计文档,添加/删除数据库管理员和成员,设置数据库修订限制(/ somedb / _revs_limit API)并对数据库执行临时视图(/ somedb / _temp_view API)。他们无法创建数据库,也无法删除数据库。

在我的示例中,我的用户joe被赋予角色standarduser,该角色映射到数据库的数据库成员

创建_user和_security文档(选项2):

请注意,我可以指定roles

,而不是使用member.names
// _user document
{
  "_id"          : "org.couchdb.user:joe",
  "type"         : "user",
  "name"         : "joe",
  "password_sha" : "316ccb0df3c8bbd8f568347827803682d2c93849",
  "salt"         : "bba45713ef54016c8ef4b56b6b147936"
}

// _security document
{
  "couchdb_auth_only": true,
  "members": {
    "names": ["joe"],
    "roles": []
  },
  "admins": {}
}

警告

确保member.namesmember.roles参数都不为空,因为这将为所有人授予读写权限:

{
  "couchdb_auth_only": true,
  "members": {
    "names": [],
    "roles": []
  },
  "admins": {}
}
  

没有成员,任何用户都可以编写常规文档(任何非设计文档)并从数据库中读取文档。

     

来源:http://wiki.apache.org/couchdb/Security_Features_Overview

答案 1 :(得分:1)

Julie--在Apache CouchDB中,您当前必须使用验证功能来启用只读访问。 (没有“交钥匙”用户角色会给你我认为你要求的东西。)

特别是,我发现这个帖子很有帮助:https://stackoverflow.com/a/10713843/1459475

它链接到关于验证函数的官方CouchDB书籍部分:http://guide.couchdb.org/draft/validation.html以及JavaScript示例和解释: https://github.com/iriscouch/manage_couchdb/blob/master/js/validate_doc_update.js https://github.com/iriscouch/manage_couchdb#an-easy-validation-function

在Cloudant中,使用API​​密钥进行只读访问时更为简单,但您仍然可以使用开箱即用的CouchDB实现只读访问。它不像你想象的那样交钥匙。祝你好运!

相关问题