diff --git a/emily/handler/src/api/handlers/chainstate.rs b/emily/handler/src/api/handlers/chainstate.rs index 213eaaf5d..0db439333 100644 --- a/emily/handler/src/api/handlers/chainstate.rs +++ b/emily/handler/src/api/handlers/chainstate.rs @@ -103,25 +103,30 @@ pub async fn get_chainstate_at_height( #[instrument(skip(context))] pub async fn set_chainstate( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: Chainstate, ) -> impl warp::reply::Reply { debug!("Attempting to set chainstate: {body:?}"); // Internal handler so `?` can be used correctly while still returning a reply. async fn handler( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: Chainstate, ) -> Result { // Convert body to the correct type. let chainstate: Chainstate = body; - let can_reorg = context.settings.trusted_reorg_api_key == api_key; + + // Get whether the API key is provided and allowed to initiate a reorg. + let can_reorg = maybe_api_key + .map(|api_key| context.settings.trusted_reorg_api_key == api_key) + .unwrap_or(false); + add_chainstate_entry_or_reorg(&context, can_reorg, &chainstate).await?; // Respond. Ok(with_status(json(&chainstate), StatusCode::CREATED)) } // Handle and respond. - handler(context, api_key, body) + handler(context, maybe_api_key, body) .await .map_err(|error| { warn!("Failed to set chainstate with error: {}", error); @@ -150,25 +155,30 @@ pub async fn set_chainstate( #[instrument(skip(context))] pub async fn update_chainstate( context: EmilyContext, - api_key: String, + maybe_api_key: Option, request: Chainstate, ) -> impl warp::reply::Reply { debug!("Attempting to update chainstate: {request:?}"); // Internal handler so `?` can be used correctly while still returning a reply. async fn handler( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: Chainstate, ) -> Result { // Convert body to the correct type. let chainstate: Chainstate = body; - let can_reorg = context.settings.trusted_reorg_api_key == api_key; + + // Get whether the API key is provided and allowed to initiate a reorg. + let can_reorg = maybe_api_key + .map(|api_key| context.settings.trusted_reorg_api_key == api_key) + .unwrap_or(false); + add_chainstate_entry_or_reorg(&context, can_reorg, &chainstate).await?; // Respond. Ok(with_status(json(&chainstate), StatusCode::CREATED)) } // Handle and respond. - handler(context, api_key, request) + handler(context, maybe_api_key, request) .await .map_or_else(Reply::into_response, Reply::into_response) } diff --git a/emily/handler/src/api/handlers/deposit.rs b/emily/handler/src/api/handlers/deposit.rs index b7eab43f6..a71551976 100644 --- a/emily/handler/src/api/handlers/deposit.rs +++ b/emily/handler/src/api/handlers/deposit.rs @@ -318,14 +318,14 @@ fn scripts_to_resource_parameters( #[instrument(skip(context))] pub async fn update_deposits( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: UpdateDepositsRequestBody, ) -> impl warp::reply::Reply { debug!("In update deposits"); // Internal handler so `?` can be used correctly while still returning a reply. async fn handler( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: UpdateDepositsRequestBody, ) -> Result { // Get the api state and error if the api state is claimed by a reorg. @@ -341,7 +341,12 @@ pub async fn update_deposits( // Infer the new chainstates that would come from these deposit updates and then // attempt to update the chainstates. let inferred_chainstates = validated_request.inferred_chainstates()?; - let can_reorg = context.settings.trusted_reorg_api_key == api_key; + + // Get whether the API key is provided and allowed to initiate a reorg. + let can_reorg = maybe_api_key + .map(|api_key| context.settings.trusted_reorg_api_key == api_key) + .unwrap_or(false); + for chainstate in inferred_chainstates { // TODO(TBD): Determine what happens if this occurs in multiple lambda // instances at once. @@ -373,7 +378,7 @@ pub async fn update_deposits( Ok(with_status(json(&response), StatusCode::CREATED)) } // Handle and respond. - handler(context, api_key, body) + handler(context, maybe_api_key, body) .await .map_or_else(Reply::into_response, Reply::into_response) } diff --git a/emily/handler/src/api/handlers/withdrawal.rs b/emily/handler/src/api/handlers/withdrawal.rs index b0feb058c..702124eb6 100644 --- a/emily/handler/src/api/handlers/withdrawal.rs +++ b/emily/handler/src/api/handlers/withdrawal.rs @@ -209,13 +209,13 @@ pub async fn create_withdrawal( #[instrument(skip(context))] pub async fn update_withdrawals( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: UpdateWithdrawalsRequestBody, ) -> impl warp::reply::Reply { // Internal handler so `?` can be used correctly while still returning a reply. async fn handler( context: EmilyContext, - api_key: String, + maybe_api_key: Option, body: UpdateWithdrawalsRequestBody, ) -> Result { // Get the api state and error if the api state is claimed by a reorg. @@ -231,7 +231,12 @@ pub async fn update_withdrawals( // Infer the new chainstates that would come from these deposit updates and then // attempt to update the chainstates. let inferred_chainstates = validated_request.inferred_chainstates()?; - let can_reorg = context.settings.trusted_reorg_api_key == api_key; + + // Get whether the API key is provided and allowed to initiate a reorg. + let can_reorg = maybe_api_key + .map(|api_key| context.settings.trusted_reorg_api_key == api_key) + .unwrap_or(false); + for chainstate in inferred_chainstates { // TODO(TBD): Determine what happens if this occurs in multiple lambda // instances at once. @@ -263,7 +268,7 @@ pub async fn update_withdrawals( Ok(with_status(json(&response), StatusCode::CREATED)) } // Handle and respond. - handler(context, api_key, body) + handler(context, maybe_api_key, body) .await .map_or_else(Reply::into_response, Reply::into_response) } diff --git a/emily/handler/src/api/routes/chainstate.rs b/emily/handler/src/api/routes/chainstate.rs index f291ae0d2..aa82a5a0e 100644 --- a/emily/handler/src/api/routes/chainstate.rs +++ b/emily/handler/src/api/routes/chainstate.rs @@ -46,7 +46,7 @@ fn set_chainstate( .map(move || context.clone()) .and(warp::path!("chainstate")) .and(warp::post()) - .and(warp::header::("x-api-key")) + .and(warp::header::optional::("x-api-key")) .and(warp::body::json()) .then(handlers::chainstate::set_chainstate) } @@ -59,7 +59,7 @@ fn update_chainstate( .map(move || context.clone()) .and(warp::path!("chainstate")) .and(warp::put()) - .and(warp::header::("x-api-key")) + .and(warp::header::optional::("x-api-key")) .and(warp::body::json()) .then(handlers::chainstate::update_chainstate) } diff --git a/emily/handler/src/api/routes/deposit.rs b/emily/handler/src/api/routes/deposit.rs index 3a67336dc..9cd5390c7 100644 --- a/emily/handler/src/api/routes/deposit.rs +++ b/emily/handler/src/api/routes/deposit.rs @@ -71,7 +71,7 @@ fn update_deposits( .map(move || context.clone()) .and(warp::path!("deposit")) .and(warp::put()) - .and(warp::header::("x-api-key")) + .and(warp::header::optional::("x-api-key")) .and(warp::body::json()) .then(handlers::deposit::update_deposits) } diff --git a/emily/handler/src/api/routes/withdrawal.rs b/emily/handler/src/api/routes/withdrawal.rs index c105c927e..2ffda8258 100644 --- a/emily/handler/src/api/routes/withdrawal.rs +++ b/emily/handler/src/api/routes/withdrawal.rs @@ -58,7 +58,7 @@ fn update_withdrawals( .map(move || context.clone()) .and(warp::path("withdrawal")) .and(warp::put()) - .and(warp::header::("x-api-key")) + .and(warp::header::optional::("x-api-key")) .and(warp::body::json()) .then(handlers::withdrawal::update_withdrawals) }