Complete customer transfers on first authenticated inventory
Rust development slice / linux (push) Failing after 8s
Rust development slice / linux (push) Failing after 8s
This commit is contained in:
@@ -158,11 +158,13 @@ async fn status(
|
||||
return Ok(private_json(json!({"transfer":null})));
|
||||
};
|
||||
destination(&mut tx, &p, t.target_tenant_id).await?;
|
||||
let hostname = target_hostname(&mut tx, &t).await?;
|
||||
let hostname = if t.completed_at.is_some() {
|
||||
target_hostname(&mut tx, &t).await?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let state = if t.completed_at.is_some() {
|
||||
"completed"
|
||||
} else if hostname.is_some() {
|
||||
"ready"
|
||||
} else if t.target_agent_id.is_some() {
|
||||
"enrolled"
|
||||
} else {
|
||||
@@ -186,7 +188,7 @@ async fn lock_source(
|
||||
sqlx::query_scalar(
|
||||
"SELECT i.revoked_at IS NULL AND i.last_authenticated_at IS NOT NULL AND a.archived_at IS NULL
|
||||
FROM agent_installations i JOIN assets a USING (tenant_id,asset_id)
|
||||
WHERE i.asset_id=$1 AND i.agent_id=$2 FOR UPDATE OF i",
|
||||
WHERE i.asset_id=$1 AND i.agent_id=$2 AND NOT i.customer_transfer_pending FOR UPDATE OF i",
|
||||
)
|
||||
.bind(asset)
|
||||
.bind(agent)
|
||||
@@ -320,34 +322,16 @@ async fn complete(
|
||||
Query(pair): Query<Pairing>,
|
||||
) -> Result<Response, Error> {
|
||||
let (p, mut tx) = source(&s, &headers, tenant, true).await?;
|
||||
lock_source(&mut tx, tenant, asset, pair.agent_id).await?;
|
||||
verify_pairing(&mut tx, asset, pair.agent_id).await?;
|
||||
let t = transfer(&mut tx, pair.agent_id)
|
||||
.await?
|
||||
.filter(|t| t.transfer_id == id)
|
||||
.ok_or(NOT_FOUND)?;
|
||||
destination(&mut tx, &p, t.target_tenant_id).await?;
|
||||
if t.completed_at.is_some() {
|
||||
return Ok(private_json(json!({"state":"completed"})));
|
||||
}
|
||||
if target_hostname(&mut tx, &t).await?.is_none() {
|
||||
// Older WebUI bundles may still request completion after the atomic cutover.
|
||||
if t.completed_at.is_none() {
|
||||
return Err(Error(StatusCode::CONFLICT, "TRANSFER_TARGET_NOT_READY"));
|
||||
}
|
||||
select_tenant(&mut tx, tenant).await?;
|
||||
ensure_idle(&mut tx, pair.agent_id).await?;
|
||||
sqlx::query(
|
||||
"UPDATE agent_installations SET revoked_at=now(), connection_generation=connection_generation+1,
|
||||
previous_certificate_sha256=NULL,previous_certificate_until=NULL
|
||||
WHERE asset_id=$1 AND agent_id=$2 AND revoked_at IS NULL",
|
||||
)
|
||||
.bind(asset).bind(pair.agent_id).execute(&mut *tx).await?;
|
||||
sqlx::query("UPDATE device_customer_transfers SET completed_at=now() WHERE transfer_id=$1")
|
||||
.bind(id)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
audit(&mut tx, &p, "device_customer_transfer_completed", json!({
|
||||
"transfer_id":id,"asset_id":asset,"agent_id":pair.agent_id,"target_tenant_id":t.target_tenant_id,
|
||||
"target_asset_id":t.target_asset_id,"target_agent_id":t.target_agent_id
|
||||
})).await?;
|
||||
tx.commit().await?;
|
||||
Ok(private_json(json!({"state":"completed"})))
|
||||
}
|
||||
@@ -443,8 +427,8 @@ async fn device_certificate(
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"INSERT INTO agent_installations(tenant_id,asset_id,agent_id,certificate_sha256,certificate_pem,connection_generation)
|
||||
VALUES($1,$2,$3,$4,$5,$6)",
|
||||
"INSERT INTO agent_installations(tenant_id,asset_id,agent_id,certificate_sha256,certificate_pem,connection_generation,customer_transfer_pending)
|
||||
VALUES($1,$2,$3,$4,$5,$6,true)",
|
||||
).bind(t.target_tenant_id).bind(new_asset).bind(new_agent)
|
||||
.bind(hash(&certificate.to_der().map_err(anyhow::Error::from)?)).bind(&pem).bind(generation)
|
||||
.execute(&mut *tx).await?;
|
||||
|
||||
Reference in New Issue
Block a user