From 2aa9588425e483c491b201bb06147b74d38ae8f3 Mon Sep 17 00:00:00 2001 From: Gemma Tipper Date: Tue, 14 Jan 2025 13:45:13 +0000 Subject: [PATCH] Remove direct interactions with env vars for fields in config, use update_env() instead --- mirrord/cli/src/container.rs | 2 ++ mirrord/cli/src/execution.rs | 7 +++---- mirrord/cli/src/main.rs | 2 ++ mirrord/config/src/lib.rs | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mirrord/cli/src/container.rs b/mirrord/cli/src/container.rs index aa396a0162f..a2659190e0d 100644 --- a/mirrord/cli/src/container.rs +++ b/mirrord/cli/src/container.rs @@ -333,6 +333,7 @@ pub(crate) async fn container_command( (CONTAINER_EXECUTION_KIND as u32).to_string(), ); + // LayerConfig must be created after setting relevant env vars let (mut config, mut analytics) = create_config_and_analytics(&mut progress, watch)?; let (_internal_proxy_tls_guards, _external_proxy_tls_guards) = @@ -427,6 +428,7 @@ pub(crate) async fn container_ext_command( env.insert("MIRRORD_IMPERSONATED_TARGET".into(), target.to_string()); } + // LayerConfig must be created after setting relevant env vars let (mut config, mut analytics) = create_config_and_analytics(&mut progress, watch)?; let (_internal_proxy_tls_guards, _external_proxy_tls_guards) = diff --git a/mirrord/cli/src/execution.rs b/mirrord/cli/src/execution.rs index 7629b466b20..747148ccf49 100644 --- a/mirrord/cli/src/execution.rs +++ b/mirrord/cli/src/execution.rs @@ -289,10 +289,9 @@ impl MirrordExecution { })?; // Provide details for layer to connect to agent via internal proxy - env_vars.insert( - MIRRORD_CONNECT_TCP_ENV.to_string(), - format!("127.0.0.1:{}", address.port()), - ); + let mut config = config.clone(); + config.connect_tcp = Some(format!("127.0.0.1:{}", address.port())); + config.update_env()?; // Fixes // by disabling the fork safety check in the Objective-C runtime. diff --git a/mirrord/cli/src/main.rs b/mirrord/cli/src/main.rs index 4631af499dc..9a43ce290e0 100644 --- a/mirrord/cli/src/main.rs +++ b/mirrord/cli/src/main.rs @@ -352,6 +352,7 @@ async fn exec(args: &ExecArgs, watch: drain::Watch) -> CliResult<()> { std::env::set_var(name, value); } + // LayerConfig must be created after setting relevant env vars let (config, mut context) = LayerConfig::from_env_with_warnings()?; let mut analytics = AnalyticsReporter::only_error(config.telemetry, Default::default(), watch); @@ -472,6 +473,7 @@ async fn port_forward(args: &PortForwardArgs, watch: drain::Watch) -> CliResult< std::env::set_var("MIRRORD_CONFIG_FILE", config_file); } + // LayerConfig must be created after setting relevant env vars let (config, mut context) = LayerConfig::from_env_with_warnings()?; let mut analytics = AnalyticsReporter::new(config.telemetry, ExecutionKind::PortForward, watch); diff --git a/mirrord/config/src/lib.rs b/mirrord/config/src/lib.rs index c71f8c4454f..1408c8481cb 100644 --- a/mirrord/config/src/lib.rs +++ b/mirrord/config/src/lib.rs @@ -349,6 +349,11 @@ impl LayerConfig { Ok(BASE64_STANDARD.encode(serialized)) } + pub fn update_env(&self) -> Result<(), ConfigError> { + std::env::set_var(MIRRORD_RESOLVED_CONFIG_ENV, self.to_env_var()?); + Ok(()) + } + /// Generate a config from the environment variables and/or a config file. /// On success, returns the config and a vec of warnings. /// To be used from CLI to verify config and print warnings