@@ 107,23 107,34 @@ impl MasterServer {
#[tonic::async_trait]
impl EmerwenProtocol for MasterServer {
async fn get_targets(&self, request: Request<()>) -> Result<Response<TargetsResponse>, 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 }))
}