chore: rustfmt the workspace and make clippy -D warnings pass

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 19:19:21 +07:00
parent d7fe99edd7
commit f13f0e6953
46 changed files with 6606 additions and 1496 deletions

View File

@@ -15,14 +15,23 @@ fn now() -> i64 {
chrono::Utc::now().timestamp()
}
fn audit(state: &HubState, actor: &OperatorActor, resource: AuditResource, allowed: bool) -> Result<(), StatusCode> {
fn audit(
state: &HubState,
actor: &OperatorActor,
resource: AuditResource,
allowed: bool,
) -> Result<(), StatusCode> {
state
.db
.audit_event(
&actor.principal,
AuditAction::Authorization,
resource,
if allowed { AuditOutcome::Allowed } else { AuditOutcome::Denied },
if allowed {
AuditOutcome::Allowed
} else {
AuditOutcome::Denied
},
actor.request_id,
)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
@@ -38,24 +47,42 @@ fn internal(error: impl std::fmt::Display) -> StatusCode {
pub(super) fn admin_only(state: &HubState, actor: &OperatorActor) -> Result<(), StatusCode> {
let allowed = actor.subject.admin;
audit(state, actor, AuditResource::Api, allowed)?;
if allowed { Ok(()) } else { Err(StatusCode::FORBIDDEN) }
if allowed {
Ok(())
} else {
Err(StatusCode::FORBIDDEN)
}
}
/// Use `permission` on one orbit. Not visible (no `orbit.read`) is 404, visible
/// but not permitted is 403. A permission that cannot be granted is admin-only.
pub(super) fn scoped(state: &HubState, actor: &OperatorActor, permission: Permission, orbit: Uuid) -> Result<(), StatusCode> {
pub(super) fn scoped(
state: &HubState,
actor: &OperatorActor,
permission: Permission,
orbit: Uuid,
) -> Result<(), StatusCode> {
let resource = AuditResource::Orbit(orbit);
if actor.subject.admin {
return audit(state, actor, resource, true);
}
let held = |permission: Permission| -> Result<bool, StatusCode> {
permission.grantable().then(|| {
state
.db
.has_permission(actor.subject.user_id.as_deref(), &actor.subject.groups, permission.as_str(), &orbit.to_string(), now())
.map_err(internal)
})
.unwrap_or(Ok(false))
if permission.grantable() {
{
state
.db
.has_permission(
actor.subject.user_id.as_deref(),
&actor.subject.groups,
permission.as_str(),
&orbit.to_string(),
now(),
)
.map_err(internal)
}
} else {
Ok(false)
}
};
if !held(Permission::OrbitRead)? {
audit(state, actor, resource, false)?;
@@ -63,40 +90,67 @@ pub(super) fn scoped(state: &HubState, actor: &OperatorActor, permission: Permis
}
let allowed = held(permission)?;
audit(state, actor, resource, allowed)?;
if allowed { Ok(()) } else { Err(StatusCode::FORBIDDEN) }
if allowed {
Ok(())
} else {
Err(StatusCode::FORBIDDEN)
}
}
/// Use one relation. Administrators always; anyone else only through a live grant on that very relation
/// (not on its orbit, not on an older version of it). Everything else answers 404, so a relation's
/// existence is not confirmed to someone who cannot use it.
#[allow(dead_code)] // used by the refresh run (M6c); the matrix already exercises the list built on the same grants
pub(super) fn relation(state: &HubState, actor: &OperatorActor, relation: Uuid) -> Result<(), StatusCode> {
pub(super) fn relation(
state: &HubState,
actor: &OperatorActor,
relation: Uuid,
) -> Result<(), StatusCode> {
let resource = AuditResource::Relation(relation);
if actor.subject.admin {
return audit(state, actor, resource, true);
}
let granted = state
.db
.granted_relations(actor.subject.user_id.as_deref(), &actor.subject.groups, now())
.granted_relations(
actor.subject.user_id.as_deref(),
&actor.subject.groups,
now(),
)
.map_err(internal)?;
let allowed = granted.iter().any(|id| id == &relation.to_string());
audit(state, actor, resource, allowed)?;
if allowed { Ok(()) } else { Err(StatusCode::NOT_FOUND) }
if allowed {
Ok(())
} else {
Err(StatusCode::NOT_FOUND)
}
}
/// The relations a caller may use: `None` for an administrator (all), otherwise their granted ids.
/// A caller with none gets 403 rather than an empty list.
pub(super) fn visible_relations(state: &HubState, actor: &OperatorActor) -> Result<Option<Vec<String>>, StatusCode> {
pub(super) fn visible_relations(
state: &HubState,
actor: &OperatorActor,
) -> Result<Option<Vec<String>>, StatusCode> {
if actor.subject.admin {
audit(state, actor, AuditResource::Api, true)?;
return Ok(None);
}
let granted = state
.db
.granted_relations(actor.subject.user_id.as_deref(), &actor.subject.groups, now())
.granted_relations(
actor.subject.user_id.as_deref(),
&actor.subject.groups,
now(),
)
.map_err(internal)?;
audit(state, actor, AuditResource::Api, !granted.is_empty())?;
if granted.is_empty() { Err(StatusCode::FORBIDDEN) } else { Ok(Some(granted)) }
if granted.is_empty() {
Err(StatusCode::FORBIDDEN)
} else {
Ok(Some(granted))
}
}
/// Which orbits a list may show.
@@ -124,7 +178,11 @@ impl Visible {
/// Orbits where the caller holds both `orbit.read` and `permission`. A caller
/// with nothing to see gets 403 rather than an empty list.
pub(super) fn visible(state: &HubState, actor: &OperatorActor, permission: Permission) -> Result<Visible, StatusCode> {
pub(super) fn visible(
state: &HubState,
actor: &OperatorActor,
permission: Permission,
) -> Result<Visible, StatusCode> {
if actor.subject.admin {
audit(state, actor, AuditResource::Api, true)?;
return Ok(Visible::All);
@@ -135,12 +193,25 @@ pub(super) fn visible(state: &HubState, actor: &OperatorActor, permission: Permi
}
state
.db
.permitted_orbits(actor.subject.user_id.as_deref(), &actor.subject.groups, permission.as_str(), now())
.permitted_orbits(
actor.subject.user_id.as_deref(),
&actor.subject.groups,
permission.as_str(),
now(),
)
.map_err(internal)
};
let read = fetch(Permission::OrbitRead)?;
let wanted = if permission == Permission::OrbitRead { None } else { Some(fetch(permission)?) };
let parse = |set: &HashSet<String>| -> HashSet<Uuid> { set.iter().filter_map(|id| Uuid::parse_str(id).ok()).collect() };
let wanted = if permission == Permission::OrbitRead {
None
} else {
Some(fetch(permission)?)
};
let parse = |set: &HashSet<String>| -> HashSet<Uuid> {
set.iter()
.filter_map(|id| Uuid::parse_str(id).ok())
.collect()
};
let result = match wanted {
None if read.everywhere => Visible::All,
None => Visible::Only(parse(&read.orbits)),
@@ -148,10 +219,19 @@ pub(super) fn visible(state: &HubState, actor: &OperatorActor, permission: Permi
(true, true) => Visible::All,
(true, false) => Visible::Only(parse(&w.orbits)),
(false, true) => Visible::Only(parse(&read.orbits)),
(false, false) => Visible::Only(parse(&read.orbits).intersection(&parse(&w.orbits)).copied().collect()),
(false, false) => Visible::Only(
parse(&read.orbits)
.intersection(&parse(&w.orbits))
.copied()
.collect(),
),
},
};
let nothing = matches!(&result, Visible::Only(set) if set.is_empty());
audit(state, actor, AuditResource::Api, !nothing)?;
if nothing { Err(StatusCode::FORBIDDEN) } else { Ok(result) }
if nothing {
Err(StatusCode::FORBIDDEN)
} else {
Ok(result)
}
}