-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcp_kms.rs
47 lines (36 loc) · 1.46 KB
/
gcp_kms.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use kms_aead::*;
use secret_vault_value::SecretValue;
pub fn config_env_var(name: &str) -> Result<String, String> {
std::env::var(name).map_err(|e| format!("{}: {}", name, e))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let subscriber = tracing_subscriber::fmt()
.with_env_filter("kms_aead=debug")
.finish();
tracing::subscriber::set_global_default(subscriber)?;
let google_project_id = config_env_var("PROJECT_ID")?;
let google_project_location = config_env_var("PROJECT_LOCATION")?;
let google_kms_key_ring = config_env_var("KMS_KEY_RING")?;
let google_kms_key = config_env_var("KMS_KEY")?;
let kms_ref = kms_aead::providers::GcpKmsKeyRef::new(
google_project_id,
google_project_location,
google_kms_key_ring,
google_kms_key,
);
let encryption = kms_aead::KmsAeadRingEnvelopeEncryption::new(
kms_aead::providers::GcpKmsProvider::new(&kms_ref).await?,
)
.await?;
let secret_value = SecretValue::from("test-secret");
let test_aad = "test-aad".to_string();
let cipher_text = encryption.encrypt_value(&test_aad, &secret_value).await?;
println!("Encrypted to {:?}", cipher_text.to_hex_string());
let secret_value = encryption.decrypt_value(&test_aad, &cipher_text).await?;
println!(
"We have our secret back: {}",
secret_value.sensitive_value_to_str().unwrap() == "test-secret"
);
Ok(())
}