#env-var #secret #gpg #env #dev #development

build read-secret

A rust library that provides an easy way to read and decrypt secrets from your environment variables and files

3 releases

0.1.2 Jun 27, 2023
0.1.1 Jun 27, 2023
0.1.0 Jun 26, 2023

#796 in Build Utils

Download history 11/week @ 2024-07-22 4/week @ 2024-08-05 4/week @ 2024-08-12 15/week @ 2024-09-23 15/week @ 2024-09-30 18/week @ 2024-10-07 17/week @ 2024-10-14 16/week @ 2024-10-21 19/week @ 2024-10-28

71 downloads per month

MIT/Apache

9KB
129 lines

Read Secret

docs

A rust library that provides an easy way to read and decrypt secrets from your environment variables and files.

Code is hosted on sourcehut, and it is recommended to open issues/send patches on this platform. However, GitHub is also acceptable.

Usage

Run command cargo add read-secret or put the following code into your cargo.toml

[dependencies]
read-secret = <version>

Example

This example code is in file examples/e1.es, and you can run it by executing cargo run --example e1

use read_secret::{DecryptMethod, SecretType};
use std::process::Command;

fn main() {
    // read secret from environment variable.
    let mut dm = DecryptMethod::None;
    let st = SecretType::Env("XDG_SESSION_TYPE".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("wayland", sr);

    // read secret from file and decrypt it using gnupg.
    let mut dm = DecryptMethod::GPG;
    let st = SecretType::File("examples/pass_gpg.asc".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("El Psy Kongaroo", sr);

    // store encrypted secret inside string and decrypt it using custom command.
    let mut temp = Command::new("wc"); // avoid E0716 -- temporary value is being dropped
    let custom_command = temp.args(["-c"]);
    let mut dm = DecryptMethod::Custom(custom_command);
    let st = SecretType::String("El Psy Kongaroo".to_string());
    let sr = read_secret::read_secret(st, &mut dm).unwrap();
    assert_eq!("15\n", sr);
}

No runtime deps