Skip to content

Commit 541bb00

Browse files
authored
chore(enterprise): Extend library functionality for secret scanning (vectordotdev#17483)
Ref OPB-710 This PR - Exposes patterns and the `interpolate` function for use in OPW. This will help deduplicate some logic and reduce maintenance burden. We use the patterns themselves in secret scanning logic, and will use the `interpolate` function in bootstrap-related logic.
1 parent 58d7f3d commit 541bb00

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/config/loading/secret.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
// - "SECRET[backend..secret.name]" will match and capture "backend" and ".secret.name"
2727
// - "SECRET[secret_name]" will not match
2828
// - "SECRET[.secret.name]" will not match
29-
static COLLECTOR: Lazy<Regex> =
29+
pub static COLLECTOR: Lazy<Regex> =
3030
Lazy::new(|| Regex::new(r"SECRET\[([[:word:]]+)\.([[:word:].]+)\]").unwrap());
3131

3232
/// Helper type for specifically deserializing secrets backends.

src/config/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ pub use format::{Format, FormatHint};
4848
pub use id::{ComponentKey, Inputs};
4949
pub use loading::{
5050
load, load_builder_from_paths, load_from_paths, load_from_paths_with_provider_and_secrets,
51-
load_from_str, load_source_from_paths, merge_path_lists, process_paths, CONFIG_PATHS,
51+
load_from_str, load_source_from_paths, merge_path_lists, process_paths, COLLECTOR,
52+
CONFIG_PATHS,
5253
};
5354
pub use provider::ProviderConfig;
5455
pub use secret::SecretBackend;
@@ -59,6 +60,7 @@ pub use transform::{
5960
};
6061
pub use unit_test::{build_unit_tests, build_unit_tests_main, UnitTestResult};
6162
pub use validation::warnings;
63+
pub use vars::{interpolate, ENVIRONMENT_VARIABLE_INTERPOLATION_REGEX};
6264
pub use vector_core::config::{
6365
init_log_schema, log_schema, proxy::ProxyConfig, LogSchema, OutputId,
6466
};

src/config/vars.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
use std::collections::HashMap;
22

3+
use once_cell::sync::Lazy;
34
use regex::{Captures, Regex};
45

6+
// Environment variable names can have any characters from the Portable Character Set other
7+
// than NUL. However, for Vector's interpolation, we are closer to what a shell supports which
8+
// is solely of uppercase letters, digits, and the '_' (that is, the `[:word:]` regex class).
9+
// In addition to these characters, we allow `.` as this commonly appears in environment
10+
// variable names when they come from a Java properties file.
11+
//
12+
// https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
13+
pub static ENVIRONMENT_VARIABLE_INTERPOLATION_REGEX: Lazy<Regex> = Lazy::new(|| {
14+
Regex::new(
15+
r"(?x)
16+
\$\$|
17+
\$([[:word:].]+)|
18+
\$\{([[:word:].]+)(?:(:?-|:?\?)([^}]*))?\}",
19+
)
20+
.unwrap()
21+
});
22+
523
/// (result, warnings)
624
pub fn interpolate(
725
input: &str,
@@ -10,22 +28,7 @@ pub fn interpolate(
1028
let mut errors = Vec::new();
1129
let mut warnings = Vec::new();
1230

13-
// Environment variable names can have any characters from the Portable Character Set other
14-
// than NUL. However, for Vector's interpolation, we are closer to what a shell supports which
15-
// is solely of uppercase letters, digits, and the '_' (that is, the `[:word:]` regex class).
16-
// In addition to these characters, we allow `.` as this commonly appears in environment
17-
// variable names when they come from a Java properties file.
18-
//
19-
// https://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html
20-
let re = Regex::new(
21-
r"(?x)
22-
\$\$|
23-
\$([[:word:].]+)|
24-
\$\{([[:word:].]+)(?:(:?-|:?\?)([^}]*))?\}",
25-
)
26-
.unwrap();
27-
28-
let interpolated = re
31+
let interpolated = ENVIRONMENT_VARIABLE_INTERPOLATION_REGEX
2932
.replace_all(input, |caps: &Captures<'_>| {
3033
let flags = caps.get(3).map(|m| m.as_str()).unwrap_or_default();
3134
let def_or_err = caps.get(4).map(|m| m.as_str()).unwrap_or_default();

0 commit comments

Comments
 (0)