Database Manual / Self-Managed Deployments / Security / Authentication / LDAP Proxy

Configure Users Using Self-Managed Active Directory with Native LDAP使用具有本机LDAP的自管理活动目录配置用户

Note

Starting in MongoDB 8.0, LDAP authentication and authorization is deprecated. LDAP is available and will continue to operate without changes throughout the lifetime of MongoDB 8. LDAP will be removed in a future major release.从MongoDB 8.0开始,LDAP身份验证和授权被弃用。LDAP是可用的,并将在MongoDB 8的整个生命周期内继续运行而不做任何更改。LDAP将在未来的主要版本中删除。

For details, see LDAP Deprecation.有关详细信息,请参阅LDAP弃用

MongoDB Enterprise provides support via platform LDAP libraries for proxy authentication and authorization requests to a specified Lightweight Directory Access Protocol (LDAP) service such as Active Directory (AD).MongoDB Enterprise通过平台LDAP库为指定的轻量级目录访问协议(LDAP)服务(如活动目录(AD))的代理身份验证和授权请求提供支持。

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

Note

For MongoDB 4.2 Enterprise binaries linked against libldap (such as when running on RHEL), access to the libldap is synchronized, incurring some performance/latency costs.对于与libldap链接的MongoDB 4.2 Enterprise二进制文件(例如在RHEL上运行时),对libldap的访问是同步的,这会产生一些性能/延迟成本。

For MongoDB 4.2 Enterprise binaries linked against libldap_r, there is no change in behavior from earlier MongoDB versions.对于与libldap_r链接的MongoDB 4.2 Enterprise二进制文件,与早期MongoDB版本相比,行为没有变化。

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. MongoDB支持使用SASL机制在MongoDB服务器和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. SASL、SASL机制或给定SASL机制的特定AD配置要求的完整描述超出了本教程的范围。This tutorial assumes prior knowledge of SASL and its related subject matter.本教程假设您对SASL及其相关主题有先验知识。

Configure Internal Member Authentication配置内部成员身份验证

You must configure internal member authentication before you can set up LDAP authentication or authorization for a cluster.您必须先配置内部成员身份验证,然后才能为群集设置LDAP身份验证或授权。

Considerations注意事项

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

To perform this procedure on your own MongoDB server, you must modify the given procedures with respect to your own specific infrastructure, especially Active Directory configurations, constructing AD queries, or managing users.要在自己的MongoDB服务器上执行此过程,您必须根据自己的特定基础架构修改给定的过程,特别是活动目录配置、构建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服务器上创建证书的副本。

User Names用户名

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字节。

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_CAMERT或TLS_CAMERTDIR选项指定AD服务器的CA证书。

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

On Microsoft Windows, load the AD server's Certificate Authority (CA) certificates with the platform's credential management tool. The exact credential management tool is Windows version dependent. To use the tool, refer to its documentation for your version of Windows.在Microsoft Windows上,使用平台的凭据管理工具加载AD服务器的证书颁发机构(CA)证书。确切的凭据管理工具取决于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

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

3

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组或组成员资格时,考虑应用最小权限原则

4

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的空文件,并从新的配置文件开始工作。
5

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使用。

You must also enable LDAP authentication by setting security.authorization to enabled and setParameter authenticationMechanisms to PLAIN您还必须通过将security.authorization设置为enabled并通过setParameterauthenticationMechanisms设置为PLAIN来启用LDAP身份验证

Example示例

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

security:
authorization: "enabled"
ldap:
servers: "activedirectory.example.net"
setParameter:
authenticationMechanisms: 'PLAIN'

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身份验证机制。

6

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转换之前。

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. This matching rule is an AD specific extension to LDAP search filters.该查询包括LDAP_MATCHING_RULE_IN_CHAIN的AD特定匹配规则OID 1.2.840.113556.1.4.1941。此匹配规则是LDAP搜索筛选器的AD特定扩展。

Warning

If the AD forest contains a large number of groups, the recursive member:1.2.840.113556.1.4.1941 filter can lead to significant performance degradation.如果AD林包含大量组,递归member:1.2.840.113556.1.4.1941筛选器可能会导致性能显著下降。

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/transfored 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 the following groups:根据活动目录组,AD服务器返回以下组:

  • CN=dba,CN=Users,DC=example,DC=com
  • CN=engineering,CN=Users,DC=example,DC=com
  • CN=PrimaryApplication,CN=Users,DC=example,DC=com

MongoDB maps each returned group DN to a role on the admin database. MongoDB将每个返回的组DN映射到admin数据库上的一个角色。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.对于每个映射的组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。If users authenticate using a different username format, such as their user principal name, you must transform the incoming usernames into DNs using security.ldap.userToDNMapping.如果用户使用不同的用户名格式进行身份验证,例如他们的用户主体名称,则必须使用security.ldap.userToDNMapping将传入的用户名转换为DN。

7

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

If your users authenticate with a username that is not a full LDAP DN, you may need to transform the username to support LDAP authentication or authorization. MongoDB uses the transformed username for both authentication and authorization.如果用户使用的用户名不是完整的LDAP DN进行身份验证,您可能需要转换用户名以支持LDAP身份验证或授权。MongoDB使用转换后的用户名进行身份验证和授权。

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示例

Given the configured queryTemplate, users must authenticate with their full LDAP DN. If users instead authenticate using their userPrincipalName, then a transformation must be applied to convert the provided username to a full LDAP DN.给定配置的queryTemplate,用户必须使用其完整的LDAP DN进行身份验证。如果用户改为使用他们的userPrincipalName进行身份验证,则必须应用转换将提供的用户名转换为完整的LDAP DN。

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

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

The Active Directory server returns the full LDAP DN associated to the user object with a matching userPrincipalName. MongoDB can then use this transformed username for authentication and authorization.活动目录服务器返回与具有匹配userPrincipalName的用户对象关联的完整LDAP DN。然后,MongoDB可以使用此转换后的用户名进行身份验证和授权。

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. 用户身份验证为alice@ENGINEERING.EXAMPLE.COMMongoDB first applies any transformations specified in userToDNMapping. 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.如果使用userToDNMappingsubstitution(替换)参数来转换组名,则替换的结果必须RFC4514转义字符串。

8

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查询的权限。

9

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

Add any additional configuration options required for your deployment. For example, you can specify your desired storage.dbPath or change the default net.port number.添加部署所需的任何其他配置选项。例如,您可以指定所需的storage.dbPath或更改默认net.port号。

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 the net.bindIp setting.如果部署的成员在不同的主机上运行,或者希望远程客户端连接到部署,则必须指定net.bindIp设置。

10

Start the MongoDB server with Active Directory authentication and authorization.使用活动目录身份验证和授权启动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服务器当前正在运行,请做好停止服务器的适当准备。

mongod --config <path-to-config-file>

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

11

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:要向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. 此角色对应于AD组。Based on the configured AD users, you can authenticate as the user sam@dba.example.com and receive the required permissions.根据配置的AD用户,您可以作为用户进行身份验证sam@dba.example.com并获得所需的权限。

mongosh --username sam@DBA.EXAMPLE.COM --password  --authenticationMechanism 'PLAIN' --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 SCRAM authentication mechanism (e.g. SCRAM-SHA-1 or SCRAM-SHA-256 as appropriate). 如果您想作为现有的非$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.这要求MongoDB服务器的setParameter身份验证机制包括SCRAM-SHA-1和/或SCRAM-SHA-256

12

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组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.COMPrimaryApplication数据库上的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上的userAdminuserAdminAnyDatabase或具有同等权限的自定义角色的用户身份验证。

13

Transition existing users from $external to the ActiveDirectory 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 AD authentication and authorization:如果使用在$external数据库上配置的用户升级现有安装,则必须满足每个用户的以下要求,以确保在配置MongoDB进行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. Consider applying the principle of least privilege when configuring MongoDB roles, AD groups, or group membership.为相应的AD组配置角色时,请记住,该组中的所有成员用户都可以获得分配的角色和权限。在配置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. For example:如果你想继续允许非$external数据库上的用户访问MongoDB,你必须通过setParameter使authenticationMechanisms配置选项中包含SCRAM身份验证机制(例如SCRAM-SHA-1和/或SCRAM-SHA-256)。例如:

setParameter:
authenticationMechanisms: "PLAIN,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: "PLAIN"

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

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