Skip to content

Commit

Permalink
env: Check for root privileges
Browse files Browse the repository at this point in the history
Check for root privileges early on.

Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
  • Loading branch information
amshinde committed Apr 27, 2023
1 parent 1016bc1 commit fb40c71
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/tools/kata-ctl/src/ops/env_ops.rs
Expand Up @@ -13,6 +13,7 @@ use crate::utils;
use kata_types::config::TomlConfig;

use anyhow::{anyhow, Context, Result};
use nix::unistd::Uid;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, Write};
Expand Down Expand Up @@ -441,6 +442,10 @@ pub fn get_env_info(toml_config: &TomlConfig) -> Result<EnvInfo> {
}

pub fn handle_env(env_args: EnvArgument) -> Result<()> {
if !Uid::effective().is_root() {
return Err(anyhow!("kata-ctl env command requires root privileges to get host information. Please run as root or use sudo"));
}

let mut file: Box<dyn Write> = if let Some(path) = env_args.file {
Box::new(
File::create(path.as_str()).with_context(|| format!("Error creating file {}", path))?,
Expand Down

0 comments on commit fb40c71

Please sign in to comment.