Skip to main content

Crate figment_keyring

Crate figment_keyring 

Source
Expand description

Figment Provider for Keyring Integration

This crate provides a Figment2 provider that fetches secrets from system keyrings (macOS Keychain, Windows Credential Manager, Linux Secret Service).

§Quick Start

use figment2::Figment;
use figment_keyring::KeyringProvider;

// Create a Figment with your configuration sources
let config_figment = Figment::new();

// Provider is configured by that Figment (late binding)
let api_key_provider = KeyringProvider::configured_by(config_figment, "api_key");

// Final Figment merges everything
// let config: YourConfig = Figment::new()
//     .merge(config_figment)
//     .merge(api_key_provider)
//     .extract().unwrap();

§Configuration

The provider is configured via a KeyringConfig which can come from any Figment source (file, environment, etc.):

# config.toml
service = "myapp"
keyrings = ["user", "team-secrets"]
optional = false

§Custom Configuration Keys

If you have existing configuration with custom field names, implement the KeyringKeyMapping trait to specify which keys to extract:

use figment2::{Figment, providers::Serialized};
use figment_keyring::{KeyringProvider, KeyringKeyMapping, KeyringConfig, Keyring};

struct MyKeyMapping;

impl KeyringKeyMapping for MyKeyMapping {
    fn service_key(&self) -> &str { "app_name" }
    fn keyrings_key(&self) -> &str { "stores" }
    fn optional_key(&self) -> &str { "allow_missing" }
}

let config_figment = Figment::from(Serialized::defaults(KeyringConfig {
    service: "myapp".to_string(),
    keyrings: vec![Keyring::User],
    optional: true,
}));
let mapping = MyKeyMapping;
let provider = KeyringProvider::configured_by_with_mapping(config_figment, &mapping, "api_key");

This allows your configuration to use custom field names:

# config.toml
app_name = "myapp"
stores = ["user", "system"]
allow_missing = false

The trait provides full flexibility:

  • Use any field names
  • Support nested key paths (e.g., "secrets.service")
  • Customize each field independently

Re-exports§

pub use error::KeyringError;
pub use keyring_config::Keyring;
pub use keyring_config::KeyringConfig;

Modules§

error
keyring_config

Structs§

KeyringProvider
Provider that fetches secrets from system keyrings.

Traits§

KeyringKeyMapping
Trait for specifying which keys to extract from Figment.