Skip to main content

SecretsResolver

Struct SecretsResolver 

Source
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>

Source

pub fn new(provider: P, scope: impl Into<String>) -> Self

Create a new secrets resolver.

§Arguments
  • provider - The secrets provider to use for lookups
  • scope - The default scope (deployment name) for resolving secrets
Source

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.

Source

pub fn provider(&self) -> &P

Get a reference to the underlying provider.

Source

pub fn scope(&self) -> &str

Get the configured scope.

Source

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)
Source

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:

  1. Scanning all values to identify secret references
  2. Grouping references by scope
  3. Fetching secrets in batches per scope
  4. 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<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more