mirror of
https://github.com/truespar/sentio.git
synced 2026-11-21 16:06:05 +00:00
Distinguish auth backend failures from invalid tokens
The bearer-token path treated any api-key lookup error as a miss and fell through to OAuth, ultimately answering a database outage with 401 'invalid or expired token'. Infrastructure errors now return 500 without leaking which backend failed. Also warn at API startup when the publicly-known bootstrap admin key from migrations/002_bootstrap.sql is still active, and fix the README reference to that migration (007 -> 002).
This commit is contained in:
@@ -102,7 +102,7 @@ curl localhost:8080/health/ready
|
||||
|
||||
### Bootstrap credentials
|
||||
|
||||
Migration `007` seeds one admin tenant and one API key:
|
||||
Migration `002` seeds one admin tenant and one API key:
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
|
||||
@@ -67,13 +67,24 @@ impl FromRequestParts<AppState> for AuthContext {
|
||||
hex::encode(hasher.finalize())
|
||||
};
|
||||
|
||||
// Try API key first
|
||||
// Try API key first. A miss is signalled by SentioError::Auth;
|
||||
// any other error is an infrastructure failure and must not be
|
||||
// reported to the caller as a bad token.
|
||||
let api_key_repo = PgApiKeyRepository::new(pool.clone());
|
||||
if let Ok(record) = api_key_repo.verify(&key_hash).await {
|
||||
return Ok(AuthContext {
|
||||
tenant_id: record.tenant_id,
|
||||
scopes: record.scopes,
|
||||
});
|
||||
match api_key_repo.verify(&key_hash).await {
|
||||
Ok(record) => {
|
||||
return Ok(AuthContext {
|
||||
tenant_id: record.tenant_id,
|
||||
scopes: record.scopes,
|
||||
});
|
||||
}
|
||||
Err(sentio_core::error::SentioError::Auth(_)) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("api key lookup failed: {e}");
|
||||
return Err(ApiError::Internal(
|
||||
"authentication backend unavailable".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to OAuth bearer token
|
||||
@@ -91,7 +102,15 @@ impl FromRequestParts<AppState> for AuthContext {
|
||||
scopes: record.scopes,
|
||||
})
|
||||
}
|
||||
Err(_) => Err(ApiError::Auth("invalid or expired token".into())),
|
||||
Err(sentio_core::error::SentioError::Auth(_)) => {
|
||||
Err(ApiError::Auth("invalid or expired token".into()))
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("oauth token lookup failed: {e}");
|
||||
Err(ApiError::Internal(
|
||||
"authentication backend unavailable".into(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,4 +136,40 @@ pub async fn auth_middleware(
|
||||
parts.extensions.insert(ctx);
|
||||
let req = axum::http::Request::from_parts(parts, body);
|
||||
Ok(next.run(req).await)
|
||||
|
||||
// ──────────────────────────────────────────────────────────────────────────────
|
||||
// Bootstrap credential check
|
||||
// ──────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Warn loudly if the well-known bootstrap admin API key is still active.
|
||||
///
|
||||
/// `migrations/002_bootstrap.sql` seeds an admin tenant with a publicly-known
|
||||
/// key (`sentio_bootstrap_admin_CHANGE_ME`) so a fresh install is usable. The
|
||||
/// README instructs operators to rotate it; this check makes a forgotten
|
||||
/// rotation impossible to miss at startup.
|
||||
pub async fn warn_if_bootstrap_key_active(pool: &sqlx::PgPool) {
|
||||
const BOOTSTRAP_KEY: &str = "sentio_bootstrap_admin_CHANGE_ME";
|
||||
|
||||
let key_hash = {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(BOOTSTRAP_KEY.as_bytes());
|
||||
hex::encode(hasher.finalize())
|
||||
};
|
||||
|
||||
let api_key_repo = PgApiKeyRepository::new(pool.clone());
|
||||
match api_key_repo.verify(&key_hash).await {
|
||||
Ok(record) => {
|
||||
tracing::warn!(
|
||||
key_prefix = %record.key_prefix,
|
||||
"the bootstrap admin API key shipped in migrations/002_bootstrap.sql \
|
||||
is still active and publicly known - rotate it now via \
|
||||
POST /v1/tenants/{}/api-keys, then delete the bootstrap key",
|
||||
record.tenant_id
|
||||
);
|
||||
}
|
||||
Err(sentio_core::error::SentioError::Auth(_)) => {}
|
||||
Err(e) => {
|
||||
tracing::debug!(error = %e, "could not check bootstrap key status");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use std::str::FromStr;
|
||||
use sentio_core::error::SentioError;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use crate::auth::warn_if_bootstrap_key_active;
|
||||
use crate::routes;
|
||||
use crate::state::AppState;
|
||||
|
||||
@@ -18,6 +19,8 @@ pub async fn start(
|
||||
SentioError::Validation(format!("invalid listen_api address '{listen_addr}': {e}"))
|
||||
})?;
|
||||
|
||||
warn_if_bootstrap_key_active(&state.pool).await;
|
||||
|
||||
let app = routes::router(state);
|
||||
|
||||
let listener = TcpListener::bind(addr)
|
||||
|
||||
Reference in New Issue
Block a user