Skip to content

Commit

Permalink
feat(config): Support loading secrets from directories
Browse files Browse the repository at this point in the history
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
tie committed Sep 12, 2024
1 parent 22720ef commit 9871cc1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/secrets/directory.rs
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)
}
}
5 changes: 5 additions & 0 deletions src/secrets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{config::SecretBackend, signal};
mod aws_secrets_manager;
mod exec;
mod file;
mod directory;
mod test;

/// Configurable secret backends in Vector.
Expand All @@ -22,6 +23,9 @@ pub enum SecretBackends {
/// File.
File(file::FileBackend),

/// Directory.
Directory(directory::DirectoryBackend),

/// Exec.
Exec(exec::ExecBackend),

Expand All @@ -39,6 +43,7 @@ impl NamedComponent for SecretBackends {
fn get_component_name(&self) -> &'static str {
match self {
Self::File(config) => config.get_component_name(),
Self::Directory(config) => config.get_component_name(),
Self::Exec(config) => config.get_component_name(),
#[cfg(feature = "secrets-aws-secrets-manager")]
Self::AwsSecretsManager(config) => config.get_component_name(),
Expand Down
1 change: 1 addition & 0 deletions tests/behavior/config/directory-secrets/jkl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jkl.retrieved
7 changes: 7 additions & 0 deletions tests/behavior/config/secret.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
type = "file"
path = "tests/behavior/config/file-secrets.json"

[secret.directory_backend]
type = "directory"
path = "tests/behavior/config/directory-secrets"
remove_trailing_whitespace = true

[transforms.add_field_from_secret]
inputs = []
type = "remap"
source = '''
.foobar = "SECRET[test_backend.abc]"
.foobarbaz = "SECRET[exec_backend.def]"
.foobarbazqux = "SECRET[file_backend.ghi]"
.foobarbazquxquux = "SECRET[directory_backend.jkl]"
'''

[[tests]]
Expand All @@ -33,4 +39,5 @@
.foobar == "this_is_a_secret_value"
.foobarbaz == "def.retrieved"
.foobarbazqux == "ghi.retrieved"
.foobarbazquxquux == "jkl.retrieved"
'''
34 changes: 34 additions & 0 deletions website/cue/reference/configuration.cue
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,40 @@ configuration: {
}
}
}
directory: {
required: true
description: """
Retrieve secrets from file contents in a directory.
The directory must contain files with names corresponding to secret keys.
If an error occurs while reading the file, Vector will log the error and exit.
Secrets are loaded when Vector starts or if Vector receives a `SIGHUP` signal triggering its
configuration reload process.
"""
type: object: options: {
path: {
description: """
The path of the directory with secrets.
"""
required: true
type: string: {
examples: [
"${CREDENTIALS_DIRECTORY}", // https://systemd.io/CREDENTIALS
"/path/to/secrets-directory",
]
}
}
remove_trailing_whitespace: {
description: """
Remove trailing whitespace from file contents.
"""
required: false
type: bool: default: false
}
}
}
exec: {
required: true
description: """
Expand Down

0 comments on commit 9871cc1

Please sign in to comment.