配置 MongoDB 以通过 Linux PAM 验证用户密码

Configuring MongoDB to authenticate user's password via Linux PAM

我正在寻找一种方法来配置 MongoDB 以使用 Linux PAM 来管理用户密码。这样,当用户更改密码时,不必在 Mongodb.

中手动更新密码

仅在网上搜索此帮助 returns 一个结果是一个博客 article 提到这是可能的,但没有描述这是如何完成的。

官方文档跳过设置 PAM 并讨论 LDAP。

如何配置 Mongodb 以通过 Linux PAM 验证数据库用户?

不幸的是,MongoDB 使用 PAM Linux 的身份验证似乎只能在 MongoDB 企业版.

中配置

这是因为 PAM 身份验证需要 PLAIN 身份验证机制,仅在 MongoDB 企业版中可用,如 documentation 中所述:

PLAIN (LDAP SASL) External authentication using LDAP. You can also use PLAIN for authenticating in-database users. PLAIN transmits passwords in plain text. This mechanism is available only in MongoDB Enterprise.

顺便说一句,在 MongoDB 企业版中,您可以使用以下方法启用 PAM 身份验证(在 Debian Stretch 上测试):

安装 saslauthd

apt-get install sasl2-bin

vi /etc/default/saslauthd

START=yes

/etc/init.d/saslauthd restart

在这一步,您可以使用("myuser" 是您的 unix 用户)测试您的 sasl 配置:

testsaslauthd -u <myuser> -p <SecretPassword>

这应该输出一条成功消息:

0: OK "Success."

创建一个 MongoDB 用户 "myuser"

将 "myuser" 替换为您要与其进行身份验证的用户。

mongo admin

db.getSiblingDB("$external").createUser(
    {
      user : "myuser",
      roles: [ { role: "read", db: "mydb" } ]
    }
)

配置 MongoDB 以启用 PLAIN 身份验证机制

vi /etc/mongod.conf

security:
  authorization: enabled

setParameter:
  authenticationMechanisms: PLAIN,MONGODB-X509,SCRAM-SHA-1,SCRAM-SHA-256

您应该将 (Linux) mongodb 用户添加到 sasl 组(这确保 MongoDB 具有访问 saslauthd 的权限)

adduser mongodb sasl

重启mongod

systemctl restart mongod.service

连接到 MongoDB

现在,在 MongoDB Enterprise 上,您应该能够使用 linux username/pwd:

进行身份验证

mongo --authenticationMechanism=PLAIN --authenticationDatabase='$external' -u myuser mydb

MongoDB shell version v4.0.7
connecting to: mongodb://127.0.0.1:27017/mydb?authMechanism=PLAIN&authSource=%24external&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("********-****-****-****-************") }
MongoDB server version: 4.0.7
MongoDB Enterprise > 

在 MongoDB Community Edition 上,遗憾地失败并出现 "Unsupported mechanism" 错误:

MongoDB shell version v4.0.7
connecting to: mongodb://127.0.0.1:27017/mydb?authMechanism=PLAIN&authSource=%24external&gssapiServiceName=mongodb
2019-03-25T18:26:51.307+0100 E QUERY    [js] Error: Unsupported mechanism 'PLAIN' on authentication database '$external' :
connect@src/mongo/shell/mongo.js:343:13
@(connect):3:6
exception: connect failed