文档菜单
文档首页
/
MongoDB 手册
/ / / /

在Linux上使用Kerberos身份验证配置自管理MongoDB

本页内容

  • 概述
  • 先决条件
  • 步骤
  • 其他注意事项
  • 测试和验证

MongoDB Enterprise支持使用Kerberos服务进行身份验证。Kerberos服务。Kerberos是一种适用于大型客户端/服务器系统的行业标准身份验证协议。MongoDB Enterprise仅支持MIT实现的Kerberos。

要验证您正在使用MongoDB Enterprise,请传递--version 命令行选项用于 mongodmongos

mongod --version

在此命令的输出中,查找字符串 modules: subscriptionmodules: enterprise 以确认您正在使用 MongoDB 企业版二进制文件。

对于副本集和分片集群,请确保您的配置使用完全限定的域名(FQDN)而不是 IP 地址或无资格的主机名。您必须使用 FQDN 以正确解析 Kerberos 域并允许您连接。

设置和配置 Kerberos 部署超出了本文档的范围。请参阅 MIT Kerberos 文档 或您的操作系统文档以获取如何配置 Kerberos 部署的信息。

为了使用 Kerberos 与 MongoDB,必须在您的 MongoDB 部署中为每个 mongodmongos 实例添加一个 Kerberos 服务主体。您可以通过在 KDC 上运行类似以下命令来添加服务主体:

kadmin.local addprinc mongodb/m1.example.com@EXAMPLE.COM

在运行 mongodmongos 的每个系统上,必须为相应的服务主体创建一个 密钥表文件。您可以在运行 mongodmongos 的系统上运行以下类似的命令来创建密钥表文件:

kadmin.local ktadd mongodb/m1.example.com@EXAMPLE.COM

以下步骤概述了将 Kerberos 用户主体添加到 MongoDB、为 Kerberos 支持配置独立的 mongod 实例以及使用 mongosh 连接并验证用户主体的步骤。

1

对于初次添加Kerberos用户,启动 mongod 时不启用Kerberos支持。

如果MongoDB中已经存在Kerberos用户且具有创建用户的权限,您可以使用Kerberos支持启动 mongod

根据您的部署情况,包括适当的额外设置。

注意

mongodmongos 默认绑定到本地主机。如果您的部署成员在不同的主机上运行或您希望远程客户端连接到您的部署,您必须指定 --bind_ipnet.bindIp

2

mongosh 连接到 mongod 实例。如果 --auth 已启用,确保您使用创建用户所需的权限进行连接。

3

将 Kerberos 主实体 <username>@<KERBEROS REALM><username>/<instance>@<KERBEROS REALM> 添加到 MongoDB 的 $external 数据库中。请使用全部大写字母指定 Kerberos 实体。在 $external 数据库中,允许 mongod 咨询外部来源(例如 Kerberos)进行身份验证。要指定用户的权限,请将 角色 分配给用户。

当与 $external 身份验证用户(Kerberos、LDAP 或 x.509 用户)一起使用 客户端会话和因果一致性保证 时,用户名不能超过 10k 字节。

以下示例将 Kerberos 主实体 application/reporting@EXAMPLE.NET 添加到对 records 数据库的只读访问中

use $external
db.createUser(
{
user: "application/reporting@EXAMPLE.NET",
roles: [ { role: "read", db: "records" } ]
}
)

根据需要添加其他主实体。对于您想要使用 Kerberos 进行身份验证的每个用户,您必须在 MongoDB 中创建相应的用户。有关创建和管理用户的信息,请参阅 用户管理命令。

4

要以 Kerberos 支持启动 mongod,请设置环境变量 KRB5_KTNAME 为密钥表文件的路径,并将 mongod 参数 authenticationMechanisms 设置为 GSSAPI,如下所示

env KRB5_KTNAME=<path to keytab file> \
mongod \
--setParameter authenticationMechanisms=GSSAPI \
<additional mongod options>

根据需要,为您配置包括额外的选项。例如,如果您希望远程客户端连接到您的部署或您的部署成员运行在不同的主机上,请指定 --bind_ip

例如,以下命令启动了一个具有Kerberos支持的独立mongod实例

env KRB5_KTNAME=/opt/mongodb/mongod.keytab \
/opt/mongodb/bin/mongod --auth \
--setParameter authenticationMechanisms=GSSAPI \
--dbpath /opt/mongodb/data --bind_ip localhost,<hostname(s)|ip address(es)>

您的mongod路径以及您的密钥表文件可能不同。该密钥表文件必须仅对mongod进程的所有者可访问。

使用官方的.deb.rpm软件包,您可以在环境设置文件中设置KRB5_KTNAME。有关详细信息,请参阅KRB5_KTNAME

5

以Kerberos主体application/reporting@EXAMPLE.NET连接mongosh客户端。在连接之前,您必须使用Kerberos的kinit程序为application/reporting@EXAMPLE.NET获取凭据。

您可以从命令行进行连接和认证。

mongosh --host hostname.example.net --authenticationMechanism=GSSAPI --authenticationDatabase='$external' --username application/reporting@EXAMPLE.NET

如果您正在连接到主机名与Kerberos名称匹配的系统,请确保您为--host选项指定完全限定的域名(FQDN),而不是IP地址或未限定的主机名。

如果您正在连接到主机名与Kerberos名称不匹配的系统,首先使用mongosh连接到mongod,然后从mongosh,使用db.auth()方法在$external数据库中进行认证。

use $external
db.auth( { mechanism: "GSSAPI", user: "application/reporting@EXAMPLE.NET" } )

如果您使用官方的 .deb.rpm 软件包安装了 MongoDB Enterprise,并且使用包含的 init/upstart 脚本来控制 mongod 实例,您可以在默认环境设置文件中设置 KRB5_KTNAME 变量,而不是每次都设置该变量。

基于 Red Hat 和 Debian 的系统的新版本使用 systemd。旧版本使用 init 进行系统初始化。根据适当的说明配置您的系统中的 KRB5_KTNAME 变量。

systemd 将配置存储在单元文件中。更新单元文件以设置 KRB5_KTNAME 变量。

1
sudo systemctl cat mongod

systemctl 命令返回文件位置并显示其内容。

2

要设置 KRB5_KTNAME 变量,编辑以下行以反映您的 keytab 文件位置

Environment="KRB5_KTNAME=<path-to-your-mongod.keytab-file>"
3

将编辑后的行添加到单元文件中。编辑后的单元文件将类似于

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
Environment="KRB5_KTNAME=/etc/mongod.keytab"
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
[Install]
WantedBy=multi-user.target
4
sudo systemctl daemon-reload
5
sudo systemctl restart mongod

对于 .rpm 安装,默认环境设置文件为 /etc/sysconfig/mongod

对于 .deb 安装,文件为 /etc/default/mongodb

通过添加以下类似的行来设置 KRB5_KTNAME

KRB5_KTNAME="<path to keytab>"

要使用 Kerberos 支持启动 mongos,请将环境变量 KRB5_KTNAME 设置为其 密钥表文件 的路径,并将 mongos 参数 authenticationMechanisms 设置为 GSSAPI,如下所示

env KRB5_KTNAME=<path to keytab file> \
mongos \
--setParameter authenticationMechanisms=GSSAPI \
<additional mongos options>

根据需要,为您配置包括额外的选项。例如,如果您希望远程客户端连接到您的部署或您的部署成员运行在不同的主机上,请指定 --bind_ip

例如,以下示例以 Kerberos 支持启动 mongos 实例

env KRB5_KTNAME=/opt/mongodb/mongos.keytab \
mongos \
--setParameter authenticationMechanisms=GSSAPI \
--configdb shard0.example.net, shard1.example.net,shard2.example.net \
--keyFile /opt/mongodb/mongos.keyfile \
--bind_ip localhost,<hostname(s)|ip address(es)>

您的 mongos 路径以及您的 密钥表文件 可能不同。该 密钥表文件 必须只能由 mongos 进程的所有者访问。

根据您的配置需要修改或包含任何额外的 mongos 选项。例如,您可以使用 x.509 成员身份验证 而不是使用 --keyFile 为分片集群成员的内部身份验证。

要使用配置文件配置 mongodmongos 以支持 Kerberos,请在配置文件中指定 authenticationMechanisms 设置。

如果使用 YAML 配置文件格式:

setParameter:
authenticationMechanisms: GSSAPI

根据需要包含额外的选项。例如,如果您希望远程客户端连接到您的部署,或者您的部署成员在不同的主机上运行,请指定 net.bindIp 设置。

例如,如果 /opt/mongodb/mongod.conf 包含以下用于独立 mongod 的配置设置:

security:
authorization: enabled
setParameter:
authenticationMechanisms: GSSAPI
storage:
dbPath: /opt/mongodb/data
net:
bindIp: localhost,<hostname(s)|ip address(es)>

要使用 Kerberos 支持启动 mongod,请使用以下格式

env KRB5_KTNAME=/opt/mongodb/mongod.keytab \
/opt/mongodb/bin/mongod --config /opt/mongodb/mongod.conf

您的 mongod 路径、密钥表文件和配置文件可能不同。密钥表文件必须只能由 mongod 进程的所有者访问。

如果您在启动 mongodmongos 并使用 Kerberos 身份验证时遇到问题,请参阅解决自管理 Kerberos 身份验证问题。

Kerberos 身份验证(GSSAPI(Kerberos))可以与以下 MongoDB 身份验证机制一起使用

按照以下方式指定机制

--setParameter authenticationMechanisms=GSSAPI,SCRAM-SHA-256

只有在使用时才添加其他机制。此参数设置不会影响 MongoDB 对集群成员的内部身份验证。

完成配置步骤后,您可以使用mongokerberos工具验证您的配置。

mongokerberos提供了一种方便的方法来验证您的平台Kerberos配置,以便与MongoDB一起使用,并测试从MongoDB客户端进行的Kerberos身份验证是否按预期工作。有关更多信息,请参阅mongokerberos文档。

mongokerberos仅在MongoDB企业版中可用。

返回

Kerberos