Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions crates/core/src/identity/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ pub trait IdentityBackend: Send + Sync {
/// - `user_id`: The ID of the user.
/// - `group_id`: The ID of the group.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
async fn add_user_to_group_expiring<'a>(
&self,
state: &ServiceState,
user_id: &'a str,
group_id: &'a str,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

/// Add user group membership relations.
Expand All @@ -90,11 +92,13 @@ pub trait IdentityBackend: Send + Sync {
/// - `state`: The service state.
/// - `memberships`: A list of (user ID, group ID) tuples.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
async fn add_users_to_groups_expiring<'a>(
&self,
state: &ServiceState,
memberships: Vec<(&'a str, &'a str)>,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

/// Authenticate a user by a password.
Expand Down
4 changes: 4 additions & 0 deletions crates/core/src/identity/provider_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ pub trait IdentityApi: Send + Sync {
/// - `user_id`: The ID of the user.
/// - `group_id`: The ID of the group.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
async fn add_user_to_group_expiring<'a>(
&self,
ctx: &ExecutionContext<'a>,
user_id: &'a str,
group_id: &'a str,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

/// Add user group memberships as specified by (uid, gid) tuples.
Expand All @@ -70,11 +72,13 @@ pub trait IdentityApi: Send + Sync {
/// - `state`: The service state.
/// - `memberships`: A list of (user ID, group ID) tuples.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
async fn add_users_to_groups_expiring<'a>(
&self,
ctx: &ExecutionContext<'a>,
memberships: Vec<(&'a str, &'a str)>,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

/// Authenticate a user by their password.
Expand Down
9 changes: 6 additions & 3 deletions crates/core/src/identity/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ impl IdentityApi for IdentityService {
user_id: &'a str,
group_id: &'a str,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
let backend_driver = self.membership_backend(ctx, user_id, group_id).await?;
if let Some(vsc) = ctx.ctx() {
Expand All @@ -703,14 +704,14 @@ impl IdentityApi for IdentityService {
),
operation: async {
backend_driver
.add_user_to_group_expiring(ctx.state(), user_id, group_id, idp_id)
.add_user_to_group_expiring(ctx.state(), user_id, group_id, idp_id, last_verified)
.await
},
on_audit_error: |_: AuditDispatchError| IdentityProviderError::Driver("audit dispatch failed".into()),
}?;
} else {
backend_driver
.add_user_to_group_expiring(ctx.state(), user_id, group_id, idp_id)
.add_user_to_group_expiring(ctx.state(), user_id, group_id, idp_id, last_verified)
.await?;
ctx.state()
.event_dispatcher
Expand Down Expand Up @@ -811,6 +812,7 @@ impl IdentityApi for IdentityService {
ctx: &ExecutionContext<'a>,
memberships: Vec<(&'a str, &'a str)>,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
let (first_user_id, group_ids): (String, Vec<String>) = if memberships.is_empty() {
(String::new(), Vec::new())
Expand Down Expand Up @@ -849,14 +851,15 @@ impl IdentityApi for IdentityService {
.map(|(u, g)| (u.as_str(), g.as_str()))
.collect(),
&idp_id,
last_verified,
)
.await
},
on_audit_error: |_: AuditDispatchError| IdentityProviderError::Driver("audit dispatch failed".into()),
}?;
} else {
backend_driver
.add_users_to_groups_expiring(ctx.state(), memberships, idp_id)
.add_users_to_groups_expiring(ctx.state(), memberships, idp_id, last_verified)
.await?;
ctx.state()
.event_dispatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ async fn remove_user_from_group_rejects_a_cross_backend_membership() {
async fn add_user_to_group_expiring_rejects_a_cross_backend_membership() {
let (provider, state) = cross_backend_fixture().await;
let error = provider
.add_user_to_group_expiring(&ExecutionContext::internal(&state), "u", "g", "idp")
.add_user_to_group_expiring(&ExecutionContext::internal(&state), "u", "g", "idp", None)
.await
.expect_err("a cross-backend membership must be rejected");
assert!(
Expand Down
2 changes: 2 additions & 0 deletions crates/core/src/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,7 @@ mod identity {
user_id: &'a str,
group_id: &'a str,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

async fn add_users_to_groups<'a>(
Expand All @@ -1418,6 +1419,7 @@ mod identity {
ctx: &ExecutionContext<'a>,
memberships: Vec<(&'a str, &'a str)>,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError>;

async fn authenticate_by_password<'a>(
Expand Down
2 changes: 2 additions & 0 deletions crates/identity-driver-ldap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ impl IdentityBackend for LdapBackend {
_user_id: &'a str,
_group_id: &'a str,
_idp_id: &'a str,
_last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
Err(not_implemented("expiring group membership"))
}
Expand All @@ -188,6 +189,7 @@ impl IdentityBackend for LdapBackend {
_state: &ServiceState,
_memberships: Vec<(&'a str, &'a str)>,
_idp_id: &'a str,
_last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
Err(not_implemented("expiring group membership"))
}
Expand Down
8 changes: 6 additions & 2 deletions crates/identity-driver-sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl IdentityBackend for SqlBackend {
/// - `user_id`: The ID of the user.
/// - `group_id`: The ID of the group.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
///
/// # Returns
/// A `Result` containing `()` if successful, or an `Error`.
Expand All @@ -105,13 +106,14 @@ impl IdentityBackend for SqlBackend {
user_id: &'a str,
group_id: &'a str,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
Ok(user_group::add_user_to_group_expiring(
&state.db.connection(),
user_id,
group_id,
idp_id,
None,
last_verified,
)
.await?)
}
Expand Down Expand Up @@ -143,6 +145,7 @@ impl IdentityBackend for SqlBackend {
/// - `state`: The service state.
/// - `memberships`: A list of user and group ID pairs.
/// - `idp_id`: The ID of the identity provider.
/// - `last_verified`: Optional last verification time; defaults to now.
///
/// # Returns
/// A `Result` containing `()` if successful, or an `Error`.
Expand All @@ -152,12 +155,13 @@ impl IdentityBackend for SqlBackend {
state: &ServiceState,
memberships: Vec<(&'a str, &'a str)>,
idp_id: &'a str,
last_verified: Option<&'a DateTime<Utc>>,
) -> Result<(), IdentityProviderError> {
Ok(user_group::add_users_to_groups_expiring(
&state.db.connection(),
memberships,
idp_id,
None,
last_verified,
)
.await?)
}
Expand Down
61 changes: 61 additions & 0 deletions crates/resource-driver-sql/src/sqlite_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,64 @@ async fn test_project_domain_id() {
// The domain's own `is_domain` entry is not a project.
assert_eq!(None, get_project_domain_id(&db, "domain").await.unwrap());
}

#[tokio::test]
async fn test_tags_are_deleted_with_the_project() {
let db = setup(&[
("domain", None),
("p1", Some("domain")),
("p2", Some("domain")),
])
.await;
// `setup` disables the foreign keys for the broken-tree cases. The tag
// cleanup relies solely on `ON DELETE CASCADE`, so switch them back on.
db.execute_unprepared("PRAGMA foreign_keys = ON")
.await
.unwrap();
let tags = vec!["a".to_string(), "b".to_string()];
crate::project_tag::insert_all(&db, "p1", &tags)
.await
.unwrap();
crate::project_tag::insert_all(&db, "p2", &tags)
.await
.unwrap();

crate::project::delete(&db, "p1").await.unwrap();

assert!(
crate::project_tag::list_by_project_id(&db, "p1")
.await
.unwrap()
.is_empty(),
"tags of the deleted project must be purged by the database"
);
assert_eq!(
tags,
crate::project_tag::list_by_project_id(&db, "p2")
.await
.unwrap(),
"tags of other projects must stay"
);
}

/// Project deletion does not delete the tags explicitly. Make sure the schema
/// generated for every supported backend carries the `ON DELETE CASCADE`
/// foreign key that purges them.
#[test]
fn test_project_tag_schema_cascades_on_project_delete() {
for backend in [
DatabaseBackend::Postgres,
DatabaseBackend::MySql,
DatabaseBackend::Sqlite,
] {
let sql = backend
.build(
&Schema::new(backend).create_table_from_entity(crate::entity::project_tag::Entity),
)
.sql;
assert!(
sql.contains("FOREIGN KEY") && sql.contains("ON DELETE CASCADE"),
"{backend:?}: project_tag must cascade on project delete: {sql}"
);
}
}
1 change: 1 addition & 0 deletions tests/integration/src/identity/user_group/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async fn test_expiring_groups() -> Result<()> {
&user.id,
&group_b.id,
"idp_id",
None,
)
.await?;

Expand Down
3 changes: 3 additions & 0 deletions tests/integration/src/identity/user_group/expiring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ async fn test_add_users_to_groups_expiring() -> Result<(), Report> {
(user.id.as_str(), group_b.id.as_str()),
],
"idp_id",
None,
)
.await?;

Expand Down Expand Up @@ -98,6 +99,7 @@ async fn test_remove_user_from_group_expiring() -> Result<(), Report> {
&user.id,
&group.id,
"idp_id",
None,
)
.await?;
state
Expand Down Expand Up @@ -135,6 +137,7 @@ async fn test_remove_user_from_groups_expiring() -> Result<(), Report> {
(user.id.as_str(), group_b.id.as_str()),
],
"idp_id",
None,
)
.await?;

Expand Down
31 changes: 13 additions & 18 deletions tests/integration/src/identity/user_group/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@

use chrono::{DateTime, Utc};
use eyre::Report;
use sea_orm::entity::*;
use tracing_test::traced_test;

use openstack_keystone_identity_driver_sql::entity::{
expiring_user_group_membership,
prelude::ExpiringUserGroupMembership as DbExpiringUserGroupMembership,
};

use super::*;
use crate::common::get_state;
use crate::{create_domain, create_group, create_user};
Expand Down Expand Up @@ -77,21 +71,22 @@ async fn test_expiring_groups() -> Result<(), Report> {
&user.id,
&group_b.id,
"idp_id",
None,
)
.await?;

// TODO: Find a way to add expired group membership for the test
DbExpiringUserGroupMembership::insert_many([
// Add expired membership
expiring_user_group_membership::ActiveModel {
user_id: Set(user.id.clone()),
group_id: Set(group_c.id.clone()),
idp_id: Set("idp_id".to_string()),
last_verified: Set(DateTime::<Utc>::default().naive_utc()),
},
])
.exec(&ExecutionContext::internal(&state).db.connection())
.await?;
// Add expired membership
state
.provider
.get_identity_provider()
.add_user_to_group_expiring(
&ExecutionContext::internal(&state),
&user.id,
&group_c.id,
"idp_id",
Some(&DateTime::<Utc>::default()),
)
.await?;

let groups = list_user_groups(&state, &user.id).await?;

Expand Down
Loading
Loading