From d335054a9c24c4bec8b101be48b94e6fc1cd5df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20I=C3=B1aki=20Bilbao?= Date: Tue, 29 Apr 2025 16:37:52 -0300 Subject: [PATCH 1/4] Check MUX loader file exists and has json ext --- crates/common/src/config/mux.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/common/src/config/mux.rs b/crates/common/src/config/mux.rs index ef0f1125..beb82502 100644 --- a/crates/common/src/config/mux.rs +++ b/crates/common/src/config/mux.rs @@ -123,6 +123,17 @@ impl MuxConfig { pub fn loader_env(&self) -> Option<(String, String, String)> { self.loader.as_ref().and_then(|loader| match loader { MuxKeysLoader::File(path_buf) => { + if !path_buf.try_exists().is_ok_and(|exists| exists) { + panic!("path doesn't exist: {:?}", path_buf); + } + + if !path_buf + .file_name() + .is_some_and(|name| name.to_string_lossy().to_lowercase().ends_with(".json")) + { + panic!("file doesn't have a .json extension"); + } + let path = path_buf.to_str().unwrap_or_else(|| panic!("invalid path: {:?}", path_buf)); let internal_path = get_mux_path(&self.id); From 3550645a8e49a97c7bf55b337b173772bae1d517 Mon Sep 17 00:00:00 2001 From: eltitanb Date: Mon, 7 Jul 2025 21:46:33 +0100 Subject: [PATCH 2/4] avoid panics --- crates/cli/src/docker_init.rs | 2 +- crates/common/src/config/mux.rs | 43 +++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/crates/cli/src/docker_init.rs b/crates/cli/src/docker_init.rs index 4453f597..8ac58d54 100644 --- a/crates/cli/src/docker_init.rs +++ b/crates/cli/src/docker_init.rs @@ -257,7 +257,7 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re if let Some(mux_config) = cb_config.muxes { for mux in mux_config.muxes.iter() { - if let Some((env_name, actual_path, internal_path)) = mux.loader_env() { + if let Some((env_name, actual_path, internal_path)) = mux.loader_env()? { let (key, val) = get_env_val(&env_name, &internal_path); pbs_envs.insert(key, val); pbs_volumes.push(Volumes::Simple(format!("{}:{}:ro", actual_path, internal_path))); diff --git a/crates/common/src/config/mux.rs b/crates/common/src/config/mux.rs index beb82502..7526ee80 100644 --- a/crates/common/src/config/mux.rs +++ b/crates/common/src/config/mux.rs @@ -120,29 +120,36 @@ pub struct MuxConfig { impl MuxConfig { /// Returns the env, actual path, and internal path to use for the file /// loader - pub fn loader_env(&self) -> Option<(String, String, String)> { - self.loader.as_ref().and_then(|loader| match loader { - MuxKeysLoader::File(path_buf) => { - if !path_buf.try_exists().is_ok_and(|exists| exists) { - panic!("path doesn't exist: {:?}", path_buf); - } + pub fn loader_env(&self) -> eyre::Result> { + let Some(loader) = self.loader.as_ref() else { + return Ok(None); + }; - if !path_buf - .file_name() - .is_some_and(|name| name.to_string_lossy().to_lowercase().ends_with(".json")) - { - panic!("file doesn't have a .json extension"); - } + match loader { + MuxKeysLoader::File(path_buf) => { + ensure!( + path_buf.try_exists().is_ok_and(|exists| exists), + "path doesn't exist: {:?}", + path_buf + ); + + ensure!( + path_buf.extension().is_some_and(|ext| ext == "json"), + "file doesn't have a .json extension: {:?}", + path_buf + ); + + let Some(path) = path_buf.to_str() else { + bail!("invalid path: {:?}", path_buf); + }; - let path = - path_buf.to_str().unwrap_or_else(|| panic!("invalid path: {:?}", path_buf)); let internal_path = get_mux_path(&self.id); - Some((get_mux_env(&self.id), path.to_owned(), internal_path)) + Ok(Some((get_mux_env(&self.id), path.to_owned(), internal_path))) } - MuxKeysLoader::HTTP { .. } => None, - MuxKeysLoader::Registry { .. } => None, - }) + MuxKeysLoader::HTTP { .. } => Ok(None), + MuxKeysLoader::Registry { .. } => Ok(None), + } } } From e1371e4153314809610f49c15ff51cf440ffee9b Mon Sep 17 00:00:00 2001 From: eltitanb Date: Tue, 8 Jul 2025 10:34:32 +0100 Subject: [PATCH 3/4] load keys to check --- crates/common/src/config/mux.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/crates/common/src/config/mux.rs b/crates/common/src/config/mux.rs index 7526ee80..e9ffe6c1 100644 --- a/crates/common/src/config/mux.rs +++ b/crates/common/src/config/mux.rs @@ -127,24 +127,16 @@ impl MuxConfig { match loader { MuxKeysLoader::File(path_buf) => { - ensure!( - path_buf.try_exists().is_ok_and(|exists| exists), - "path doesn't exist: {:?}", - path_buf - ); - - ensure!( - path_buf.extension().is_some_and(|ext| ext == "json"), - "file doesn't have a .json extension: {:?}", - path_buf - ); - let Some(path) = path_buf.to_str() else { bail!("invalid path: {:?}", path_buf); }; - let internal_path = get_mux_path(&self.id); + let file = load_file(path)?; + // make sure we can load the pubkeys correctly + let _: Vec = + serde_json::from_str(&file).wrap_err("failed to parse mux keys file")?; + let internal_path = get_mux_path(&self.id); Ok(Some((get_mux_env(&self.id), path.to_owned(), internal_path))) } MuxKeysLoader::HTTP { .. } => Ok(None), From 2bea1994136284e12714c9f4beef30feeba3e65b Mon Sep 17 00:00:00 2001 From: ltitanb <163874448+ltitanb@users.noreply.github.com> Date: Tue, 8 Jul 2025 20:12:51 +0100 Subject: [PATCH 4/4] Update crates/common/src/config/mux.rs Co-authored-by: Joe Clapis --- crates/common/src/config/mux.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/common/src/config/mux.rs b/crates/common/src/config/mux.rs index e9ffe6c1..bf23ce4a 100644 --- a/crates/common/src/config/mux.rs +++ b/crates/common/src/config/mux.rs @@ -119,7 +119,7 @@ pub struct MuxConfig { impl MuxConfig { /// Returns the env, actual path, and internal path to use for the file - /// loader + /// loader. In File mode, validates the mux file prior to returning. pub fn loader_env(&self) -> eyre::Result> { let Some(loader) = self.loader.as_ref() else { return Ok(None);