Skip to content
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

reinstall: avoid duplicate users in loginctl_users #1094

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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}]
Loading