Remove one retired device pairing and its owned history from the device detail
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::jobs::Error;
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{Path, State},
|
||||
extract::{Path, Query, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::post,
|
||||
routing::{delete, post},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
@@ -25,6 +25,7 @@ pub fn routes(r: Router<crate::State>) -> Router<crate::State> {
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/revocation",
|
||||
post(revoke_device),
|
||||
)
|
||||
.route("/api/v1/tenants/{tenant}/devices/{asset}", delete(remove_device))
|
||||
.route(
|
||||
"/api/v1/tenants/{tenant}/devices/{asset}/archive",
|
||||
post(archive_device),
|
||||
@@ -127,23 +128,53 @@ async fn archive_device(
|
||||
tx.commit().await?;
|
||||
Ok(Json(json!({"asset_id":asset, "archived":input.archived})))
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct Pairing {
|
||||
agent_id: Uuid,
|
||||
}
|
||||
|
||||
async fn remove_device(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path((tenant, asset)): Path<(Uuid, Uuid)>,
|
||||
Query(pairing): Query<Pairing>,
|
||||
) -> Result<StatusCode, Error> {
|
||||
let (p, mut tx) = context(&s, &h, tenant, "devices:revoke", "device_deleted").await?;
|
||||
let status: String = sqlx::query_scalar("SELECT remove_revoked_device($1,$2,$3,$4)")
|
||||
.bind(tenant).bind(asset).bind(pairing.agent_id).bind(&p.name)
|
||||
.fetch_one(&mut *tx).await?;
|
||||
match status.as_str() {
|
||||
"deleted" => audit(&mut tx, &p, "device_deleted",
|
||||
json!({"asset_id":asset, "agent_id":pairing.agent_id})).await?,
|
||||
"absent" => (),
|
||||
"active" => return Err(Error(StatusCode::CONFLICT, "DEVICE_REVOCATION_REQUIRED")),
|
||||
"busy" => return Err(Error(StatusCode::CONFLICT, "DEVICE_HAS_OPEN_WORK")),
|
||||
"forbidden" => return Err(Error(StatusCode::FORBIDDEN, "FORBIDDEN")),
|
||||
_ => return Err(Error(StatusCode::INTERNAL_SERVER_ERROR, "DEVICE_DELETION_FAILED")),
|
||||
}
|
||||
tx.commit().await?;
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
pub async fn revoke_device(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path((t, asset)): Path<(Uuid, Uuid)>,
|
||||
Query(selection): Query<crate::devices::DetailQuery>,
|
||||
Json(i): Json<Reason>,
|
||||
) -> Result<Json<Value>, Error> {
|
||||
let (p, mut tx) = context(&s, &h, t, "devices:revoke", &i.reason).await?;
|
||||
let n=sqlx::query("UPDATE agent_installations SET revoked_at=COALESCE(revoked_at,now()),connection_generation=connection_generation+1,previous_certificate_sha256=NULL,previous_certificate_until=NULL WHERE asset_id=$1").bind(asset).execute(&mut *tx).await?.rows_affected();
|
||||
let n=sqlx::query("UPDATE agent_installations SET revoked_at=COALESCE(revoked_at,now()),connection_generation=connection_generation+1,previous_certificate_sha256=NULL,previous_certificate_until=NULL WHERE asset_id=$1 AND ($2::uuid IS NULL OR agent_id=$2)").bind(asset).bind(selection.agent_id).execute(&mut *tx).await?.rows_affected();
|
||||
if n == 0 {
|
||||
return Err(Error(StatusCode::NOT_FOUND, "NOT_FOUND"));
|
||||
}
|
||||
sqlx::query("UPDATE jobs SET cancel_requested=true WHERE asset_id=$1 AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(asset).execute(&mut *tx).await?;
|
||||
sqlx::query("UPDATE jobs SET cancel_requested=true WHERE asset_id=$1 AND ($2::uuid IS NULL OR agent_id=$2) AND state NOT IN ('succeeded','failed','cancelled','expired','closed_unknown')").bind(asset).bind(selection.agent_id).execute(&mut *tx).await?;
|
||||
audit(
|
||||
&mut tx,
|
||||
&p,
|
||||
"device_revoked",
|
||||
json!({"asset_id":asset,"reason":i.reason}),
|
||||
json!({"asset_id":asset,"agent_id":selection.agent_id,"reason":i.reason}),
|
||||
)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
|
||||
Reference in New Issue
Block a user