使用此页面为 mongot 和 MongoDB Community Edition 部署配置身份验证和授权。有关将 mongot 与 Kubernetes 操作符的 MongoDB 控制器一起使用的信息,请参阅 将 MongoDB Search 和向量搜索与 Kubernetes 结合使用。
mongot 通过两个独立的渠道进行通信,每个渠道都需要自己的安全配置:
同步源 (
syncSource.replicaSet、syncSource.router):mongot出站连接到您的 MongoDB 部署以进行复制。您在此处配置身份验证机制,以便mongot可以对mongod或mongos进行身份验证。gRPC 服务器 (
server.grpc):入站渠道,mongod通过该渠道连接到mongot以执行搜索查询和索引管理命令。mongod和mongot通过 mTLS 相互验证。
注意
健康检查 (healthCheck) 和指标 (metrics) 终结点不支持 TLS 或身份验证。在网络层限制对这些端口的访问。
Considerations
在配置身份验证之前,请查看以下几点:
必须为同步源配置一个身份验证机制:即 SCRAM 或 X.509。这两种机制在每个连接块中互相排斥。
对于分片集群,必须在
mongot配置文件中指定syncSource.replicaSet和syncSource.router,并在每个连接上独立配置身份验证。syncSource.router不是由syncSource.replicaSet隐含的,因此您必须显式配置它。每个分片都需要自己的专用mongot实例。syncSource.replicaSet将mongot实例映射到其分片的副本集。syncSource.router为集群范围协调提供mongos连接。
在
mongot能够进行身份验证之前,searchCoordinator内置角色必须存在于 MongoDB 部署中。此角色在 MongoDB 8.2 及更高版本中可用。
开始之前
将以下 setParameter 选项添加到 mongod 配置文件中,然后重启 mongod:
setParameter: mongotHost: <host>:<port> searchIndexManagementHostAndPort: <host>:<port> skipAuthenticationToSearchIndexManagementServer: false useGrpcForSearch: true
setParameter 选项设置服务器参数,告诉 mongod 服务器如何定位和与 mongot 通信。在 mongot 连接到部署之前,必须设置这些参数。
若要了解有关 mongod 配置文件的更多信息,请参阅 自管理配置文件选项。
配置同步源身份验证
在同步源连接上配置以下身份验证机制之一,以便 mongot 可以对 MongoDB 部署进行身份验证。
SCRAM 使用用户名和密码对 mongot 进行身份验证,以连接到 MongoDB 部署。
要为 mongot 配置 SCRAM,请完成以下步骤:
在 mongot 托管上创建密码文件。
将 <password> 替换为您的密码,然后为您的操作系统运行命令以创建密码文件。该文件不得包含尾随换行符。
适用于 Linux 或 macOS:
echo -n "<password>" > /etc/mongot/secrets/passwordFile chmod 400 /etc/mongot/secrets/passwordFile
对于 Windows 命令提示符:
echo|set /p="<password>" > passwordFile
对于 Windows Powershell:
[System.IO.File]::WriteAllText("passwordFile", "<password>")
将 scramAuth 块添加到 mongot 配置。
在 syncSource.replicaSet 下添加 scramAuth 块。对于分片集群,还要在 syncSource.router 下添加相同的块。在 hostAndPort 中列出多个托管,以便在一个托管不可用时 mongot 仍可以发现副本集:
syncSource: replicaSet: hostAndPort: - "<host1>:27017" - "<host2>:27017" scramAuth: username: mongotUser authSource: admin passwordFile: /etc/mongot/secrets/passwordFile tls: enabled: false
对于分片集群,请在 syncSource.replicaSet 和 syncSource.router 下配置 scramAuth。syncSource.replicaSet 对 mongot 进行身份验证以连接到分片的副本集,syncSource.router 对 mongot 进行身份验证以连接到 mongos 进行集群范围协调。您可以对这两个连接使用相同的凭证,也可以在独立管理时使用不同的凭证:
syncSource: replicaSet: hostAndPort: - "<shard-host1>:27017" - "<shard-host2>:27017" scramAuth: username: mongotShardUser authSource: admin passwordFile: /etc/mongot/secrets/shard-passwordFile tls: enabled: false router: hostAndPort: - "<mongos1>:27017" - "<mongos2>:27017" scramAuth: username: mongotRouterUser authSource: admin passwordFile: /etc/mongot/secrets/router-passwordFile tls: enabled: false
有关 mongot 配置文件的更多信息,请参阅配置 mongot。
如有需,为同步源连接启用 TLS。
如果您的 MongoDB 部署已启用 TLS,请将 tls.enabled 设置为 true,并根据需要向 mongot 配置文件提供以下字段:
字段 | 说明 |
|---|---|
| 包含一个或多个 X.509 的 PEM 文件验证 |
| 包含客户端 X.509 证书及其私钥的 PEM 文件。仅当 |
| 解密客户端证书密钥的密码。仅当 |
X.509 身份验证允许 mongot 使用 TLS 客户端证书而不是用户名和密码对 MongoDB 部署进行身份验证。使用 X.509 时始终启用 TLS。
要为 mongot 配置 X.509,请完成以下步骤:
将 x509 块添加到 mongot 配置。
在 syncSource.replicaSet 下添加 x509 块。对于分片集群,请在 syncSource.router 下添加相同的块。
在 x509 块中,tlsCertificateKeyFile 和 caFile 是必需的。在 hostAndPort 中列出多个主机,以便在一个主机不可用时 mongot 仍然可以发现副本集:
syncSource: replicaSet: hostAndPort: - "<host1>:27017" - "<host2>:27017" x509: tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem caFile: /etc/mongot/tls/ca.pem # tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password
字段 | 说明 |
|---|---|
| 必填。包含 X.509 证书和其私钥的 PEM 文件。 |
| 必填。包含一个或多个 X.509 的 PEM 文件验证 |
| 可选.包含密码的文件,如果密钥已加密,则该密码将解密 |
对于分片集群,请在 syncSource.replicaSet 和 syncSource.router 下配置 x509。对于这两个连接使用相同的字段结构,并在 syncSource.router.hostAndPort 中提供 mongos 终结点:
syncSource: replicaSet: hostAndPort: - "<shard-host1>:27017" - "<shard-host2>:27017" x509: tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem caFile: /etc/mongot/tls/ca.pem # tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password router: hostAndPort: - "<mongos1>:27017" - "<mongos2>:27017" x509: tlsCertificateKeyFile: /etc/mongot/tls/mongot-client.pem caFile: /etc/mongot/tls/ca.pem # tlsCertificateKeyFilePasswordFile: /etc/mongot/secrets/cert-key-password
配置 gRPC 服务器传输安全
gRPC 服务器是入站渠道,mongod 通过该渠道连接到 mongot 以进行搜索查询和索引管理命令。您可以使用 TLS 在传输层保护此渠道。要相互认证 mongod 和 mongot,请使用 mTLS。
您在 mongot 上使用 server.grpc.tls.mode 选项,并在 mongod 上使用 searchTLSMode 参数来配置 TLS 模式。当您未设置 searchTLSMode 时,mongod 会从 net.tls.mode 继承其连接到 mongot 的 TLS 模式,因为 searchTLSMode 默认采用 globalTLS。
您可以在 mongot 配置文件中配置以下其中一种 TLS 模式:
残疾人
无 TLS。仅对 mongod 和 mongot 在同一托管上运行的部署使用此模式:
server: grpc: address: "localhost:27028" tls: mode: "disabled"
TLS
mongot 提供服务器证书,mongod 在建立连接前对其进行验证。此模式需要 certificateKeyFile:
server: grpc: address: "0.0.0.0:27028" tls: mode: "tls" certificateKeyFile: /etc/mongot/tls/mongot-server.pem # certificateKeyFilePasswordFile: /etc/mongot/secrets/server-key-password
双向 TLS (mTLS)
mongot 和 mongod 都会呈现并验证对方的证书。此模式需要 certificateKeyFile 和 caFile。使用 mtls 时,还必须为 mongod 配置对应的 TLS 设置,以便它连接到 mongot:
server: grpc: address: "0.0.0.0:27028" tls: mode: "mtls" certificateKeyFile: /etc/mongot/tls/mongot-server.pem caFile: /etc/mongot/tls/ca.pem