在 Windows 上使用 Kerberos 认证配置自管理 MongoDB
概述
MongoDB Enterprise支持使用Kerberos服务进行身份验证。Kerberos服务。Kerberos是一种针对大型客户端/服务器系统的行业标准认证协议。Kerberos允许MongoDB和应用程序利用现有的认证基础设施和流程。MongoDB Enterprise仅支持Kerberos的MIT实现。
先决条件
本文档的范围不包括Kerberos部署的设置和配置。本教程假设已经为每个 Kerberos服务主体 以及 mongod.exe
和 mongos.exe
实例配置了。
对于副本集和分片集群,确保您的配置使用完全限定的域名(FQDN)而不是IP地址或未限定的主机名。您必须使用FQDN以正确解析Kerberos领域并允许您连接。
程序
在不使用Kerberos的情况下启动 mongod.exe
。
对于Kerberos用户的初始添加,在不支持Kerberos的情况下启动 mongod.exe
。
如果MongoDB中已经有Kerberos用户并且有创建用户的必要权限,您可以启动带有Kerberos支持的 mongod.exe
。
根据您的部署适当包含其他设置。
注意
mongod
和 mongos
默认绑定到本地主机。如果您的部署成员在不同的主机上运行,或者您希望远程客户端连接到您的部署,则必须指定 --bind_ip
或 net.bindIp
。
连接到 mongod
。
使用 mongosh
连接到 mongod.exe
实例。如果 mongod.exe
启用了 --auth
,确保您使用创建用户所需的权限连接。
将 Kerberos 主实体添加到 MongoDB。
将 Kerberos 主实体 <username>@<KERBEROS REALM>
添加到 MongoDB 的 $external
数据库中。请使用 全部大写 指定 Kerberos 域。$external
数据库允许 mongod.exe
咨询外部源(例如 Kerberos)进行身份验证。要指定用户的权限,请将 角色 分配给用户。
要使用 客户端会话和因果一致性保证 与 $external
身份验证用户(Kerberos、LDAP 或 x.509 用户),用户名长度不能超过 10k 字节。
以下示例将 Kerberos 主实体 reportingapp@EXAMPLE.NET
添加到具有只读访问权限的 records
数据库中
use $external db.createUser( { user: "reportingapp@EXAMPLE.NET", roles: [ { role: "read", db: "records" } ] } )
根据需要添加额外的管理员。对于每个您想要使用Kerberos进行认证的用户,您必须在MongoDB中创建相应的用户。有关创建和管理用户的更多信息,请参阅用户管理命令。
以Kerberos支持方式启动 mongod.exe
。
您必须以服务主体账户。的身份启动 mongod.exe
。
要使用Kerberos支持启动 mongod.exe
,将 mongod.exe
参数 authenticationMechanisms
设置为 GSSAPI
mongod.exe --setParameter authenticationMechanisms=GSSAPI <additional mongod.exe options>
根据您的配置需要包含其他选项。例如,如果您希望远程客户端连接到您的部署或您的部署成员在不同的主机上运行,请指定 --bind_ip
。
例如,以下示例展示了如何以Kerberos支持方式启动独立 mongod.exe
实例
mongod.exe --auth --setParameter authenticationMechanisms=GSSAPI --bind_ip localhost,<hostname(s)|ip address(es)>
将 mongosh.exe
壳连接到 mongod.exe
并进行认证。
将 mongosh
壳客户端作为Kerberos主体 application@EXAMPLE.NET
连接。
您可以从命令行进行连接和认证。
使用 cmd.exe
mongosh.exe --host hostname.example.net --authenticationMechanism=GSSAPI --authenticationDatabase=$external --username reportingapp@EXAMPLE.NET
使用 Windows PowerShell
mongosh.exe --host hostname.example.net --authenticationMechanism=GSSAPI --authenticationDatabase='$external' --username reportingapp@EXAMPLE.NET
如果您连接到的主机名与Kerberos名称匹配,请确保您为 --host
选项指定完全限定域名(FQDN),而不是IP地址或无资格的主机名。
如果您要连接到一个主机名与Kerberos名称不匹配的系统,首先将mongosh
shell连接到mongod.exe
,然后从mongosh
shell中,使用db.auth()
方法在$external
数据库中进行身份验证。
use $external db.auth( { mechanism: "GSSAPI", user: "reportingapp@EXAMPLE.NET" } )
其他注意事项
为Kerberos配置mongos.exe
要启用Kerberos支持启动mongos.exe
,将mongos.exe
参数authenticationMechanisms
设置为GSSAPI
。您必须以服务主体账户:的身份启动mongos.exe
。
mongos.exe --setParameter authenticationMechanisms=GSSAPI <additional mongos options>
根据您的配置需要包含其他选项。例如,如果您希望远程客户端连接到您的部署或您的部署成员在不同的主机上运行,请指定 --bind_ip
。
例如,以下示例启动了一个具有Kerberos支持的mongos
实例
mongos.exe --setParameter authenticationMechanisms=GSSAPI --configdb shard0.example.net, shard1.example.net,shard2.example.net --keyFile C:\<path>\mongos.keyfile --bind_ip localhost,<hostname(s)|ip address(es)>
根据您的配置需要修改或包含任何其他mongos.exe
选项。例如,您可以使用x.509成员身份验证代替--keyFile
用于内部身份验证分片集群成员。
将服务主体名称(SPN)分配给MongoDB Windows服务
使用 setspn.exe
将服务主体名称(SPN)分配给运行 mongod.exe
和 mongos.exe
服务的帐户
setspn.exe -S <service>/<fully qualified domain name> <service account name>
示例
如果 mongod.exe
作为名为 mongodb
的服务在 testserver.mongodb.com
上运行,服务帐户名称为 mongodtest
,则按以下方式分配SPN
setspn.exe -S mongodb/testserver.mongodb.com mongodtest
集成其他认证机制
Kerberos认证(GSSAPI(Kerberos))可以与
MongoDB的SCRAM认证机制
MongoDB的LDAP认证机制
PLAIN(LDAP SASL)
MongoDB的x.509认证机制
按照以下方式指定机制
--setParameter authenticationMechanisms=GSSAPI,SCRAM-SHA-256
只有在使用其他机制时才添加。此参数设置不影响MongoDB对集群成员的内部认证。
测试和验证
完成配置步骤后,您可以使用 mongokerberos
工具验证您的配置。
mongokerberos
提供了一种方便的方法来验证您的平台 Kerberos 配置以用于 MongoDB,并测试 MongoDB 客户端的 Kerberos 身份验证是否按预期工作。有关更多信息,请参阅 mongokerberos
文档。
mongokerberos
仅在 MongoDB Enterprise 中可用。