-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 files and directories (#21282
) * feat(config): Support loading secrets from files * 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. * chore: Update spelling dictionary * chore: Add changelog entry for file/directory secrets backends * fix: Move test data from tests/behavior to tests/data directory
- Loading branch information
Showing
9 changed files
with
191 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 |
---|---|---|
|
@@ -388,6 +388,8 @@ Fomichev | |
fonttbl | ||
foob | ||
foobarbaz | ||
foobarbazqux | ||
foobarbazquxquux | ||
foobarfoobarfoo | ||
FOOBARy | ||
foobaz | ||
|
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,4 @@ | ||
Vector now supports two additional back-ends for loading secrets: `file`, for reading a set of | ||
secrets from a JSON file, and `directory`, for loading secrets from a list of files. | ||
|
||
authors: tie |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 `file` secrets backend. | ||
#[configurable_component(secrets("file"))] | ||
#[derive(Clone, Debug)] | ||
pub struct FileBackend { | ||
/// File path to read secrets from. | ||
pub path: PathBuf, | ||
} | ||
|
||
impl GenerateConfig for FileBackend { | ||
fn generate_config() -> toml::Value { | ||
toml::Value::try_from(FileBackend { | ||
path: PathBuf::from("/path/to/secret"), | ||
}) | ||
.unwrap() | ||
} | ||
} | ||
|
||
impl SecretBackend for FileBackend { | ||
async fn retrieve( | ||
&mut self, | ||
secret_keys: HashSet<String>, | ||
_: &mut signal::SignalRx, | ||
) -> crate::Result<HashMap<String, String>> { | ||
let contents = tokio::fs::read_to_string(&self.path).await?; | ||
let output = serde_json::from_str::<HashMap<String, String>>(&contents)?; | ||
let mut secrets = HashMap::new(); | ||
for k in secret_keys.into_iter() { | ||
if let Some(secret) = output.get(&k) { | ||
if secret.is_empty() { | ||
return Err(format!("secret for key '{}' was empty", k).into()); | ||
} | ||
secrets.insert(k, secret.to_string()); | ||
} else { | ||
return Err(format!("secret for key '{}' was not retrieved", k).into()); | ||
} | ||
} | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"ghi":"ghi.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