Skip to content

Commit

Permalink
Unify diesel error conversions under 'public_error_from_diesel_pool' (#…
Browse files Browse the repository at this point in the history
…647)

- Unifies diesel error conversions under `public_error_from_diesel_pool`
- The function now takes an `ErrorHandler` as an argument, which allows callers to customize
the user-level errors that may be returned.
  • Loading branch information
smklein authored Jan 29, 2022
1 parent 908599d commit c4e76cb
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 244 deletions.
19 changes: 18 additions & 1 deletion nexus/src/authz/api_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,22 @@ pub trait ApiResource: Clone + Send + Sync + 'static {
/// can affect access to this resource, return the parent resource.
/// Otherwise, returns `None`.
fn parent(&self) -> Option<&dyn AuthorizedResource>;
}

/// Practically, all objects which implement [`ApiResourceError`]
/// also implement [`ApiResource`]. However, [`ApiResource`] is not object
/// safe because it implements [`std::clone::Clone`].
///
/// This allows callers to use [`ApiResourceError`] as a trait object.
pub trait ApiResourceError {
/// Returns an error as though this resource were not found, suitable for
/// use when an actor should not be able to see that this resource exists
fn not_found(&self) -> Error;
}

impl<T: ApiResource + oso::PolarClass> AuthorizedResource for T {
impl<T: ApiResource + ApiResourceError + oso::PolarClass> AuthorizedResource
for T
{
fn load_roles<'a, 'b, 'c, 'd, 'e, 'f>(
&'a self,
opctx: &'b OpContext,
Expand Down Expand Up @@ -238,7 +247,9 @@ impl ApiResource for FleetChild {
fn parent(&self) -> Option<&dyn AuthorizedResource> {
Some(&FLEET)
}
}

impl ApiResourceError for FleetChild {
fn not_found(&self) -> Error {
self.lookup_type.clone().into_not_found(self.resource_type)
}
Expand Down Expand Up @@ -309,7 +320,9 @@ impl ApiResource for Organization {
fn parent(&self) -> Option<&dyn AuthorizedResource> {
Some(&FLEET)
}
}

impl ApiResourceError for Organization {
fn not_found(&self) -> Error {
self.lookup_type.clone().into_not_found(ResourceType::Organization)
}
Expand Down Expand Up @@ -393,7 +406,9 @@ impl ApiResource for Project {
fn parent(&self) -> Option<&dyn AuthorizedResource> {
Some(&self.parent)
}
}

impl ApiResourceError for Project {
fn not_found(&self) -> Error {
self.lookup_type.clone().into_not_found(ResourceType::Project)
}
Expand Down Expand Up @@ -450,7 +465,9 @@ impl ApiResource for ProjectChild {
fn parent(&self) -> Option<&dyn AuthorizedResource> {
Some(&self.parent)
}
}

impl ApiResourceError for ProjectChild {
fn not_found(&self) -> Error {
self.lookup_type.clone().into_not_found(self.resource_type)
}
Expand Down
2 changes: 1 addition & 1 deletion nexus/src/authz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
mod actor;

mod api_resources;
pub use api_resources::ApiResource;
pub use api_resources::ApiResourceError;
pub use api_resources::Fleet;
pub use api_resources::FleetChild;
pub use api_resources::Organization;
Expand Down
2 changes: 1 addition & 1 deletion nexus/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub struct OpContext {
kind: OpKind,
}

pub enum OpKind {
enum OpKind {
/// Handling an external API request
ExternalApiRequest,
/// Background operations in Nexus
Expand Down
Loading

0 comments on commit c4e76cb

Please sign in to comment.