From a10abbc6c142abb0bda216e49669ac8baa29df99 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 11 Nov 2024 10:32:37 +0200 Subject: [PATCH] fix(master): get_targets error handling --- emerwen-master/src/main.rs | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/emerwen-master/src/main.rs b/emerwen-master/src/main.rs index 204b121..c8b6968 100644 --- a/emerwen-master/src/main.rs +++ b/emerwen-master/src/main.rs @@ -107,23 +107,34 @@ impl MasterServer { #[tonic::async_trait] impl EmerwenProtocol for MasterServer { async fn get_targets(&self, request: Request<()>) -> Result, Status> { - // We should not get here without valid authorization. - let token = request - .metadata() - .get("authorization") - .expect("request had no authorization, when it should") - .to_str() - .unwrap() - .trim_start_matches("Bearer "); - - // Like we really really shouldn't get here if the target doesn't exist. - let targets = self + let token = match request.metadata().get("authorization") { + Some(value) => match value.to_str() { + Ok(bearer_token) => bearer_token.trim_start_matches("Bearer "), + Err(_) => { + return Err(Status::invalid_argument( + "couldn't read bearer auth to string", + )) + } + }, + None => { + return Err(Status::unauthenticated( + "request had no authorization, when it should", + )) + } + }; + + let targets = match self .workers .iter() .find(|worker| *worker.auth_token == *token) - .expect("dafuq") - .targets - .clone(); + { + Some(worker) => worker.targets.clone(), + None => { + return Err(Status::invalid_argument( + "no worker exists with the auth token", + )) + } + }; Ok(Response::new(TargetsResponse { targets })) } -- 2.44.1