LDAP Authorization

在本页面

3.4 版中的新功能:MongoDB Enterprise支持在 LDAP 服务器中查询经过身份验证的用户所属的 LDAP 组。 MongoDB 将每个返回的组的专有名称(DN)Map 到admin数据库上的roles。 MongoDB 根据 Map 的角色及其关联的特权来授权用户。有关更多信息,请参见LDAP Authorization

LDAP 授权过程总结如下:

在版本 3.6.3 中更改:要与$external个身份验证用户(即 Kerberos,LDAP,x.509 用户)一起使用会话,用户名不能超过 10k 字节。

MongoDB 默认使用简单绑定,但是如果在security.ldap.bind.methodsecurity.ldap.bind.saslMechanisms中配置,则可以使用sasl绑定。

MongoDB 可以使用security.ldap.userToDNMapping选项来转换用户名以支持查询模板。

Considerations

LDAP 的完整描述超出了本文档的范围。该页面假定您具有 LDAP 的先验知识。

本文档仅描述 MongoDB LDAP 授权,而不替代 LDAP 上的其他资源。我们鼓励您在配置 LDAP 身份验证之前彻底熟悉 LDAP 及其相关主题。

MongoDB 可以提供professional services为您的 MongoDB 部署优化配置 LDAP 授权。

兼容的身份验证机制

以下身份验证机制与 MongoDB LDAP 授权兼容:

User Management

使用 LDAP 授权,可以在 LDAP 服务器上进行用户创建和 Management。 MongoDB 要求在admin数据库上创建roles,每个角色的名称必须与 LDAP 组专有名称(DN)完全匹配。这与 MongoDB 托管授权相反,后者需要在$external数据库上创建用户。

要在 MongoDB 服务器上 Management 角色,请以组身份与具有角色 Management 特权(例如userAdmin提供的特权)的admin数据库角色相对应的用户身份进行身份验证。创建或更新与 LDAP 组 DN 对应的角色,以使具有该组成员身份的用户获得适当的角色和特权。

例如,数据库 Management 员的 LDAP 组可能具有具有 Management 角色和特权的角色。用于市场营销或分析用户的 LDAP 组可能只具有对某些数据库的读取特权的角色。

Important

为相应的 LDAP 组配置角色时,请记住该组中具有成员身份的所有用户都可以接收配置的角色和特权。在配置 MongoDB 角色,LDAP 组或组成员身份时,请考虑应用最小特权原则。

如果不存在具有角色 Management 特权的角色并且不存在具有这些特权的非$external用户,则您实际上将无法执行用户 Management,因为不能更改新角色或现有角色以反映对 LDAP 上组或组成员的添加或更改服务器。

要解决无法在 MongoDB 服务器上 Management 角色的情况,请执行以下过程:

Existing Users

使用 LDAP 进行授权的 MongoDB 服务器使$external数据库上的所有现有用户均无法访问。如果$external数据库中已有用户,则必须满足$external数据库中每个用户的以下要求,以确保 continue 访问:

如果要 continue 允许用户_2 数据库上的 not *用户访问,请确保authenticationMechanisms参数包含SCRAM-SHA-1。或者,应用上面列出的要求将这些用户转换为 LDAP 授权。

Replica Sets

对于replica sets,请先在secondaryarbiter成员上配置 LDAP 授权,然后再配置primary。这也适用于分片副本集配置服务器副本集。一次配置一个副本集成员,以维护大多数成员的写可用性。

Sharded Clusters

sharded clusters中,您必须在config servers上为群集级用户配置 LDAP 授权。您可以选择在每个shard上为本地本地用户配置 LDAP 授权。

Configuration

您必须配置以下设置才能使用 LDAP 授权:

要使用 LDAP 通过 os 库进行授权,请在mongodmongos配置文件中指定以下设置:

option description required
security.ldap.servers host[:port]格式括起来的用引号括起来的逗号分隔的 LDAP 服务器列表。 YES
security.ldap.authz.queryTemplate MongoDB 执行的RFC4515RFC4516 LDAP 格式的查询 URL 模板,以获取用户所属的 LDAP 组。该查询相对于servers中指定的一个或多个主机。

使用{USER}特殊占位符将经过身份验证的用户名或transformed用户名替换为 LDAP 查询。
mongod支持此参数。 mongos遵照其config servers
中的配置进行此设置
security.ldap.bind.queryUser MongoDB 服务器在连接到 LDAP 服务器并在其上执行操作和查询时绑定的身份。
queryPassword一起使用。
指定的用户必须具有适当的特权才能支持从已配置的queryTemplate生成的 LDAP 查询。
security.ldap.bind.queryPassword 使用queryUser时用于绑定到 LDAP 服务器的密码。
security.ldap.bind.method 用于指定mongodmongos用来认证或绑定到 LDAP 服务器的方法。指定sasl以使用security.ldap.bind.saslMechanisms中定义的 SASL 协议之一。
默认为simple ,除非使用sasl绑定到 LDAP 服务器。
security.ldap.bind.saslMechanisms 用于指定验证或绑定到 LDAP 服务器时可以使用的 SASL 机制mongodmongos。 MongoDB 和 LDAP 服务器必须在至少一种 SASL 机制上达成共识。
默认为DIGEST-MD5 ,除非将bindMethod设置为sasl,并且您需要其他或其他 SASL 机制。
security.ldap.bind.useOSDefaults 与连接到 LDAP 服务器时一样,Windows MongoDB 部署可以使用 os 凭据代替queryUserqueryPassword进行身份验证或绑定。 ,除非替换queryUserqueryPassword
security.ldap.userToDNMapping 根据您的queryTemplate,经过身份验证的 Client 端用户名可能需要转换以支持 LDAP 查询 URL。 userToDNMapping允许 MongoDB 转换传入的用户名。 ,除非 Client 端用户名需要转换为 LDAP DN。

LDAP 查询模板

MongoDB 使用security.ldap.authz.queryTemplate创建RFC4516格式的 LDAP 查询 URL。在模板中,使用{USER}占位符将经过身份验证的用户名替换为 LDAP 查询 URL。设计查询模板以检索经过身份验证的用户的组。如果 MongoDB 使用userToDNMapping转换了用户名,则 MongoDB 在构造 LDAP 查询 URL 时将{USER}令牌替换为转换后的用户名。

Example

以下查询模板返回 LDAP 用户对象的memberOf属性中列出的所有组。该查询假定memberOf属性存在-您的特定 LDAP 部署可能使用其他属性或方法来跟踪组成员身份。此查询还假定用户使用其完整 LDAP DN 作为用户名进行身份验证。

"{USER}?memberOf?base"

LDAP 查询 URL 必须符合RFC4516中定义的格式:

[ dn  [ ? [attributes] [ ? [scope] [ ? [filter] [ ? [Extensions] ] ] ] ] ]

考虑一下 RFC4516 中引用的每个组件的定义:

The ``dn`` is an LDAP Distinguished Name using the string format described
in `RFC4514 <https://tools.ietf.org/html/rfc4514>`_. It identifies the base
object of the LDAP search or the target of a non-search operation.

The ``attributes`` construct is used to indicate which attributes should be
returned from the entry or entries.

The ``scope`` construct is used to specify the scope of the search to perform
in the given LDAP server. The allowable scopes are "base" for a base object
search, "one" for a one-level search, or "sub" for a subtree search.

The ``filter`` is used to specify the search filter to apply to entries
within the specified scope during the search. It has the format specified
in [RFC4515].

The ``extensions`` construct provides the LDAP URL with an extensibility
mechanism, allowing the capabilities of the URL to be extended in the
future.

如果查询包含attribute,则 MongoDB 假定查询检索到该实体所属的 DN。

如果查询不包含属性,则 MongoDB 假定查询检索用户所属的所有实体。

MongoDB 当前忽略 LDAP 查询中指定的任何 extensions。

Important

RFC4516 或 LDAP 查询 URL 构造的完整描述超出了本文档的范围。

Tutorials

以下教程包含通过 osLDAP 库连接到 LDAP 服务器的过程:

使用 LDAP 授权连接到 MongoDB 服务器

使用 LDAP 进行授权时,通过mongo shell 连接的用户必须:

如果使用LDAP authentication,请将其设置为PLAIN

如果使用Kerberos authentication,请将其设置为GSSAPI

如果使用x.509,请将其设置为MONGODB-X.509

包括 MongoDB 服务器的--host--port以及与您的部署相关的任何其他选项。

例如,以下操作对使用 LDAP 身份验证和授权运行的 MongoDB 服务器进行身份验证:

mongo --username alice@dba.example.com --password "secret123" --authenticationDatabase '$external' --authenticationMechanism "PLAIN"  --host "mongodb.example.com" --port 27017

Important

$external参数必须放在单引号中,而不是双引号中,以防止 Shell 程序将$external解释为变量。

用于 LDAP 授权的 MongoDB 角色

MongoDB 将 LDAP query返回的每个返回的组专有名称(DN)Map 到admin数据库上的role

如果 MongoDB 获取的 DN 完全匹配现有角色的名称的组,则 MongoDB 会授予经过身份验证的用户角色和与该角色关联的privileges。如果 MongoDB 无法将任何返回的组 Map 到角色,则 MongoDB 不会向用户授予任何特权。

Note

LDAPkerberos身份验证通常需要在$external数据库中创建用户。如果您还使用 LDAP 进行授权,则无需$external数据库中创建用户。您只需要在admin数据库中创建适当的角色。用户仍然针对$external数据库进行身份验证。

Important

如果您使用 LDAP 进行授权,并且 LDAP 组 DN 包含RFC4514转义的序列,则在admin数据库中创建的角色也必须遵循 RFC4514 进行转义。

Example

数据库在admin数据库上配置了以下角色:

{
role: "CN=dba,CN=Users,DC=example,DC=com",
privileges: [],
roles: [ "dbAdminAnyDatabase", "clusterAdmin" ]
}
{
role: "CN=analytics,CN=Users,DC=example,DC=com"
privileges: [],
roles: [
{ role : "read", db : "web_statistics" },
{ role : "read", db : "user_statistics" }
]
}

在针对$external数据库对用户alice@dba.example.com进行身份验证之后,MongoDB 服务器执行从已配置query template派生的查询,以检索包括已身份验证的用户作为成员的组。在此示例中,MongoDB 服务器为用户检索以下组 DN:

dn:CN=dba,CN=Users,dc=example,dc=com
dn:CN=admin,CN=Users,dc=example,dc=com

MongoDB 将这些组 DNMap 到admin数据库上的角色。第一组 DN 匹配第一个角色,MongoDB 授予经过身份验证的用户其角色和特权。第二个组 DN 与服务器上的任何角色都不匹配,因此 MongoDB 不授予任何其他权限。

新用户bob@analytics.example.com$external数据库进行身份验证。 MongoDB 服务器使用查询模板中提供的用户名重复查询过程。在此示例中,MongoDB 服务器为用户检索以下组 DN:

dn:cn=analytics,CN=Users,dc=example,dc=com

MongoDB 将这些组 DNMap 到admin数据库上的角色,并向经过身份验证的用户授予第二个角色的角色和特权。

新用户workstation@guest.example.com$external数据库进行身份验证。 MongoDB 服务器使用查询模板中提供的用户名重复查询过程。在此示例中,MongoDB 服务器为用户检索以下组 DN:

dn:cn=guest,CN=Users,dc=example,dc=com

MongoDB 将组 Map 到admin数据库上的角色,并且由于不存在匹配的角色,因此不会向用户授予其他权限。

首页