Overview
在本指南中,您可以了解如何使用 MongoDB Enterprise Edition 中提供的身份验证机制对 MongoDB 进行身份验证。 连接到 MongoDB 时,可以使用身份验证机制在驱动程序和服务器之间建立信任。
Rust 驱动程序支持使用 LDAP (PLAIN)企业身份验证机制向轻量级目录访问协议 (LDAP) 服务器进行身份验证。
注意
GSSAPI/Kerberos 身份验证
驱动程序不支持 GSSAPI/Kerberos 身份验证机制,但您可以使用其他方法进行身份验证。 要了解有关这些方法的更多信息,请参阅Kerberos MongoDB Server手册中的 身份验证 。
要选择特定的身份验证机制,请在连接字符串的选项或 Credential
结构中指定该机制、您的档案和其他必要信息。
LDAP 身份验证 (PLAIN)
您可以使用目录服务器用户名和密码向轻量级目录访问协议 (LDAP) 服务器进行身份验证。
The name of the authentication mechanism is PLAIN
instead of LDAP because the mechanism uses the PLAIN Simple Authentication and Security Layer (SASL) defined in RFC-4616.
警告
例子
要指定PLAIN
身份验证机制,请将Credential
结构的mechanism
字段设置为AuthMechanism::Plain
。 此示例使用以下占位符指定身份验证机制:
username
:您的 LDAP 用户名password
:您的 LDAP 密码
let plain_cred = Credential::builder() .username("<username>".to_string()) .password("<password>".to_string()) .mechanism(AuthMechanism::Plain) .source("$external".to_string()) .build(); client_options.credential = Some(plain_cred); let client = Client::with_options(client_options)?;
注意
身份验证数据库
由于您的档案存储在 MongoDB 外部,因此您必须使用$external
数据库进行身份验证。 The source
field of the Credential
struct defaults to $external
, so you can omit this field.
或者,您可以通过将authMechanism
连接字符串选项的值设置为PLAIN
来使用连接字符串 URI 进行身份验证。 此示例演示如何使用以下占位符在连接字符串 URI 中指定PLAIN
身份验证机制:
username
:您的 LDAP 用户名password
:您的 LDAP 密码hostname
:你的 MongoDB Server 的网络地址
let uri = "mongodb://<username>:<password>@<hostname>/?authSource=$external&authMechanism=PLAIN";
MONGODB-OIDC
重要
MONGODB-OIDC 身份验证机制要求在 Linux 平台上运行 MongoDB Server v 7.0或更高版本。
Rust驾驶员支持对工作负载身份进行 OpenID Connect (OIDC) 身份身份验证。工作负载身份是分配给软件工作负载(例如应用程序、服务、脚本或容器)的身份,用于验证和访问权限其他服务和资源。
以下部分介绍如何使用 MONGODB-OIDC 身份验证机制对各种平台进行身份验证。
要学习;了解有关 MONGODB-OIDC身份验证机制的更多信息,请参阅服务器手册中的 OpenID Connect 身份验证和MongoDB Server参数。
Azure IMDS
If your application runs on an Azure VM, or otherwise uses the Azure Instance Metadata Service (IMDS), you can authenticate to MongoDB by using the Rust driver's built-in Azure support.
您可以通过将 Credential
结构的 mechanism
字段设置为 AuthMechanism::MongoDbOidc
来为Azure IMDS 配置 OIDC。 此示例使用以下占位符指定身份验证机制:
username
:如果使用的是 Azure 托管标识,请将其设置为托管标识的客户端 ID。 如果使用服务主体代表企业应用程序,请将其设置为服务主体的应用程序 ID。mechanism_properties
:将ENVIRONMENT
属性设置为azure
,并将TOKEN_RESOURCE
设置为MongoDB 部署上配置的受众参数的值。
以下代码示例展示了如何在创建Client
时设立这些选项:
let credential = Credential::builder() .username("<username>".to_owned()) .mechanism(AuthMechanism::MongoDbOidc) .mechanism_properties( doc! { "ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>" } ) .build(); client_options.credential = Some(credential); let client = Client::with_options(client_options)?; let res = client .database("test") .collection::<Document>("test") .find_one(doc! {}) .await?;
GCP IMDS
If your application runs on a Google Compute Engine VM, or otherwise uses the GCP Instance Metadata Service, you can authenticate to MongoDB by using the Rust driver's built-in GCP support.
您可以通过将 Credential
结构的 mechanism
字段设置为 AuthMechanism::MongoDbOidc
来为GCP IMDS 配置 OIDC。 然后,通过在 mechanism_properties
字段中设置以下值来指定身份验证机制:
ENVIRONMENT
:将此属性设置为gcp
。TOKEN_RESOURCE
:将此属性设置为MongoDB 部署上配置的受众参数值。
以下代码示例展示了如何在创建Client
时设立这些选项:
let credential = Credential::builder() .mechanism(AuthMechanism::MongoDbOidc) .mechanism_properties( doc! { "ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>" } ) .build(); client_options.credential = Some(credential); let client = Client::with_options(client_options)?; let res = client .database("test") .collection::<Document>("test") .find_one(doc! {}) .await?;
自定义回调
Rust驾驶员并不为所有平台提供内置支持,包括Amazon Web Services Elastic Kubernetes Service (EKS)。 要使用 OIDC 对不受支持的平台进行身份验证,您必须定义自定义回调函数。
以下代码是针对 EKS集群的自定义回调的示例实施。 首先,将 Credential
结构的 oidc_callback
字段设立为 oidc::Callback::machine
。 然后,从 AWS_WEB_IDENTITY_TOKEN_FILE
环境变量中设立的路径读取访问权限令牌。 最后,设立IdpServerResponse
结构的 access_token
字段的值。 (可选)设立expires
和 refresh_token
字段的值。
let credential = Credential::builder() .mechanism(AuthMechanism::MongoDbOidc) .oidc_callback(oidc::Callback::machine(move |_| { async move { let token_file_path = std::env::var("AWS_WEB_IDENTITY_TOKEN_FILE").map_err(mongodb::error::Error::custom)?; let access_token = tokio::fs::read_to_string(token_file_path).await?; Ok(IdpServerResponse::builder().access_token(access_token).build()) } .boxed() })) .build() .into(); client_options.credential = Some(credential); let client = Client::with_options(client_options)?; let res = client .database("test") .collection::<Document>("test") .find_one(doc! {}) .await?;
当员工身份身份验证进程涉及人机交互时,您必须通过将 Credential
结构的 oidc_callback
字段设置为 oidc::Callback::human
而不是 oidc::Callback::machine
来配置客户端。 Rust驾驶员在以下进程中使用回调:
驾驶员检索所提供的用户名的身份提供程序信息 (IDPInfo)。
该回调与 IDP 协商以获取
access_token
以及任何潜在的refresh_token
和超时值(如果已配置)。 回调会返回一个Result<IdpServerInfo>
。
The following example defines a custom callback to handle workforce identity. To customize this example for your use case, replace <human flow>
with your own custom flow. Refer to Authorization Code Flow with OIDC for more details.
let callback = Callback::human(move |context| { async move { "<human flow>"; todo!("human flow") } .boxed() }); let credential = Credential::builder() .mechanism(AuthMechanism::MongoDbOidc) .oidc_callback(callback) .build(); client_options.credential = Some(credential); let client = Client::with_options(client_options)?; let res = client .database("test") .collection::<Document>("test") .find_one(doc! {}) .await?;
更多信息
要了解有关本指南中概念的更多信息,请参阅以下文档:
MongoDB ServerLDAPMongoDB MongoDB Server手册中的 MongoDB Server 代理身份验证的支持
连接选项指南
手册中的 连接字符串MongoDB Server
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: