Configure MongoDB with Kerberos Authentication on Linux在Linux上使用Kerberos身份验证配置MongoDB

On this page本页内容

Overview概述

MongoDB Enterprise supports authentication using a Kerberos service. MongoDB Enterprise支持使用Kerberos服务进行身份验证。Kerberos is an industry standard authentication protocol for large client/server systems. 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,以确认您正在使用MongoDBEnterprise二进制文件。

For replica sets and sharded clusters, ensure that your configuration uses fully qualified domain names (FQDN) rather than IP addresses or unqualified hostnames. 对于副本集和分片群集,请确保您的配置使用完全限定的域名(FQDN),而不是IP地址或非限定的主机名。You must use the FQDN for GSSAPI to correctly resolve the Kerberos realms and allow you to connect.您必须使用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、为Kerberos支持配置独立的mongod实例以及使用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注意

Starting in MongoDB 3.6, mongod and mongos bind to localhost by default. 从MongoDB 3.6开始,mongodmongos默认绑定到localhost。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.bindIpFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

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.要对$external身份验证用户(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以只读方式访问记录数据库:

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. 对于要使用Kerberos进行身份验证的每个用户,必须在MongoDB中创建相应的用户。For more information about creating and managing users, see User Management Commands.有关创建和管理用户的详细信息,请参阅用户管理命令

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_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

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. 使用正式的.deb.rpm包,您可以在环境设置文件中设置KRB5_KTNAMESee KRB5_KTNAME for details.有关详细信息,请参阅KRB5_KTNAME

5

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

Connect mongosh client as the Kerberos principal application/reporting@EXAMPLE.NET. mongosh客户端连接为Kerberos主体应用程序/reporting@EXAMPLENET.Before 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/upstart脚本来控制mongod实例,则可以在默认环境设置文件中设置KRB5_KTNAME变量,而不是每次都设置该变量。

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

For .deb packages, the file is /etc/default/mongodb.对于.deb包,文件为/etc/default/mongodb

Set the KRB5_KTNAME value in 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_ipFor more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改

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.要使用配置文件为Kerberos支持配置mongodmongos,请在配置文件中指定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 more information, see Localhost Binding Compatibility Changes.有关详细信息,请参阅本地主机绑定兼容性更改。

For example, if /opt/mongodb/mongod.conf contains the following configuration settings for a standalone mongod:例如,如果/opt/mongodb/mongod.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 Kerberos Authentication.如果在使用Kerberos身份验证启动mongodmongos时遇到问题,请参阅Kerberos验证疑难解答

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

Kerberos authentication (GSSAPI (Kerberos)) can work alongside:Kerberos身份验证(GSSAPI(Kerberos))可以与以下内容一起工作:

  • 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工具验证配置。

Introduced alongside MongoDB 4.4, 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. mongokerberos与MongoDB 4.4一起推出,它提供了一种方便的方法来验证您的平台的Kerberos配置以用于MongoDB,并测试来自MongoDB客户端的Kerbero身份验证是否如预期那样工作。See the mongokerberos documentation for more information.有关更多信息,请参阅mongokerberos文档。

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

←  Kerberos AuthenticationConfigure MongoDB with Kerberos Authentication on Windows →