| Crates.io | aws_utils_secretsmanager |
| lib.rs | aws_utils_secretsmanager |
| version | 0.3.0 |
| created_at | 2025-07-23 23:15:50.061803+00 |
| updated_at | 2025-09-16 23:46:02.837648+00 |
| description | AWS Secrets Manager utilities for retrieving secret values |
| homepage | https://github.com/UniqueVision/utilities.aws-utils |
| repository | https://github.com/UniqueVision/utilities.aws-utils |
| max_upload_size | |
| id | 1765361 |
| size | 74,894 |
AWS Secrets Manager utilities for retrieving secret values from AWS Secrets Manager.
Add this to your Cargo.toml:
[dependencies]
aws_utils_secretsmanager = "0.1.0"
use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create Secrets Manager client with default timeout configuration
let client = make_client_with_timeout_default(None).await;
// Get secret value
let secret = get_secret_value(&client, "my-secret-name").await?;
println!("Secret value: {}", secret);
Ok(())
}
use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create client with custom endpoint (e.g., LocalStack)
let client = make_client_with_timeout_default(Some("http://localhost:4566".to_string())).await;
let secret = get_secret_value(&client, "test-secret").await?;
println!("Secret value: {}", secret);
Ok(())
}
use std::time::Duration;
use aws_utils_secretsmanager::{make_client_with_timeout, secretsmanager::get_secret_value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create client with custom timeout settings
let client = make_client_with_timeout(
None,
Some(Duration::from_secs(5)), // 5 second connect timeout
Some(Duration::from_secs(30)), // 30 second operation timeout
Some(Duration::from_secs(25)), // 25 second operation attempt timeout
Some(Duration::from_secs(20)), // 20 second read timeout
).await;
let secret = get_secret_value(&client, "test-secret").await?;
println!("Secret value: {}", secret);
Ok(())
}
use aws_config::timeout::{TimeoutConfig, TimeoutConfigBuilder};
use aws_utils_secretsmanager::{make_client, secretsmanager::get_secret_value};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Build custom timeout configuration
let timeout_config = TimeoutConfigBuilder::new()
.connect_timeout(Duration::from_secs(10))
.operation_timeout(Duration::from_secs(120))
.build();
// Create client with custom timeout configuration
let client = make_client(None, Some(timeout_config)).await;
let secret = get_secret_value(&client, "long-running-secret").await?;
println!("Secret value: {}", secret);
Ok(())
}
use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value_raw};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = make_client_with_timeout_default(None).await;
// Get specific version of secret
let output = get_secret_value_raw(
&client,
Some("my-secret-name"),
Some("version-uuid"), // Version ID
Some("AWSCURRENT") // Version stage
).await?;
if let Some(secret_string) = output.secret_string() {
println!("Secret: {}", secret_string);
}
if let Some(version_id) = output.version_id() {
println!("Version ID: {}", version_id);
}
Ok(())
}
use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value_raw};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = make_client_with_timeout_default(None).await;
// Get current version (default behavior)
let output = get_secret_value_raw(
&client,
Some("my-secret-name"),
None::<String>, // No specific version ID
Some("AWSCURRENT") // Get current version
).await?;
println!("Current secret: {:?}", output.secret_string());
Ok(())
}
make_client_with_timeout_default(endpoint_url: Option<String>) -> ClientCreates an AWS Secrets Manager client with default timeout configuration.
endpoint_url: Optional custom endpoint URL for testing (e.g., LocalStack)make_client_with_timeout(endpoint_url: Option<String>, connect_timeout: Option<Duration>, operation_timeout: Option<Duration>, operation_attempt_timeout: Option<Duration>, read_timeout: Option<Duration>) -> ClientCreates an AWS Secrets Manager client with custom timeout configuration.
endpoint_url: Optional custom endpoint URL for testing (e.g., LocalStack)connect_timeout: Optional timeout for establishing connectionsoperation_timeout: Optional timeout for entire operationsoperation_attempt_timeout: Optional timeout for individual operation attemptsread_timeout: Optional timeout for reading responsesmake_client(endpoint_url: Option<String>, timeout_config: Option<TimeoutConfig>) -> ClientCreates an AWS Secrets Manager client with optional custom endpoint URL and timeout configuration.
endpoint_url: Optional custom endpoint URL for testing (e.g., LocalStack)timeout_config: Optional timeout configurationget_secret_value(client: &Client, secret_id: &str) -> Result<String, Error>Retrieves a secret value as a string from the current version.
client: AWS Secrets Manager clientsecret_id: Secret identifier (name or ARN)get_secret_value_raw(client: &Client, secret_id: Option<impl Into<String>>, version_id: Option<impl Into<String>>, version_stage: Option<impl Into<String>>) -> Result<GetSecretValueOutput, Error>Retrieves raw secret output from AWS Secrets Manager with version control.
client: AWS Secrets Manager clientsecret_id: Optional secret identifier (name or ARN)version_id: Optional version UUID to retrieve specific versionversion_stage: Optional version stage (e.g., "AWSCURRENT", "AWSPENDING")The crate defines custom error types:
Error::BuildError: AWS SDK build errorsError::AwsSdk: AWS SDK service errorsError::ValidationError: Validation errorsError::NotFound: Secret not foundAWS Secrets Manager supports versioning of secrets. You can:
"AWSCURRENT" stage"AWSPENDING" stageAWSCURRENT: The current version of the secretAWSPENDING: The version that will become current after rotation completesSet up your test environment:
# Optional: Custom Secrets Manager endpoint (e.g., LocalStack)
export SECRETSMANAGER_ENDPOINT_URL=http://localhost:4566
# Run tests
cargo test
# Run all tests
cargo test
# Run with logging
RUST_LOG=info cargo test -- --nocapture
# Run specific test
cargo test test_get_secret_value -- --nocapture
The client uses the AWS SDK's default credential chain for authentication:
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION)use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value};
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = make_client_with_timeout_default(None).await;
let secret_json = get_secret_value(&client, "prod/db/credentials").await?;
let credentials: Value = serde_json::from_str(&secret_json)?;
let username = credentials["username"].as_str().unwrap();
let password = credentials["password"].as_str().unwrap();
// Use credentials to connect to database
println!("Connecting as user: {}", username);
Ok(())
}
use aws_utils_secretsmanager::{make_client_with_timeout_default, secretsmanager::get_secret_value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = make_client_with_timeout_default(None).await;
let api_key = get_secret_value(&client, "prod/external-api/key").await?;
// Use API key for external service calls
println!("API Key retrieved successfully");
Ok(())
}
MIT