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
6 changes: 1 addition & 5 deletions rust/agama-server/src/users/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use crate::{error::Error, users::password::PasswordChecker};
use agama_lib::{
error::ServiceError,
users::{model::RootPatchSettings, proxies::Users1Proxy, FirstUser, RootUser, UsersClient},
users::{model::RootPatchSettings, FirstUser, RootUser, UsersClient},
};
use axum::{
extract::State,
Expand All @@ -37,7 +37,6 @@ use axum::{
Json, Router,
};
use serde::Deserialize;
use tokio_stream::{Stream, StreamExt};

use super::password::PasswordCheckResult;

Expand All @@ -48,9 +47,6 @@ struct UsersState<'a> {

/// Sets up and returns the axum service for the users module.
pub async fn users_service(dbus: zbus::Connection) -> Result<Router, ServiceError> {
const DBUS_SERVICE: &str = "org.opensuse.Agama.Manager1";
const DBUS_PATH: &str = "/org/opensuse/Agama/Users1";

let users = UsersClient::new(dbus.clone()).await?;
let state = UsersState { users };
// FIXME: use anyhow temporarily until we adapt all these methods to return
Expand Down
67 changes: 25 additions & 42 deletions rust/agama-users/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@ pub trait ModelAdapter: Send + 'static {
fn install(&self, _config: &Config) -> Result<(), service::Error> {
Ok(())
}

fn add_first_user(&self, _user: &FirstUserConfig) -> Result<(), service::Error> {
Ok(())
}

fn add_root_user(&self, _root: &RootUserConfig) -> Result<(), service::Error> {
Ok(())
}

fn set_user_password(
&self,
_user_name: &str,
_user_password: &UserPassword,
) -> Result<(), service::Error> {
Ok(())
}

fn update_authorized_keys(&self, _ssh_key: &str) -> Result<(), service::Error> {
Ok(())
}

fn update_user_fullname(&self, _user: &FirstUserConfig) -> Result<(), service::Error> {
Ok(())
}
}

/// [ModelAdapter] implementation for systemd-based systems.
Expand All @@ -72,18 +48,14 @@ impl Model {
install_dir: PathBuf::from(install_dir.as_ref()),
}
}
}

impl ModelAdapter for Model {
fn install(&self, config: &Config) -> Result<(), service::Error> {
if let Some(first_user) = &config.first_user {
self.add_first_user(&first_user)?;
}
if let Some(root_user) = &config.root {
self.add_root_user(&root_user)?;
}
/// Wrapper for creating Command which works in installation chroot
fn chroot_command(&self) -> Command {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. Thinking about it, and having into account that there is some repetitive code when running the commands, perhaps we could even think about having our own struct.

Something like:

struct ChrootedCommand(Command);

And you could do:

ChrootedCommand::on("/mnt")
  .command("adduser")
  .arg("blah")
  .output()?; // we could even have a more convenient way of running it if we want to

It is just an idea. WDYT? We could even reuse it in other parts of Agama.

let mut cmd = Command::new("chroot");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why

those

empty

lines? ;-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well just my way of formatting .. kind of separates things into logical blocks

Ok(())
cmd.arg(&self.install_dir);

cmd
}

/// Reads first user's data from given config and updates its setup accordingly
Expand All @@ -95,9 +67,9 @@ impl ModelAdapter for Model {
return Err(service::Error::MissingUserData);
};

let useradd = Command::new("chroot")
.arg(&self.install_dir)
.args(["useradd", &user_name])
let useradd = self
.chroot_command()
.args(["useradd", "-G", "wheel", &user_name])
.output()?;

if !useradd.status.success() {
Expand All @@ -109,7 +81,6 @@ impl ModelAdapter for Model {
}

self.set_user_password(user_name, user_password)?;

self.update_user_fullname(user)
}

Expand Down Expand Up @@ -140,8 +111,7 @@ impl ModelAdapter for Model {
user_name: &str,
user_password: &UserPassword,
) -> Result<(), service::Error> {
let mut passwd_cmd = Command::new("chroot");
passwd_cmd.arg(&self.install_dir);
let mut passwd_cmd = self.chroot_command();
passwd_cmd.arg("chpasswd");

if user_password.hashed_password {
Expand Down Expand Up @@ -193,8 +163,8 @@ impl ModelAdapter for Model {
return Ok(());
};

let chfn = Command::new("chroot")
.arg(&self.install_dir)
let chfn = self
.chroot_command()
.args(["chfn", "-f", &full_name, &user_name])
.output()?;

Expand All @@ -213,3 +183,16 @@ impl ModelAdapter for Model {
Ok(())
}
}

impl ModelAdapter for Model {
fn install(&self, config: &Config) -> Result<(), service::Error> {
if let Some(first_user) = &config.first_user {
self.add_first_user(&first_user)?;
}
if let Some(root_user) = &config.root {
self.add_root_user(&root_user)?;
}

Ok(())
}
}
Loading