diff --git a/Cargo.lock b/Cargo.lock index 37c35a9..8cf4d78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -485,7 +485,7 @@ dependencies = [ [[package]] name = "crypteia" -version = "1.1.2" +version = "2.0.0" dependencies = [ "anyhow", "aws-config", diff --git a/src/main.rs b/src/main.rs index e3f0768..38a4161 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,9 +11,9 @@ const ENV_FILE: &str = "/tmp/crypteia.json"; async fn main() -> Result<(), Error> { log::cloudwatch_metric("main", "initialized", false, None); let env_vars: HashMap = std::env::vars().collect(); - let env_map = ssm::get_envs(env_vars).await.unwrap(); + let env_map = ssm::get_envs(env_vars).await?; log::cloudwatch_metric("main", "fetched", false, None); - write_envs_to_tmp_json(env_map); + write_envs_to_tmp_json(env_map)?; let func = service_fn(parameters_extension); lambda_extension::run(func).await } @@ -28,8 +28,9 @@ async fn parameters_extension(event: LambdaEvent) -> Result<(), Error> { Ok(()) } -fn write_envs_to_tmp_json(env_map: HashMap) { +fn write_envs_to_tmp_json(env_map: HashMap) -> Result<(), std::io::Error> { let envs_json = serde_json::to_string(&env_map).unwrap(); - let mut file = File::create(ENV_FILE).unwrap(); - file.write_all(envs_json.as_bytes()).unwrap(); -} + let mut file = File::create(ENV_FILE)?; + file.write_all(envs_json.as_bytes())?; + Ok(()) +} \ No newline at end of file diff --git a/src/ssm.rs b/src/ssm.rs index d4f9aae..953db3f 100644 --- a/src/ssm.rs +++ b/src/ssm.rs @@ -4,6 +4,7 @@ use futures::future::join_all; use std::collections::HashMap; use tokio::{spawn, task::JoinHandle}; + pub async fn get_envs(env_vars: HashMap) -> Result> { let sdk_config = aws_config::load_from_env().await; let ssm_client: aws_sdk_ssm::Client = aws_sdk_ssm::Client::new(&sdk_config); @@ -31,7 +32,7 @@ pub async fn get_envs(env_vars: HashMap) -> Result return Err(error), // Return error if parameter is not found + Err(error) => return Err(anyhow::anyhow!(format!("Parameter not found: {}", error))), // Return error if parameter is not found }, Err(error) => return Err(anyhow::anyhow!(error.to_string())), // Return error if task fails } @@ -55,6 +56,8 @@ async fn ssm_get_parameter( Ok(response) => { if let Some(parameter) = response.parameter { items.insert(name, parameter.value.unwrap()); + } else { + return Err(anyhow::anyhow!("Parameter not found: {}", path)); } } Err(error) => { @@ -120,6 +123,9 @@ async fn ssm_get_parameters_by_path( } } } + if items.is_empty() { + return Err(anyhow::anyhow!("Parameters not found for path: {}", path)); + } Ok(items) } @@ -166,6 +172,7 @@ mod test { .overwrite(true) .send() .await?; + let env_vars: HashMap = HashMap::from([ ("EXISTING".to_string(), "existingvalue".to_string()), ( @@ -189,18 +196,53 @@ mod test { ("DB_URL".to_string(), "mysql2://u:p@host:3306".to_string()), ("NR_KEY".to_string(), "z6y5x4w3v2u1".to_string()), ]); + + match get_envs(env_vars.clone()).await { + Ok(result) => println!("BLAH ::: {:?}", result), + Err(e) => println!("Error: {:?}", e), + } + let results = get_envs(env_vars).await.expect("Should fetch parameters"); + + assert_eq!(results, expected); Ok(()) } #[tokio::test] - async fn should_fail_if_param_not_found() { + async fn should_fail_if_param_not_found() -> Result<()> { + let sdk_config = aws_config::load_from_env().await; + let ssm_client = aws_sdk_ssm::Client::new(&sdk_config); + ssm_client + .put_parameter() + .name("/crypteia/v5/myapp/SECRET2".to_owned()) + .value("1A2B3C4D5E6F".to_owned()) + .r#type(ParameterType::SecureString) + .overwrite(true) + .send() + .await?; let env_vars: HashMap = HashMap::from([ + ("EXISTING".to_string(), "existingvalue".to_string()), + ( + "SECRET2".to_string(), + "x-crypteia-ssm:/crypteia/v5/myapp/SECRET".to_string(), + ), ("NON_EXISTENT_PARAM".to_string(), "x-crypteia-ssm:/crypteia/v5/myapp/NON_EXISTENT_PARAM".to_string()), ]); - let result = get_envs(env_vars).await; assert!(result.is_err(), "Expected an error when parameter is not found"); + Ok(()) + } + + + + #[tokio::test] + async fn should_fail_if_param_not_found_in_path() -> Result<()> { + let sdk_config = aws_config::load_from_env().await; + let ssm_client = aws_sdk_ssm::Client::new(&sdk_config); + let result = ssm_get_parameters_by_path(&ssm_client, "NON_EXISTENT_PARAM".to_string(), "/crypteia/v5/myapp/non_existent_path".to_string()).await; + assert!(result.is_err(), "Expected an error when parameters are not found for the given path"); + + Ok(()) } } \ No newline at end of file