From 9389b4b8e32a8cd7eb7304ed1ed9f776b9abab47 Mon Sep 17 00:00:00 2001 From: Nikhil Sinha Date: Mon, 10 Jun 2024 16:41:27 +0530 Subject: [PATCH] fix: URL validation and error message for env var P_INGESTOR_ENDPOINT corrected error messages for socket address and URL validations --- server/src/handlers/airplane.rs | 3 ++- server/src/utils.rs | 40 +++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/server/src/handlers/airplane.rs b/server/src/handlers/airplane.rs index d6b814615..c898d0bd0 100644 --- a/server/src/handlers/airplane.rs +++ b/server/src/handlers/airplane.rs @@ -324,7 +324,8 @@ pub fn server() -> impl Future:` without the scheme (e.g., 192.168.1.1:8000)", +CONFIG.parseable.address, err)); addr.set_port(CONFIG.parseable.flight_port); let service = AirServiceImpl {}; diff --git a/server/src/utils.rs b/server/src/utils.rs index 84b604a51..bc300f906 100644 --- a/server/src/utils.rs +++ b/server/src/utils.rs @@ -238,13 +238,21 @@ pub fn get_url() -> Url { CONFIG.parseable.address ) .parse::() // if the value was improperly set, this will panic before hand - .expect("Valid URL"); + .unwrap_or_else(|err| panic!("{}, failed to parse `{}` as Url. Please set the environment variable `P_ADDR` to `:` without the scheme (e.g., 192.168.1.1:8000)", + err, CONFIG.parseable.address)); + } + + let ingestor_endpoint = &CONFIG.parseable.ingestor_endpoint; + + if ingestor_endpoint.starts_with("http") { + panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `:` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000)", ingestor_endpoint); + } + + let addr_from_env = ingestor_endpoint.split(':').collect::>(); + + if addr_from_env.len() != 2 { + panic!("Invalid value `{}`, please set the environement variable `P_INGESTOR_ENDPOINT` to `:` without the scheme (e.g., 192.168.1.1:8000 or example.com:8000)", ingestor_endpoint); } - let addr_from_env = CONFIG - .parseable - .ingestor_endpoint - .split(':') - .collect::>(); let mut hostname = addr_from_env[0].to_string(); let mut port = addr_from_env[1].to_string(); @@ -254,15 +262,29 @@ pub fn get_url() -> Url { if hostname.starts_with('$') { let var_hostname = hostname[1..].to_string(); hostname = get_from_env(&var_hostname); - } - if !hostname.starts_with("http") { - hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname); + + if hostname.is_empty() { + panic!("The environement variable `{}` is not set, please set as without the scheme (e.g., 192.168.1.1 or example.com)", var_hostname); + } + if hostname.starts_with("http") { + panic!("Invalid value `{}`, please set the environement variable `{}` to `` without the scheme (e.g., 192.168.1.1 or example.com)", hostname, var_hostname); + } else { + hostname = format!("{}://{}", CONFIG.parseable.get_scheme(), hostname); + } } if port.starts_with('$') { let var_port = port[1..].to_string(); port = get_from_env(&var_port); + + if port.is_empty() { + panic!( + "Port is not set in the environement variable `{}`", + var_port + ); + } } + format!("{}:{}", hostname, port) .parse::() .expect("Valid URL")