-
Notifications
You must be signed in to change notification settings - Fork 3
/
auto_refresh_secret_manager_vault.rs
49 lines (39 loc) · 1.55 KB
/
auto_refresh_secret_manager_vault.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
48
49
use secret_vault::*;
use std::sync::Arc;
use std::time::Duration;
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("secret_vault=trace")
.finish();
tracing::subscriber::set_global_default(subscriber)?;
// Describing secrets and marking them non-required
let secret1 = SecretVaultRef::new("test-secret-xRnpry".into())
.with_required(false)
.with_secret_version("AWSCURRENT".into())
.with_auto_refresh(true);
// Building the vault
let vault = Arc::new(
SecretVaultBuilder::with_source(
aws::AwsSecretManagerSource::new(&config_env_var("ACCOUNT_ID")?).await?,
)
.with_secret_refs(vec![&secret1])
.build()?,
);
// Refresh the secrets first to make sure they loaded first time
vault.refresh().await?;
let mut vault_refresher = SecretVaultAutoRefresher::new(
vault,
SecretVaultAutoRefresherOptions::new(
Duration::from_secs(5), // refresh every 5 seconds, please use appropriate (and usually bigger) interval, this is only for example not to wait long
),
);
vault_refresher.start().await?;
// You supposed to wait signals, etc, but for the sake of example here it is just sleep
tokio::time::sleep(Duration::from_secs(10)).await;
vault_refresher.shutdown().await?;
Ok(())
}