feat: store Outpost credentials as digests and rate limit public endpoints (auth M4)
Credentials - pairing codes and Outpost upload tokens are stored only as SHA-256 digests; the token is minted at claim (256-bit OsRng) and returned once - the old clear-text wormhole_tokens table is migrated in place and dropped with secure_delete; active tokens keep working with a fresh lifetime - admin rotate (with grace window) and revoke endpoints, audited; a revoked Outpost is refused on its next request - optional end date for the shared admin token (TESSERA_ADMIN_TOKEN_EXPIRES) Limits - governor per-source limits for emergency login, pairing claim and SSO start/callback; X-Forwarded-For only from TESSERA_TRUSTED_PROXIES; IPv6 by /64 - at most two concurrent Argon2 verifications, refused immediately when busy - denials audited at most once a minute per source - the mTLS listener now supplies the peer address to handlers Existing hubs: rotate every Outpost token after upgrading; old database copies still contain the clear-text tokens. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,6 +37,8 @@ static WEB_DIST: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../../web/dist");
|
||||
start_reconstitute,
|
||||
pairing_start,
|
||||
pairing_claim,
|
||||
rotate_outpost_token,
|
||||
revoke_outpost,
|
||||
wormhole_upload_init,
|
||||
wormhole_upload_chunk,
|
||||
wormhole_restic_commit,
|
||||
@@ -55,6 +57,7 @@ static WEB_DIST: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../../web/dist");
|
||||
PairingResponse,
|
||||
ClaimRequest,
|
||||
ClaimResponse,
|
||||
RotateResponse,
|
||||
UploadInit,
|
||||
UploadChunk,
|
||||
ResticCommit,
|
||||
@@ -325,6 +328,8 @@ pub fn router(state: HubState) -> Router {
|
||||
.route("/api/v1/wormhole/outposts", get(outposts))
|
||||
.route("/api/v1/wormhole/pairing", post(pairing_start))
|
||||
.route("/api/v1/wormhole/claim", post(pairing_claim))
|
||||
.route("/api/v1/wormhole/outposts/:id/rotate", post(rotate_outpost_token))
|
||||
.route("/api/v1/wormhole/outposts/:id/revoke", post(revoke_outpost))
|
||||
.route("/api/v1/wormhole/upload/init", post(wormhole_upload_init))
|
||||
.route("/api/v1/wormhole/upload/chunk", post(wormhole_upload_chunk))
|
||||
.route(
|
||||
@@ -737,16 +742,10 @@ async fn pairing_start(
|
||||
(0..12).map(|_| ALPHABET[random.gen_range(0..ALPHABET.len())] as char).collect()
|
||||
};
|
||||
let expires = chrono::Utc::now() + chrono::Duration::minutes(10);
|
||||
let outpost_id = Uuid::new_v4();
|
||||
let upload_token = hex::encode(Uuid::new_v4().as_bytes());
|
||||
// Only a digest of the code is stored, and no token exists yet: it is minted at claim.
|
||||
state
|
||||
.db
|
||||
.insert_wormhole_token(
|
||||
&upload_token,
|
||||
&outpost_id.to_string(),
|
||||
&code,
|
||||
&expires.to_rfc3339(),
|
||||
)
|
||||
.insert_pairing(&Uuid::new_v4().to_string(), &code, expires.timestamp())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Json(PairingResponse {
|
||||
code,
|
||||
@@ -754,21 +753,40 @@ async fn pairing_start(
|
||||
}))
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/api/v1/wormhole/claim", request_body = ClaimRequest, responses((status = 200, body = ClaimResponse)))]
|
||||
#[utoipa::path(post, path = "/api/v1/wormhole/claim", request_body = ClaimRequest, responses((status = 200, body = ClaimResponse), (status = 429)))]
|
||||
async fn pairing_claim(
|
||||
peer: Option<ConnectInfo<SocketAddr>>,
|
||||
State(state): State<HubState>,
|
||||
headers: HeaderMap,
|
||||
Json(req): Json<ClaimRequest>,
|
||||
) -> Result<Json<ClaimResponse>, StatusCode> {
|
||||
let source = state.limits.source(peer.map(|ConnectInfo(addr)| addr), &headers);
|
||||
if !state.limits.claim_allowed(source) {
|
||||
if state.limits.audit_denial(source) {
|
||||
let _ = state.db.audit_event(&Principal::Anonymous, AuditAction::OutpostPair,
|
||||
AuditResource::Api, AuditOutcome::Denied, Uuid::new_v4());
|
||||
}
|
||||
return Err(StatusCode::TOO_MANY_REQUESTS);
|
||||
}
|
||||
let name = req.outpost_name.trim();
|
||||
if name.is_empty() || name.len() > 128 || name.chars().any(char::is_control) {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
let claimed = state
|
||||
.db
|
||||
.claim_wormhole_code(&req.code, name, &now)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::GONE)?;
|
||||
.claim_pairing(&req.code, name, chrono::Utc::now().timestamp())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let Some(claimed) = claimed else {
|
||||
let _ = state.db.audit_event(&Principal::Anonymous, AuditAction::OutpostPair,
|
||||
AuditResource::Api, AuditOutcome::Failed, Uuid::new_v4());
|
||||
return Err(StatusCode::GONE);
|
||||
};
|
||||
let outpost_id = Uuid::parse_str(&claimed.outpost_id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
state
|
||||
.db
|
||||
.audit_event(&Principal::Outpost(outpost_id), AuditAction::OutpostPair,
|
||||
AuditResource::Outpost(outpost_id), AuditOutcome::Succeeded, Uuid::new_v4())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let hub_url = std::env::var("TESSERA_PUBLIC_URL").unwrap_or_else(|_| {
|
||||
if state.wormhole_mtls {
|
||||
"https://127.0.0.1:8788".into()
|
||||
@@ -776,19 +794,97 @@ async fn pairing_claim(
|
||||
"http://127.0.0.1:8787".into()
|
||||
}
|
||||
});
|
||||
state
|
||||
.db
|
||||
.audit("wormhole.paired", name)
|
||||
.await
|
||||
.ok();
|
||||
let outpost_id = Uuid::parse_str(&claimed.1).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
state.db.audit("wormhole.paired", name).await.ok();
|
||||
Ok(Json(ClaimResponse {
|
||||
outpost_id,
|
||||
upload_token: claimed.0,
|
||||
upload_token: claimed.token,
|
||||
hub_url,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct RotateQuery {
|
||||
/// Minutes the previous token keeps working (default 60, 0 = cut at once, max 1440).
|
||||
grace_minutes: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, utoipa::ToSchema)]
|
||||
struct RotateResponse {
|
||||
outpost_id: Uuid,
|
||||
upload_token: String,
|
||||
expires_at: chrono::DateTime<chrono::Utc>,
|
||||
previous_valid_until: Option<chrono::DateTime<chrono::Utc>>,
|
||||
}
|
||||
|
||||
fn timestamp(seconds: i64) -> chrono::DateTime<chrono::Utc> {
|
||||
chrono::DateTime::from_timestamp(seconds, 0).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Issue a new upload token for a paired Outpost. The response is the only place
|
||||
/// the token appears; the operator puts it on the agent within the grace window.
|
||||
#[utoipa::path(post, path = "/api/v1/wormhole/outposts/{id}/rotate",
|
||||
params(("id" = Uuid, Path, description = "paired Outpost")),
|
||||
responses((status = 200, body = RotateResponse), (status = 404)))]
|
||||
async fn rotate_outpost_token(
|
||||
ConnectInfo(peer): ConnectInfo<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
headers: HeaderMap,
|
||||
Path(id): Path<Uuid>,
|
||||
axum::extract::Query(query): axum::extract::Query<RotateQuery>,
|
||||
) -> Result<(HeaderMap, Json<RotateResponse>), StatusCode> {
|
||||
let actor = require_admin_mutation(&state, peer, &headers)?;
|
||||
let grace = query.grace_minutes.unwrap_or(60);
|
||||
if !(0..=1440).contains(&grace) {
|
||||
return Err(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
let issued = state
|
||||
.db
|
||||
.rotate_credential(&id.to_string(), grace * 60, now)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let outcome = if issued.is_some() { AuditOutcome::Succeeded } else { AuditOutcome::Failed };
|
||||
state
|
||||
.db
|
||||
.audit_event(&actor.principal, AuditAction::CredentialRotate, AuditResource::Outpost(id), outcome, actor.request_id)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let issued = issued.ok_or(StatusCode::NOT_FOUND)?;
|
||||
let mut response_headers = HeaderMap::new();
|
||||
response_headers.insert(axum::http::header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
|
||||
Ok((
|
||||
response_headers,
|
||||
Json(RotateResponse {
|
||||
outpost_id: id,
|
||||
upload_token: issued.token,
|
||||
expires_at: timestamp(issued.expires_at),
|
||||
previous_valid_until: issued.previous_valid_until.map(timestamp),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
/// Revoke every credential of an Outpost. Its next request is refused; a leased
|
||||
/// job it was running can no longer report. It has to be paired again.
|
||||
#[utoipa::path(post, path = "/api/v1/wormhole/outposts/{id}/revoke",
|
||||
params(("id" = Uuid, Path, description = "Outpost to revoke")),
|
||||
responses((status = 204), (status = 404)))]
|
||||
async fn revoke_outpost(
|
||||
ConnectInfo(peer): ConnectInfo<SocketAddr>,
|
||||
State(state): State<HubState>,
|
||||
headers: HeaderMap,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
let actor = require_admin_mutation(&state, peer, &headers)?;
|
||||
let known = state
|
||||
.db
|
||||
.revoke_outpost(&id.to_string(), chrono::Utc::now().timestamp())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let outcome = if known { AuditOutcome::Succeeded } else { AuditOutcome::Failed };
|
||||
state
|
||||
.db
|
||||
.audit_event(&actor.principal, AuditAction::CredentialRevoke, AuditResource::Outpost(id), outcome, actor.request_id)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
if known { Ok(StatusCode::NO_CONTENT) } else { Err(StatusCode::NOT_FOUND) }
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, utoipa::ToSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct OutpostBinding {
|
||||
@@ -1140,9 +1236,8 @@ mod tests {
|
||||
state.admin.token = Some("synthetic-admin".into());
|
||||
let a = Uuid::new_v4();
|
||||
let b = Uuid::new_v4();
|
||||
for (id, token, code) in [(a, "outpost-a", "code-a"), (b, "outpost-b", "code-b")] {
|
||||
state.db.insert_wormhole_token(token, &id.to_string(), code, "9999").unwrap();
|
||||
state.db.claim_wormhole_code(code, token, "2026").unwrap().unwrap();
|
||||
for (id, token) in [(a, "outpost-a"), (b, "outpost-b")] {
|
||||
state.db.insert_test_credential(token, &id.to_string(), "agent");
|
||||
}
|
||||
let orbit = Uuid::new_v4();
|
||||
state.db.upsert_orbit(&OrbitRow {
|
||||
@@ -1257,8 +1352,7 @@ mod tests {
|
||||
assert_eq!(state.db.list_orbits().await.unwrap()[1].last_status, "idle");
|
||||
|
||||
let outpost = Uuid::new_v4();
|
||||
state.db.insert_wormhole_token("fixture-token", &outpost.to_string(), "fixture-code", "9999").unwrap();
|
||||
state.db.claim_wormhole_code("fixture-code", "fixture", "2026").unwrap().unwrap();
|
||||
state.db.insert_test_credential("fixture-token", &outpost.to_string(), "agent");
|
||||
assert!(state.db.bind_legacy_outpost(orbit.id, outpost).unwrap());
|
||||
{
|
||||
let conn = rusqlite::Connection::open(dir.path().join("tessera.db")).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user