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

store role assignments in the database #520

Merged
merged 60 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0b68b13
populate built-in roles into database
davepacheco Dec 7, 2021
b003327
Merge branch 'main' into authz-roles
davepacheco Dec 10, 2021
22ce267
lots of rework
davepacheco Dec 10, 2021
9aa6369
more work
davepacheco Dec 10, 2021
0635bb3
fix pagination condition
davepacheco Dec 10, 2021
a006cd4
give up on diesel
davepacheco Dec 13, 2021
28c2a21
remove cruft
davepacheco Dec 13, 2021
d1f8019
starting test
davepacheco Dec 13, 2021
b481442
fix broken tests
davepacheco Dec 13, 2021
abf9e26
flesh out test
davepacheco Dec 13, 2021
a163fb9
merge fix
davepacheco Dec 14, 2021
7983cd9
fix up roles test
davepacheco Dec 14, 2021
5d53966
fix up iter impl
davepacheco Dec 14, 2021
0841cf3
remove duplication in populate()
davepacheco Dec 14, 2021
47196c5
add API to fetch one built-in role
davepacheco Dec 14, 2021
eb48065
remove vestigial oso patch
davepacheco Dec 14, 2021
3591dca
remove spurious delta
davepacheco Dec 14, 2021
d69d119
fix style
davepacheco Dec 14, 2021
0f6a9fc
add role assignments to the database
davepacheco Dec 14, 2021
ecaed59
some work
davepacheco Dec 14, 2021
a4170c5
tests pass, no more hardcoded test user policies
davepacheco Dec 15, 2021
1a9ca7c
fix some tests
davepacheco Dec 15, 2021
fd247a1
fix test, commonize code, split out actors
davepacheco Dec 15, 2021
04e58f9
fix more tests
davepacheco Dec 15, 2021
2b208b8
clean up some role impl
davepacheco Dec 15, 2021
dfd9271
todos
davepacheco Dec 15, 2021
432550b
move, clean up, and document API resources
davepacheco Dec 15, 2021
fb81b27
more cleanup and docs
davepacheco Dec 15, 2021
4c8ca6d
compiler panic
davepacheco Dec 15, 2021
71f45c9
document authz and roles a bit better
davepacheco Dec 16, 2021
78651ad
doc cleanup
davepacheco Dec 16, 2021
8184cb8
clean up db-init authz
davepacheco Dec 16, 2021
98ec479
doc edits
davepacheco Dec 16, 2021
b01cc67
some doc fixes
davepacheco Dec 16, 2021
29d9063
fix style
davepacheco Dec 16, 2021
f410fd6
Add multi-column pagination support
smklein Dec 16, 2021
ee21650
Add unused attr, fix comment typo
smklein Dec 16, 2021
c00fd91
fix docs; figured out unexpected Oso behavior
davepacheco Dec 16, 2021
4df6a71
doc nits
davepacheco Dec 16, 2021
96cb462
remove xxx
davepacheco Dec 16, 2021
f177556
review feedback
davepacheco Dec 16, 2021
7799728
add newtype for role names
davepacheco Dec 16, 2021
a4524ba
don't parse role name resource type
davepacheco Dec 17, 2021
bd843dd
Add tests
smklein Dec 17, 2021
aa8837b
Remove errant println
smklein Dec 17, 2021
1207b13
Merge branch 'main' into pag_multicolumn
smklein Dec 17, 2021
38fccbf
review feedback: underscores to hyphens, extra comment
davepacheco Dec 17, 2021
600c9fa
Merge remote-tracking branch 'origin/main' into authz-roles
davepacheco Dec 17, 2021
59e6bfd
fix mismerge
davepacheco Dec 17, 2021
346666f
Merge remote-tracking branch 'origin/pag_multicolumn' into authz-roles
davepacheco Dec 17, 2021
7c8f57b
use new paginated_multicolumn
davepacheco Dec 17, 2021
0f7ee09
remove vestigial comment
davepacheco Dec 17, 2021
3f322dc
Merge remote-tracking branch 'origin/authz-roles' into authz-role-ass…
davepacheco Dec 17, 2021
00ac778
review feedback
davepacheco Dec 17, 2021
1788bab
Merge remote-tracking branch 'origin/main' into authz-role-assignments
davepacheco Dec 17, 2021
a920a06
Merge remote-tracking branch 'origin/main' into authz-role-assignments
davepacheco Dec 17, 2021
9d12b9e
example could be a bit better
davepacheco Dec 20, 2021
2ffc5b9
remove vestigial AnyActor Eq/PartialEq
davepacheco Dec 21, 2021
7020feb
clarify Perm struct
davepacheco Dec 21, 2021
cc17658
Merge branch 'main' into authz-role-assignments
davepacheco Dec 21, 2021
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
35 changes: 34 additions & 1 deletion common/src/sql/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,10 @@ CREATE INDEX ON omicron.public.console_session (
/*******************************************************************/

/*
* IAM
* Identity and Access Management (IAM)
*
* **For more details and a worked example using the tables here, see the
* documentation for the omicron_nexus crate, "authz" module.**
*/

/*
Expand Down Expand Up @@ -757,6 +760,36 @@ CREATE TABLE omicron.public.role_builtin (
PRIMARY KEY(resource_type, role_name)
);

/*
* Assignments between users, roles, and resources
*
* A built-in user has role on a resource if there's a record in this table that
* points to that user, role, and resource.
*
* For more details and a worked example, see the omicron_nexus::authz
* module-level documentation.
*/

CREATE TABLE omicron.public.role_assignment_builtin (
/* Composite foreign key into "role_builtin" table */
resource_type STRING(63) NOT NULL,
role_name STRING(63) NOT NULL,

/*
* Foreign key into some other resource table. Which table? This is
* identified implicitly by "resource_type" above.
*/
resource_id UUID NOT NULL,

/*
* Foreign key into table of built-in users.
*/
user_builtin_id UUID NOT NULL,

/* The entire row is the primary key. */
PRIMARY KEY(user_builtin_id, resource_type, resource_id, role_name)
);

/*******************************************************************/

/*
Expand Down
84 changes: 84 additions & 0 deletions nexus/src/authz/actor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Oso integration for Actor types

use super::roles::RoleSet;
use crate::authn;
use omicron_common::api::external::ResourceType;
use uuid::Uuid;

/// Represents [`authn::Context`] (which is either an authenticated or
/// unauthenticated actor) for Polar
#[derive(Clone, Debug)]
pub struct AnyActor {
authenticated: bool,
actor_id: Option<Uuid>,
roles: RoleSet,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called AnyActor instead of Actor?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to better distinguish it from AuthenticatedActor.

(That predates this change -- this code just moved.)


impl AnyActor {
pub fn new(authn: &authn::Context, roles: RoleSet) -> Self {
let actor = authn.actor();
AnyActor {
authenticated: actor.is_some(),
actor_id: actor.map(|a| a.0),
roles,
}
}
}

impl oso::PolarClass for AnyActor {
fn get_polar_class_builder() -> oso::ClassBuilder<Self> {
oso::Class::builder()
.add_attribute_getter("authenticated", |a: &AnyActor| {
a.authenticated
})
.add_attribute_getter("authn_actor", |a: &AnyActor| {
a.actor_id.map(|actor_id| AuthenticatedActor {
actor_id,
roles: a.roles.clone(),
})
})
}
}

/// Represents an authenticated [`authn::Context`] for Polar
#[derive(Clone, Debug)]
pub struct AuthenticatedActor {
actor_id: Uuid,
roles: RoleSet,
}

impl AuthenticatedActor {
/// Returns whether this actor has the given role for the given resource
pub fn has_role_resource(
&self,
resource_type: ResourceType,
resource_id: Uuid,
role: &str,
) -> bool {
self.roles.has_role(resource_type, resource_id, role)
}
}

impl PartialEq for AuthenticatedActor {
fn eq(&self, other: &Self) -> bool {
self.actor_id == other.actor_id
}
}

impl Eq for AuthenticatedActor {}

impl oso::PolarClass for AuthenticatedActor {
fn get_polar_class_builder() -> oso::ClassBuilder<Self> {
oso::Class::builder().with_equality_check().add_constant(
AuthenticatedActor {
actor_id: authn::USER_DB_INIT.id,
roles: RoleSet::new(),
},
"USER_DB_INIT",
)
}
}
Loading