[control] Domi CA and UI sign-in: intermediate renewal by CSR with manual offline signing, domi host certificates, device requests approved by code, one-time PKCS#12 installs, revocation; Remote-User trusted only with edge's per-boot proof
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
#![allow(clippy::result_large_err)]
|
||||
|
||||
pub mod handlers;
|
||||
pub mod pki;
|
||||
pub mod ui;
|
||||
pub mod views;
|
||||
|
||||
use std::collections::HashMap;
|
||||
@@ -317,7 +319,7 @@ fn body_error(e: &serde_json::Error) -> ApiError {
|
||||
|
||||
// ---------------------------------------------------------------- middleware
|
||||
|
||||
fn host_allowed(ctl: &Control, headers: &HeaderMap) -> bool {
|
||||
pub(crate) fn host_allowed(ctl: &Control, headers: &HeaderMap) -> bool {
|
||||
let Some(host) = headers.get(header::HOST).and_then(|h| h.to_str().ok()) else {
|
||||
return false;
|
||||
};
|
||||
@@ -328,7 +330,14 @@ fn host_allowed(ctl: &Control, headers: &HeaderMap) -> bool {
|
||||
format!("127.0.0.1:{port}"),
|
||||
format!("[::1]:{port}"),
|
||||
];
|
||||
ok.iter().any(|o| o.eq_ignore_ascii_case(host))
|
||||
// The admin host edge forwards the UI on, with or without edge's HTTPS port.
|
||||
let ui = ctl.cfg.ui.as_ref().and_then(|u| u.hostname.as_deref());
|
||||
let ui_ok = ui.is_some_and(|u| {
|
||||
let edge_port = ctl.cfg.compile.https_listen.port();
|
||||
host.eq_ignore_ascii_case(u) || host.eq_ignore_ascii_case(&format!("{u}:{edge_port}"))
|
||||
});
|
||||
ui_ok
|
||||
|| ok.iter().any(|o| o.eq_ignore_ascii_case(host))
|
||||
|| ctl
|
||||
.cfg
|
||||
.allowed_hosts
|
||||
@@ -336,6 +345,45 @@ fn host_allowed(ctl: &Control, headers: &HeaderMap) -> bool {
|
||||
.any(|a| a.eq_ignore_ascii_case(host))
|
||||
}
|
||||
|
||||
enum UiIdentity {
|
||||
/// No sign-in headers: an API-token request.
|
||||
None,
|
||||
/// `Remote-*` or a proof header without the right proof.
|
||||
Forged,
|
||||
/// The right proof but no usable `Remote-User`.
|
||||
NoUser,
|
||||
User(String),
|
||||
}
|
||||
|
||||
fn ui_identity(ctl: &Control, headers: &HeaderMap) -> UiIdentity {
|
||||
let names_user = headers.keys().any(|k| k.as_str().starts_with("remote-"));
|
||||
let proof = headers.get("x-ianitor-proof");
|
||||
if !names_user && proof.is_none() {
|
||||
return UiIdentity::None;
|
||||
}
|
||||
let ok = match (proof, &ctl.ui_proof) {
|
||||
(Some(p), Some(want)) => crate::util::ct_eq(p.as_bytes(), want),
|
||||
_ => false,
|
||||
};
|
||||
if !ok {
|
||||
return UiIdentity::Forged;
|
||||
}
|
||||
let users: Vec<&HeaderValue> = headers.get_all("remote-user").iter().collect();
|
||||
match users.as_slice() {
|
||||
[one] => match one.to_str() {
|
||||
Ok(u)
|
||||
if !u.is_empty()
|
||||
&& u.len() <= 64
|
||||
&& u.bytes().all(|c| c.is_ascii_graphic() && c != b':') =>
|
||||
{
|
||||
UiIdentity::User(u.to_owned())
|
||||
}
|
||||
_ => UiIdentity::NoUser,
|
||||
},
|
||||
_ => UiIdentity::NoUser,
|
||||
}
|
||||
}
|
||||
|
||||
/// Host check, CSRF check, token, rate limit; logs method, route template, status, time.
|
||||
async fn guard(State(st): State<Arc<AppState>>, mut req: Request, next: Next) -> Response {
|
||||
let started = Instant::now();
|
||||
@@ -367,6 +415,46 @@ async fn guard(State(st): State<Arc<AppState>>, mut req: Request, next: Next) ->
|
||||
Params::new(),
|
||||
));
|
||||
}
|
||||
// The web UI, through edge on the admin host (docs/api.md, "Sign-in"): only a request
|
||||
// with the per-boot proof edge adds is identified by its Remote-User. Any other
|
||||
// request that names a user is refused, so a local process cannot claim to be one.
|
||||
match ui_identity(&st.ctl, req.headers()) {
|
||||
UiIdentity::None => {}
|
||||
UiIdentity::Forged => {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"ui.no_proof",
|
||||
Params::new(),
|
||||
));
|
||||
}
|
||||
UiIdentity::NoUser => {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::UNAUTHORIZED,
|
||||
"ui.no_user",
|
||||
Params::new(),
|
||||
));
|
||||
}
|
||||
UiIdentity::User(user) => {
|
||||
// Cookie-based sign-in: every write carries the UI header, browser or not.
|
||||
if write && !ui {
|
||||
return Err(ApiError::new(
|
||||
StatusCode::FORBIDDEN,
|
||||
"request.csrf",
|
||||
Params::new(),
|
||||
));
|
||||
}
|
||||
st.limit(&format!("user:{user}"), RATE_PER_MINUTE, 60)
|
||||
.map_err(|s| {
|
||||
ApiError::new(StatusCode::TOO_MANY_REQUESTS, "rate.limited", Params::new())
|
||||
.retry(s)
|
||||
})?;
|
||||
req.extensions_mut().insert(Actor {
|
||||
token_id: format!("user:{user}"),
|
||||
name: user,
|
||||
});
|
||||
return Ok(next.run(req).await);
|
||||
}
|
||||
}
|
||||
let auth = req
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
@@ -429,7 +517,7 @@ async fn finish(req: Request, next: Next) -> Response {
|
||||
let out = match serde_json::from_slice::<serde_json::Value>(&bytes) {
|
||||
Ok(serde_json::Value::Object(mut m)) => {
|
||||
m.insert("server_time".into(), rfc3339(now()).into());
|
||||
if m.contains_key("error") {
|
||||
if m.get("error").is_some_and(serde_json::Value::is_object) {
|
||||
m.insert("request_id".into(), request_id.into());
|
||||
}
|
||||
serde_json::to_vec(&m).unwrap_or_else(|_| bytes.to_vec())
|
||||
@@ -503,14 +591,35 @@ pub fn router(st: Arc<AppState>) -> Router {
|
||||
post(h::stop_all_share_links),
|
||||
)
|
||||
.route("/api/v1/share-links/{id}", delete(h::stop_share_link))
|
||||
.route("/api/v1/certs", get(pki::get_certs))
|
||||
.route("/api/v1/certs/devices", post(pki::request_device))
|
||||
.route("/api/v1/certs/devices/{id}", delete(pki::revoke_device))
|
||||
.route("/api/v1/certs/devices/{id}/bundle", post(pki::make_bundle))
|
||||
.route("/api/v1/certs/intermediate/renewal", post(pki::renew))
|
||||
.route(
|
||||
"/api/v1/pki-requests/{id}",
|
||||
get(pki::get_request).delete(pki::cancel_request),
|
||||
)
|
||||
.route("/api/v1/pki-requests/{id}/csr", get(pki::get_csr))
|
||||
.route(
|
||||
"/api/v1/pki-requests/{id}/certificate",
|
||||
axum::routing::put(pki::put_certificate),
|
||||
)
|
||||
.route(
|
||||
"/api/v1/engine",
|
||||
get(pki::get_engine).post(pki::switch_engine),
|
||||
)
|
||||
.route("/api/v1/health", get(h::health))
|
||||
.route("/api/v1/openapi.json", get(h::openapi))
|
||||
.method_not_allowed_fallback(method_not_allowed)
|
||||
.fallback(not_found)
|
||||
.layer(middleware::from_fn_with_state(st.clone(), guard))
|
||||
.layer(DefaultBodyLimit::max(MAX_BODY))
|
||||
.with_state(st);
|
||||
api.layer(middleware::from_fn(finish))
|
||||
.with_state(st.clone());
|
||||
// In front of the guard: the built UI (static files, no token: they hold no data) and
|
||||
// the one-time device downloads (the link is the secret). Both still check the Host.
|
||||
api.layer(middleware::from_fn_with_state(st, ui::front))
|
||||
.layer(middleware::from_fn(finish))
|
||||
}
|
||||
|
||||
/// The OpenAPI document.
|
||||
|
||||
Reference in New Issue
Block a user