|
| 1 | +use axum::extract::FromRequestParts; |
| 2 | +use axum::http::{StatusCode, header, request::Parts}; |
| 3 | +use objectstore_service::BackendStream; |
| 4 | +use objectstore_service::{ObjectPath, StorageService}; |
| 5 | +use objectstore_types::Metadata; |
| 6 | + |
| 7 | +use super::{AuthContext, AuthError, Permission}; |
| 8 | +use crate::state::ServiceState; |
| 9 | + |
| 10 | +const BEARER_PREFIX: &str = "Bearer "; |
| 11 | + |
| 12 | +/// Wrapper around [`objectstore_service::StorageService`] that ensures each storage operation is |
| 13 | +/// authorized according to the request's authorization details. See also: [`AuthContext`]. |
| 14 | +/// |
| 15 | +/// When [`crate::config::AuthZ::enforce`] is false, authorization failures are logged but any |
| 16 | +/// unauthorized operations are still allowed to proceed. |
| 17 | +/// |
| 18 | +/// Objectstore API endpoints can use `AuthAwareService` simply by adding it to their handler |
| 19 | +/// function's argument list like so: |
| 20 | +/// ```no_run |
| 21 | +/// # use axum::extract::Path; |
| 22 | +/// # use axum::response::IntoResponse; |
| 23 | +/// # use axum::http::StatusCode; |
| 24 | +/// # use objectstore_server::{auth::AuthAwareService, error::ApiResult}; |
| 25 | +/// # use objectstore_service::ObjectPath; |
| 26 | +/// async fn delete_object( |
| 27 | +/// service: AuthAwareService, // <- Constructed automatically from request parts |
| 28 | +/// Path(path): Path<ObjectPath>, |
| 29 | +/// ) -> ApiResult<impl IntoResponse> { |
| 30 | +/// service.delete_object(&path).await?; |
| 31 | +/// |
| 32 | +/// Ok(StatusCode::NO_CONTENT) |
| 33 | +/// } |
| 34 | +/// ``` |
| 35 | +pub struct AuthAwareService { |
| 36 | + service: StorageService, |
| 37 | + |
| 38 | + enforce: bool, |
| 39 | + |
| 40 | + auth_context: Option<AuthContext>, |
| 41 | +} |
| 42 | + |
| 43 | +impl AuthAwareService { |
| 44 | + fn assert_authorized(&self, perm: Permission, path: &ObjectPath) -> anyhow::Result<()> { |
| 45 | + let auth_result = self |
| 46 | + .auth_context |
| 47 | + .as_ref() |
| 48 | + .ok_or(AuthError::VerificationFailure) |
| 49 | + .and_then(|ac| ac.assert_authorized(perm, path)); |
| 50 | + if self.enforce { |
| 51 | + return Ok(auth_result?); |
| 52 | + } |
| 53 | + Ok(()) |
| 54 | + } |
| 55 | + |
| 56 | + /// Auth-aware wrapper around [`StorageService::put_object`]. |
| 57 | + pub async fn put_object( |
| 58 | + &self, |
| 59 | + path: ObjectPath, |
| 60 | + metadata: &Metadata, |
| 61 | + stream: BackendStream, |
| 62 | + ) -> anyhow::Result<ObjectPath> { |
| 63 | + self.assert_authorized(Permission::ObjectWrite, &path)?; |
| 64 | + |
| 65 | + self.service.put_object(path, metadata, stream).await |
| 66 | + } |
| 67 | + |
| 68 | + /// Auth-aware wrapper around [`StorageService::get_object`]. |
| 69 | + pub async fn get_object( |
| 70 | + &self, |
| 71 | + path: &ObjectPath, |
| 72 | + ) -> anyhow::Result<Option<(Metadata, BackendStream)>> { |
| 73 | + self.assert_authorized(Permission::ObjectRead, path)?; |
| 74 | + |
| 75 | + self.service.get_object(path).await |
| 76 | + } |
| 77 | + |
| 78 | + /// Auth-aware wrapper around [`StorageService::delete_object`]. |
| 79 | + pub async fn delete_object(&self, path: &ObjectPath) -> anyhow::Result<()> { |
| 80 | + self.assert_authorized(Permission::ObjectDelete, path)?; |
| 81 | + |
| 82 | + self.service.delete_object(path).await |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl FromRequestParts<ServiceState> for AuthAwareService { |
| 87 | + type Rejection = StatusCode; |
| 88 | + |
| 89 | + async fn from_request_parts( |
| 90 | + parts: &mut Parts, |
| 91 | + state: &ServiceState, |
| 92 | + ) -> Result<Self, Self::Rejection> { |
| 93 | + let encoded_token = parts |
| 94 | + .headers |
| 95 | + .get(header::AUTHORIZATION) |
| 96 | + .and_then(|v| v.to_str().ok()) |
| 97 | + // TODO: Handle case-insensitive bearer prefix |
| 98 | + .and_then(|v| v.strip_prefix(BEARER_PREFIX)); |
| 99 | + |
| 100 | + let auth_context = AuthContext::from_encoded_jwt(encoded_token, &state.config.auth); |
| 101 | + if auth_context.is_err() && state.config.auth.enforce { |
| 102 | + tracing::debug!("Authorization failed when enforcement is enabled"); |
| 103 | + return Err(StatusCode::UNAUTHORIZED); |
| 104 | + } |
| 105 | + |
| 106 | + Ok(AuthAwareService { |
| 107 | + service: state.authless_service.clone(), |
| 108 | + enforce: state.config.auth.enforce, |
| 109 | + auth_context: auth_context.ok(), |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments