Skip to content

Commit

Permalink
New config env.unset that allows user to unset environment variables …
Browse files Browse the repository at this point in the history
…in the executed process. (#2344)
  • Loading branch information
aviramha authored Apr 1, 2024
1 parent 9dfe77f commit 0adf8bc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog.d/2260.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
New config `env.unset` that allows user to unset environment variables in the executed process.
This is useful for unsetting env like `HTTP_PROXY`, `AWS_PROFILE` that come from the local environment
and cause undesired behavior (because those aren't needed for deployed apps).
12 changes: 12 additions & 0 deletions mirrord-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,18 @@
"additionalProperties": {
"type": "string"
}
},
"unset": {
"title": "feature.env.unset {#feature-env-unset}",
"description": "Allows unsetting environment variables in the executed process.\n\nThis is useful for when some system/user-defined environment like `AWS_PROFILE` make the application behave as if it's running locally, instead of using the remote settings.",
"anyOf": [
{
"$ref": "#/definitions/VecOrSingle_for_String"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
Expand Down
9 changes: 9 additions & 0 deletions mirrord/cli/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub(crate) struct MirrordExecution {

/// The path to the patched binary, if patched for SIP sidestepping.
pub patched_path: Option<String>,

pub env_to_unset: Vec<String>,
}

/// Struct that when dropped will cancel the token and wait on the join handle
Expand Down Expand Up @@ -236,6 +238,13 @@ impl MirrordExecution {
environment: env_vars,
child: proxy_process,
patched_path,
env_to_unset: config
.feature
.env
.unset
.clone()
.map(|unset| unset.to_vec())
.unwrap_or_default(),
})
}

Expand Down
4 changes: 4 additions & 0 deletions mirrord/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ where
std::env::set_var(key, value);
}

for key in &execution_info.env_to_unset {
std::env::remove_var(key);
}

let mut binary_args = args.binary_args.clone();
// Put original executable in argv[0] even if actually running patched version.
binary_args.insert(0, args.binary.clone());
Expand Down
16 changes: 16 additions & 0 deletions mirrord/config/src/feature/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ pub struct EnvConfig {
/// This setting is meant to resolve issues when using mirrord via the IntelliJ plugin on WSL
/// and the remote environment contains a lot of variables.
pub load_from_process: Option<bool>,

/// ### feature.env.unset {#feature-env-unset}
///
/// Allows unsetting environment variables in the executed process.
///
/// This is useful for when some system/user-defined environment like `AWS_PROFILE` make the
/// application behave as if it's running locally, instead of using the remote settings.
pub unset: Option<VecOrSingle<String>>,
}

impl MirrordToggleableConfig for EnvFileConfig {
Expand All @@ -92,6 +100,7 @@ impl MirrordToggleableConfig for EnvFileConfig {
.or_else(|| Some(VecOrSingle::Single("*".to_owned()))),
load_from_process: None,
r#override: None,
unset: None,
})
}
}
Expand Down Expand Up @@ -119,6 +128,13 @@ impl CollectAnalytics for &EnvConfig {
.map(|v| v.len() as u32)
.unwrap_or_default(),
);
analytics.add(
"unset_count",
self.unset
.as_ref()
.map(|v| v.len() as u32)
.unwrap_or_default(),
);
}
}

Expand Down

0 comments on commit 0adf8bc

Please sign in to comment.