-
Notifications
You must be signed in to change notification settings - Fork 58
[SCIM 3/4]: SCIM client token CRUD + Bearer auth #9180
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
Open
jmpesp
wants to merge
5
commits into
oxidecomputer:main
Choose a base branch
from
jmpesp:scim_token_crud_and_auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dea9bf9
[SCIM 3/4]: SCIM client token CRUD + Bearer auth
jmpesp 56d5288
remove the "delete all tokens" endpoint
jmpesp 6e67ae9
remove delete all from VERIFY_ENDPOINTS
jmpesp 9bbb011
patterm
jmpesp ed37a89
Use authz lookup on bearer token itself
jmpesp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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/. | ||
|
||
use chrono::DateTime; | ||
use chrono::Utc; | ||
use nexus_db_schema::schema::scim_client_bearer_token; | ||
use nexus_types::external_api::views; | ||
use uuid::Uuid; | ||
|
||
/// A SCIM client sends requests to a SCIM provider (in this case, Nexus) using | ||
/// some sort of authentication. Nexus currently only supports Bearer token auth | ||
/// from SCIM clients, and these tokens are stored here. | ||
#[derive(Queryable, Insertable, Clone, Debug, Selectable)] | ||
#[diesel(table_name = scim_client_bearer_token)] | ||
pub struct ScimClientBearerToken { | ||
pub id: Uuid, | ||
|
||
pub time_created: DateTime<Utc>, | ||
pub time_deleted: Option<DateTime<Utc>>, | ||
pub time_expires: Option<DateTime<Utc>>, | ||
|
||
pub silo_id: Uuid, | ||
|
||
pub bearer_token: String, | ||
} | ||
|
||
impl From<ScimClientBearerToken> for views::ScimClientBearerToken { | ||
fn from(t: ScimClientBearerToken) -> views::ScimClientBearerToken { | ||
views::ScimClientBearerToken { | ||
id: t.id, | ||
time_created: t.time_created, | ||
time_expires: t.time_expires, | ||
} | ||
} | ||
} | ||
|
||
impl From<ScimClientBearerToken> for views::ScimClientBearerTokenValue { | ||
fn from(t: ScimClientBearerToken) -> views::ScimClientBearerTokenValue { | ||
views::ScimClientBearerTokenValue { | ||
id: t.id, | ||
time_created: t.time_created, | ||
time_expires: t.time_expires, | ||
bearer_token: t.bearer_token, | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// 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/. | ||
|
||
//! [`DataStore`] methods related to SCIM | ||
|
||
use super::DataStore; | ||
use crate::authz; | ||
use crate::context::OpContext; | ||
use crate::db::model::ScimClientBearerToken; | ||
use async_bb8_diesel::AsyncRunQueryDsl; | ||
use chrono::Utc; | ||
use diesel::prelude::*; | ||
use nexus_db_errors::ErrorHandler; | ||
use nexus_db_errors::public_error_from_diesel; | ||
use omicron_common::api::external::CreateResult; | ||
use omicron_common::api::external::DeleteResult; | ||
use omicron_common::api::external::ListResultVec; | ||
use omicron_common::api::external::LookupResult; | ||
use rand::{RngCore, SeedableRng, rngs::StdRng}; | ||
use uuid::Uuid; | ||
|
||
// XXX this is the same as generate_session_token! | ||
fn generate_scim_client_bearer_token() -> String { | ||
let mut rng = StdRng::from_os_rng(); | ||
let mut random_bytes: [u8; 20] = [0; 20]; | ||
rng.fill_bytes(&mut random_bytes); | ||
hex::encode(random_bytes) | ||
} | ||
|
||
impl DataStore { | ||
// SCIM tokens | ||
|
||
pub async fn scim_idp_get_tokens( | ||
&self, | ||
opctx: &OpContext, | ||
authz_silo: &authz::Silo, | ||
) -> ListResultVec<ScimClientBearerToken> { | ||
opctx.authorize(authz::Action::ListChildren, authz_silo).await?; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
|
||
use nexus_db_schema::schema::scim_client_bearer_token::dsl; | ||
let tokens = dsl::scim_client_bearer_token | ||
.filter(dsl::silo_id.eq(authz_silo.id())) | ||
.filter(dsl::time_deleted.is_null()) | ||
.select(ScimClientBearerToken::as_select()) | ||
.load_async::<ScimClientBearerToken>(&*conn) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(tokens) | ||
} | ||
|
||
pub async fn scim_idp_create_token( | ||
&self, | ||
opctx: &OpContext, | ||
authz_silo: &authz::Silo, | ||
) -> CreateResult<ScimClientBearerToken> { | ||
opctx.authorize(authz::Action::CreateChild, authz_silo).await?; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
|
||
let new_token = ScimClientBearerToken { | ||
id: Uuid::new_v4(), | ||
time_created: Utc::now(), | ||
time_deleted: None, | ||
// TODO: allow setting an expiry? have a silo default? | ||
time_expires: None, | ||
silo_id: authz_silo.id(), | ||
bearer_token: generate_scim_client_bearer_token(), | ||
}; | ||
|
||
use nexus_db_schema::schema::scim_client_bearer_token::dsl; | ||
diesel::insert_into(dsl::scim_client_bearer_token) | ||
.values(new_token.clone()) | ||
.execute_async(&*conn) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(new_token) | ||
} | ||
|
||
pub async fn scim_idp_get_token_by_id( | ||
&self, | ||
opctx: &OpContext, | ||
authz_silo: &authz::Silo, | ||
id: Uuid, | ||
) -> LookupResult<Option<ScimClientBearerToken>> { | ||
opctx.authorize(authz::Action::ListChildren, authz_silo).await?; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
|
||
use nexus_db_schema::schema::scim_client_bearer_token::dsl; | ||
let token = dsl::scim_client_bearer_token | ||
.filter(dsl::silo_id.eq(authz_silo.id())) | ||
.filter(dsl::id.eq(id)) | ||
.filter(dsl::time_deleted.is_null()) | ||
.select(ScimClientBearerToken::as_select()) | ||
.first_async::<ScimClientBearerToken>(&*conn) | ||
.await | ||
.optional() | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(token) | ||
} | ||
|
||
pub async fn scim_idp_delete_token_by_id( | ||
&self, | ||
opctx: &OpContext, | ||
authz_silo: &authz::Silo, | ||
id: Uuid, | ||
) -> DeleteResult { | ||
opctx.authorize(authz::Action::Modify, authz_silo).await?; | ||
|
||
let conn = self.pool_connection_authorized(opctx).await?; | ||
|
||
use nexus_db_schema::schema::scim_client_bearer_token::dsl; | ||
diesel::update(dsl::scim_client_bearer_token) | ||
.filter(dsl::silo_id.eq(authz_silo.id())) | ||
.filter(dsl::id.eq(id)) | ||
.filter(dsl::time_deleted.is_null()) | ||
.set(dsl::time_deleted.eq(Utc::now())) | ||
.execute_async(&*conn) | ||
.await | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// SCIM clients should _not_ authenticate to an Actor in the traditional | ||
/// sense: they shouldn't have permission on any resources under a Silo, | ||
/// only enough to CRUD Silo users and groups. | ||
pub async fn scim_idp_lookup_token_by_bearer( | ||
&self, | ||
bearer_token: String, | ||
) -> LookupResult<Option<ScimClientBearerToken>> { | ||
let conn = self.pool_connection_unauthorized().await?; | ||
|
||
use nexus_db_schema::schema::scim_client_bearer_token::dsl; | ||
let maybe_token = dsl::scim_client_bearer_token | ||
.filter(dsl::bearer_token.eq(bearer_token)) | ||
.filter(dsl::time_deleted.is_null()) | ||
.first_async(&*conn) | ||
.await | ||
.optional() | ||
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?; | ||
|
||
Ok(maybe_token) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm thinking you should check the read permission on a value of type
authz::ScimClientBearerToken
instead, right?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.
agreed, my bad - done in ed37a89