Pre-flight a specific operation before paying for a network round trip. The server is the authority and re-checks everything; this exists so an out-of-scope push fails with "your agent may not push to acme/api" instead of an opaque 403 after the upload.
(
resolved: &ResolvedDelegation,
permission: DelegationPermission,
resource: &ResourceRef<'_>,
identity_name: &str,
)
| 249 | /// out-of-scope push fails with "your agent may not push to acme/api" instead |
| 250 | /// of an opaque 403 after the upload. |
| 251 | pub fn check_permission( |
| 252 | resolved: &ResolvedDelegation, |
| 253 | permission: DelegationPermission, |
| 254 | resource: &ResourceRef<'_>, |
| 255 | identity_name: &str, |
| 256 | ) -> AuthResult<()> { |
| 257 | if resolved.delegation.allows(permission, resource) { |
| 258 | return Ok(()); |
| 259 | } |
| 260 | |
| 261 | let granted = resolved |
| 262 | .delegation |
| 263 | .scope |
| 264 | .permissions |
| 265 | .iter() |
| 266 | .map(|p| p.to_string()) |
| 267 | .collect::<Vec<_>>() |
| 268 | .join(", "); |
| 269 | |
| 270 | let mut message = format!( |
| 271 | "Agent '{identity_name}' is not authorized to {permission} here.\n \ |
| 272 | Granted: {granted}" |
| 273 | ); |
| 274 | if let Some(project) = resource.project { |
| 275 | if !resolved.delegation.scope.allows_project(project) { |
| 276 | message.push_str(&format!( |
| 277 | "\n Scoped to projects: {}\n Requested: {project}", |
| 278 | if resolved.delegation.scope.projects.is_empty() { |
| 279 | "(all)".to_string() |
| 280 | } else { |
| 281 | resolved.delegation.scope.projects.join(", ") |
| 282 | } |
| 283 | )); |
| 284 | } |
| 285 | } |
| 286 | message.push_str(&format!( |
| 287 | "\n Widen it with: atomic identity delegate {identity_name} --can {permission}" |
| 288 | )); |
| 289 | |
| 290 | Err(AuthError::DelegationError { message }) |
| 291 | } |
| 292 | |
| 293 | #[cfg(test)] |
| 294 | mod tests { |
no test coverage detected