4个版本 (重大更新)
0.4.0 | 2024年7月12日 |
---|---|
0.3.0 | 2024年6月27日 |
0.2.0 | 2024年5月25日 |
0.1.0 | 2024年5月7日 |
在Web编程中排名第2269
每月下载量61次
8MB
139K SLoC
google-cloud-kms
Google Cloud Platform密钥管理系统客户端库。
安装
[dependencies]
google-cloud-kms = "version"
身份验证
有两种方式创建与Google Cloud进行身份验证的客户端。
自动
with_auth()
函数将尝试从环境变量 GOOGLE_APPLICATION_CREDENTIALS
、GOOGLE_APPLICATION_CREDENTIALS_JSON
或元数据服务器中读取凭据。
这也在 google-cloud-auth 中进行了描述。
use google_cloud_kms::client::{Client, ClientConfig};
async fn run() {
let config = ClientConfig::default().with_auth().await.unwrap();
let client = Client::new(config);
}
手动
当您不能使用 gcloud
身份验证但您有其他方式获取凭据(例如不同的环境变量)时,您可以解析自己的 'credentials-file' 版本并像那样使用它。
use google_cloud_auth::credentials::CredentialsFile;
// or google_cloud_kms::client::google_cloud_auth::credentials::CredentialsFile
use google_cloud_kms::client::{Client, ClientConfig};
async fn run(cred: CredentialsFile) {
let config = ClientConfig::default().with_credentials(cred).await.unwrap();
let client = Client::new(config);
}
用法
密钥环操作
use std::collections::HashMap;
use prost_types::FieldMask;
use google_cloud_googleapis::cloud::kms::v1::{CreateKeyRingRequest, GetKeyRingRequest, ListKeyRingsRequest};
use google_cloud_kms::client::{Client, ClientConfig};
async fn run(config: ClientConfig) {
// Create client
let client = Client::new(config).await.unwrap();
// Key ring
// create
match client
.create_key_ring(
CreateKeyRingRequest {
parent: "projects/qovery-gcp-tests/locations/europe-west9".to_string(),
key_ring_id: "123-456".to_string(),
key_ring: None,
},
None,
)
.await
{
Ok(mut r) => println!("Created key ring {:?}", r),
Err(err) => panic!("err: {:?}", err),
};
// list
match client
.list_key_rings(
ListKeyRingsRequest {
parent: "projects/qovery-gcp-tests/locations/europe-west9".to_string(),
page_size: 5,
page_token: "".to_string(),
filter: "".to_string(),
order_by: "".to_string(),
},
None,
)
.await
{
Ok(response) => {
println!("List key rings");
for r in response.key_rings {
println!("- {:?}", r);
}
}
Err(err) => panic!("err: {:?}", err),
};
// get
match client
.get_key_ring(
GetKeyRingRequest {
name: "projects/qovery-gcp-tests/locations/europe-west9/keyRings/key-ring-for-documentation"
.to_string(),
},
None,
)
.await
{
Ok(response) => {
println!("Get keyring: {:?}", response);
}
Err(err) => panic!("err: {:?}", err),
}
}
Ethereum集成
启用'eth'功能。
[dependencies]
google-cloud-kms = { version="version", features=["eth"] }
use ethers::prelude::SignerMiddleware;
use ethers::providers::{Http, Middleware, Provider};
use ethers_core::types::{TransactionReceipt, TransactionRequest};
use ethers_signers::Signer as EthSigner;
use google_cloud_kms::client::Client;
use google_cloud_kms::signer::ethereum::{Error, Signer};
pub async fn send_bnb(client: Client, key_name: &str, rpc_node: &str) {
// BSC testnet
let chain_id = 97;
let signer = Signer::new(client, key_name, chain_id, None).await.unwrap();
let provider = Provider::<Http>::try_from(rpc_node).unwrap();
let signer_address = signer.address();
let eth_client = SignerMiddleware::new_with_provider_chain(provider, signer).await.unwrap();
let tx = TransactionRequest::new()
.to(signer_address)
.value(100_000_000_000_000_u128)
.gas(1_500_000_u64)
.gas_price(4_000_000_000_u64)
.chain_id(chain_id);
let res = eth_client.send_transaction(tx, None).await.unwrap();
let receipt: TransactionReceipt = res.confirmations(3).await.unwrap().unwrap();
}
依赖关系
~14–31MB
~572K SLoC