Skip to content

Feedback! #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
huntc opened this issue May 26, 2022 · 1 comment
Open

Feedback! #14

huntc opened this issue May 26, 2022 · 1 comment

Comments

@huntc
Copy link

huntc commented May 26, 2022

Thanks for providing this library!

I couldn't see how to feedback on my experience, but I just wanted to convey that using the library has been very pleasant. Here's a code snippet of how I set up my cache to read secrets from Hashicorp's Vault:

let cache: TtlCache = LoadingCache::with_meta_loader(
    TtlCacheBacking::with_backing(
        *unauthorized_timeout,
        LruCacheBacking::new(max_secrets_cached),
    ),
    move |secret_path| {
        let task_state = Arc::clone(&state);
        async move {
            let result = task_state
                .client
                .get(
                    task_state
                        .server
                        .join(&format!(
                            "{}v1/secret/data/{}",
                            task_state.server, secret_path
                        ))
                        .unwrap(),
                )
                .bearer_auth(&task_state.client_token)
                .send()
                .await;
            if let Ok(response) = result {
                let secret_reply = if response.status().is_success() {
                    response.json::<GetSecretReply>().await.ok()
                } else {
                    None
                };
                let lease_duration = secret_reply.as_ref().map(|sr| {
                    let mut lease_duration = None;
                    if let Some(ttl_field) = task_state.ttl_field.as_ref() {
                        if let Some(ttl) = sr.data.data.get(ttl_field) {
                            if let Ok(ttl_duration) = ttl.parse::<humantime::Duration>() {
                                lease_duration = Some(ttl_duration.into());
                            }
                        }
                    }
                    lease_duration.unwrap_or_else(|| Duration::from_secs(sr.lease_duration))
                });
                Ok(secret_reply).with_meta(lease_duration.map(TtlMeta::from))
            } else {
                Err(Error::Unavailable)
            }
        }
    },
);

Feel free to use this as a more (incomplete) complex example if that helps. Thanks once again!

@ByteAlex
Copy link
Owner

ByteAlex commented Sep 9, 2022

Hey! Sorry - Somehow this notification got lost in my inbox, - just some PR popped in and I saw this. Thank you for providing this example, I will see if I can link it somewhere!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants