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=VALUElines. 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 (adatabase:line yields an emptydatabaseentry andpassword: ENC(...)yieldspassword). Use a real YAML crate plusdecrypt_mapfor complex documents.load_json: full JSON via serde. Every string inside objects and arrays, at any depth, that looks likeENC(...)is decrypted; strings that fail to decrypt are kept as-is.
Implementations§
Source§impl ConfigLoader<Encryptor>
impl ConfigLoader<Encryptor>
Source§impl<E: StringEncryptor> ConfigLoader<E>
impl<E: StringEncryptor> ConfigLoader<E>
Sourcepub fn with_encryptor(encryptor: E) -> Self
pub fn with_encryptor(encryptor: E) -> Self
Creates a loader backed by any StringEncryptor, for example a
JasyptEncryptor or an Encryptor with
custom iterations.
Sourcepub fn load_env_file(
&self,
path: impl AsRef<Path>,
) -> Result<HashMap<String, String>>
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"]);Sourcepub fn load_yaml(
&self,
path: impl AsRef<Path>,
) -> Result<HashMap<String, String>>
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.
Sourcepub fn load_json<T: DeserializeOwned>(
&self,
path: impl AsRef<Path>,
) -> Result<T>
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);Sourcepub fn decrypt_json_value(&self, value: &mut Value)
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.
Sourcepub fn set_to_env(&self, path: impl AsRef<Path>) -> Result<()>
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).