From 95d5ecf8087cb1af4e91c6bdfd27423000c56a2c Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Thu, 12 Nov 2026 18:30:04 +0100 Subject: [PATCH] test: Stop integration tests bypassing providers Integration tests wrote to the database directly where the providers could be used instead. Create trusts through the trust provider rather than inserting trust rows. Add an optional last_verified argument to add_user_to_group_expiring and add_users_to_groups_expiring so tests can create an expired membership through the identity provider. None keeps the previous behaviour of stamping the current time. The LDAP driver ignores it. Drop the raw project_tag count from the integration test. Cover tag purging on project deletion with driver unit tests instead: a SQLite test with foreign keys enabled and a check that the schema of every backend carries ON DELETE CASCADE. Signed-off-by: Artem Goncharov --- crates/core/src/identity/backend.rs | 4 ++ crates/core/src/identity/provider_api.rs | 4 ++ crates/core/src/identity/service.rs | 9 ++- .../service/tests/per_domain_dispatch.rs | 2 +- crates/core/src/mocks.rs | 2 + crates/identity-driver-ldap/src/lib.rs | 2 + crates/identity-driver-sql/src/lib.rs | 8 ++- .../resource-driver-sql/src/sqlite_tests.rs | 61 +++++++++++++++++ .../src/identity/user_group/add.rs | 1 + .../src/identity/user_group/expiring.rs | 3 + .../src/identity/user_group/list.rs | 31 ++++----- .../src/identity/user_group/members.rs | 46 ++++++------- .../integration/src/resource/project/tags.rs | 22 ++----- tests/integration/src/token/validate/trust.rs | 65 ++++++++----------- 14 files changed, 160 insertions(+), 100 deletions(-) diff --git a/crates/core/src/identity/backend.rs b/crates/core/src/identity/backend.rs index 1e8e5a456..73f03c3a3 100644 --- a/crates/core/src/identity/backend.rs +++ b/crates/core/src/identity/backend.rs @@ -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>, ) -> Result<(), IdentityProviderError>; /// Add user group membership relations. @@ -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>, ) -> Result<(), IdentityProviderError>; /// Authenticate a user by a password. diff --git a/crates/core/src/identity/provider_api.rs b/crates/core/src/identity/provider_api.rs index bba179df6..b444b47d7 100644 --- a/crates/core/src/identity/provider_api.rs +++ b/crates/core/src/identity/provider_api.rs @@ -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>, ) -> Result<(), IdentityProviderError>; /// Add user group memberships as specified by (uid, gid) tuples. @@ -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>, ) -> Result<(), IdentityProviderError>; /// Authenticate a user by their password. diff --git a/crates/core/src/identity/service.rs b/crates/core/src/identity/service.rs index c2a1efcd3..9972e1692 100644 --- a/crates/core/src/identity/service.rs +++ b/crates/core/src/identity/service.rs @@ -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>, ) -> Result<(), IdentityProviderError> { let backend_driver = self.membership_backend(ctx, user_id, group_id).await?; if let Some(vsc) = ctx.ctx() { @@ -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 @@ -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>, ) -> Result<(), IdentityProviderError> { let (first_user_id, group_ids): (String, Vec) = if memberships.is_empty() { (String::new(), Vec::new()) @@ -849,6 +851,7 @@ impl IdentityApi for IdentityService { .map(|(u, g)| (u.as_str(), g.as_str())) .collect(), &idp_id, + last_verified, ) .await }, @@ -856,7 +859,7 @@ impl IdentityApi for IdentityService { }?; } 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 diff --git a/crates/core/src/identity/service/tests/per_domain_dispatch.rs b/crates/core/src/identity/service/tests/per_domain_dispatch.rs index f988cecee..5b57aa190 100644 --- a/crates/core/src/identity/service/tests/per_domain_dispatch.rs +++ b/crates/core/src/identity/service/tests/per_domain_dispatch.rs @@ -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!( diff --git a/crates/core/src/mocks.rs b/crates/core/src/mocks.rs index cf26e35fa..b09b7027e 100644 --- a/crates/core/src/mocks.rs +++ b/crates/core/src/mocks.rs @@ -1405,6 +1405,7 @@ mod identity { user_id: &'a str, group_id: &'a str, idp_id: &'a str, + last_verified: Option<&'a DateTime>, ) -> Result<(), IdentityProviderError>; async fn add_users_to_groups<'a>( @@ -1418,6 +1419,7 @@ mod identity { ctx: &ExecutionContext<'a>, memberships: Vec<(&'a str, &'a str)>, idp_id: &'a str, + last_verified: Option<&'a DateTime>, ) -> Result<(), IdentityProviderError>; async fn authenticate_by_password<'a>( diff --git a/crates/identity-driver-ldap/src/lib.rs b/crates/identity-driver-ldap/src/lib.rs index 4c884af1e..fa9b23c34 100644 --- a/crates/identity-driver-ldap/src/lib.rs +++ b/crates/identity-driver-ldap/src/lib.rs @@ -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>, ) -> Result<(), IdentityProviderError> { Err(not_implemented("expiring group membership")) } @@ -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>, ) -> Result<(), IdentityProviderError> { Err(not_implemented("expiring group membership")) } diff --git a/crates/identity-driver-sql/src/lib.rs b/crates/identity-driver-sql/src/lib.rs index 6f2a3b6f1..da8158a4b 100644 --- a/crates/identity-driver-sql/src/lib.rs +++ b/crates/identity-driver-sql/src/lib.rs @@ -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`. @@ -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>, ) -> Result<(), IdentityProviderError> { Ok(user_group::add_user_to_group_expiring( &state.db.connection(), user_id, group_id, idp_id, - None, + last_verified, ) .await?) } @@ -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`. @@ -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>, ) -> Result<(), IdentityProviderError> { Ok(user_group::add_users_to_groups_expiring( &state.db.connection(), memberships, idp_id, - None, + last_verified, ) .await?) } diff --git a/crates/resource-driver-sql/src/sqlite_tests.rs b/crates/resource-driver-sql/src/sqlite_tests.rs index a9aa1879d..779367728 100644 --- a/crates/resource-driver-sql/src/sqlite_tests.rs +++ b/crates/resource-driver-sql/src/sqlite_tests.rs @@ -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}" + ); + } +} diff --git a/tests/integration/src/identity/user_group/add.rs b/tests/integration/src/identity/user_group/add.rs index f19c270f1..b9ac4113a 100644 --- a/tests/integration/src/identity/user_group/add.rs +++ b/tests/integration/src/identity/user_group/add.rs @@ -46,6 +46,7 @@ async fn test_expiring_groups() -> Result<()> { &user.id, &group_b.id, "idp_id", + None, ) .await?; diff --git a/tests/integration/src/identity/user_group/expiring.rs b/tests/integration/src/identity/user_group/expiring.rs index b70c76f19..248dedbe6 100644 --- a/tests/integration/src/identity/user_group/expiring.rs +++ b/tests/integration/src/identity/user_group/expiring.rs @@ -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?; @@ -98,6 +99,7 @@ async fn test_remove_user_from_group_expiring() -> Result<(), Report> { &user.id, &group.id, "idp_id", + None, ) .await?; state @@ -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?; diff --git a/tests/integration/src/identity/user_group/list.rs b/tests/integration/src/identity/user_group/list.rs index e95f02381..0b65ce391 100644 --- a/tests/integration/src/identity/user_group/list.rs +++ b/tests/integration/src/identity/user_group/list.rs @@ -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}; @@ -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::::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::::default()), + ) + .await?; let groups = list_user_groups(&state, &user.id).await?; diff --git a/tests/integration/src/identity/user_group/members.rs b/tests/integration/src/identity/user_group/members.rs index f1c7d7f4b..26b44c9af 100644 --- a/tests/integration/src/identity/user_group/members.rs +++ b/tests/integration/src/identity/user_group/members.rs @@ -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}; @@ -122,16 +116,20 @@ async fn test_list_group_users_expiring() -> Result<(), Report> { &fresh.id, &group.id, "idp_id", + None, ) .await?; - DbExpiringUserGroupMembership::insert(expiring_user_group_membership::ActiveModel { - user_id: Set(expired.id.clone()), - group_id: Set(group.id.clone()), - idp_id: Set("idp_id".to_string()), - last_verified: Set(DateTime::::default().naive_utc()), - }) - .exec(&ExecutionContext::internal(&state).db.connection()) - .await?; + state + .provider + .get_identity_provider() + .add_user_to_group_expiring( + &ExecutionContext::internal(&state), + &expired.id, + &group.id, + "idp_id", + Some(&DateTime::::default()), + ) + .await?; let mut expected = vec![direct.id.clone(), fresh.id.clone()]; expected.sort(); @@ -181,16 +179,20 @@ async fn test_check_user_in_group_expiring() -> Result<(), Report> { &user.id, &fresh_group.id, "idp_id", + None, + ) + .await?; + state + .provider + .get_identity_provider() + .add_user_to_group_expiring( + &ExecutionContext::internal(&state), + &user.id, + &expired_group.id, + "idp_id", + Some(&DateTime::::default()), ) .await?; - DbExpiringUserGroupMembership::insert(expiring_user_group_membership::ActiveModel { - user_id: Set(user.id.clone()), - group_id: Set(expired_group.id.clone()), - idp_id: Set("idp_id".to_string()), - last_verified: Set(DateTime::::default().naive_utc()), - }) - .exec(&ExecutionContext::internal(&state).db.connection()) - .await?; assert!(is_member(&state, &user.id, &fresh_group.id).await?); assert!( diff --git a/tests/integration/src/resource/project/tags.rs b/tests/integration/src/resource/project/tags.rs index c5a32203e..00154c36e 100644 --- a/tests/integration/src/resource/project/tags.rs +++ b/tests/integration/src/resource/project/tags.rs @@ -14,7 +14,6 @@ //! Test project tags. use eyre::Result; -use sea_orm::{ConnectionTrait, DatabaseBackend, Statement}; use tracing_test::traced_test; use openstack_keystone_core::auth::ExecutionContext; @@ -360,22 +359,11 @@ async fn test_tags_removed_with_project() -> Result<()> { .await?; provider.delete_project(&ctx, &project.id).await?; - let db = state.db.connection(); - let backend = db.get_database_backend(); - // MySQL uses `?` placeholders, Postgres and SQLite accept `$1`. - let sql = match backend { - DatabaseBackend::MySql => "SELECT COUNT(*) AS cnt FROM project_tag WHERE project_id = ?", - _ => "SELECT COUNT(*) AS cnt FROM project_tag WHERE project_id = $1", - }; - let row = db - .query_one_raw(Statement::from_sql_and_values( - backend, - sql, - [project.id.clone().into()], - )) - .await? - .expect("count row"); - assert_eq!(row.try_get::("", "cnt")?, 0); + assert!(provider.get_project(&ctx, &project.id).await?.is_none()); + assert!(matches!( + provider.list_project_tags(&ctx, &project.id).await, + Err(ResourceProviderError::ProjectNotFound(_)) + )); Ok(()) } diff --git a/tests/integration/src/token/validate/trust.rs b/tests/integration/src/token/validate/trust.rs index 7b6716997..492b05418 100644 --- a/tests/integration/src/token/validate/trust.rs +++ b/tests/integration/src/token/validate/trust.rs @@ -14,17 +14,15 @@ // use eyre::Report; -use sea_orm::{DbConn, entity::*}; use std::sync::Arc; use tracing_test::traced_test; -use openstack_keystone_trust_driver_sql::entity::{trust as db_trust, trust_role as db_trust_role}; - use openstack_keystone::keystone::Service; use openstack_keystone::token::{FernetToken, TokenProviderError}; use openstack_keystone_api_types::v3::auth::token::TokenBuilder; use openstack_keystone_core::auth::ExecutionContext; use openstack_keystone_core_types::auth::*; +use openstack_keystone_core_types::role::RoleRefBuilder; use openstack_keystone_core_types::trust::*; use super::grant_role_to_user_on_project; @@ -33,41 +31,34 @@ use crate::common::get_state; use crate::token::validate::revoke_role_from_user_on_project; use crate::{create_domain, create_project, create_role, create_user}; +/// Create a trust through the trust provider. async fn create_trust>( - db: &DbConn, + state: &Arc, trust_id: S, trustor_id: S, trustee_id: S, project_id: S, role_ids: Vec, -) -> Result<(), Report> { - let trust_id = trust_id.into(); - db_trust::ActiveModel { - id: Set(trust_id.clone()), - trustor_user_id: Set(trustor_id.into()), - trustee_user_id: Set(trustee_id.into()), - project_id: Set(Some(project_id.into())), - impersonation: Set(false), - deleted_at: NotSet, - expires_at: NotSet, - remaining_uses: NotSet, - extra: Set(Some("{}".into())), - expires_at_int: NotSet, - redelegated_trust_id: NotSet, - redelegation_count: NotSet, - } - .insert(db) - .await?; - for role_id in role_ids { - db_trust_role::ActiveModel { - trust_id: Set(trust_id.clone()), - role_id: Set(role_id.into()), - } - .insert(db) - .await?; - } - - Ok(()) +) -> Result { + Ok(state + .provider + .get_trust_provider() + .create_trust( + &ExecutionContext::internal(state), + TrustCreateBuilder::default() + .id(trust_id.into()) + .trustor_user_id(trustor_id.into()) + .trustee_user_id(trustee_id.into()) + .project_id(project_id.into()) + .roles( + role_ids + .into_iter() + .map(|id| RoleRefBuilder::default().id(id.into()).build()) + .collect::, _>>()?, + ) + .build()?, + ) + .await?) } async fn get_trust>(state: &Arc, id: U) -> Result, Report> { @@ -91,7 +82,7 @@ async fn test_valid() -> Result<(), Report> { grant_role_to_user_on_project(&state, &user_a.id, &project.id, &role_a.id).await?; create_trust( - &state.db.connection(), + &state, "trust_a".to_string(), user_a.id.clone(), user_b.id.clone(), @@ -209,7 +200,7 @@ async fn test_valid_redelegated() -> Result<(), Report> { let user_c = create_user!(state, domain.id.clone())?; grant_role_to_user_on_project(&state, &user_a.id, &project.id, &role_a.id).await?; create_trust( - &state.db.connection(), + &state, "trust_a_b".to_string(), user_a.id.clone(), user_c.id.clone(), @@ -326,7 +317,7 @@ async fn test_fewer_roles() -> Result<(), Report> { grant_role_to_user_on_project(&state, &user_a.id, &project.id, &role_a.id).await?; create_trust( - &state.db.connection(), + &state, "trust_a".to_string(), user_a.id.clone(), user_b.id.clone(), @@ -433,7 +424,7 @@ async fn test_exclude_local_roles() -> Result<(), Report> { grant_role_to_user_on_project(&state, &user_a.id, &project.id, &role_x.id).await?; create_trust( - &state.db.connection(), + &state, "trust_a".to_string(), user_a.id.clone(), user_b.id.clone(), @@ -563,7 +554,7 @@ async fn test_trust_populated_in_api_token_response() -> Result<(), Report> { .await?; create_trust( - &state.db.connection(), + &state, "trust_a".to_string(), user_a.id.clone(), user_b.id.clone(),