本文档介绍了如何在 Rust 中切换默认的 rustls 加密提供程序。如果您的应用有特定要求,而我们的默认加密提供程序无法满足这些要求,您可能需要切换默认加密提供程序。
Rust 客户端库使用 Rustls 来确保应用与 Google Cloud之间的通信安全。Rustls 可以使用不同的提供程序来实现加密功能。默认情况下,客户端库会启用 aws-lc-rs 提供程序。客户端库默认使用此提供程序,因为它基于 BoringSSL(Google 实现的备受好评的加密库),并且是唯一经过 FIPS 认证的 Rust 加密提供程序。
不过,某些应用可能需要使用其他提供程序。本指南将向您展示如何停用默认提供程序并配置您自己的提供程序,并使用 google-cloud-storage 来说明如何更改 Rust 的要求,同时使用 ring 作为替代加密提供程序。此流程可针对其他客户端库进行自定义。如需了解其他提供程序,请参阅 rustls 文档。
将客户端库与默认加密提供程序搭配使用
如需使用默认加密提供程序,请将客户端库作为依赖项添加到您的 Cargo.toml 文件中:
[dependencies]
google-cloud-storage = { version = "1" }
使用默认加密提供程序,同时停用其他默认功能
有些应用倾向于停用依赖项中的所有默认功能,然后仅启用它们想要使用的功能。例如,您的 Cargo.toml 可能如下所示:
[dependencies]
google-cloud-storage = { version = "1", default-features = false }
如需仅启用默认提供商功能,请使用:
[dependencies]
google-cloud-storage = { version = "1", default-features = false, features = ["default-rustls-provider"] }
使用自己的加密提供程序
如需选择自己的提供商,请执行以下操作:
添加 Rust,但停用默认功能,并添加提供程序所需的任何依赖项。在本指南中,我们使用
ring:您必须使用与 `google-cloud-storage` 相同的 `rustls` 版本。[dependencies] google-cloud-storage = { version = "1", default-features = false } rustls = { version = "0.23", features = ["ring"] }将
main()函数更改为安装此提供程序:use rustls::crypto::{CryptoProvider, ring::default_provider}; #[tokio::main] async fn main() -> anyhow::Result<()> { // Install a default crypto provider. CryptoProvider::install_default(default_provider()) .map_err(|_| anyhow::anyhow!("default crypto provider already installed"))?; // ... ... ... Ok(()) }