Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: api key is optional in all api headers #1088

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions emily/handler/src/api/handlers/chainstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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<String>,
body: Chainstate,
) -> Result<impl warp::reply::Reply, Error> {
// 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);
Expand Down Expand Up @@ -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<String>,
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<String>,
body: Chainstate,
) -> Result<impl warp::reply::Reply, Error> {
// 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)
}
Expand Down
13 changes: 9 additions & 4 deletions emily/handler/src/api/handlers/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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<String>,
body: UpdateDepositsRequestBody,
) -> Result<impl warp::reply::Reply, Error> {
// Get the api state and error if the api state is claimed by a reorg.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 9 additions & 4 deletions emily/handler/src/api/handlers/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
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<String>,
body: UpdateWithdrawalsRequestBody,
) -> Result<impl warp::reply::Reply, Error> {
// Get the api state and error if the api state is claimed by a reorg.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions emily/handler/src/api/routes/chainstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn set_chainstate(
.map(move || context.clone())
.and(warp::path!("chainstate"))
.and(warp::post())
.and(warp::header::<String>("x-api-key"))
.and(warp::header::optional::<String>("x-api-key"))
.and(warp::body::json())
.then(handlers::chainstate::set_chainstate)
}
Expand All @@ -59,7 +59,7 @@ fn update_chainstate(
.map(move || context.clone())
.and(warp::path!("chainstate"))
.and(warp::put())
.and(warp::header::<String>("x-api-key"))
.and(warp::header::optional::<String>("x-api-key"))
.and(warp::body::json())
.then(handlers::chainstate::update_chainstate)
}
Expand Down
2 changes: 1 addition & 1 deletion emily/handler/src/api/routes/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn update_deposits(
.map(move || context.clone())
.and(warp::path!("deposit"))
.and(warp::put())
.and(warp::header::<String>("x-api-key"))
.and(warp::header::optional::<String>("x-api-key"))
.and(warp::body::json())
.then(handlers::deposit::update_deposits)
}
Expand Down
2 changes: 1 addition & 1 deletion emily/handler/src/api/routes/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn update_withdrawals(
.map(move || context.clone())
.and(warp::path("withdrawal"))
.and(warp::put())
.and(warp::header::<String>("x-api-key"))
.and(warp::header::optional::<String>("x-api-key"))
.and(warp::body::json())
.then(handlers::withdrawal::update_withdrawals)
}
Expand Down
Loading