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 942aaee commit de04027
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/config/loading/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use crate::{
// - "SECRET[backend..secret.name]" will match and capture "backend" and ".secret.name"
// - "SECRET[secret_name]" will not match
// - "SECRET[.secret.name]" will not match
//
// Note that `directory` backend assumes that secret key name does not contain path separator.
pub static COLLECTOR: Lazy<Regex> =
Lazy::new(|| Regex::new(r"SECRET\[([[:word:]]+)\.([[:word:].]+)\]").unwrap());

Expand Down
54 changes: 54 additions & 0 deletions src/secrets/directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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() {
// NB secret key name cannot contain path separator, so we do not access files outside
// of the configured directory path.
let path = self.path.join(&k);
let contents = tokio::fs::read_to_string(&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]"

Check failure on line 24 in tests/behavior/config/secret.toml

View workflow job for this annotation

GitHub Actions / Check Spelling

`foobarbazqux` is not a recognized word. (unrecognized-spelling)
.foobarbazquxquux = "SECRET[directory_backend.jkl]"

Check failure on line 25 in tests/behavior/config/secret.toml

View workflow job for this annotation

GitHub Actions / Check Spelling

`foobarbazquxquux` is not a recognized word. (unrecognized-spelling)
'''

[[tests]]
Expand All @@ -33,4 +39,5 @@
.foobar == "this_is_a_secret_value"
.foobarbaz == "def.retrieved"
.foobarbazqux == "ghi.retrieved"

Check failure on line 41 in tests/behavior/config/secret.toml

View workflow job for this annotation

GitHub Actions / Check Spelling

`foobarbazqux` is not a recognized word. (unrecognized-spelling)
.foobarbazquxquux == "jkl.retrieved"

Check failure on line 42 in tests/behavior/config/secret.toml

View workflow job for this annotation

GitHub Actions / Check Spelling

`foobarbazquxquux` is not a recognized word. (unrecognized-spelling)
'''
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 occurrs while reading the file, Vector will log the error and exit.

Check failure on line 518 in website/cue/reference/configuration.cue

View workflow job for this annotation

GitHub Actions / Check Spelling

`occurrs` is not a recognized word. (unrecognized-spelling)
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 de04027

Please sign in to comment.