Skip to content

Commit

Permalink
Merge pull request #1094 from omertuc/setusers
Browse files Browse the repository at this point in the history
reinstall: avoid duplicate users in `loginctl_users`
  • Loading branch information
omertuc authored Feb 10, 2025
2 parents c681850 + 961c0bf commit f082d86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
32 changes: 27 additions & 5 deletions system-reinstall-bootc/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ use rustix::process::geteuid;
use rustix::process::getuid;
use rustix::thread::set_thread_res_uid;
use serde_json::Value;
use std::collections::BTreeSet;
use std::fmt::Display;
use std::fmt::Formatter;
use std::process::Command;
use uzers::os::unix::UserExt;

fn loginctl_users() -> Result<Vec<String>> {
let users = loginctl_run_compat()?;
fn loginctl_users() -> Result<BTreeSet<String>> {
let loginctl_raw_output = loginctl_run_compat()?;

loginctl_parse(loginctl_raw_output)
}

/// See [`test::test_parse_lsblk`] for example loginctl output
fn loginctl_parse(users: Value) -> Result<BTreeSet<String>> {
users
.as_array()
.context("loginctl output is not an array")?
Expand All @@ -27,10 +33,10 @@ fn loginctl_users() -> Result<Vec<String>> {
.context("user name field is not a string")
.map(String::from)
})
// Artificially add the root user to the list of users as it doesn't appear in loginctl
// list-sessions
// Artificially add the root user to the list of users as it doesn't always appear in
// `loginctl list-sessions`
.chain(std::iter::once(Ok("root".to_string())))
.collect::<Result<Vec<String>>>()
.collect::<Result<_>>()
.context("error parsing users")
}

Expand Down Expand Up @@ -162,3 +168,19 @@ pub(crate) fn get_all_users_keys() -> Result<Vec<UserKeys>> {

Ok(all_users_authorized_keys)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
pub(crate) fn test_parse_lsblk() {
let fixture = include_str!("../tests/fixtures/loginctl.json");

let result = loginctl_parse(serde_json::from_str(fixture).unwrap()).unwrap();

assert_eq!(result.len(), 2);
assert!(result.contains("root"));
assert!(result.contains("foo-doe"));
}
}
1 change: 1 addition & 0 deletions system-reinstall-bootc/tests/fixtures/loginctl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"session":"2","uid":1000,"user":"foo-doe","seat":"seat0","leader":3045,"class":"user","tty":"tty1","idle":false,"since":null},{"session":"3","uid":1000,"user":"foo-doe","seat":null,"leader":3148,"class":"manager","tty":null,"idle":false,"since":null}]

0 comments on commit f082d86

Please sign in to comment.