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

Configure Self-Managed MongoDB with Kerberos and Active Directory Authorization使用Kerberos和活动目录授权配置自管理MongoDB

MongoDB Enterprise supports querying an LDAP server for the LDAP groups to which an authenticated user belongs. 支持向LDAP服务器查询经过身份验证的用户所属的LDAP组。MongoDB maps the LDAP distinguished names (DN) of each returned group to roles on the admin database. MongoDB将每个返回组的LDAP可分辨名称(DN)映射到admin数据库上的角色MongoDB authorizes the user based on the mapped roles and their associated privileges. See LDAP Authorization for more information.MongoDB根据映射的角色及其相关权限授权用户。有关更多信息,请参阅LDAP授权

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

This tutorial describes how to configuring MongoDB to perform authentication through a Kerberos server and authorization through an Active Directory (AD) server via the platform libraries.本教程描述了如何配置MongoDB以通过Kerberos服务器执行身份验证,并通过平台库通过活动目录(AD)服务器执行授权。

Prerequisites先决条件

Important

Thoroughly familiarize yourself with the following subjects before proceeding:在继续之前,请彻底熟悉以下主题:

A full description of AD is beyond the scope of this tutorial. This tutorial assumes prior knowledge of AD.AD的完整描述超出了本教程的范围。本教程假设您具备AD的先验知识。

MongoDB supports using SASL mechanisms for binding between the MongoDB server and AD. A full description of SASL, SASL mechanisms, or the specific AD configuration requirements for a given SASL mechanism are beyond the scope of this tutorial. MongoDB支持使用SASL机制在MongoDB服务器和AD之间进行绑定。有关SASL、SASL机制或给定SASL机制的特定AD配置要求的完整描述超出了本教程的范围。This tutorial assumes prior knowledge of SASL and its related subject matter.本教程假设您对SASL及其相关主题有先验知识。

Setting up and configuring a Kerberos deployment is beyond the scope of this document. 设置和配置Kerberos部署超出了本文档的范围。This tutorial assumes you have configured a Kerberos service principal for each mongod and mongos instance in your MongoDB deployment, and you have a valid keytab file for for each mongod and mongos instance.本教程假设您已经为MongoDB部署中的每个mongodmongos实例配置了Kerberos服务主体,并且您为每个mongode和mongos实例都有一个有效的keytab文件

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领域并允许您连接。

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二进制文件。

Considerations注意事项

This tutorial explains configuring MongoDB for Kerberos authentication and AD authorization.本教程解释了如何配置MongoDB进行Kerberos身份验证和AD授权。

To perform this procedure on your own MongoDB server, you must modify the given procedures with respect to your own specific infrastructure, especially Kerberos configurations, constructing AD queries, or managing users.要在自己的MongoDB服务器上执行此过程,您必须根据自己的特定基础设施修改给定的过程,特别是Kerberos配置、构建AD查询或管理用户。

Transport Layer Security传输层安全性协议

By default, MongoDB creates a TLS/SSL connection when binding to the AD server. This requires configuring the host of the MongoDB server to have access to the AD server's Certificate Authority (CA) certificates.默认情况下,MongoDB在绑定到AD服务器时创建TLS/SSL连接。这需要配置MongoDB服务器的主机以访问AD服务器的证书颁发机构(CA)证书。

This tutorial provides instructions for the required host configurations.本教程提供了所需主机配置的说明。

This tutorial assumes you have access to the AD server's CA certificates and can create a copy of the certificates on the MongoDB server.本教程假设您可以访问AD服务器的CA证书,并可以在MongoDB服务器上创建证书的副本。

Example Active Directory Schema活动目录架构示例

This tutorial uses the following example AD objects as the basis for the provided queries, configurations, and output. Each object shows only a subset of the possible attributes.本教程使用以下示例AD对象作为提供的查询、配置和输出的基础。每个对象仅显示可能属性的一个子集。

User Objects用户对象

dn:CN=bob,CN=Users,DC=marketing,DC=example,DC=com
userPrincipalName: bob@marketing.example.com
memberOf: CN=marketing,CN=Users,DC=example,DC=com

dn:CN=alice,CN=Users,DC=engineering,DC=example,DC=com
userPrincipalName: alice@engineering.example.com
memberOf: CN=web,CN=Users,DC=example,DC=com
memberOf: CN=PrimaryApplication,CN=Users,DC=example,DC=com

dn:CN=sam,CN=Users,DC=dba,DC=example,DC=com
userPrincipalName: sam@dba.example.com
memberOf: CN=dba,CN=Users,DC=example,DC=com
memberOf: CN=PrimaryApplication,CN=Users,DC=example,DC=com

dn:CN=joe,CN=Users,DC=analytics,DC=example,DC=com
userPrincipalName: joe@analytics.example.com
memberof: CN=marketing,CN=Users,DC=example,DC=com

Group Objects组对象

dn:CN=marketing,CN=Users,DC=example,DC=com
member:CN=bob,CN=Users,DC=marketing,DC=example,DC=com
member:CN=joe,CN=Users,DC=analytics,DC=example,DC=com

dn:CN=engineering,CN=Users,DC=example,DC=com
member:CN=web,CN=Users,DC=example,DC=com
member:CN=dba,CN=users,DC=example,DC=com

dn:CN=web,CN=Users,DC=example,DC=com
member:CN=alice,CN=Users,DC=engineering,DC=example,DC=com

dn:CN=dba,CN=Users,DC=example,DC=com
member:CN=sam,CN=Users,DC=dba,DC=example,DC=com

dn:CN=PrimaryApplication,CN=Users,DC=example,DC=com
member:CN=sam,CN=Users,DC=dba,DC=example,DC=com
member:CN=alice,CN=Users,DC=engineering,DC=example,DC=com

Active Directory Credentials活动目录凭据

This tutorial uses a username and password for performing queries on the AD server. 本教程使用用户名和密码在AD服务器上执行查询。The credentials provided must have sufficient privileges on the AD server for supporting queries related to security.ldap.userToDNMapping or security.ldap.authz.queryTemplate.提供的凭据必须在AD服务器上具有足够的权限,以支持与security.ldap.userToDNMappingsecurity.ldap.authz.queryTemplate相关的查询。

Replica Sets副本集

MongoDB LDAP authorization requires every mongod in the replica set to be on at least MongoDB 3.4.0 or later.MongoDB LDAP授权要求副本集中的每个mongod至少在MongoDB 3.4.0或更高版本上。

Sharded Clusters分片集群

MongoDB LDAP authorization requires every mongod and mongos in the sharded cluster to be on at least MongoDB 3.4.0 or later.MongoDB LDAP授权要求分片集群中的每个mongodmongos至少在MongoDB 3.4.0或更高版本上。

Procedure过程

1

Configure TLS/SSL for the server running MongoDB.为运行MongoDB的服务器配置TLS/SSL。

To connect to the AD (AD) server via TLS/SSL, the mongod or mongos require access to the AD server's Certificate Authority (CA) certificate.要通过TLS/SSL连接到AD(AD)服务器,mongodmongos需要访问AD服务器的证书颁发机构(CA)证书。

On Linux, specify the AD server's CA certificates via the TLS_CACERT or TLS_CACERTDIR option in the ldap.conf file.在Linux上,通过ldap.conf文件中的TLS_CAMERTTLS_CAMERTDIR选项指定AD服务器的CA证书。

Your platform's package manager creates the ldap.conf file while installing MongoDB Enterprise's libldap dependency. 在安装MongoDB Enterprise的libldap依赖项时,平台的包管理器会创建ldap.conf文件。For complete documentation on the configuration file or the referenced options, see ldap.conf.有关配置文件或引用选项的完整文档,请参阅ldap.conf

On Microsoft Windows, load the AD server's Certificate Authority (CA) certificates with the platform's credential management tool. 在Microsoft Windows上,使用平台的凭据管理工具加载AD服务器的证书颁发机构(CA)证书。The exact credential management tool is Windows version dependent. To use the tool, refer to its documentation for your version of Windows.确切的凭据管理工具取决于Windows版本。要使用该工具,请参阅适用于Windows版本的文档。

If mongod or mongos cannot access to the AD CA files, they cannot create TLS/SSL connections to the Active Directory server.如果mongodmongos无法访问AD CA文件,则无法创建到活动目录服务器的TLS/SSL连接。

Optionally, set security.ldap.transportSecurity to none to disable TLS/SSL.(可选)将security.ldap.transportSecurity设置为none以禁用TLS/SSL。

Warning

Setting transportSecurity to none transmits plaintext information, including user credentials, between MongoDB and the AD server.transportSecurity设置为none会在MongoDB和AD服务器之间传输明文信息,包括用户凭据。

2

(Windows only) Assign Service Principal Name to MongoDB Windows Service.(仅限Windows)将服务主体名称分配给MongoDB Windows服务。

For MongoDB servers running on the Windows operating system, you must use setspn.exe to assign the service principal name (SPN) to the account running the MongoDB service.对于在Windows操作系统上运行的MongoDB服务器,您必须使用setspn.exe将服务主体名称(SPN)分配给运行MongoDB服务的帐户。

setspn.exe -S <service>/<fully qualified domain name> <service account name>

Example示例

For example, if a mongod runs as a service named mongodb on mongodbserver.example.com with the service account name mongodb_dev@example.com, the command to assign the SPN would look as follows:例如,如果一个mongodmongodbserver.example.com上以服务帐户名作为名为mongodb的服务运行mongodb_dev@example.com,分配SPN的命令如下:

setspn.exe -S mongodb/mongodbserver.example.com mongodb_dev@example.com

Note

Windows Server 2003 does not support setspn.exe -S. Windows Server 2003不支持setspn.exe -SFor complete documentation on setspn.exe, see setspn.exe.有关setspn.exe的完整文档,请参阅setspn.exe

3

(Linux only) Create keytab file for the MongoDB server.(仅限Linux)为MongoDB服务器创建keytab文件。

For MongoDB servers running on the Linux platform, you must ensure the server has a copy of the keytab file specific to the MongoDB instance running on that server.对于在Linux平台上运行的MongoDB服务器,您必须确保该服务器具有特定于该服务器上运行的MongoDB实例的keytab文件的副本。

You must grant the Linux user running the MongoDB service read permissions on the keytab file. Take note of the full path of the keytab file location.您必须授予运行MongoDB服务的Linux用户对keytab文件的读取权限。记下keytab文件位置的完整路径。

4

Connect to the MongoDB server.连接到MongoDB服务器。

Connect to the MongoDB server using mongosh using the --host and --port options.使用--host--port选项使用mongosh连接到MongoDB服务器。

mongosh --host <hostname> --port <port>

If your MongoDB server currently enforces authentication, you must authenticate to the admin database as a user with role management privileges, such as those provided by userAdmin or userAdminAnyDatabase. 如果MongoDB服务器当前强制执行身份验证,则必须以具有角色管理权限的用户身份向admin数据库进行身份验证,例如userAdminuserAdminAnyDatabase提供的权限。Include the appropriate --authenticationMechanism for the MongoDB server's configured authentication mechanism.为MongoDB服务器配置的身份验证机制包括适当的--authenticationMechanism

mongosh --host <hostname> --port <port> --username <user> --password <pass> --authenticationDatabase="admin" --authenticationMechanism="<mechanism>"

Note

For Windows MongoDB deployments, you should replace mongosh with mongo.exe对于Windows MongoDB部署,您应该用mongo.exe替换mongosh

5

Create user administrative role.创建用户管理角色。

To manage MongoDB users using AD, you need to create at least one role on the admin database that can create and manage roles, such as those provided by userAdmin or userAdminAnyDatabase.要使用AD管理MongoDB用户,您需要在admin数据库上创建至少一个可以创建和管理角色的角色,例如userAdminuserAdminAnyDatabase提供的角色。

The role's name must exactly match the Distinguished Name of an AD group. The group must have at least one AD user as a member.角色的名称必须与AD组的可分辨名称完全匹配。该组必须至少有一个AD用户作为成员。

Given the available Active Directory groups,the following operation:给定可用的活动目录组,执行以下操作:

  • Creates a role named for the AD group CN=dba,CN=Users,DC=example,DC=com, and创建一个以AD组CN=dba,CN=Users,DC=example,DC=com命名的角色,以及
  • Assigns it the userAdminAnyDatabase role on the admin database.admin数据库上为其分配userAdminAnyDatabase角色。
var admin = db.getSiblingDB("admin")
admin.createRole(
{
role: "CN=dba,CN=Users,DC=example,DC=com",
privileges: [],
roles: [ "userAdminAnyDatabase" ]
}
)

You could alternatively grant the userAdmin role for each database the user should have user administrative privileges on. These roles provide the necessary privileges for role creation and management.您也可以为每个用户应该具有用户管理权限的数据库授予userAdmin角色。这些角色为角色创建和管理提供了必要的权限。

Important

Consider applying the principle of least privilege when configuring MongoDB roles, AD groups, or group membership.在配置MongoDB角色、AD组或组成员资格时,考虑应用最小权限原则

6

Create a MongoDB configuration file.创建MongoDB配置文件。

A MongoDB configuration file is a plain-text YAML file with the .conf file extension.MongoDB配置文件是一个扩展名为.conf的纯文本YAML文件。

  • If you are upgrading an existing MongoDB deployment, copy the current configuration file and work from that copy.如果要升级现有的MongoDB部署,请复制当前配置文件并从该副本开始工作。
  • (Linux Only) If this is a new deployment and you used your platform's package manager to install MongoDB Enterprise, the installation includes the /etc/mongod.conf default configuration file. Use that default configuration file, or make a copy of that file to work from.(仅限Linux)如果这是一个新的部署,并且您使用平台的包管理器安装了MongoDB Enterprise,则安装包括/etc/mongod.conf默认配置文件。使用默认配置文件,或复制该文件以从中工作。
  • If no such file exists, create an empty file with the .conf extension and work from that new configuration file.如果不存在这样的文件,请创建一个扩展名为.conf的空文件,并从新的配置文件开始工作。
7

Configure MongoDB to connect to Active Directory`.配置MongoDB以连接到活动目录。

In the MongoDB configuration file, set security.ldap.servers to the host and port of the AD server. 在MongoDB配置文件中,将security.ldap.servers设置为AD服务器的主机和端口。If your AD infrastructure includes multiple AD servers for the purpose of replication, specify the host and port of the servers as a comma-delimited list to security.ldap.servers.如果AD基础架构包含多个用于复制的AD服务器,请将服务器的主机和端口指定为逗号分隔的列表,以供security.ldap.servers使用。

Example示例

To connect to an AD server located at activedirectory.example.net, include the following in the configuration file:要连接到位于activedirectory.example.net的AD服务器,请在配置文件中包含以下内容:

security:
ldap:
servers: "activedirectory.example.net"

MongoDB must bind to the AD server to perform queries. By default, MongoDB uses the simple authentication mechanism to bind itself to the AD server.MongoDB必须绑定到AD服务器才能执行查询。默认情况下,MongoDB使用简单的身份验证机制将自己绑定到AD服务器。

Alternatively, you can configure the following settings in the configuration file to bind to the AD server using SASL:或者,您可以在配置文件中配置以下设置,以使用SASL绑定到AD服务器:

This tutorial uses the default simple LDAP authentication mechanism.本教程使用默认的simpleLDAP身份验证机制。

8

Configure MongoDB for Kerberos authentication.配置MongoDB进行Kerberos身份验证。

In the MongoDB configuration file, set security.authorization to enabled and setParameter authenticationMechanisms to GSSAPI在MongoDB配置文件中,将security.authorization设置为enabled,并将setParameter authenticationMechanisms设置为GSSAPI

To enable authentication via Kerberos, include the following in the configuration file:要通过Kerberos启用身份验证,请在配置文件中包含以下内容:

security:
authorization: "enabled"
setParameter:
authenticationMechanisms: "GSSAPI"
9

Configure LDAP Query Template for authorization.配置LDAP查询模板以进行授权。

In the MongoDB configuration file, set security.ldap.authz.queryTemplate to an RFC4516 formatted LDAP query URL template.在MongoDB配置文件中,将security.ldap.authz.queryTemplate设置为RFC4516格式的LDAP查询URL模板。

In the template, you can use either:在模板中,您可以使用:

  • {USER} placeholder to substitute the authenticated username into the LDAP query URL.占位符,用于将经过身份验证的用户名替换为LDAP查询URL。
  • {PROVIDED_USER} placeholder to substitute the supplied username, i.e. before either authentication or LDAP transformation, into the LDAP query.占位符,用于将提供的用户名替换为LDAP查询,即在身份验证或LDAP转换之前。

Design the query template to retrieve the user's groups.设计查询模板以检索用户的组。

Note

A full description of RFC4515, RFC4516, or AD queries is out of scope for this tutorial. RFC4515、RFC4516或AD查询的完整描述超出了本教程的范围。The queryTemplate provided in this tutorial is an example only, and may not be applicable for your specific AD deployment.本教程中提供的queryTemplate只是一个示例,可能不适用于特定AD部署。

Example示例

The following query template returns any groups that list {USER} as a member, following recursive group memberships. 以下查询模板返回任何将{USER}列为成员的组,遵循递归组成员关系。This LDAP query assumes that group objects track user membership by storing full user Distinguished Name (DN) using the member attribute. 此LDAP查询假定组对象通过使用member属性存储完整的用户可分辨名称(DN)来跟踪用户成员资格。The query includes the AD specific matching rule OID 1.2.840.113556.1.4.1941 for LDAP_MATCHING_RULE_IN_CHAIN. 该查询包括LDAP_MATCHING_RULE_IN_CHAIN的AD特定匹配规则OID 1.2.840.113556.4.1941This matching rule is an AD specific extension to LDAP search filters.此匹配规则是LDAP搜索筛选器的AD特定扩展。

security:
ldap:
authz:
queryTemplate:
"DC=example,DC=com??sub?(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={USER}))"

Using the query template, MongoDB substitutes {USER} with the authenticated username to query the LDAP server.使用查询模板,MongoDB用经过身份验证的用户名替换{USER}来查询LDAP服务器。

For example, a user authenticates as CN=sam,CN=Users,DC=dba,DC=example,DC=com. MongoDB creates an LDAP query based on the queryTemplate, substituting the {USER} token with the authenticated username. 例如,用户的身份验证为CN=sam,CN=Users,DC=dba,DC=example,DC=com。MongoDB基于queryTemplate创建LDAP查询,将{USER}令牌替换为经过身份验证的用户名。The Active Directory server performs a recursive group lookup for any group that either directly or transitively lists the user as a member. 活动目录服务器对直接或传递地将用户列为成员的任何组执行递归组查找。Based on the Active Directory groups, the AD server returns CN=dba,CN=Users,DC=example,DC=com and CN=engineering,CN=Users,DC=example,DC=com.根据活动目录组,AD服务器返回CN=dba,CN=Users,DC=example,DC=comCN=engineering,CN=Users,DC=example,DC=com

MongoDB maps each returned group DN to a role on the admin database. For each mapped group DN, if there is an existing role on the admin database whose name exactly matches the DN, MongoDB grants the user the roles and privileges assigned to that role.MongoDB将每个返回的组DN映射到admin数据库上的一个角色。对于每个映射的组DN,如果admin数据库上存在一个名称与DN完全匹配的现有角色,MongoDB会授予用户分配给该角色的角色和权限。

The matching rule LDAP_MATCHING_RULE_IN_CHAIN requires providing the full DN of the authenticating user. 匹配规则LDAP_MATCHING_RULE_IN_CHAIN要求提供身份验证用户的完整DN。Since Kerberos requires authenticating with a user's userPrincipalName, you must transform the incoming usernames into DNs using security.ldap.userToDNMapping. 由于Kerberos需要使用用户的userPrincipalName进行身份验证,因此必须使用security.ldap.userToDNMapping将传入的用户名转换为DN。The next step provides guidance on transforming incoming usernames to support the queryTemplate.下一步将提供有关转换传入用户名以支持queryTemplate的指导。

10

Transform incoming usernames for authentication via Active Directory.转换传入用户名以通过活动目录进行身份验证。

In the MongoDB configuration file, set userToDNMapping to transform the authenticating user's provided username into an AD DN to support the queryTemplate.在MongoDB配置文件中,设置userToDNMapping,将认证用户提供的用户名转换为AD DN,以支持queryTemplate

Example示例

The following userToDNMapping configuration uses the match regular expression filter to capture the provided username. MongoDB inserts the captured username into the ldapQuery query template before executing the query.以下userToDNMapping配置使用match正则表达式筛选器来捕获提供的用户名。MongoDB在执行查询之前将捕获的用户名插入ldapQuery查询模板中。

security:
ldap:
userToDNMapping:
'[
{
match : "(.+)",
ldapQuery: "DC=example,DC=com??sub?(userPrincipalName={0})"
}
]'

You must modify the given sample configuration to match your deployment. 您必须修改给定的示例配置以匹配部署。For example, the ldapQuery base DN must match the base DN which contains your user entities. Other modifications may be necessary to support your AD deployment.例如,ldapQuery基DN必须与包含用户实体的基DN匹配。可能需要进行其他修改以支持AD部署。

Example示例

A user authenticates as alice@ENGINEERING.EXAMPLE.COM. MongoDB first applies any transformations specified in userToDNMapping. 用户身份验证为alice@ENGINEERING.EXAMPLE.COM。MongoDB首先应用userToDNMapping中指定的任何转换。Based on the provided configuration, MongoDB captures the username in the match stage and executes an LDAP query:根据提供的配置,MongoDB在match阶段捕获用户名并执行LDAP查询:

DC=example,DC=com??sub?(userPrincipalName=alice@ENGINEERING.EXAMPLE.COM)

Based on the configured Active Directory users, the AD server should return CN=alice,CN=Users,DC=engineering,DC=example,DC=com.根据配置的活动目录用户,AD服务器应返回CN=alice,CN=Users,DC=engineering,DC=example,DC=com

MongoDB then executes the LDAP query configured in queryTemplate, replacing the {USER} token with the transformed username CN=alice,CN=Users,DC=engineering,DC=example,DC=com.然后,MongoDB执行queryTemplate中配置的LDAP查询,将{USER}令牌替换为转换后的用户名CN=alice,CN=Users,DC=engineering,DC=example,DC=com

Important

If you use userToDNMapping's substitution parameter to transform the group name, the result of the substitution must be an RFC4514 escaped string.如果使用userToDNMapping的替换参数来转换组名,则替换的结果必须是RFC4514转义字符串。

11

Configure query credentials.配置查询凭据。

MongoDB requires credentials for performing queries on the AD server.MongoDB需要凭据才能在AD服务器上执行查询。

Configure the following settings in the configuration file:在配置文件中配置以下设置:

security:
ldap:
bind:
queryUser: "mongodbadmin@dba.example.com"
queryPassword: "secret123"

On Windows MongoDB servers, you can set security.ldap.bind.useOSDefaults to true to use the credentials of the OS user instead of queryUser and queryPassword.在Windows MongoDB服务器上,您可以将security.ldap.bind.useOSDefaults设置为true,以使用操作系统用户的凭据,而不是queryUserqueryPassword

The queryUser must have permission to perform all LDAP queries on behalf of MongoDB.queryUser必须具有代表MongoDB执行所有LDAP查询的权限。

12

Optional: Add additional configuration settings.可选:添加其他配置设置。

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设置。

13

Start the MongoDB server with Kerberos authentication and Active Directory authorization.使用Kerberos身份验证和活动目录授权启动MongoDB服务器。

Start the MongoDB server with the --config option, specifying the path to the configuration file created during this procedure. If the MongoDB server is currently running, make the appropriate preparations to stop the server.使用--config选项启动MongoDB服务器,指定在此过程中创建的配置文件的路径。如果MongoDB服务器当前正在运行,请做好停止服务器的适当准备。

Linux MongoDB Servers

On Linux, you must specify the KRB5_KTNAME environmental variable, specifying the path to the keytab file for the MongoDB server.在Linux上,您必须指定KRB5_KTNAME环境变量,指定MongoDB服务器的keytab文件的路径。

env KRB5_KTNAME <path-to-keytab> mongod --config <path-to-config-file>

Microsoft Windows MongoDB Servers

On Windows, you must start the MongoDB server as the service principal account as configured earlier in the procedure:在Windows上,您必须按照过程中前面配置的服务主体帐户启动MongoDB服务器:

mongod.exe --config <path-to-config-file>
14

Connect to the MongoDB server.连接到MongoDB服务器。

Connect to the MongoDB server, authenticating as a user whose direct or transitive group membership corresponds to a MongoDB role on the admin database with userAdmin, userAdminAnyDatabase, or a custom role with equivalent privileges.连接到MongoDB服务器,作为一个用户进行身份验证,该用户的直接或可传递组成员资格对应于admin数据库上具有userAdminuserAdminAnyDatabase或具有同等权限的自定义角色的MongoDB角色。

Use mongosh to authenticate to the MongoDB server, set the following options:使用mongosh对MongoDB服务器进行身份验证,设置以下选项:

Example示例

Previously in this procedure, you configured the dn:CN=dba,CN=Users,DC=example,DC=com role on the admin database with the required permissions. 在此过程之前,您已在admin数据库上配置了dn:CN=dba,CN=Users,DC=example,DC=com角色,并赋予其所需权限。This role corresponds to an AD group. Based on the configured AD users, you can authenticate as the user sam@dba.example.com and receive the required permissions.此角色对应于AD组。根据配置的AD用户,您可以作为用户进行身份验证sam@dba.example.com并获得所需的权限。

mongosh --username sam@DBA.EXAMPLE.COM --password  --authenticationMechanisms="GSSAPI" --authenticationDatabase "$external" --host <hostname> --port <port>

If you do not specify the password to the -p command-line option, mongosh prompts for the password.如果不在-p命令行选项中指定密码,mongosh会提示输入密码。

Windows MongoDB deployments must use mongo.exe instead of mongosh.Windows MongoDB部署必须使用mongo.exe而不是mongosh

Given the configured Active Directory users, the user authenticates successfully and receives the appropriate permissions.给定配置的活动目录用户,用户将成功进行身份验证并获得相应的权限。

Note

If you want to authenticate as an existing non-$external user, set --authenticationMechanism to a SCRAM authentication mechanism (e.g. SCRAM-SHA-1 or SCRAM-SHA-256). 如果您想作为现有的非$external用户进行身份验证,请将--authenticationMechanism设置为SCRAM身份验证机制(例如SCRAM-SHA-1SCRAM-SHA-256)。This requires that the MongoDB server's setParameter authenticationMechanisms includes SCRAM-SHA-1 and/or SCRAM-SHA-256 as appropriate.这要求MongoDB服务器的setParameter身份验证机制包括SCRAM-SHA-1和/或SCRAM-SHA-256(视情况而定)。

15

Create roles for mapping returned AD groups.为映射返回的AD组创建角色。

For each group on the AD server you wish to use for MongoDB authorization, you must create a matching role on the MongoDB server's admin database.对于您希望用于MongoDB授权的AD服务器上的每个组,您必须在MongoDB服务器的admin数据库上创建一个匹配的角色。

Example示例

The following operation creates a role named after the AD group DN CN=PrimaryApplication,CN=Users,DC=example,DC=com, assigning roles and privileges appropriate to that group:以下操作将创建一个以AD组DN CN=PrimaryApplication,CN=Users,DC=example,DC=com命名的角色,并为该组分配相应的角色和权限:

db.getSiblingDB("admin").createRole(
{
role: "CN=PrimaryApplication,CN=Users,DC=example,DC=com",
privileges: [],
roles: [
{ role: "readWrite", db: "PrimaryApplication" }
]
}
)

Given the configured Active Directory groups, MongoDB grants a user authenticating as either sam@DBA.EXAMPLE.COM or alice@ENGINEERING.EXAMPLE.COM the readWrite role on the PrimaryApplication database.给定配置的活动目录组,MongoDB授予用户以下身份验证sam@DBA.EXAMPLE.COMalice@ENGINEERING.EXAMPLE.COM PrimaryApplication数据库上的readWrite角色。

Note

To manage roles on the admin database, you must be authenticated as a user with userAdmin on admin, userAdminAnyDatabase, or a custom role on with equivalent privileges.要管理admin数据库上的角色,您必须通过admin上的userAdminuserAdminAnyDatabase或具有同等权限的自定义角色的用户身份验证。

16

Optional: Transition existing users from $external to the Active Directory server.可选:将现有用户从$external迁移到活动目录服务器。

If upgrading an existing installation with users configured on the $external database, you must meet the following requirements for each user to ensure access after configuring MongoDB for Kerberos authentication and AD authorization:如果使用在$external数据库上配置的用户升级现有安装,则必须满足每个用户的以下要求,以确保在配置MongoDB进行Kerberos身份验证和AD授权后能够访问:

  • User has a corresponding user object on the AD server.用户在AD服务器上有一个相应的用户对象。
  • User has membership in the appropriate groups on the AD server.用户具有AD服务器上相应组的成员资格。
  • MongoDB contains the roles on the admin database named for the user's AD groups, such that the authorized user retains its privileges.MongoDB包含以用户AD组命名的admin数据库上的角色,以便授权用户保留其权限。

Example示例

The following user exists on the $external database:$external数据库上存在以下用户:

{
user : "joe@ANALYTICS.EXAMPLE.COM",
roles: [
{ role : "read", db : "web_analytics" },
{ role : "read", db : "PrimaryApplication" }
]
}

Assuming the user belongs to the AD group CN=marketing,CN=Users,DC=example,DC=com, the following operation creates a matching role with the appropriate privileges:假设用户属于AD组CN=marketing,CN=Users,DC=example,DC=com,则以下操作将创建具有适当权限的匹配角色:

db.getSiblingDB("admin").createRole(
{
role: "CN=marketing,CN=Users,DC=example,DC=com",
privileges: [],
roles: [
{ role: "read", db: "web_analytics" }
{ role: "read", db: "PrimaryApplication" }
]
}
)

Based on the configured queryTemplate, MongoDB authorizes any user who has direct or transitive membership in the CN=marketing,CN=Users,DC=example,DC=com group to perform read operations on the web_analytics and PrimaryApplication databases.基于配置的queryTemplate,MongoDB授权在CN=marketing,CN=Users,DC=example,DC=com组中具有直接或可传递成员资格的任何用户对web_analyticsPrimaryApplication数据库执行read操作。

Important

When configuring a role for a corresponding AD group, remember that all users with membership in that group can receive the assigned roles and privileges. 为相应的AD组配置角色时,请记住,该组中的所有成员用户都可以获得分配的角色和权限。Consider applying the principle of least privilege when configuring MongoDB roles, AD groups, or group membership.在配置MongoDB角色、AD组或组成员资格时,考虑应用最小权限原则

If you want to continue allowing users on non-$external databases to access MongoDB, you must include SCRAM authentication mechanism (e.g. SCRAM-SHA-1 and/or SCRAM-SHA-256) in the setParameter authenticationMechanisms configuration option.如果你想继续允许非$external数据库上的用户访问MongoDB,你必须在setParameter authenticationMechanisms配置选项中包含SCRAM身份验证机制(例如SCRAM-SHA-1和/或SCRAM-SHA-256)。

setParameter:
authenticationMechanisms: "GSSAPI,SCRAM-SHA-1,SCRAM-SHA-256"

Alternatively, transition non-$external users to AD by following the above procedure.或者,按照上述过程将非$external用户转换为AD。

This procedure produces the following configuration file:此过程生成以下配置文件:

security:
authorization: "enabled"
ldap:
servers: activedirectory.example.net"
bind:
queryUser: "mongodbadmin@dba.example.com"
queryPassword: "secret123"
userToDNMapping:
'[
{
match: "(.+)"
ldapQuery: "DC=example,DC=com??sub?(userPrincipalName={0})"
}
]'
authz:
queryTemplate: "DC=example,DC=com??sub?(&(objectClass=group)(member:1.2.840.113556.1.4.1941:={USER}))"
setParameter:
authenticationMechanisms: "GSSAPI"

Important

The given sample configuration requires modification to match your AD schema, directory structure, and configuration. You may also require additional configuration file options for your deployment.给定的示例配置需要修改以匹配AD架构、目录结构和配置。您可能还需要为部署提供其他配置文件选项

For more information on configuring roles and privileges, see:有关配置角色和权限的更多信息,请参阅:

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中可用。