-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Support loading secrets from directories
This change allows loading secrets from files in directories. In particular, this is useful for loading secrets from systemd credentials directory (see https://systemd.io/CREDENTIALS) and similar mechanisms.
- Loading branch information
Showing
5 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::collections::{HashMap, HashSet}; | ||
use std::path::PathBuf; | ||
|
||
use vector_lib::configurable::{component::GenerateConfig, configurable_component}; | ||
|
||
use crate::{config::SecretBackend, signal}; | ||
|
||
/// Configuration for the `directory` secrets backend. | ||
#[configurable_component(secrets("directory"))] | ||
#[derive(Clone, Debug)] | ||
pub struct DirectoryBackend { | ||
/// Directory path to read secrets from. | ||
pub path: PathBuf, | ||
|
||
/// Remove trailing whitespace from file contents. | ||
#[serde(default)] | ||
pub remove_trailing_whitespace: bool, | ||
} | ||
|
||
impl GenerateConfig for DirectoryBackend { | ||
fn generate_config() -> toml::Value { | ||
toml::Value::try_from(DirectoryBackend { | ||
path: PathBuf::from("/path/to/secrets"), | ||
remove_trailing_whitespace: false, | ||
}) | ||
.unwrap() | ||
} | ||
} | ||
|
||
impl SecretBackend for DirectoryBackend { | ||
async fn retrieve( | ||
&mut self, | ||
secret_keys: HashSet<String>, | ||
_: &mut signal::SignalRx, | ||
) -> crate::Result<HashMap<String, String>> { | ||
let mut secrets = HashMap::new(); | ||
for k in secret_keys.into_iter() { | ||
let file_path = self.path.join(&k); | ||
let contents = tokio::fs::read_to_string(&file_path).await?; | ||
let secret = if self.remove_trailing_whitespace { | ||
contents.trim_end() | ||
} else { | ||
&contents | ||
}; | ||
if secret.is_empty() { | ||
return Err(format!("secret in file '{}' was empty", k).into()); | ||
} | ||
secrets.insert(k, secret.to_string()); | ||
} | ||
Ok(secrets) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jkl.retrieved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters