Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify diesel error conversions under 'public_error_from_diesel_pool' #647

Merged
merged 6 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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