vector/secrets/
directory.rs

1use std::{
2    collections::{HashMap, HashSet},
3    path::PathBuf,
4};
5
6use vector_lib::configurable::{component::GenerateConfig, configurable_component};
7
8use crate::{config::SecretBackend, signal};
9
10/// Configuration for the `directory` secrets backend.
11#[configurable_component(secrets("directory"))]
12#[derive(Clone, Debug)]
13pub struct DirectoryBackend {
14    /// Directory path to read secrets from.
15    pub path: PathBuf,
16
17    /// Remove trailing whitespace from file contents.
18    #[serde(default)]
19    pub remove_trailing_whitespace: bool,
20}
21
22impl GenerateConfig for DirectoryBackend {
23    fn generate_config() -> toml::Value {
24        toml::Value::try_from(DirectoryBackend {
25            path: PathBuf::from("/path/to/secrets"),
26            remove_trailing_whitespace: false,
27        })
28        .unwrap()
29    }
30}
31
32impl SecretBackend for DirectoryBackend {
33    async fn retrieve(
34        &mut self,
35        secret_keys: HashSet<String>,
36        _: &mut signal::SignalRx,
37    ) -> crate::Result<HashMap<String, String>> {
38        let mut secrets = HashMap::new();
39        for k in secret_keys.into_iter() {
40            let file_path = self.path.join(&k);
41            let contents = tokio::fs::read_to_string(&file_path).await?;
42            let secret = if self.remove_trailing_whitespace {
43                contents.trim_end()
44            } else {
45                &contents
46            };
47            if secret.is_empty() {
48                return Err(format!("secret in file '{k}' was empty").into());
49            }
50            secrets.insert(k, secret.to_string());
51        }
52        Ok(secrets)
53    }
54}