Skip to content

Commit

Permalink
Enable passwordless sudo for the user
Browse files Browse the repository at this point in the history
Currently admin user is part of the sudoers
group, adding the user for passwordless sudo
from this issue Azure#69

Signed-off-by: Bala Konda Reddy M <bala12352@gmail.com>
  • Loading branch information
balakreddy committed Jul 10, 2024
1 parent 773f4cf commit 6b69b1c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions libazureinit/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ pub(crate) fn provision_ssh(
Ok(())
}

pub fn add_user_for_passwordless_sudo(
username: &str,
) -> Result<(), Error>{
let mut sudoers_file = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open("/etc/sudoers")?;
write!(sudoers_file, "{} ALL=(ALL) NOPASSWD: ALL \n", username.to_string())?;
sudoers_file.flush()?;
Ok(())
}

#[cfg(test)]
mod tests {

Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ async fn provision() -> Result<(), anyhow::Error> {
|| format!("Unabled to set an empty password for user '{username}'"),
)?;

// Add user for the passwordless sudo
user::add_user_for_passwordless_sudo(
username.as_str())
.with_context(|| format!(
"Unable to add user for the passwordless sudo '{username}'"))?;

user::set_ssh_keys(instance_metadata.compute.public_keys, &username)
.with_context(|| "Failed to write ssh public keys.")?;

Expand Down

0 comments on commit 6b69b1c

Please sign in to comment.