Skip to content

Commit bae894d

Browse files
enhancement(http provider): allow environment interpolation from http provider config (#24341)
* enhancement(http provider): allow environment interpolation from http provider config * use to_string_lossy * make fmt --------- Co-authored-by: Thomas <[email protected]>
1 parent e512d47 commit bae894d

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/providers/http.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use vector_lib::configurable::configurable_component;
1010

1111
use super::BuildResult;
1212
use crate::{
13-
config::{self, Format, ProxyConfig, provider::ProviderConfig},
13+
config::{self, Format, ProxyConfig, interpolate, provider::ProviderConfig},
1414
http::HttpClient,
1515
signal,
1616
tls::{TlsConfig, TlsSettings},
@@ -57,6 +57,9 @@ pub struct HttpConfig {
5757
/// Which config format expected to be loaded
5858
#[configurable(derived)]
5959
config_format: Format,
60+
61+
/// Enable environment variable interpolation
62+
interpolate_env: bool,
6063
}
6164

6265
impl Default for HttpConfig {
@@ -68,6 +71,7 @@ impl Default for HttpConfig {
6871
tls_options: None,
6972
proxy: Default::default(),
7073
config_format: Format::default(),
74+
interpolate_env: false,
7175
}
7276
}
7377
}
@@ -135,12 +139,31 @@ async fn http_request_to_config_builder(
135139
headers: &IndexMap<String, String>,
136140
proxy: &ProxyConfig,
137141
config_format: &Format,
142+
interpolate_env: bool,
138143
) -> BuildResult {
139144
let config_str = http_request(url, tls_options, headers, proxy)
140145
.await
141146
.map_err(|e| vec![e.to_owned()])?;
142147

143-
config::load(config_str.chunk(), *config_format)
148+
if !interpolate_env {
149+
return config::load(config_str.chunk(), *config_format);
150+
}
151+
152+
let env_vars = std::env::vars_os()
153+
.map(|(k, v)| {
154+
(
155+
k.as_os_str().to_string_lossy().to_string(),
156+
v.as_os_str().to_string_lossy().to_string(),
157+
)
158+
})
159+
.collect::<std::collections::HashMap<String, String>>();
160+
161+
let config_str = interpolate(
162+
std::str::from_utf8(&config_str).map_err(|e| vec![e.to_string()])?,
163+
&env_vars,
164+
)?;
165+
166+
config::load(config_str.as_bytes().chunk(), *config_format)
144167
}
145168

146169
/// Polls the HTTP endpoint after/every `poll_interval_secs`, returning a stream of `ConfigBuilder`.
@@ -151,6 +174,7 @@ fn poll_http(
151174
headers: IndexMap<String, String>,
152175
proxy: ProxyConfig,
153176
config_format: Format,
177+
interpolate_env: bool,
154178
) -> impl Stream<Item = signal::SignalTo> {
155179
let duration = time::Duration::from_secs(poll_interval_secs);
156180
let mut interval = time::interval_at(time::Instant::now() + duration, duration);
@@ -159,7 +183,7 @@ fn poll_http(
159183
loop {
160184
interval.tick().await;
161185

162-
match http_request_to_config_builder(&url, tls_options.as_ref(), &headers, &proxy, &config_format).await {
186+
match http_request_to_config_builder(&url, tls_options.as_ref(), &headers, &proxy, &config_format, interpolate_env).await {
163187
Ok(config_builder) => yield signal::SignalTo::ReloadFromConfigBuilder(config_builder),
164188
Err(_) => {},
165189
};
@@ -191,6 +215,7 @@ impl ProviderConfig for HttpConfig {
191215
&request.headers,
192216
&proxy,
193217
&config_format,
218+
self.interpolate_env,
194219
)
195220
.await?;
196221

@@ -202,6 +227,7 @@ impl ProviderConfig for HttpConfig {
202227
request.headers.clone(),
203228
proxy.clone(),
204229
config_format,
230+
self.interpolate_env,
205231
));
206232

207233
Ok(config_builder)

0 commit comments

Comments
 (0)