From 669a610c16ecea769525edae4b86c0d7084b8676 Mon Sep 17 00:00:00 2001 From: Mattie Conover Date: Fri, 21 Apr 2023 15:16:41 -0700 Subject: [PATCH] Better checks on config files (#2134) ### Description This adds some additional validation to the config files that are provided by users to make sure that they are valid before trying to load them. ### Drive-by changes None ### Related issues - Fixes #2104 ### Backward compatibility _Are these changes backward compatible?_ Yes - probably, might be an edge case it drops support for like if someone was using a config file without a `.json` extension _Are there any infrastructure implications, e.g. changes that would prohibit deploying older commits using this infra tooling?_ None ### Testing _What kind of testing have these changes undergone?_ Manual --- rust/hyperlane-base/src/settings/loader.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/rust/hyperlane-base/src/settings/loader.rs b/rust/hyperlane-base/src/settings/loader.rs index 45f3cbde7..51ee205e5 100644 --- a/rust/hyperlane-base/src/settings/loader.rs +++ b/rust/hyperlane-base/src/settings/loader.rs @@ -4,7 +4,7 @@ use std::error::Error; use std::path::PathBuf; use config::{Config, Environment, File}; -use eyre::{Context, Result}; +use eyre::{bail, Context, Result}; use serde::Deserialize; use crate::settings::RawSettings; @@ -53,12 +53,23 @@ where // Load a set of additional user specified config files let config_file_paths: Vec = env::var("CONFIG_FILES") - .map(|s| s.split(',').map(|s| s.to_string()).collect()) + .map(|s| s.split(',').map(|s| s.to_owned()).collect()) .unwrap_or_default(); - let builder = config_file_paths.iter().fold(builder, |builder, path| { - builder.add_source(File::with_name(path)) - }); + for path in &config_file_paths { + let p = PathBuf::from(path); + if p.is_file() { + if p.extension() == Some("json".as_ref()) { + builder = builder.add_source(File::from(p)); + } else { + bail!("Provided config path via CONFIG_FILES is of an unsupported type ({p:?})") + } + } else if !p.exists() { + bail!("Provided config path via CONFIG_FILES does not exist ({p:?})") + } else { + bail!("Provided config path via CONFIG_FILES is not a file ({p:?})") + } + } let config_deserializer = builder // Use a base configuration env variable prefix