[control] Share and token fixes: settle the epoch before minting, mint in its own task so a dropped client frees its key, token checks off the async threads, CLI refuses to run as another user than the state dir's owner
This commit is contained in:
@@ -997,14 +997,32 @@ pub async fn mint_share_link(
|
||||
{
|
||||
return Ok(replay(&st, s));
|
||||
}
|
||||
let out = mint_inner(&st, &actor, &hostname, body.value).await;
|
||||
if let Some(k) = &key {
|
||||
st.idem_finish(
|
||||
k,
|
||||
&fp,
|
||||
out.as_ref().ok().map(|v| Stored::Json(201, v.clone())),
|
||||
);
|
||||
}
|
||||
// In its own task: a client that goes away mid-request neither cancels the mint nor
|
||||
// leaves its Idempotency-Key reserved.
|
||||
let task = tokio::spawn({
|
||||
let (st, actor, key, fp) = (st.clone(), actor.clone(), key.clone(), fp.clone());
|
||||
let value = body.value;
|
||||
async move {
|
||||
let out = mint_inner(&st, &actor, &hostname, value).await;
|
||||
if let Some(k) = &key {
|
||||
st.idem_finish(
|
||||
k,
|
||||
&fp,
|
||||
out.as_ref().ok().map(|v| Stored::Json(201, v.clone())),
|
||||
);
|
||||
}
|
||||
out
|
||||
}
|
||||
});
|
||||
let out = match task.await {
|
||||
Ok(out) => out,
|
||||
Err(e) => {
|
||||
if let Some(k) = &key {
|
||||
st.idem_finish(k, &fp, None);
|
||||
}
|
||||
Err(ApiError::internal(e.to_string()))
|
||||
}
|
||||
};
|
||||
out.map(|v| json(StatusCode::CREATED, &v))
|
||||
}
|
||||
|
||||
|
||||
@@ -370,8 +370,14 @@ async fn guard(State(st): State<Arc<AppState>>, mut req: Request, next: Next) ->
|
||||
let auth = req
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|v| v.to_str().ok());
|
||||
let tok = st.ctl.tokens.check(auth).map_err(|r| {
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(str::to_owned);
|
||||
// The token store takes a file lock: off the async threads.
|
||||
let ctl = st.ctl.clone();
|
||||
let checked = tokio::task::spawn_blocking(move || ctl.tokens.check(auth.as_deref()))
|
||||
.await
|
||||
.unwrap_or(Err(Refusal::Unknown));
|
||||
let tok = checked.map_err(|r| {
|
||||
let (code, params) = match r {
|
||||
Refusal::Missing => ("token.missing", Params::new()),
|
||||
Refusal::Unknown => ("token.unknown", Params::new()),
|
||||
|
||||
Reference in New Issue
Block a user