Skip to content

Commit f02972f

Browse files
authored
Merge pull request #78 from VibeEngineeringAI/fix/role-sync-ordering
Ingestors install the stored role on role sync
2 parents c6576a6 + fff61ed commit f02972f

1 file changed

Lines changed: 230 additions & 19 deletions

File tree

‎src/handlers/http/modal/ingest/ingestor_role.rs‎

Lines changed: 230 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
use std::collections::HashSet;
19+
use std::collections::{HashMap, HashSet};
2020

2121
use actix_web::{
2222
HttpRequest, HttpResponse, Responder,
@@ -26,10 +26,14 @@ use actix_web::{
2626
use crate::{
2727
handlers::http::{
2828
modal::{ingest::SyncRole, utils::rbac_utils::get_metadata},
29+
rbac::UPDATE_LOCK,
2930
role::RoleError,
3031
},
3132
parseable::DEFAULT_TENANT,
32-
rbac::map::{mut_roles, mut_sessions, read_user_groups, users},
33+
rbac::{
34+
map::{mut_roles, mut_sessions, read_user_groups, users},
35+
role::model::{Role, RoleType},
36+
},
3337
storage,
3438
utils::get_tenant_id_from_request,
3539
};
@@ -49,48 +53,70 @@ pub async fn put(
4953
"non super-admin user trying to create role for another tenant",
5054
)));
5155
}
52-
let mut metadata = get_metadata(&sync_req.tenant_id).await?;
53-
metadata
54-
.roles
55-
.insert(name.clone(), sync_req.privileges.clone());
56-
57-
let _ = storage::put_staging_metadata(&metadata, &sync_req.tenant_id);
5856
let tenant_id = sync_req
5957
.tenant_id
6058
.as_deref()
6159
.unwrap_or(DEFAULT_TENANT)
6260
.to_owned();
63-
mut_roles()
64-
.entry(tenant_id.clone())
65-
.or_default()
66-
.insert(name.clone(), sync_req.privileges);
61+
// The querier sends syncs after releasing its lock, so they can arrive out
62+
// of order and the body may be stale. Install what storage holds instead;
63+
// holding the lock across read and install means a later sync always reads
64+
// state at least as new as an earlier one.
65+
let _guard = UPDATE_LOCK.lock().await;
66+
let metadata = get_metadata(&sync_req.tenant_id).await?;
67+
apply_stored_role(&metadata.roles, &tenant_id, &name);
68+
69+
if let Err(e) = storage::put_staging_metadata(&metadata, &sync_req.tenant_id) {
70+
tracing::warn!("Failed to write staging metadata after role sync: {e}");
71+
}
72+
73+
Ok(HttpResponse::Ok().finish())
74+
}
75+
76+
/// Makes the live role `name` match `stored` (the tenant's roles in storage),
77+
/// removing it if storage no longer has it, and drops the sessions of every
78+
/// user holding the role.
79+
/// Internal roles exist only in memory and are left untouched.
80+
fn apply_stored_role(stored: &HashMap<String, Role>, tenant_id: &str, name: &str) {
81+
{
82+
let mut roles = mut_roles();
83+
let tenant_roles = roles.entry(tenant_id.to_owned()).or_default();
84+
if tenant_roles
85+
.get(name)
86+
.is_some_and(|role| role.role_type() == &RoleType::Internal)
87+
{
88+
return;
89+
}
90+
match stored.get(name) {
91+
Some(role) => tenant_roles.insert(name.to_owned(), role.clone()),
92+
None => tenant_roles.remove(name),
93+
};
94+
}
6795

6896
// refresh the sessions of all users using this role
6997
// for this, iterate over all user_groups and users and create a hashset of users
7098
let mut session_refresh_users: HashSet<String> = HashSet::new();
71-
if let Some(groups) = read_user_groups().get(&tenant_id) {
99+
if let Some(groups) = read_user_groups().get(tenant_id) {
72100
for user_group in groups.values() {
73-
if user_group.roles.contains(&name) {
101+
if user_group.roles.contains(name) {
74102
session_refresh_users
75103
.extend(user_group.users.iter().map(|u| u.userid().to_string()));
76104
}
77105
}
78106
}
79107

80108
// iterate over all users to see if they have this role
81-
if let Some(users) = users().get(&tenant_id) {
109+
if let Some(users) = users().get(tenant_id) {
82110
for user in users.values() {
83-
if user.roles.contains(&name) {
111+
if user.roles.contains(name) {
84112
session_refresh_users.insert(user.userid().to_string());
85113
}
86114
}
87115
}
88116

89117
for userid in session_refresh_users {
90-
mut_sessions().remove_user(&userid, &tenant_id);
118+
mut_sessions().remove_user(&userid, tenant_id);
91119
}
92-
93-
Ok(HttpResponse::Ok().finish())
94120
}
95121

96122
// Handler for DELETE /api/v1/role/{name}
@@ -140,3 +166,188 @@ pub async fn delete(
140166

141167
Ok(HttpResponse::Ok().finish())
142168
}
169+
170+
#[cfg(test)]
171+
mod tests {
172+
use parking_lot::RwLock;
173+
use ulid::Ulid;
174+
175+
use chrono::{TimeDelta, Utc};
176+
177+
use super::*;
178+
use crate::rbac::{
179+
map::{
180+
ROLES, SESSIONS, SessionKey, Sessions, TEST_SERIAL, USER_GROUPS, USERS, UserGroups,
181+
mut_users, roles, sessions, write_user_groups,
182+
},
183+
role::model::DefaultPrivilege,
184+
user::{Basic, GroupUser, User, UserGroup, UserType},
185+
};
186+
187+
const ROLE: &str = "writers";
188+
189+
// `rbac::map::init` needs `PARSEABLE`, so tests create the maps directly.
190+
fn init_auth_maps() {
191+
USERS.get_or_init(|| RwLock::new(crate::rbac::map::Users::default()));
192+
SESSIONS.get_or_init(|| RwLock::new(Sessions::default()));
193+
USER_GROUPS.get_or_init(|| RwLock::new(UserGroups::default()));
194+
ROLES.get_or_init(|| RwLock::new(HashMap::new()));
195+
}
196+
197+
fn role_a() -> Role {
198+
Role::create_user_role(vec![DefaultPrivilege::Reader { resource: None }])
199+
}
200+
201+
fn role_b() -> Role {
202+
Role::create_user_role(vec![DefaultPrivilege::Admin])
203+
}
204+
205+
fn live_role(tenant: &str) -> Option<Role> {
206+
roles().get(tenant).and_then(|r| r.get(ROLE)).cloned()
207+
}
208+
209+
// The querier sends syncs after releasing its lock, so PUT A's sync can
210+
// arrive after PUT B was committed. Storage then holds B and the live map
211+
// may still hold A. `apply_stored_role` takes no body, so the stale A in
212+
// the request cannot be installed.
213+
#[tokio::test]
214+
async fn installs_stored_role_over_live_role() {
215+
// Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
216+
let _serial = TEST_SERIAL.lock().await;
217+
init_auth_maps();
218+
let tenant = format!("ingestor-role-{}", Ulid::new());
219+
mut_roles()
220+
.entry(tenant.clone())
221+
.or_default()
222+
.insert(ROLE.to_owned(), role_a());
223+
let stored = HashMap::from([(ROLE.to_owned(), role_b())]);
224+
225+
apply_stored_role(&stored, &tenant, ROLE);
226+
227+
assert_eq!(live_role(&tenant), Some(role_b()), "live role must be B");
228+
}
229+
230+
// PUT then DELETE: the PUT's sync arriving after the delete was committed
231+
// must not resurrect the role.
232+
#[tokio::test]
233+
async fn sync_for_role_deleted_in_storage_removes_live_role() {
234+
// Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
235+
let _serial = TEST_SERIAL.lock().await;
236+
init_auth_maps();
237+
let tenant = format!("ingestor-role-{}", Ulid::new());
238+
mut_roles()
239+
.entry(tenant.clone())
240+
.or_default()
241+
.insert(ROLE.to_owned(), role_a());
242+
apply_stored_role(&HashMap::new(), &tenant, ROLE);
243+
244+
assert_eq!(live_role(&tenant), None, "deleted role must not be live");
245+
}
246+
247+
fn insert_user(tenant: &str, roles: &[&str]) -> String {
248+
let user = User {
249+
ty: UserType::Native(Basic {
250+
username: format!("user-{}", Ulid::new()),
251+
password_hash: String::new(),
252+
email: None,
253+
}),
254+
roles: roles.iter().map(|role| role.to_string()).collect(),
255+
user_groups: HashSet::new(),
256+
tenant: Some(tenant.to_owned()),
257+
protected: false,
258+
};
259+
let userid = user.userid().to_owned();
260+
mut_users().insert(user);
261+
userid
262+
}
263+
264+
fn track_session(tenant: &str, userid: &str) {
265+
mut_sessions().track_new(
266+
userid.to_owned(),
267+
SessionKey::SessionId(Ulid::new()),
268+
Utc::now() + TimeDelta::hours(1),
269+
Vec::new(),
270+
&Some(tenant.to_owned()),
271+
);
272+
}
273+
274+
fn has_session(tenant: &str, userid: &str) -> bool {
275+
sessions()
276+
.get_active_sessions()
277+
.iter()
278+
.any(|(_, user, t)| user == userid && t == tenant)
279+
}
280+
281+
#[tokio::test]
282+
async fn drops_sessions_of_direct_and_group_holders_only() {
283+
// Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
284+
let _serial = TEST_SERIAL.lock().await;
285+
init_auth_maps();
286+
let tenant = format!("ingestor-role-{}", Ulid::new());
287+
let other_tenant = format!("ingestor-role-{}", Ulid::new());
288+
let direct = insert_user(&tenant, &[ROLE]);
289+
let member = insert_user(&tenant, &[]);
290+
let unrelated = insert_user(&tenant, &["other"]);
291+
let elsewhere = insert_user(&other_tenant, &[ROLE]);
292+
write_user_groups().insert(
293+
UserGroup {
294+
name: "group".to_owned(),
295+
roles: HashSet::from([ROLE.to_owned()]),
296+
users: HashSet::from([GroupUser {
297+
userid: member.clone(),
298+
username: member.clone(),
299+
method: "native".to_owned(),
300+
tenant_id: Some(tenant.clone()),
301+
}]),
302+
},
303+
&tenant,
304+
);
305+
for (t, user) in [
306+
(&tenant, &direct),
307+
(&tenant, &member),
308+
(&tenant, &unrelated),
309+
(&other_tenant, &elsewhere),
310+
] {
311+
track_session(t, user);
312+
}
313+
314+
apply_stored_role(&HashMap::from([(ROLE.to_owned(), role_b())]), &tenant, ROLE);
315+
316+
assert!(
317+
!has_session(&tenant, &direct),
318+
"direct holder keeps a session"
319+
);
320+
assert!(
321+
!has_session(&tenant, &member),
322+
"group member keeps a session"
323+
);
324+
assert!(
325+
has_session(&tenant, &unrelated),
326+
"unrelated user lost a session"
327+
);
328+
assert!(
329+
has_session(&other_tenant, &elsewhere),
330+
"another tenant's user lost a session"
331+
);
332+
}
333+
334+
// Internal roles such as `super-admin` exist only in memory, never in
335+
// storage, so a sync naming one must not remove or replace it.
336+
#[tokio::test]
337+
async fn sync_leaves_internal_role_alone() {
338+
// Other modules' tests watch the RBAC map locks; see `map::TEST_SERIAL`.
339+
let _serial = TEST_SERIAL.lock().await;
340+
init_auth_maps();
341+
let tenant = format!("ingestor-role-{}", Ulid::new());
342+
let internal = Role::create_internal_role(vec![DefaultPrivilege::Admin]);
343+
mut_roles()
344+
.entry(tenant.clone())
345+
.or_default()
346+
.insert(ROLE.to_owned(), internal.clone());
347+
348+
apply_stored_role(&HashMap::new(), &tenant, ROLE);
349+
assert_eq!(live_role(&tenant), Some(internal.clone()));
350+
apply_stored_role(&HashMap::from([(ROLE.to_owned(), role_a())]), &tenant, ROLE);
351+
assert_eq!(live_role(&tenant), Some(internal));
352+
}
353+
}

0 commit comments

Comments
 (0)