通过 ActiveDirectory 使用 SASL 和 LDAP 进行身份验证

MongoDB Enterprise 提供对用户代理身份验证的支持。 这允许管理员配置 MongoDB 集群,通过将身份验证请求代理到指定的轻量级目录访问协议 (LDAP) 服务来对用户进行身份验证。

[NOTE]注意

对于链接到 libldap 的 MongoDB 4.2 Enterprise 二进制文件(例如在 RHEL 上运行时),对 libldap 的访问是同步的,会产生一些性能/延迟成本。

对于链接到libldap_r 的 MongoDB 4.2 Enterprise 二进制文件,与早期 MongoDB 版本相比,行为没有变化。

注意事项

[WARNING]警告

MongoDB Enterprise for Windows 不支持通过 saslauthd 进行绑定。

  • Linux MongoDB 服务器支持通过 saslauthd 守护进程绑定到 LDAP 服务器。
  • 在客户端和服务器之间以及 saslauthd 和 LDAP 服务器之间使用安全的加密或可信连接。 LDAP 服务器使用 SASL PLAIN 机制,以明文形式发送和接收数据。 您应该只使用受信任的渠道,例如 VPN、使用 TLS/SSL 加密的连接或受信任的有线网络。

配置saslauthd

LDAP 对用户身份验证的支持需要正确配置 saslauthd 守护进程以及 MongoDB 服务器。

  1. 指定机制

    在使用 /etc/sysconfig/saslauthd 文件配置 saslauthd 的系统上,例如 Red Hat Enterprise Linux、Fedora、CentOS 和 Amazon Linux AMI,将机制 MECH 设置为 ldap

    MECH=ldap
    

    在使用 /etc/default/saslauthd 文件配置 saslauthd 的系统上,例如 Ubuntu,将 MECHANISMS 选项设置为 ldap

    MECHANISMS="ldap"
    
  2. 调整缓存行为

    在某些 Linux 发行版上,saslauthd 以启用身份验证凭据的缓存开始。 在重新启动或缓存过期之前,saslauthd 不会联系 LDAP 服务器以在其身份验证缓存中重新验证用户。 这允许 saslauthd 成功验证其缓存中的用户,即使在 LDAP 服务器已关闭或缓存的用户凭证已被撤销的情况下也是如此。

    要设置身份验证缓存的过期时间(以秒为单位),请参阅-t optionsaslauthd

  3. 使用 ActiveDirectory 配置 LDAP 选项

    如果 saslauthd.conf 文件不存在,请创建它。 saslauthd.conf文件通常位于 /etc 文件夹中。 如果指定不同的文件路径,请参阅-O optionsaslauthd

    要与 ActiveDirectory 一起使用,请使用 saslauthd.conf文件中设置的以下配置选项启动 saslauthd

    ldap_servers: <ldap uri>
    ldap_use_sasl: yes
    ldap_mech: DIGEST-MD5
    ldap_auth_method: fastbind
    

    对于 <ldap uri>,指定 ldap 服务器的 uri。 例如,ldap_servers:ldaps://ad.example.net

    有关 saslauthd 配置的更多信息,请参阅http://www.openldap.org/doc/admin24/guide.html#Configuringsaslauthd

  4. 测试 saslauthd 配置。

    使用 testsaslauthd 实用程序测试 saslauthd 配置。 例如:

    testsaslauthd -u testuser -p testpassword -f /var/run/saslauthd/mux
    
    • 0:OK “Success”表示认证成功。

    • 0:NO “authentication failed”表示用户名、密码或配置错误。

    根据主机操作系统上 saslauthd 目录的位置修改文件路径。

    [IMPORTANT]重要 指定给 security.sasl.saslauthdSocketPath--setParameter saslauthdPathsaslauthd Unix 域套接字文件的父目录必须授予读取和执行 (r-x) 权限:

    • 该用户所属的组。

    如果没有对 saslauthd 目录及其内容的指定权限,mongodmongos 无法通过 saslauthd 成功进行身份验证。

配置 MongoDB

  1. 将用户添加到 MongoDB 进行身份验证

    将用户添加到 MongoDB 中的 $external 数据库。 要指定用户的权限,请为用户分配角色。

    要对 $external 身份验证用户(Kerberos、LDAP 或 x.509 用户)使用Client Sessions and Causal Consistency Guarantees ,用户名不能大于 10k 字节。

    例如,以下添加了一个对记录数据库具有只读访问权限的用户。

    db.getSiblingDB("$external").createUser(
        {
          user : <username>,
          roles: [ { role: "read", db: "records" } ]
        }
    )
    

    根据需要添加其他主体。 有关创建和管理用户的更多信息,请参阅User Management Commands

  2. 配置 MongoDB 服务器

    要将 MongoDB 服务器配置为使用 saslauthd 实例进行代理身份验证,请在启动 mongod 时包括以下选项:

    [IMPORTANT]重要 指定给 security.sasl.saslauthdSocketPath--setParameter saslauthdPathsaslauthd Unix 域套接字文件的父目录必须授予读取和执行 (r-x) 权限:

    • 该用户所属的组。

    如果没有对 saslauthd 目录及其内容的指定权限,mongodmongos 无法通过 saslauthd 成功进行身份验证。

    如果您使用authorization选项来强制执行身份验证,您将需要权限来创建用户。

    使用特定的 saslauthd 套接字路径

    对于 /<some>/<path>/saslauthd 的套接字路径,将 saslauthdPath 设置为 /<some>/<path>/saslauthd/mux,如以下命令行示例所示:

    mongod --auth --setParameter saslauthdPath=/<some>/<path>/saslauthd/mux --setParameter authenticationMechanisms=PLAIN
    

    根据您的配置需要包括其他选项。 例如,如果您希望远程客户端连接到您的部署或您的部署成员在不同的主机上运行,请指定 --bind_ip。 有关详细信息,请参阅Localhost Binding Compatibility Changes

    或者,如果使用 YAML 格式的配置文件,请在文件中指定以下设置:

    security:
       authorization: enabled
    
    setParameter:
       saslauthdPath: /<some>/<path>/saslauthd/mux
       authenticationMechanisms: PLAIN
    

    或者,如果使用older configuration file format

    auth=true
    setParameter=saslauthdPath=/<some>/<path>/saslauthd/mux
    setParameter=authenticationMechanisms=PLAIN
    

    使用默认的 Unix 域套接字路径

    要使用默认的 Unix 域套接字路径,请将 saslauthdPath 设置为空字符串"",如以下命令行示例所示:

    mongod --auth --setParameter saslauthdPath="" --setParameter authenticationMechanisms=PLAIN
    

    根据您的配置需要包括其他选项。 例如,如果您希望远程客户端连接到您的部署或您的部署成员在不同的主机上运行,请指定 --bind_ip。 有关详细信息,请参阅Localhost Binding Compatibility Changes

    或者,如果使用 YAML format configuration file,请在文件中指定以下设置:

    security:
       authorization: enabled
    
    setParameter:
       saslauthdPath: ""
       authenticationMechanisms: PLAIN
    

    或者,如果使用older configuration file format

    auth=true
    setParameter=saslauthdPath=""
    setParameter=authenticationMechanisms=PLAIN
    

    根据您的配置需要包括其他选项。 例如,如果您希望远程客户端连接到您的部署或您的部署成员在不同的主机上运行,请指定 net.bindIp 设置。 有关详细信息,请参阅Localhost Binding Compatibility Changes

  3. mongosh 中验证用户

    您可以在连接期间从命令行进行身份验证,或者先连接然后使用 db.auth() 方法进行身份验证。

    在连接期间进行身份验证

    要在连接 mongosh 时进行身份验证,请使用以下命令行选项运行 mongosh,替换 <host><user>,并在出现提示时输入您的密码:

    mongosh --host <host> --authenticationMechanism PLAIN --authenticationDatabase '$external' -u <user> -p
    

    服务器以明文形式转发密码。 通常,仅在受信任的通道(VPN、TLS/SSL、受信任的有线网络)上使用。 请参阅注意事项。

    连接后验证

    或者,在不提供凭据的情况下进行连接,然后在 $external 数据库上调用 db.auth() 方法。 在机制字段中指定值“PLAIN”,在用户和密码字段中分别指定用户和密码。 使用默认的 digestPassword 值 (false),因为服务器必须收到未消化的密码才能转发到 saslauthd,如以下示例所示:

    [TIP]提示

    mongo shell 版本 4.2 开始,您可以使用 passwordPrompt() 方法结合各种用户身份验证/管理方法/命令来提示输入密码,而不是直接在方法/命令调用中指定密码。 但是,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。

    db.getSiblingDB("$external").auth(
       {
         mechanism: "PLAIN",
         user: <username>,
         pwd:  passwordPrompt() // or cleartext password
       }
    )
    

    出现提示时输入密码。

    服务器以明文形式转发密码。 通常,仅在受信任的通道(VPN、TLS/SSL、受信任的有线网络)上使用。 请参阅注意事项。

    参见

原文 - Authenticate Using SASL and LDAP with ActiveDirectory

译者:景圣

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-02-17 14:11:51

results matching ""

    No results matching ""