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
asaj/ci-try
Mattie Conover 2 years ago committed by GitHub
parent df09062706
commit 669a610c16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      rust/hyperlane-base/src/settings/loader.rs

@ -4,7 +4,7 @@ use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use config::{Config, Environment, File}; use config::{Config, Environment, File};
use eyre::{Context, Result}; use eyre::{bail, Context, Result};
use serde::Deserialize; use serde::Deserialize;
use crate::settings::RawSettings; use crate::settings::RawSettings;
@ -53,12 +53,23 @@ where
// Load a set of additional user specified config files // Load a set of additional user specified config files
let config_file_paths: Vec<String> = env::var("CONFIG_FILES") let config_file_paths: Vec<String> = 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(); .unwrap_or_default();
let builder = config_file_paths.iter().fold(builder, |builder, path| { for path in &config_file_paths {
builder.add_source(File::with_name(path)) 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 let config_deserializer = builder
// Use a base configuration env variable prefix // Use a base configuration env variable prefix

Loading…
Cancel
Save