Skip to content

Commit 14fc0b4

Browse files
committed
refactor: separate SSH key setup commands for better error handling
Split the single complex shell command in SshKeySetupAction::execute into four focused private methods: - create_ssh_directory: handles 'mkdir -p ~/.ssh' - add_public_key_to_authorized_keys: adds public key to authorized_keys file - set_ssh_directory_permissions: handles 'chmod 700' on SSH directory - set_authorized_keys_permissions: handles 'chmod 600' on authorized_keys file This provides better error granularity, making it easier to identify which specific operation fails during SSH key setup. Each command now executes individually with its own error handling.
1 parent 96397a2 commit 14fc0b4

File tree

1 file changed

+64
-11
lines changed

1 file changed

+64
-11
lines changed

src/e2e/containers/actions/ssh_key_setup.rs

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,29 +100,82 @@ impl SshKeySetupAction {
100100
let user_ssh_dir = format!("/home/{ssh_user}/.ssh");
101101
let authorized_keys_path = format!("{user_ssh_dir}/authorized_keys");
102102

103-
// Execute the command to setup SSH keys
103+
// Execute each command separately for better error handling
104+
Self::create_ssh_directory(container, &user_ssh_dir)?;
105+
Self::add_public_key_to_authorized_keys(
106+
container,
107+
&public_key_content,
108+
&authorized_keys_path,
109+
)?;
110+
Self::set_ssh_directory_permissions(container, &user_ssh_dir)?;
111+
Self::set_authorized_keys_permissions(container, &authorized_keys_path)?;
112+
113+
info!(
114+
ssh_user = ssh_user,
115+
authorized_keys = authorized_keys_path,
116+
"SSH key authentication configured"
117+
);
118+
119+
Ok(())
120+
}
121+
122+
/// Create the SSH directory for the user
123+
fn create_ssh_directory<T: ContainerExecutor>(container: &T, user_ssh_dir: &str) -> Result<()> {
124+
let command = ExecCommand::new(["sh", "-c", &format!("mkdir -p {user_ssh_dir}")]);
125+
126+
container
127+
.exec(command)
128+
.map_err(|source| SshKeySetupError::SshKeySetupFailed { source })?;
129+
130+
Ok(())
131+
}
132+
133+
/// Add the public key to the `authorized_keys` file
134+
fn add_public_key_to_authorized_keys<T: ContainerExecutor>(
135+
container: &T,
136+
public_key_content: &str,
137+
authorized_keys_path: &str,
138+
) -> Result<()> {
104139
let command = ExecCommand::new([
105140
"sh",
106141
"-c",
107142
&format!(
108-
"mkdir -p {} && echo '{}' >> {} && chmod 700 {} && chmod 600 {}",
109-
user_ssh_dir,
143+
"echo '{}' >> {authorized_keys_path}",
110144
public_key_content.trim(),
111-
authorized_keys_path,
112-
user_ssh_dir,
113-
authorized_keys_path
114145
),
115146
]);
116147

117148
container
118149
.exec(command)
119150
.map_err(|source| SshKeySetupError::SshKeySetupFailed { source })?;
120151

121-
info!(
122-
ssh_user = ssh_user,
123-
authorized_keys = authorized_keys_path,
124-
"SSH key authentication configured"
125-
);
152+
Ok(())
153+
}
154+
155+
/// Set permissions on the SSH directory (700)
156+
fn set_ssh_directory_permissions<T: ContainerExecutor>(
157+
container: &T,
158+
user_ssh_dir: &str,
159+
) -> Result<()> {
160+
let command = ExecCommand::new(["sh", "-c", &format!("chmod 700 {user_ssh_dir}")]);
161+
162+
container
163+
.exec(command)
164+
.map_err(|source| SshKeySetupError::SshKeySetupFailed { source })?;
165+
166+
Ok(())
167+
}
168+
169+
/// Set permissions on the `authorized_keys` file (600)
170+
fn set_authorized_keys_permissions<T: ContainerExecutor>(
171+
container: &T,
172+
authorized_keys_path: &str,
173+
) -> Result<()> {
174+
let command = ExecCommand::new(["sh", "-c", &format!("chmod 600 {authorized_keys_path}")]);
175+
176+
container
177+
.exec(command)
178+
.map_err(|source| SshKeySetupError::SshKeySetupFailed { source })?;
126179

127180
Ok(())
128181
}

0 commit comments

Comments
 (0)