Skip to content

Commit 484ae6b

Browse files
committed
code review feedback
1 parent 70a0144 commit 484ae6b

File tree

6 files changed

+39
-42
lines changed

6 files changed

+39
-42
lines changed

nexus/src/authz/api_resources.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// parts we need for authorization.
2121

2222
use super::actor::AnyActor;
23-
use super::context::Authorize;
23+
use super::context::AuthorizedResource;
2424
use super::roles::{
2525
load_roles_for_resource, load_roles_for_resource_tree, RoleSet,
2626
};
@@ -37,9 +37,7 @@ use uuid::Uuid;
3737

3838
/// Describes an authz resource that corresponds to an API resource that has a
3939
/// corresponding ResourceType and is stored in the database
40-
///
41-
/// This is a helper trait used to impl [`AuthzResource`].
42-
pub trait AuthzApiResource: Clone + Send + Sync + 'static {
40+
pub trait ApiResource: Clone + Send + Sync + 'static {
4341
/// If roles can be assigned to this resource, return the type and id of the
4442
/// database record describing this resource
4543
///
@@ -49,14 +47,14 @@ pub trait AuthzApiResource: Clone + Send + Sync + 'static {
4947
/// If this resource has a parent in the API hierarchy whose assigned roles
5048
/// can affect access to this resource, return the parent resource.
5149
/// Otherwise, returns `None`.
52-
fn parent(&self) -> Option<&dyn Authorize>;
50+
fn parent(&self) -> Option<&dyn AuthorizedResource>;
5351

5452
/// Returns an error as though this resource were not found, suitable for
5553
/// use when an actor should not be able to see that this resource exists
5654
fn not_found(&self) -> Error;
5755
}
5856

59-
impl<T: AuthzApiResource + oso::PolarClass> Authorize for T {
57+
impl<T: ApiResource + oso::PolarClass> AuthorizedResource for T {
6058
fn load_roles<'a, 'b, 'c, 'd, 'e, 'f>(
6159
&'a self,
6260
opctx: &'b OpContext,
@@ -81,21 +79,21 @@ impl<T: AuthzApiResource + oso::PolarClass> Authorize for T {
8179
error: Error,
8280
actor: AnyActor,
8381
action: Action,
84-
) -> Result<(), Error> {
82+
) -> Error {
8583
if action == Action::Read {
86-
return Err(self.not_found());
84+
return self.not_found();
8785
}
8886

8987
// If the user failed an authz check, and they can't even read this
9088
// resource, then we should produce a 404 rather than a 401/403.
9189
match authz.is_allowed(&actor, Action::Read, self) {
92-
Err(error) => Err(Error::internal_error(&format!(
90+
Err(error) => Error::internal_error(&format!(
9391
"failed to compute read authorization to determine visibility: \
9492
{:#}",
9593
error
96-
))),
97-
Ok(false) => Err(self.not_found()),
98-
Ok(true) => Err(error),
94+
)),
95+
Ok(false) => self.not_found(),
96+
Ok(true) => error,
9997
}
10098
}
10199
}
@@ -163,13 +161,13 @@ impl oso::PolarClass for Fleet {
163161
}
164162
}
165163

166-
impl Authorize for Fleet {
164+
impl AuthorizedResource for Fleet {
167165
fn load_roles<'a, 'b, 'c, 'd, 'e, 'f>(
168166
&'a self,
169-
opctx: &'b crate::context::OpContext,
170-
datastore: &'c crate::db::DataStore,
171-
authn: &'d crate::authn::Context,
172-
roleset: &'e mut super::roles::RoleSet,
167+
opctx: &'b OpContext,
168+
datastore: &'c DataStore,
169+
authn: &'d authn::Context,
170+
roleset: &'e mut RoleSet,
173171
) -> futures::future::BoxFuture<'f, Result<(), Error>>
174172
where
175173
'a: 'f,
@@ -195,8 +193,8 @@ impl Authorize for Fleet {
195193
error: Error,
196194
_: AnyActor,
197195
_: Action,
198-
) -> Result<(), Error> {
199-
Err(error)
196+
) -> Error {
197+
error
200198
}
201199
}
202200

@@ -232,12 +230,12 @@ impl oso::PolarClass for FleetChild {
232230
}
233231
}
234232

235-
impl AuthzApiResource for FleetChild {
233+
impl ApiResource for FleetChild {
236234
fn db_resource(&self) -> Option<(ResourceType, Uuid)> {
237235
None
238236
}
239237

240-
fn parent(&self) -> Option<&dyn Authorize> {
238+
fn parent(&self) -> Option<&dyn AuthorizedResource> {
241239
Some(&FLEET)
242240
}
243241

@@ -303,12 +301,12 @@ impl oso::PolarClass for Organization {
303301
}
304302
}
305303

306-
impl AuthzApiResource for Organization {
304+
impl ApiResource for Organization {
307305
fn db_resource(&self) -> Option<(ResourceType, Uuid)> {
308306
Some((ResourceType::Organization, self.organization_id))
309307
}
310308

311-
fn parent(&self) -> Option<&dyn Authorize> {
309+
fn parent(&self) -> Option<&dyn AuthorizedResource> {
312310
Some(&FLEET)
313311
}
314312

@@ -382,12 +380,12 @@ impl oso::PolarClass for Project {
382380
}
383381
}
384382

385-
impl AuthzApiResource for Project {
383+
impl ApiResource for Project {
386384
fn db_resource(&self) -> Option<(ResourceType, Uuid)> {
387385
Some((ResourceType::Project, self.project_id))
388386
}
389387

390-
fn parent(&self) -> Option<&dyn Authorize> {
388+
fn parent(&self) -> Option<&dyn AuthorizedResource> {
391389
Some(&self.parent)
392390
}
393391

@@ -438,13 +436,13 @@ impl oso::PolarClass for ProjectChild {
438436
}
439437
}
440438

441-
impl AuthzApiResource for ProjectChild {
439+
impl ApiResource for ProjectChild {
442440
fn db_resource(&self) -> Option<(ResourceType, Uuid)> {
443441
// We do not support assigning roles to children of Projects.
444442
None
445443
}
446444

447-
fn parent(&self) -> Option<&dyn Authorize> {
445+
fn parent(&self) -> Option<&dyn AuthorizedResource> {
448446
Some(&self.parent)
449447
}
450448

nexus/src/authz/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl Context {
7777
resource: Resource,
7878
) -> Result<(), Error>
7979
where
80-
Resource: oso::ToPolar + Authorize + Clone,
80+
Resource: AuthorizedResource + Clone,
8181
{
8282
let mut roles = RoleSet::new();
8383
resource
@@ -105,13 +105,13 @@ impl Context {
105105
}
106106
};
107107

108-
resource.on_unauthorized(&self.authz, error, actor, action)
108+
Err(resource.on_unauthorized(&self.authz, error, actor, action))
109109
}
110110
}
111111
}
112112
}
113113

114-
pub trait Authorize: Send + Sync + 'static {
114+
pub trait AuthorizedResource: oso::ToPolar + Send + Sync + 'static {
115115
/// Find all roles for the user described in `authn` that might be used to
116116
/// make an authorization decision on `self` (a resource)
117117
///
@@ -148,7 +148,7 @@ pub trait Authorize: Send + Sync + 'static {
148148
error: Error,
149149
actor: AnyActor,
150150
action: Action,
151-
) -> Result<(), Error>;
151+
) -> Error;
152152
}
153153

154154
#[cfg(test)]

nexus/src/authz/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub use api_resources::ProjectChild;
171171
pub use api_resources::FLEET;
172172

173173
mod context;
174-
pub use context::Authorize;
174+
pub use context::AuthorizedResource;
175175
pub use context::Authz;
176176
pub use context::Context;
177177

nexus/src/authz/oso_generic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::api_resources::FleetChild;
1111
use super::api_resources::Organization;
1212
use super::api_resources::Project;
1313
use super::api_resources::ProjectChild;
14-
use super::context::Authorize;
14+
use super::context::AuthorizedResource;
1515
use super::roles::RoleSet;
1616
use super::Authz;
1717
use crate::authn;
@@ -150,7 +150,7 @@ impl oso::PolarClass for Database {
150150
}
151151
}
152152

153-
impl Authorize for Database {
153+
impl AuthorizedResource for Database {
154154
fn load_roles<'a, 'b, 'c, 'd, 'e, 'f>(
155155
&'a self,
156156
_: &'b OpContext,
@@ -183,7 +183,7 @@ impl Authorize for Database {
183183
error: Error,
184184
_: AnyActor,
185185
_: Action,
186-
) -> Result<(), Error> {
187-
Err(error)
186+
) -> Error {
187+
error
188188
}
189189
}

nexus/src/authz/roles.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
//! its parent Organization or the parent Fleet. This isn't as large as it
1818
//! might sound, since the hierarchy is not _that_ deep, but we do have to make
1919
//! multiple database queries to load all this. This is all done by
20-
//! [`AuthzResource::fetch_all_related_roles_for_user()`]. This is really done
21-
//! by the impl of [`AuthzResource`] for [`AuthzApiResource`].
20+
//! [`load_roles_for_resource_tree`].
2221
//!
2322
//! Once we've got the complete list of roles, we include that in the data
2423
//! structure we pass into Oso. When it asks whether an actor has a role on a
@@ -35,7 +34,7 @@
3534
//! request, and we don't want that thread to block while we hit the database.
3635
//! Both of these issues could be addressed with considerably more work.
3736
38-
use super::api_resources::AuthzApiResource;
37+
use super::api_resources::ApiResource;
3938
use crate::authn;
4039
use crate::context::OpContext;
4140
use crate::db::DataStore;
@@ -93,7 +92,7 @@ pub async fn load_roles_for_resource_tree<R>(
9392
roleset: &mut RoleSet,
9493
) -> Result<(), Error>
9594
where
96-
R: AuthzApiResource,
95+
R: ApiResource,
9796
{
9897
// If roles can be assigned directly on this resource, load them.
9998
if let Some((resource_type, resource_id)) = resource.db_resource() {

nexus/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::db;
1212
use super::Nexus;
1313
use crate::authn::external::session_cookie::{Session, SessionStore};
1414
use crate::authn::Actor;
15-
use crate::authz::Authorize;
15+
use crate::authz::AuthorizedResource;
1616
use crate::db::model::ConsoleSession;
1717
use crate::db::DataStore;
1818
use async_trait::async_trait;
@@ -311,7 +311,7 @@ impl OpContext {
311311
resource: &Resource,
312312
) -> Result<(), Error>
313313
where
314-
Resource: oso::ToPolar + Authorize + Debug + Clone,
314+
Resource: AuthorizedResource + Debug + Clone,
315315
{
316316
/*
317317
* TODO-cleanup In an ideal world, Oso would consume &Action and

0 commit comments

Comments
 (0)