Skip to main content

ConfigLoader

Struct ConfigLoader 

Source
pub struct ConfigLoader<E: StringEncryptor = Encryptor> { /* private fields */ }
Expand description

Loads configuration files and transparently decrypts every ENC(...) value with the configured encryptor.

The loader is generic over the encryptor. ConfigLoader::new uses Encryptor (AES-256-GCM) like GoCrypt’s NewConfigLoader; use ConfigLoader::with_encryptor to read files shared with Java Jasypt:

use rustcrypt_jasypt::{ConfigLoader, JasyptEncryptor};

// AES-256-GCM (family-internal format)
let loader = ConfigLoader::new("password")?;

// PBEWithMD5AndDES, readable by Java Jasypt (e.g. a JasperReport service)
let loader = ConfigLoader::with_encryptor(JasyptEncryptor::new("password")?);

§File formats

  • load_env_file: KEY=VALUE lines. Blank lines and lines starting with # are skipped, keys and values are trimmed and surrounding single or double quotes are removed from the value.
  • load_yaml: the same line-based parser splitting on the first :. This intentionally mirrors GoCrypt’s simplified loader: it flattens nesting (a database: line yields an empty database entry and password: ENC(...) yields password). Use a real YAML crate plus decrypt_map for complex documents.
  • load_json: full JSON via serde. Every string inside objects and arrays, at any depth, that looks like ENC(...) is decrypted; strings that fail to decrypt are kept as-is.

Implementations§

Source§

impl ConfigLoader<Encryptor>

Source

pub fn new(password: &str) -> Result<Self>

Creates a loader backed by an Encryptor (AES-256-GCM) with default parameters. Returns Error::EmptyPassword for an empty password.

Source§

impl<E: StringEncryptor> ConfigLoader<E>

Source

pub fn with_encryptor(encryptor: E) -> Self

Creates a loader backed by any StringEncryptor, for example a JasyptEncryptor or an Encryptor with custom iterations.

Source

pub fn encryptor(&self) -> &E

The underlying encryptor.

Source

pub fn load_env_file( &self, path: impl AsRef<Path>, ) -> Result<HashMap<String, String>>

Loads a .env-style file and returns its key/value pairs with every ENC(...) value decrypted.

A value that fails to decrypt aborts with Error::KeyDecrypt naming the key. I/O problems are reported as Error::Io.

use rustcrypt_jasypt::ConfigLoader;

let loader = ConfigLoader::new(&std::env::var("RUSTCRYPT_PASSWORD").unwrap())?;
let config = loader.load_env_file(".env")?;
println!("{}", config["DATABASE_PASSWORD"]);
Source

pub fn load_yaml( &self, path: impl AsRef<Path>, ) -> Result<HashMap<String, String>>

Loads a simple flat YAML file (key: value lines) and returns its pairs with every ENC(...) value decrypted. See the type-level docs for the (GoCrypt-compatible) limitations of this parser.

Source

pub fn load_json<T: DeserializeOwned>( &self, path: impl AsRef<Path>, ) -> Result<T>

Loads a JSON file into T, decrypting every ENC(...) string at any depth first. Strings that fail to decrypt are left unchanged so the caller can surface them.

use rustcrypt_jasypt::ConfigLoader;
use serde::Deserialize;

#[derive(Deserialize)]
struct Database { host: String, password: String }

#[derive(Deserialize)]
struct Config { database: Database }

let loader = ConfigLoader::new("password")?;
let config: Config = loader.load_json("config.json")?;
println!("{}", config.database.password);
Source

pub fn decrypt_json_value(&self, value: &mut Value)

Recursively decrypts every ENC(...) string inside value (objects, arrays and nested combinations). Strings that fail to decrypt are left unchanged; non-string values are untouched.

Source

pub fn set_to_env(&self, path: impl AsRef<Path>) -> Result<()>

Loads a .env-style file with load_env_file and exports every pair as a process environment variable.

Call this during single-threaded start-up: the standard library’s set_var is not safe to run while other threads read the environment. Returns Error::InvalidParameter for a key that cannot be an environment variable name (empty, or containing = / NUL).

Trait Implementations§

Source§

impl<E: Clone + StringEncryptor> Clone for ConfigLoader<E>

Source§

fn clone(&self) -> Self

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<E: Debug + StringEncryptor> Debug for ConfigLoader<E>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> Forget for ConfigLoader<E>

§

impl<E> Freeze for ConfigLoader<E>
where E: Freeze,

§

impl<E> Move for ConfigLoader<E>

§

impl<E> RefUnwindSafe for ConfigLoader<E>
where E: RefUnwindSafe,

§

impl<E> Send for ConfigLoader<E>
where E: Send,

§

impl<E> Sync for ConfigLoader<E>
where E: Sync,

§

impl<E> Unpin for ConfigLoader<E>
where E: Unpin,

§

impl<E> UnsafeUnpin for ConfigLoader<E>
where E: UnsafeUnpin,

§

impl<E> UnwindSafe for ConfigLoader<E>
where E: UnwindSafe,

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, 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.