Database Manual / Self-Managed Deployments / Security / Authentication / Kerberos

Configure Self-Managed MongoDB with Kerberos Authentication on Linux在Linux上配置具有Kerberos身份验证的自管理MongoDB

Overview概述

MongoDB Enterprise supports authentication using a Kerberos service. Kerberos is an industry standard authentication protocol for large client/server systems. MongoDB Enterprise支持使用Kerberos服务进行身份验证。Kerberos是用于大型客户端/服务器系统的行业标准身份验证协议。MongoDB Enterprise only supports the MIT implementation of Kerberos.MongoDB Enterprise仅支持Kerberos的MIT实现

Prerequisites先决条件

To verify that you are using MongoDB Enterprise, pass the --version command line option to the mongod or mongos:要验证您是否正在使用MongoDB Enterprise,请将--version命令行选项传递给mongodmongos

mongod --version

In the output from this command, look for the string modules: subscription or modules: enterprise to confirm you are using the MongoDB Enterprise binaries.在此命令的输出中,查找字符串modules:subscriptionmodules:enterprise,以确认您正在使用MongoDB enterprise二进制文件。

For replica sets and sharded clusters, ensure that your configuration uses fully qualified domain names (FQDN) rather than IP addresses or unqualified hostnames. You must use the FQDN for GSSAPI to correctly resolve the Kerberos realms and allow you to connect.对于副本集和分片群集,请确保配置使用完全限定域名(FQDN),而不是IP地址或非限定主机名。您必须使用GSSAPI的FQDN来正确解析Kerberos领域并允许您连接。

Setting up and configuring a Kerberos deployment is beyond the scope of this document. 设置和配置Kerberos部署超出了本文档的范围。Please refer to the MIT Kerberos documentation or your operating system documentation for information on how to configure a Kerberos deployment.有关如何配置Kerberos部署的信息,请参阅MIT Kerberos文档或操作系统文档。

In order to use MongoDB with Kerberos, a Kerberos service principal for each mongod and mongos instance in your MongoDB deployment must be added to the Kerberos database. 为了将MongoDB与Kerberos一起使用,必须将MongoDB部署中每个mongodmongos实例的Kerberos服务主体添加到Kerberos数据库中You can add the service principal by running a command similar to the following on your KDC:您可以通过在KDC上运行类似于以下命令来添加服务主体:

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

On each system running mongod or mongos, a keytab file must be created for the respective service principal. 在运行mongodmongos的每个系统上,必须为相应的服务主体创建一个keytab文件You can create the keytab file by running a command similar to the following on the system running mongod or mongos:您可以在运行mongodmongos的系统上运行类似于以下的命令来创建keytab文件:

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

Procedure过程

The following procedure outlines the steps to add a Kerberos user principal to MongoDB, configure a standalone mongod instance for Kerberos support, and connect using mongosh and authenticate the user principal.以下过程概述了将Kerberos用户主体添加到MongoDB、配置独立的mongod实例以支持Kerberos、使用mongosh连接并验证用户主体的步骤。

1

Start mongod without Kerberos.在没有Kerberos的情况下启动mongod

For the initial addition of Kerberos users, start mongod without Kerberos support.对于最初添加的Kerberos用户,请在不支持Kerberos的情况下启动mongod

If a Kerberos user is already in MongoDB and has the privileges required to create a user, you can start mongod with Kerberos support.如果Kerberos用户已经在MongoDB中,并且具有创建用户所需的权限,则可以使用Kerberos支持启动mongod

Include additional settings as appropriate to your deployment.根据部署情况包括其他设置。

Note

mongod and mongos bind to localhost by default. mongodmongos默认绑定到本地主机。If the members of your deployment are run on different hosts or if you wish remote clients to connect to your deployment, you must specify --bind_ip or net.bindIp.如果部署的成员在不同的主机上运行,或者希望远程客户端连接到部署,则必须指定--bind_ipnet.bindIp

2

Connect to mongod.连接到mongod

Connect mongosh to the mongod instance. mongosh连接到mongod实例。If mongod has --auth enabled, ensure you connect with the privileges required to create a user.如果mongod启用了--auth,请确保您具有创建用户所需的权限进行连接。

3

Add Kerberos Principal(s) to MongoDB.将Kerberos主体添加到MongoDB。

Add a Kerberos principal, <username>@<KERBEROS REALM> or <username>/<instance>@<KERBEROS REALM>, to MongoDB in the $external database. $external数据库中的MongoDB中添加一个Kerberos主体,<username>@<KERBEROS REALM><username>/<instance>@<KERBEROS REALM>Specify the Kerberos realm in all uppercase. 以大写字母指定Kerberos领域。The $external database allows mongod to consult an external source (e.g. Kerberos) to authenticate. $external数据库允许mongod查询外部源(例如Kerberos)进行身份验证。To specify the user's privileges, assign roles to the user.要指定用户的权限,请为用户分配角色

To use Client Sessions and Causal Consistency Guarantees with $external authentication users (Kerberos, LDAP, or X.509 users), usernames cannot be greater than 10k bytes.要对$外部身份验证用户(Kerberos、LDAP或X.509用户)使用客户端会话和因果一致性保证,用户名不能大于10k字节。

The following example adds the Kerberos principal application/reporting@EXAMPLE.NET with read-only access to the records database:以下示例添加了Kerberos主体application/reporting@EXAMPLE.NET具有对records数据库的只读访问权限:

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

Add additional principals as needed. For every user you want to authenticate using Kerberos, you must create a corresponding user in MongoDB. For more information about creating and managing users, see User Management Commands.根据需要添加其他主体。对于每个要使用Kerberos进行身份验证的用户,您必须在MongoDB中创建一个相应的用户。有关创建和管理用户的详细信息,请参阅用户管理命令

4

Start mongod with Kerberos support.使用Kerberos支持启动mongod

To start mongod with Kerberos support, set the environmental variable KRB5_KTNAME to the path of the keytab file and the mongod parameter authenticationMechanisms to GSSAPI in the following form:要使用Kerberos支持启动mongod,请将环境变量KRB5_KTNAME设置为keytab文件的路径,并将mongod参数authenticationMechanisms设置为GSSAPI,格式如下:

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

Include additional options as required for your configuration. 根据配置要求,包括其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip.例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip

For example, the following starts a standalone mongod instance with Kerberos support:例如,以下内容启动了一个支持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)>

The path to your mongod as well as your keytab file may differ. mongodkeytab文件的路径可能不同。The keytab file must be only accessible to the owner of the mongod process.keytab文件必须只能由mongod进程的所有者访问。

With the official .deb or .rpm packages, you can set the KRB5_KTNAME in a environment settings file. See KRB5_KTNAME for details.使用官方.deb.rpm包,您可以在环境设置文件中设置KRB5_KTNAME。详见KRB5_KTNAME

5

Connect mongosh to mongod and authenticate.mongosh连接到mongod并进行身份验证。

Connect mongosh client as the Kerberos principal application/reporting@EXAMPLE.NET. mongosh客户端连接为Kerberos主体application/reporting@EXAMPLE.NETBefore connecting, you must have used Kerberos's kinit program to get credentials for application/reporting@EXAMPLE.NET.在连接之前,您必须使用Kerberos的kinit程序来获取application/reporting@EXAMPLE.NET的凭据。

You can connect and authenticate from the command line.您可以通过命令行进行连接和身份验证。

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

If you are connecting to a system whose hostname matches the Kerberos name, ensure that you specify the fully qualified domain name (FQDN) for the --host option, rather than an IP address or unqualified hostname.如果要连接到主机名与Kerberos名称匹配的系统,请确保为--host选项指定完全限定域名(FQDN),而不是IP地址或非限定主机名。

If you are connecting to a system whose hostname does not match the Kerberos name, first connect mongosh to the mongod, and then from mongosh, use the db.auth() method to authenticate in the $external database.如果要连接到主机名与Kerberos名称不匹配的系统,请先将mongosh连接到mongod,然后从mongosh使用db.auth()方法在$external数据库中进行身份验证。

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

Additional Considerations其他注意事项

KRB5_KTNAME

If you installed MongoDB Enterprise using one of the official .deb or .rpm packages, and you use the included init/upstart scripts to control the mongod instance, you can set the KRB5_KTNAME variable in the default environment settings file instead of setting the variable each time.如果您使用官方.deb.rpm包之一安装了MongoDB Enterprise,并使用附带的init/unstart脚本来控制mongod实例,则可以在默认环境设置文件中设置KRB5_KTNAME变量,而不是每次都设置该变量。

Recent versions of Red Hat and Debian-based systems use systemd. Older versions use init for system initialization. Follow the appropriate instructions to configure the KRB5_KTNAME variable for your system.基于Red Hat和Debian的系统的最新版本使用systemd。旧版本使用init进行系统初始化。按照相应的说明为系统配置KRB5_KTNAME变量。

systemd Configuration Files配置文件

systemd stores configuration in unit files. Update the unit file to set the KRB5_KTNAME variable.将配置存储在单元文件中。更新单位文件以设置KRB5_KTNAME变量。

1
Find the unit file查找单位文件
sudo systemctl cat mongod

The systemctl command returns the file location and displays its contents.systemctl命令返回文件位置并显示其内容。

2
Set 设置KRB5_KTNAME

To set the KRB5_KTNAME variable, edit the following line to reflect the location of your keytab file:要设置KRB5_KTNAME变量,请编辑以下行以反映keytab文件的位置:

Environment="KRB5_KTNAME=<path-to-your-mongod.keytab-file>"
3
Edit the unit file编辑单位文件

Add the edited line to the unit file. The edited unit file will resemble:将编辑后的行添加到单位文件中。编辑后的单位文件将类似于:

[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
Reload the updated unit file:重新加载更新的单位文件:
sudo systemctl daemon-reload
5
Restart the mongod service重新启动mongod服务
sudo systemctl restart mongod

init Configuration Files配置文件

For .rpm installations, the default environment settings file is /etc/sysconfig/mongod.对于.rpm安装,默认环境设置文件是/etc/sysconfig/mongod

For .deb installations, the file is /etc/default/mongodb.对于.deb安装,文件是/etc/default/mongodb

Set the KRB5_KTNAME value by adding a line that resembles the following:通过添加类似于以下内容的行来设置KRB5_KTNAME值:

KRB5_KTNAME="<path to keytab>"

Configure mongos for Kerberos为Kerberos配置mongos

To start mongos with Kerberos support, set the environmental variable KRB5_KTNAME to the path of its keytab file and the mongos parameter authenticationMechanisms to GSSAPI in the following form:要启动支持Kerberos的mongos,请将环境变量KRB5_KTNAME设置为其keytab文件的路径,并将mongos参数authenticationMechanisms设置为GSSAPI,格式如下:

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

Include additional options as required for your configuration. 根据配置要求,包括其他选项。For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the --bind_ip.例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定--bind_ip

For example, the following starts a mongos instance with Kerberos support:例如,以下代码启动了一个支持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)>

The path to your mongos as well as your keytab file may differ. mongoskeytab文件的路径可能不同。The keytab file must be only accessible to the owner of the mongos process.keytab文件必须只能由mongos进程的所有者访问。

Modify or include any additional mongos options as required for your configuration. 根据配置需要修改或包含任何其他mongos选项。For example, instead of using --keyFile for internal authentication of sharded cluster members, you can use X.509 member authentication instead.例如,您可以使用X.509成员身份验证,而不是使用--keyFile对分片集群成员进行内部身份验证。

Use a Config File使用配置文件

To configure mongod or mongos for Kerberos support using a configuration file, specify the authenticationMechanisms setting in the configuration file.要使用配置文件配置mongodmongos以支持Kerberos,请在配置文件中指定authenticationMechanisms设置。

If using the YAML configuration file format:如果使用YAML配置文件格式

setParameter:
authenticationMechanisms: GSSAPI

Include additional options as required for your configuration. For instance, if you wish remote clients to connect to your deployment or your deployment members are run on different hosts, specify the net.bindIp setting.根据配置要求,包括其他选项。例如,如果您希望远程客户端连接到部署,或者部署成员在不同的主机上运行,请指定net.bindIp设置。

For example, if /opt/mongodb/mongod.conf contains the following configuration settings for a standalone mongod:例如,如果/opt/mongdb/mongd.conf包含独立mongod的以下配置设置:

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

To start mongod with Kerberos support, use the following form:要使用Kerberos支持启动mongod,请使用以下表单:

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

The path to your mongod, keytab file, and configuration file may differ. mongodkeytab文件和配置文件的路径可能不同。The keytab file must be only accessible to the owner of the mongod process.keytab文件必须只能由mongod进程的所有者访问。

Troubleshoot Kerberos Setup for MongoDBMongoDB的Kerberos设置疑难解答

If you encounter problems when starting mongod or mongos with Kerberos authentication, see Troubleshoot Self-Managed Kerberos Authentication.如果在使用Kerberos身份验证启动mongodmongos时遇到问题,请参阅自我管理Kerberos身份验证疑难解答

Incorporate Additional Authentication Mechanisms加入其他身份验证机制

Kerberos authentication (GSSAPI (Kerberos)) can work alongside:

  • MongoDB's SCRAM authentication mechanism:MongoDB的SCRAM认证机制:

  • MongoDB's authentication mechanism for LDAP:MongoDB的LDAP身份验证机制:

  • MongoDB's authentication mechanism for X.509:MongoDB的X.509认证机制:

Specify the mechanisms as follows:按如下方式指定机制:

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

Only add the other mechanisms if in use. This parameter setting does not affect MongoDB's internal authentication of cluster members.仅在使用时添加其他机制。此参数设置不会影响MongoDB对集群成员的内部身份验证。

Testing and Verification测试与验证

After completing the configuration steps, you can validate your configuration with the mongokerberos tool.完成配置步骤后,您可以使用mongokerberos工具验证配置。

mongokerberos provides a convenient method to verify your platform's Kerberos configuration for use with MongoDB, and to test that Kerberos authentication from a MongoDB client works as expected. 提供了一种方便的方法来验证平台与MongoDB一起使用的Kerberos配置,并测试来自MongoDB客户端的Kerberos身份验证是否按预期工作。See the mongokerberos documentation for more information.有关更多信息,请参阅mongokerberos文档。

mongokerberos is available in MongoDB Enterprise only.仅在MongoDB Enterprise中可用。