Skip to content

Commit f0ddb47

Browse files
authored
fix: Substitute environment variables in profiles only if explicitly opted-in (#1600)
Hi! This PR fix the regression introduced in #1577 by adding an opt-in flag for environment variable substitution in profile files. Users can still use `$` in their file if they enable substitution by escaping them (`\$`). This PR closes #1598 and #942
1 parent 05914d6 commit f0ddb47

7 files changed

+22
-10
lines changed

src/config.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,17 @@ impl RusticConfig {
120120

121121
if let Some(path) = paths.iter().find(|path| path.exists()) {
122122
merge_logs.push((Level::Info, format!("using config {}", path.display())));
123-
let config_content = subst::substitute(
124-
&std::fs::read_to_string(AbsPathBuf::canonicalize(path)?)?,
125-
&subst::Env,
126-
)
127-
.map_err(|e| {
128-
abscissa_core::error::context::Context::new(
129-
FrameworkErrorKind::ParseError,
130-
Some(Box::new(e)),
131-
)
132-
})?;
123+
let config_content = std::fs::read_to_string(AbsPathBuf::canonicalize(path)?)?;
124+
let config_content = if self.global.profile_substitute_env {
125+
subst::substitute(&config_content, &subst::Env).map_err(|e| {
126+
abscissa_core::error::context::Context::new(
127+
FrameworkErrorKind::ParseError,
128+
Some(Box::new(e)),
129+
)
130+
})?
131+
} else {
132+
config_content
133+
};
133134
let mut config = Self::load_toml(config_content)?;
134135
// if "use_profile" is defined in config file, merge the referenced profiles first
135136
for profile in &config.global.use_profiles.clone() {
@@ -157,6 +158,11 @@ impl RusticConfig {
157158
#[derive(Default, Debug, Parser, Clone, Deserialize, Serialize, Merge)]
158159
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
159160
pub struct GlobalOptions {
161+
/// Substitute environment variables in profiles
162+
#[clap(long, env = "RUSTIC_PROFILE_SUBSTITUTE_ENV")]
163+
#[merge(strategy=conflate::bool::overwrite_false)]
164+
pub profile_substitute_env: bool,
165+
160166
/// Config profile to use. This parses the file `<PROFILE>.toml` in the config directory.
161167
/// [default: "rustic"]
162168
#[clap(

src/snapshots/rustic_rs__config__tests__default_config_display_passes.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: src/config.rs
33
expression: config
44
---
55
[global]
6+
profile-substitute-env = false
67
use-profiles = []
78
dry-run = false
89
check-index = false

src/snapshots/rustic_rs__config__tests__default_config_passes.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: config
44
---
55
RusticConfig {
66
global: GlobalOptions {
7+
profile_substitute_env: false,
78
use_profiles: [],
89
group_by: None,
910
dry_run: false,

src/snapshots/rustic_rs__config__tests__global_env_roundtrip_passes-2.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: src/config.rs
33
expression: deserialized
44
---
55
[global]
6+
profile-substitute-env = false
67
use-profiles = []
78
dry-run = false
89
check-index = false

src/snapshots/rustic_rs__config__tests__global_env_roundtrip_passes-3.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ expression: deserialized
44
---
55
RusticConfig {
66
global: GlobalOptions {
7+
profile_substitute_env: false,
78
use_profiles: [],
89
group_by: None,
910
dry_run: false,

src/snapshots/rustic_rs__config__tests__global_env_roundtrip_passes.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: src/config.rs
33
expression: serialized
44
---
55
[global]
6+
profile-substitute-env = false
67
use-profiles = []
78
dry-run = false
89
check-index = false

tests/snapshots/show_config__show_config_passes.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: tests/show-config.rs
33
expression: output
44
---
55
[global]
6+
profile-substitute-env = false
67
use-profiles = []
78
dry-run = false
89
check-index = false

0 commit comments

Comments
 (0)