keyring-rs

repository·main·Indexed 20 days ago

https://github.com/open-source-cooperative/keyring-rs

A Rust ecosystem for managing secrets in native OS secure stores across Mac, Windows, and *nix. It provides a high-level wrapper via the `keyring` crate (v1 API) for simple secret management and a modular core via `keyring-core` for advanced custom credential store integrations. Supported stores include keychain, windows, secret-service, keyutils, and others. Also provides a Python wrapper via the `rust-native-keyring` project and a `keyring-cli` for manual testing.

Tokens
2.4K
Snippets
10
Records
16
Agent score
73%

What's inside keyring-rs

  1. How to build advanced applications with the Keyring ecosystem

    main

    If you are building an application that requires full control over specific credential stores, do not link directly against the keyring crate using the cli feature, as this will include unnecessary dependencies.

    Instead, follow this pattern:

    1. Take a dependency on the keyring-core crate.
    2. Add the specific credential store crates you want to use as dependencies.
    3. Use the sample code found in the cli module of the keyring crate as a reference for instantiating and accessing your desired stores.
  2. Use the keyring crate for simple secret management

    main

    To provide your application with the ability to set, get, and delete plain-text and binary secrets in native secure stores (Mac, Windows, and *nix), link to the keyring crate using the default v1 feature. This provides a simple wrapper around the native OS credential stores using the original 'v1' API.

    /* Add to your Cargo.toml to use the default v1 API */
    [dependencies]
    keyring = "4.1.6"
  3. Install and use the Python keyring wrapper

    main

    For scripting keyring commands in Python, use the rust-native-keyring project on PyPI instead of the Rust CLI. This is more efficient for automation as it avoids the overhead of reloading stores for every single command.

    Install via pip:

    pip install rust-native-keyring

    Use in Python:

    import rust_native_keyring
  4. Use the v1 API for platform-independent secret management

    main
    When the v1 feature is enabled, the library provides a simplified, platform-independent interface for managing secrets (passwords) across macOS, Windows, and *nix. This mode is intended for applications that simply need to read or write specific secrets without managing the underlying credential stores manually.
  5. Choose between v1 or cli features

    main

    The keyring-rs library operates in one of two primary modes determined by enabled Cargo features. You must enable at least one of v1 or cli to compile the library.

    • v1 feature: Provides platform-independent setting and reading of passwords/secrets on macOS, Windows, and *nix platforms. This is the standard mode for simple secret management.
    • cli feature: Provides the glue for the Rust CLI example app, the rust-native-keyring Python module, and the keyring-demo cross-platform application to access all available credential stores on all platforms.

    Important Note for Developers: If you are building an application that needs fine-grained control over which credential stores are used, or if you want to avoid the heavy dependency tree introduced by the cli feature, do not link to this library. Instead, link directly to keyring-core and implement the specific stores you need. You can copy/paste useful implementation patterns from the cli module of this crate into your own project.

  6. Set the default credential store by name

    main

    Use use_named_store(name: &str) to set the default credential store using its string identifier. If the name is "sample", it defaults to a persistent sample store. For all other names, it uses default configurations. This function returns an Error::Invalid if the name is not recognized.

    Available store names include:

    • android
    • keychain
    • keyutils
    • protected
    • sample
    • secret-service
    • dbus-secret-service
    • sqlite
    • windows
    use_named_store("keychain");
  7. Manage passwords and secrets with Entry

    main

    The Entry struct provides methods to manipulate data within the credential store:

    MethodDescription
    set_password(&self, password: &str) -> Result<()>Sets a text password for the entry.
    get_password(&self) -> Result<String>Retrieves the text password for the entry.
    set_secret(&self, secret: &[u8]) -> Result<()>Sets a binary secret for the entry.
    get_secret(&self) -> Result<Vec<u8>>Retrieves the binary secret for the entry.
    delete_credential(&self) -> Result<()>Deletes the credential associated with this entry.
  8. Use the platform's native credential store

    main

    Use use_native_store(prefer_secret_service: bool) to automatically select the most appropriate OS-provided credential store for the current platform.

    Platform Behavior:

    • Android: Uses android store.
    • macOS: Uses keychain store.
    • Windows: Uses windows store.
    • Linux: Uses secret-service if prefer_secret_service is true; otherwise, uses keyutils.
    • FreeBSD/OpenBSD: Uses secret-service store.
    • Other: Falls back to the sample store.

    If no native store is available, it defaults to the sample store.

    use_native_store(true).unwrap();
  9. Manage and inspect the default credential store

    main

    The following functions allow you to manage the lifecycle and inspect the state of the global default store:

    • release_store(): Unsets the current default store.
    • store_info() -> String: Returns a debug description of the currently active default store. Returns "None" if no store is set.
    • use_sample_store(config: &HashMap<&str, &str>): Sets the default store to the keyring-core::Sample store, which is available on all platforms.
  10. Use the Entry API to manage secrets

    main

    The Entry type provides a high-level interface for managing text or binary secrets in the platform's native credential store. To use this API, you must enable the v1 feature in your Cargo.toml.

    Supported platforms and stores:

    • macOS: Keychain Services
    • Windows: Windows Credential Manager
    • *nix: Secret Service

    Common operations include creating a new entry, setting/getting passwords (strings), setting/getting secrets (bytes), and deleting credentials.

    use keyring::v1::Entry;
    
    // Create a new entry
    let entry = Entry::new("my_service", "my_username")?;
    
    // Manage a text password
    entry.set_password("my_password")?;
    let password = entry.get_password()?;
    
    // Manage a binary secret
    entry.set_secret(b"binary_data")?;
    let secret = entry.get_secret()?;
    
    // Delete the credential
    entry.delete_credential()?;