Merge pull request #44 from DeterminateSystems/hoverbear/ds-435-check-if-running-as-root

Check if running as root during install and uninstall
This commit is contained in:
Ana Hobden 2022-11-10 08:43:35 -08:00 committed by GitHub
commit daf4fdc47b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 5 deletions

View file

@ -40,3 +40,7 @@ impl CommandExecute for HarmonicCli {
}
}
}
pub fn is_root() -> bool {
nix::unistd::getuid() == nix::unistd::Uid::from_raw(0)
}

View file

@ -3,7 +3,9 @@ use std::{
process::ExitCode,
};
use crate::{action::ActionState, plan::RECEIPT_LOCATION, BuiltinPlanner, InstallPlan, Planner};
use crate::{
action::ActionState, cli::is_root, plan::RECEIPT_LOCATION, BuiltinPlanner, InstallPlan, Planner,
};
use clap::{ArgAction, Parser};
use eyre::{eyre, WrapErr};
@ -46,6 +48,12 @@ impl CommandExecute for Install {
explain,
} = self;
if !is_root() {
return Err(eyre!(
"`harmonic install` must be run as `root`, try `sudo harmonic install`"
));
}
let existing_receipt: Option<InstallPlan> = match Path::new(RECEIPT_LOCATION).exists() {
true => {
let install_plan_string = tokio::fs::read_to_string(&RECEIPT_LOCATION)

View file

@ -9,11 +9,11 @@ use crate::cli::CommandExecute;
/// Plan an install that can be repeated on an identical host later
#[derive(Debug, Parser)]
#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
#[command(arg_required_else_help = true)]
pub struct Plan {
#[clap(subcommand)]
pub planner: Option<BuiltinPlanner>,
#[clap(env = "HARMONIC_PLAN")]
#[clap(env = "HARMONIC_PLAN", default_value = "/dev/stdout")]
pub output: PathBuf,
}

View file

@ -1,8 +1,8 @@
use std::{path::PathBuf, process::ExitCode};
use crate::{plan::RECEIPT_LOCATION, InstallPlan};
use crate::{cli::is_root, plan::RECEIPT_LOCATION, InstallPlan};
use clap::{ArgAction, Parser};
use eyre::WrapErr;
use eyre::{eyre, WrapErr};
use crate::{cli::CommandExecute, interaction};
@ -37,6 +37,12 @@ impl CommandExecute for Uninstall {
explain,
} = self;
if !is_root() {
return Err(eyre!(
"`harmonic install` must be run as `root`, try `sudo harmonic install`"
));
}
let install_receipt_string = tokio::fs::read_to_string(receipt)
.await
.wrap_err("Reading receipt")?;