pub struct SecretsResolver<P: SecretsProvider> { /* private fields */ }Expand description
Resolver for secret references in configuration values.
The resolver parses $S: and $secret:// prefixed values and replaces
them with actual secret values from the underlying provider. This enables
declarative secret references in deployment configurations.
§Syntax
$S:name- Deployment-level secret$S:@service/name- Service-level secret$S:name/field- Field extraction from structured (JSON) secrets$secret://<env>/<KEY>- Environment-scoped secret lookup$secret://<env>/<KEY>/<field>- With JSON field extraction
The $secret:// form requires an EnvScopeProvider to be wired up via
SecretsResolver::with_env_resolver; otherwise such references fail with
a clear error.
§Example
use zlayer_secrets::{SecretsResolver, PersistentSecretsStore};
use std::collections::HashMap;
async fn resolve_env_vars(
store: PersistentSecretsStore,
env: HashMap<String, String>,
) -> Result<HashMap<String, String>> {
let resolver = SecretsResolver::new(store, "my-deployment");
resolver.resolve_env(&env).await
}Implementations§
Source§impl<P: SecretsProvider> SecretsResolver<P>
impl<P: SecretsProvider> SecretsResolver<P>
Sourcepub fn new(provider: P, scope: impl Into<String>) -> Self
pub fn new(provider: P, scope: impl Into<String>) -> Self
Create a new secrets resolver.
§Arguments
provider- The secrets provider to use for lookupsscope- The default scope (deployment name) for resolving secrets
Sourcepub fn with_env_resolver(self, env_resolver: Arc<dyn EnvScopeProvider>) -> Self
pub fn with_env_resolver(self, env_resolver: Arc<dyn EnvScopeProvider>) -> Self
Attach an EnvScopeProvider to enable resolution of
$secret://<env>/<KEY> references.
Without this, any $secret:// reference will fail with a clear error.
Sourcepub async fn resolve_value(&self, value: &str) -> Result<String>
pub async fn resolve_value(&self, value: &str) -> Result<String>
Resolve a single value that may contain a secret reference.
If the value starts with $S:, it will be parsed and replaced with
the actual secret value. Otherwise, the original value is returned.
§Arguments
value- The value to potentially resolve
§Errors
Returns an error if the value is a secret reference but:
- The reference syntax is invalid
- The secret doesn’t exist
- Field extraction fails (for JSON secrets)
Sourcepub async fn resolve_env(
&self,
env: &HashMap<String, String>,
) -> Result<HashMap<String, String>>
pub async fn resolve_env( &self, env: &HashMap<String, String>, ) -> Result<HashMap<String, String>>
Resolve all secret references in a map of environment variables.
This method efficiently batches secret lookups by:
- Scanning all values to identify secret references
- Grouping references by scope
- Fetching secrets in batches per scope
- Resolving all values with the fetched secrets
§Arguments
env- Map of environment variable names to values
§Returns
A new map with all secret references replaced by their actual values. Non-secret values are passed through unchanged.
§Errors
Returns an error if any secret reference is invalid, or if a referenced secret cannot be found.
Auto Trait Implementations§
impl<P> !RefUnwindSafe for SecretsResolver<P>
impl<P> !UnwindSafe for SecretsResolver<P>
impl<P> Freeze for SecretsResolver<P>where
P: Freeze,
impl<P> Send for SecretsResolver<P>
impl<P> Sync for SecretsResolver<P>
impl<P> Unpin for SecretsResolver<P>where
P: Unpin,
impl<P> UnsafeUnpin for SecretsResolver<P>where
P: UnsafeUnpin,
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more