-
Notifications
You must be signed in to change notification settings - Fork 40
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
authz: protect various endpoints #790
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,7 +135,8 @@ impl Fleet { | |
} | ||
|
||
/// Returns an authz resource representing some other kind of child (e.g., | ||
/// a built-in user, built-in role, etc. -- but _not_ an Organization) | ||
/// a built-in user, built-in role, etc. -- but _not_ an Organization or | ||
/// Sled) | ||
/// | ||
/// Aside from Organizations (which you create with | ||
/// [`Fleet::organization()`] instead), all instances of all types of Fleet | ||
|
@@ -149,6 +150,11 @@ impl Fleet { | |
) -> FleetChild { | ||
FleetChild { resource_type, lookup_type } | ||
} | ||
|
||
/// Returns an authz resource representing a Sled | ||
pub fn sled(&self, sled_id: Uuid, lookup_type: LookupType) -> Sled { | ||
Sled { sled_id, lookup_type } | ||
} | ||
} | ||
|
||
impl Eq for Fleet {} | ||
|
@@ -255,6 +261,52 @@ impl ApiResourceError for FleetChild { | |
} | ||
} | ||
|
||
/// Represents a Sled for authz purposes | ||
/// | ||
/// This object is used for authorization checks on such resources by passing | ||
/// this as the `resource` argument to | ||
/// [`crate::context::OpContext::authorize()`]. You construct one of these | ||
/// using [`Fleet::sled()`]. | ||
#[derive(Clone, Debug)] | ||
pub struct Sled { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to keep things simple and put sled permissions under Note that I want to refactor all the authz API resources to be more type-safe, but that'll be a bigger (future) change. |
||
sled_id: Uuid, | ||
lookup_type: LookupType, | ||
} | ||
|
||
impl Sled { | ||
pub fn id(&self) -> Uuid { | ||
self.sled_id | ||
} | ||
} | ||
|
||
impl oso::PolarClass for Sled { | ||
fn get_polar_class_builder() -> oso::ClassBuilder<Self> { | ||
oso::Class::builder() | ||
.add_method( | ||
"has_role", | ||
// Roles are not supported on Sleds today. | ||
|_: &Sled, _: AuthenticatedActor, _: String| false, | ||
) | ||
.add_attribute_getter("fleet", |_: &Sled| FLEET) | ||
} | ||
} | ||
|
||
impl ApiResource for Sled { | ||
fn db_resource(&self) -> Option<(ResourceType, Uuid)> { | ||
None | ||
} | ||
|
||
fn parent(&self) -> Option<&dyn AuthorizedResource> { | ||
Some(&FLEET) | ||
} | ||
} | ||
|
||
impl ApiResourceError for Sled { | ||
fn not_found(&self) -> Error { | ||
self.lookup_type.clone().into_not_found(ResourceType::Sled) | ||
} | ||
} | ||
|
||
/// Represents a [`crate::db::model::Organization`] for authz purposes | ||
/// | ||
/// This object is used for authorization checks on an Organization by passing | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ has_role(actor: AuthenticatedActor, "init", _resource: Database) | |
# | ||
# - fleet.admin (superuser for the whole system) | ||
# - fleet.collaborator (can create and own orgs) | ||
# - fleet.viewer (can read fleet-wide data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this new "Fleet Viewer" role for the new "internal-read" user (described later). |
||
# - organization.admin (complete control over an organization) | ||
# - organization.collaborator (can create, modify, and delete projects) | ||
# - project.admin (complete control over a project) | ||
|
@@ -94,14 +95,17 @@ resource Fleet { | |
"create_child", | ||
]; | ||
|
||
roles = [ "admin", "collaborator" ]; | ||
roles = [ "admin", "collaborator", "viewer" ]; | ||
|
||
# Fleet viewers can view Fleet-wide data | ||
"list_children" if "viewer"; | ||
"read" if "viewer"; | ||
|
||
# Fleet collaborators can create Organizations and see fleet-wide | ||
# information, including Organizations that they don't have permissions | ||
# on. (They cannot list projects within those organizations, however.) | ||
# They cannot modify fleet-wide information. | ||
"list_children" if "collaborator"; | ||
"read" if "collaborator"; | ||
"viewer" if "collaborator"; | ||
"create_child" if "collaborator"; | ||
|
||
# Fleet administrators are whole-system superusers. | ||
|
@@ -185,7 +189,7 @@ resource ProjectChild { | |
} | ||
|
||
# Similarly, we use a generic resource to represent every kind of fleet-wide | ||
# resource that's not part of the Organization/Project hierarchy. | ||
# resource that's not part of the Organization/Project hierarchy and not a Sled. | ||
resource FleetChild { | ||
permissions = [ | ||
"list_children", | ||
|
@@ -195,8 +199,23 @@ resource FleetChild { | |
]; | ||
|
||
relations = { parent_fleet: Fleet }; | ||
"list_children" if "admin" on "parent_fleet"; | ||
"read" if "admin" on "parent_fleet"; | ||
"list_children" if "viewer" on "parent_fleet"; | ||
"read" if "viewer" on "parent_fleet"; | ||
"modify" if "admin" on "parent_fleet"; | ||
"create_child" if "admin" on "parent_fleet"; | ||
} | ||
|
||
resource Sled { | ||
permissions = [ | ||
"list_children", | ||
"modify", | ||
"read", | ||
"create_child", | ||
]; | ||
|
||
relations = { parent_fleet: Fleet }; | ||
"list_children" if "viewer" on "parent_fleet"; | ||
"read" if "viewer" on "parent_fleet"; | ||
"modify" if "admin" on "parent_fleet"; | ||
"create_child" if "admin" on "parent_fleet"; | ||
} | ||
|
@@ -210,6 +229,8 @@ has_relation(project: Project, "parent_project", project_child: ProjectChild) | |
if project_child.project = project; | ||
has_relation(fleet: Fleet, "parent_fleet", fleet_child: FleetChild) | ||
if fleet_child.fleet = fleet; | ||
has_relation(fleet: Fleet, "parent_fleet", sled: Sled) | ||
if sled.fleet = fleet; | ||
|
||
# Define role relationships | ||
has_role(actor: AuthenticatedActor, role: String, resource: Resource) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,30 +158,34 @@ impl DataStore { | |
|
||
pub async fn sled_list( | ||
&self, | ||
opctx: &OpContext, | ||
pagparams: &DataPageParams<'_, Uuid>, | ||
) -> ListResultVec<Sled> { | ||
opctx.authorize(authz::Action::Read, &authz::FLEET).await?; | ||
use db::schema::sled::dsl; | ||
paginated(dsl::sled, dsl::id, pagparams) | ||
.select(Sled::as_select()) | ||
.load_async(self.pool()) | ||
.load_async(self.pool_authorized(opctx).await?) | ||
.await | ||
.map_err(|e| public_error_from_diesel_pool(e, ErrorHandler::Server)) | ||
} | ||
|
||
pub async fn sled_fetch(&self, id: Uuid) -> LookupResult<Sled> { | ||
pub async fn sled_fetch( | ||
&self, | ||
opctx: &OpContext, | ||
authz_sled: &authz::Sled, | ||
) -> LookupResult<Sled> { | ||
opctx.authorize(authz::Action::Read, authz_sled).await?; | ||
use db::schema::sled::dsl; | ||
dsl::sled | ||
.filter(dsl::id.eq(id)) | ||
.filter(dsl::id.eq(authz_sled.id())) | ||
.select(Sled::as_select()) | ||
.first_async(self.pool()) | ||
.first_async(self.pool_authorized(opctx).await?) | ||
.await | ||
.map_err(|e| { | ||
public_error_from_diesel_pool( | ||
e, | ||
ErrorHandler::NotFoundByLookup( | ||
ResourceType::Sled, | ||
LookupType::ById(id), | ||
), | ||
ErrorHandler::NotFoundByResource(authz_sled), | ||
) | ||
}) | ||
} | ||
|
@@ -3153,6 +3157,7 @@ impl DataStore { | |
// Note: "db_init" is also a builtin user, but that one by necessity | ||
// is created with the database. | ||
&*authn::USER_INTERNAL_API, | ||
&*authn::USER_INTERNAL_READ, | ||
&*authn::USER_SAGA_RECOVERY, | ||
&*authn::USER_TEST_PRIVILEGED, | ||
&*authn::USER_TEST_UNPRIVILEGED, | ||
|
@@ -3338,30 +3343,37 @@ impl DataStore { | |
|
||
pub async fn update_available_artifact_upsert( | ||
&self, | ||
opctx: &OpContext, | ||
artifact: UpdateAvailableArtifact, | ||
) -> CreateResult<UpdateAvailableArtifact> { | ||
opctx.authorize(authz::Action::Modify, &authz::FLEET).await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @iliana here's one of the couple of authz checks added for updates. I started as restrictive as possible: you basically have to have a near-superuser privilege on the fleet-level ("Fleet Collaborator" or "Fleet Admin") to have this permission. I imagine we'll want to rethink this but I hope this will work for now. |
||
|
||
use db::schema::update_available_artifact::dsl; | ||
diesel::insert_into(dsl::update_available_artifact) | ||
.values(artifact.clone()) | ||
.on_conflict((dsl::name, dsl::version, dsl::kind)) | ||
.do_update() | ||
.set(artifact.clone()) | ||
.returning(UpdateAvailableArtifact::as_returning()) | ||
.get_result_async(self.pool()) | ||
.get_result_async(self.pool_authorized(opctx).await?) | ||
.await | ||
.map_err(|e| public_error_from_diesel_pool(e, ErrorHandler::Server)) | ||
} | ||
|
||
pub async fn update_available_artifact_hard_delete_outdated( | ||
&self, | ||
opctx: &OpContext, | ||
current_targets_role_version: i64, | ||
) -> DeleteResult { | ||
// We use the `targets_role_version` column in the table to delete any old rows, keeping | ||
// the table in sync with the current copy of artifacts.json. | ||
opctx.authorize(authz::Action::Modify, &authz::FLEET).await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CC @iliana |
||
|
||
// We use the `targets_role_version` column in the table to delete any | ||
// old rows, keeping the table in sync with the current copy of | ||
// artifacts.json. | ||
use db::schema::update_available_artifact::dsl; | ||
diesel::delete(dsl::update_available_artifact) | ||
.filter(dsl::targets_role_version.lt(current_targets_role_version)) | ||
.execute_async(self.pool()) | ||
.execute_async(self.pool_authorized(opctx).await?) | ||
.await | ||
.map(|_rows_deleted| ()) | ||
.map_err(|e| { | ||
|
@@ -3374,8 +3386,11 @@ impl DataStore { | |
|
||
pub async fn update_available_artifact_fetch( | ||
&self, | ||
opctx: &OpContext, | ||
artifact: &UpdateArtifact, | ||
) -> LookupResult<UpdateAvailableArtifact> { | ||
opctx.authorize(authz::Action::Read, &authz::FLEET).await?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CC @iliana |
||
|
||
use db::schema::update_available_artifact::dsl; | ||
dsl::update_available_artifact | ||
.filter( | ||
|
@@ -3385,7 +3400,7 @@ impl DataStore { | |
.and(dsl::kind.eq(UpdateArtifactKind(artifact.kind))), | ||
) | ||
.select(UpdateAvailableArtifact::as_select()) | ||
.first_async(self.pool()) | ||
.first_async(self.pool_authorized(opctx).await?) | ||
.await | ||
.map_err(|e| { | ||
Error::internal_error(&format!( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,15 @@ lazy_static! { | |
"used by Nexus when handling internal API requests", | ||
); | ||
|
||
/// Internal user used by Nexus to read privileged control plane data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a new built-in user called "internal-read". This is used by Nexus when it needs to read control plane data that an ordinary user wouldn't have access to. For example, when a user provisions an Instance (or really any time we need to make a request to a Sled Agent), we need to look up information about the Sled in the database. That's now protected by authz. But the end user making the request probably doesn't have permission to read information about sleds. Instead, Nexus uses this new "internal-read" user. |
||
pub static ref USER_INTERNAL_READ: UserBuiltinConfig = | ||
UserBuiltinConfig::new_static( | ||
// "4ead" looks like "read" | ||
"001de000-05e4-4000-8000-000000004ead", | ||
"internal-read", | ||
"used by Nexus to read privileged control plane data", | ||
); | ||
|
||
/// Internal user used by Nexus when recovering sagas | ||
pub static ref USER_SAGA_RECOVERY: UserBuiltinConfig = | ||
UserBuiltinConfig::new_static( | ||
|
@@ -83,6 +92,7 @@ mod test { | |
use super::super::assert_valid_uuid; | ||
use super::USER_DB_INIT; | ||
use super::USER_INTERNAL_API; | ||
use super::USER_INTERNAL_READ; | ||
use super::USER_SAGA_RECOVERY; | ||
use super::USER_TEST_PRIVILEGED; | ||
use super::USER_TEST_UNPRIVILEGED; | ||
|
@@ -91,6 +101,7 @@ mod test { | |
fn test_builtin_user_ids_are_valid() { | ||
assert_valid_uuid(&USER_DB_INIT.id); | ||
assert_valid_uuid(&USER_INTERNAL_API.id); | ||
assert_valid_uuid(&USER_INTERNAL_READ.id); | ||
assert_valid_uuid(&USER_SAGA_RECOVERY.id); | ||
assert_valid_uuid(&USER_TEST_PRIVILEGED.id); | ||
assert_valid_uuid(&USER_TEST_UNPRIVILEGED.id); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this file are all part of adding a new "internal-read" user (described later).