Browse Source

Simplify SetuidCommandRunner

master
Adrian Heine 3 years ago
parent
commit
70786ebf40
  1. 19
      src/builder.rs
  2. 14
      src/command_runner.rs
  3. 19
      src/symbols/systemd/user_service.rs

19
src/builder.rs

@ -115,12 +115,8 @@ impl<D: Clone> ImplementationBuilder<Cert<D>> for DefaultBuilder {
) )
} }
type Implementation = CertSymbol<
SetuidCommandRunner<'static, String, StdCommandRunner>,
SetuidCommandRunner<'static, String, StdCommandRunner>,
D,
PathBuf,
>;
type Implementation =
CertSymbol<SetuidCommandRunner<String>, SetuidCommandRunner<String>, D, PathBuf>;
fn create( fn create(
resource: &Cert<D>, resource: &Cert<D>,
target: &<Cert<D> as Resource>::Artifact, target: &<Cert<D> as Resource>::Artifact,
@ -128,7 +124,7 @@ impl<D: Clone> ImplementationBuilder<Cert<D>> for DefaultBuilder {
) -> Self::Implementation { ) -> Self::Implementation {
CertSymbol::new( CertSymbol::new(
resource.0.clone(), resource.0.clone(),
SetuidCommandRunner::new(user_name.0, &StdCommandRunner),
SetuidCommandRunner::new(user_name.0),
root_cert.into(), root_cert.into(),
account_key.into(), account_key.into(),
challenges_dir.into(), challenges_dir.into(),
@ -448,7 +444,7 @@ impl<D, P: AsRef<Path>> ImplementationBuilder<SystemdSocketService<D, P>> for De
FileSymbol<PathBuf, String>, FileSymbol<PathBuf, String>,
SystemdUserSessionSymbol<'static, String, StdCommandRunner>, SystemdUserSessionSymbol<'static, String, StdCommandRunner>,
OwnerSymbol<StdCommandRunner, StdCommandRunner, PathBuf, String>, OwnerSymbol<StdCommandRunner, StdCommandRunner, PathBuf, String>,
UserServiceSymbol<'static, PathBuf, String, StdCommandRunner>,
UserServiceSymbol<'static, PathBuf, String>,
); );
fn create( fn create(
resource: &SystemdSocketService<D, P>, resource: &SystemdSocketService<D, P>,
@ -475,12 +471,7 @@ impl<D, P: AsRef<Path>> ImplementationBuilder<SystemdSocketService<D, P>> for De
user_name.0.clone(), user_name.0.clone(),
StdCommandRunner, StdCommandRunner,
), ),
UserServiceSymbol::new(
socket_path.clone().into(),
user_name.0.clone(),
resource.1,
&StdCommandRunner,
),
UserServiceSymbol::new(socket_path.clone().into(), user_name.0.clone(), resource.1),
) )
} }
} }

14
src/command_runner.rs

@ -78,17 +78,13 @@ impl CommandRunner for StdCommandRunner {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct SetuidCommandRunner<'a, U: AsRef<str>, C: CommandRunner> {
command_runner: &'a C,
pub struct SetuidCommandRunner<U: AsRef<str>> {
user_name: U, user_name: U,
} }
impl<'a, U: AsRef<str>, C: CommandRunner> SetuidCommandRunner<'a, U, C> {
pub fn new(user_name: U, command_runner: &'a C) -> Self {
SetuidCommandRunner {
command_runner,
user_name,
}
impl<'a, U: AsRef<str>> SetuidCommandRunner<U> {
pub fn new(user_name: U) -> Self {
Self { user_name }
} }
} }
@ -121,7 +117,7 @@ impl Drop for TempSetEnv<'_> {
} }
#[async_trait(?Send)] #[async_trait(?Send)]
impl<U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, U, C> {
impl<U: AsRef<str>> CommandRunner for SetuidCommandRunner<U> {
async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> { async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult<Output> {
let uid = get_user_by_name(self.user_name.as_ref()) let uid = get_user_by_name(self.user_name.as_ref())
.expect("User does not exist") .expect("User does not exist")

19
src/symbols/systemd/user_service.rs

@ -8,28 +8,23 @@ use std::path::Path;
use std::time::Duration; use std::time::Duration;
#[derive(Debug)] #[derive(Debug)]
pub struct UserService<'a, S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> {
pub struct UserService<'a, S: AsRef<Path>, U: AsRef<str>> {
socket_path: S, socket_path: S,
service_name: &'a str, service_name: &'a str,
command_runner: SetuidCommandRunner<'a, U, R>,
command_runner: SetuidCommandRunner<U>,
} }
impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'static, S, U, R> {
pub fn new(
socket_path: S,
user_name: U,
service_name: &'static str,
command_runner: &'static R,
) -> Self {
impl<S: AsRef<Path>, U: AsRef<str>> UserService<'static, S, U> {
pub fn new(socket_path: S, user_name: U, service_name: &'static str) -> Self {
Self { Self {
socket_path, socket_path,
service_name, service_name,
command_runner: SetuidCommandRunner::new(user_name, command_runner),
command_runner: SetuidCommandRunner::new(user_name),
} }
} }
} }
impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'_, S, U, R> {
impl<S: AsRef<Path>, U: AsRef<str>> UserService<'_, S, U> {
async fn systemctl_wait_for_dbus(&self, args: &[&OsStr]) -> Result<String, Box<dyn Error>> { async fn systemctl_wait_for_dbus(&self, args: &[&OsStr]) -> Result<String, Box<dyn Error>> {
let mut tries = 5; let mut tries = 5;
loop { loop {
@ -85,7 +80,7 @@ impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> UserService<'_, S, U, R> {
} }
#[async_trait(?Send)] #[async_trait(?Send)]
impl<S: AsRef<Path>, U: AsRef<str>, R: CommandRunner> Symbol for UserService<'_, S, U, R> {
impl<S: AsRef<Path>, U: AsRef<str>> Symbol for UserService<'_, S, U> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> { async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
self.check_if_service().await self.check_if_service().await
} }

Loading…
Cancel
Save