Skip to main content

SecretStore

Struct SecretStore 

Source
pub struct SecretStore;
Expand description

Cross-platform secret store backed by the host OS keychain.

Stateless by design — it holds no cached secrets. Every call round-trips to the OS. That makes concurrent usage safe and avoids any in-process leak surface beyond the immediate call’s return value.

Implementations§

Source§

impl SecretStore

Source

pub fn new() -> Self

Source

pub fn put(&self, r: &SecretRef, value: &str) -> Result<(), SecretError>

Store a UTF-8 secret under (service, key). Replaces any existing value at the same ref.

On macOS, writes via /usr/bin/security add-generic-password -U -A so the resulting item has a permissive ACL — readable by any binary the user runs. This is necessary because the legacy keychain’s default ACL binds an item to the calling binary’s code-signing hash, which changes on every cargo rebuild and silently revokes read access from later versions of the same CLI tool. (/usr/bin/security is Apple-signed with full keychain entitlements — the same path reads, status checks, and deletes use, and the same path users invoke manually.)

Trade-off: the value transits argv during the spawn (visible to ps from the same user for ~milliseconds). Acceptable for the “single-user developer machine” threat model; any process that can see argv on this machine can also read the keychain directly via security. On other platforms, behavior is unchanged (keyring crate’s native backend).

Source

pub fn put_json<T: Serialize>( &self, r: &SecretRef, value: &T, ) -> Result<(), SecretError>

Store a structured value serialized as JSON.

Source

pub fn get(&self, r: &SecretRef) -> Result<String, SecretError>

Read a UTF-8 secret. Returns NotFound if no entry exists.

On macOS, reads through /usr/bin/security first so repeated helper rebuilds do not churn Keychain prompts against each binary’s CDHash. Backend/authorization failures are returned directly instead of falling back to an in-process read path that can trigger a second prompt.

Source

pub fn get_json<T: for<'de> Deserialize<'de>>( &self, r: &SecretRef, ) -> Result<T, SecretError>

Read a structured value previously stored via put_json.

Source

pub fn delete(&self, r: &SecretRef) -> Result<(), SecretError>

Delete an entry. Returns Ok even if the entry didn’t exist — idempotent from the caller’s perspective.

On macOS, deletes through /usr/bin/security first so the Apple-signed helper, not the rebuilt caller binary, owns Keychain authorization.

Source

pub fn status(&self, r: &SecretRef) -> Result<SecretStatus, SecretError>

Existence check without returning the value. Safe to log.

On macOS, checks status through /usr/bin/security first for the same CDHash-stable authorization behavior as get.

Source

pub fn is_available(&self) -> bool

Probe whether the OS secret store is reachable.

Opens an Entry for an internal-only sentinel and attempts to read it. Returns true iff the backend responds with either a value or NoEntry — both mean the store is reachable; PlatformFailure / NoStorageAccess mean it isn’t.

§Side effects
  • On macOS with a locked keychain, this may trigger a user unlock prompt. Call only when the caller is ready to handle that UX.
  • On Linux it opens a DBus connection to Secret Service.
  • Performance: one round-trip to the OS store. Not cached.
Source

pub fn availability(&self) -> AvailabilityCheck

Detailed availability probe. Same round-trip as is_available, but distinguishes “no backend at all” from a specific platform failure so the FFI surface can emit a reason matching the pattern used by the other v0.4 capability probes (accountsList, calendarList, etc.).

Trait Implementations§

Source§

impl Clone for SecretStore

Source§

fn clone(&self) -> SecretStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SecretStore

Source§

impl Debug for SecretStore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SecretStore

Source§

fn default() -> SecretStore

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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