Skip to content

Feedback! #14

@huntc

Description

@huntc

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions