Crates.io | aws-secretsmanager-cache |
lib.rs | aws-secretsmanager-cache |
version | 0.5.0 |
source | src |
created_at | 2021-11-20 08:48:50.177249 |
updated_at | 2023-04-29 03:59:11.180741 |
description | A client for in-process caching of secrets from AWS Secrets manager |
homepage | https://github.com/adamjq/aws-secretsmanager-cache-rust |
repository | https://github.com/adamjq/aws-secretsmanager-cache-rust |
max_upload_size | |
id | 484853 |
size | 75,815 |
This crate provides a client for in-process caching of secrets from AWS Secrets Manager for Rust applications. It's heavily inspired by the AWS Secrets Manager Go Caching Client and the AWS SDK for Rust.
The client internally uses an LRU (least-recently used) caching scheme that provides O(1) insertions and O(1) lookups for cached values.
To use this client you must have:
The following sample demonstrates how to get started using the client:
use aws_sdk_secretsmanager::Client;
use aws_secretsmanager_cache::SecretCache;
#[tokio::main]
async fn main() {
// instantiate an AWS SecretsManager client using the AWS Rust SDK
let aws_config = aws_config::from_env().load().await;
let client = Client::new(&aws_config);
let mut cache = SecretCache::new(client);
match cache.get_secret_string("YOUR_SECRET_ID".to_string()).send().await {
Ok(secret_value) => {
// use secret value
}
// e.g. ResourceNotFoundException: Secrets Manager can't find the specified secret.
Err(e) => println!("ERROR: {}", e),
}
}
If a secret has been rotated since the last value was fetched and cached, and hasn't expired in the cache, it's necessary to force a cache refresh for the value by calling AWS and updating the value.
This can be done with force_refresh()
, for example:
match cache
.get_secret_string("YOUR_SECRET_ID".to_string())
.force_refresh()
.send()
.await
max_cache_size usize
The maximum number of secrets to maintain in the cache
before evicting the least frequently accessedcache_item_ttl u128
The number of nanoseconds a cached secret will be considered
valid before the secret value requires a refresh. Refreshing happens synchronously.use aws_sdk_secretsmanager::Client;
use aws_secretsmanager_cache::{CacheConfig, SecretCache};
use std::time;
#[tokio::main]
async fn main() {
let aws_config = aws_config::from_env().load().await;
let client = Client::new(&aws_config);
// cache configuration with 30 second expiry time and maximum 1000 secrets
let cache_config = CacheConfig::new()
.cache_item_ttl(time::Duration::from_secs(30).as_nanos())
.max_cache_size(1000);
let mut cache = SecretCache::new_with_config(client, cache_config);
}
Certain cloud environments like AWS Lambda encourage initializing clients in the global scope to avoid initialization for
each function invocation. This can be achieved using the lazy_static
crate, for example:
use async_once::AsyncOnce;
use aws_sdk_secretsmanager::Client;
use aws_secretsmanager_cache::SecretCache;
use lazy_static::lazy_static;
use std::sync::Mutex;
// store the cache in the global scope - useful for runtime environments like AWS Lambda
lazy_static! {
static ref CACHE: AsyncOnce<Mutex<SecretCache>> = AsyncOnce::new(async {
Mutex::new(SecretCache::new(Client::new(
&aws_config::from_env().load().await,
)))
});
}
#[tokio::main]
async fn main() {
// use cache
}
The project uses rustfmt and clippy for
formatting and linting. Follow the instructions to install rustfmt
and clippy
and run:
cargo fix
Run unit tests locally with:
cargo test
Licensed under the Apache License, Version 2.0 or the MIT license, at your option. Files in the project may not be copied, modified, or distributed except according to those terms.