Skip to content

Commit

Permalink
Merge pull request zhaofengli#91 from dminuoso/ssh-without-user
Browse files Browse the repository at this point in the history
Do not use an explicit user for ssh when deploymentUser is null
  • Loading branch information
zhaofengli authored Jun 2, 2022
2 parents 7681d26 + 012328d commit 66d65b6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 24 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ sys-info = "0.9.0"
snafu = "0.7.0"
tempfile = "3.1.0"
tokio-stream = "0.1.8"
users = "0.11.0"
uuid = { version = "1.0.0", features = ["serde", "v4"] }
validator = { version = "0.15.0", features = ["derive"] }

Expand Down
4 changes: 3 additions & 1 deletion package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ rustPlatform.buildRustPackage rec {
src = lib.cleanSource ./.;
};

cargoSha256 = "sha256-GxWhzDx3hK7ONtLINYz0Ssw4DL+BNX6uH0JoQuooCCU=";
cargoLock = {
lockFile = ./Cargo.lock;
};

nativeBuildInputs = [ installShellFiles ];

Expand Down
11 changes: 7 additions & 4 deletions src/nix/host/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::{CopyDirection, CopyOptions, RebootOptions, Host, key_uploader};
#[derive(Debug)]
pub struct Ssh {
/// The username to use to connect.
user: String,
user: Option<String>,

/// The hostname or IP address to connect to.
host: String,
Expand Down Expand Up @@ -166,7 +166,7 @@ impl Host for Ssh {
}

impl Ssh {
pub fn new(user: String, host: String) -> Self {
pub fn new(user: Option<String>, host: String) -> Self {
Self {
user,
host,
Expand Down Expand Up @@ -197,7 +197,7 @@ impl Ssh {
pub fn ssh(&self, command: &[&str]) -> Command {
let options = self.ssh_options();
let options_str = options.join(" ");
let privilege_escalation_command = if self.user != "root" {
let privilege_escalation_command = if self.user.as_deref() != Some("root") {
self.privilege_escalation_command.as_slice()
} else {
&[]
Expand All @@ -224,7 +224,10 @@ impl Ssh {
}

fn ssh_target(&self) -> String {
format!("{}@{}", self.user, self.host)
match &self.user {
Some(n) => format!("{}@{}", n, self.host),
None => self.host.clone(),
}
}

fn nix_copy_closure(&self, path: &StorePath, direction: CopyDirection, options: CopyOptions) -> Command {
Expand Down
8 changes: 1 addition & 7 deletions src/nix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::path::Path;

use serde::de;
use serde::{Deserialize, Deserializer, Serialize};
use users::get_current_username;
use validator::{Validate, ValidationError as ValidationErrorType};

use crate::error::{ColmenaResult, ColmenaError};
Expand Down Expand Up @@ -164,12 +163,7 @@ impl NodeConfig {

pub fn to_ssh_host(&self) -> Option<Ssh> {
self.target_host.as_ref().map(|target_host| {
let username =
match &self.target_user {
Some(uname) => uname.clone(),
None => get_current_username().unwrap().into_string().unwrap(),
};
let mut host = Ssh::new(username, target_host.clone());
let mut host = Ssh::new(self.target_user.clone(), target_host.clone());
host.set_privilege_escalation_command(self.privilege_escalation_command.clone());

if let Some(target_port) = self.target_port {
Expand Down

0 comments on commit 66d65b6

Please sign in to comment.