Skip to content

Commit c5983f6

Browse files
fix: Added UserInfo impl to set pass phrase for open ssh private keys encrypted with passphrase (#242)
# Conflicts: # src/main/java/io/kestra/plugin/fs/ssh/Command.java
1 parent 48dfbea commit c5983f6

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/main/java/io/kestra/plugin/fs/ssh/Command.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jakarta.validation.constraints.NotNull;
1616
import lombok.*;
1717
import lombok.experimental.SuperBuilder;
18+
import lombok.extern.slf4j.Slf4j;
1819

1920
import java.io.*;
2021
import java.nio.charset.StandardCharsets;
@@ -220,6 +221,8 @@ public ScriptOutput run(RunContext runContext) throws Exception {
220221
}
221222

222223
var rPassword = runContext.render(this.password).as(String.class);
224+
var rPrivateKeyPassphrase = runContext.render(this.privateKeyPassphrase).as(String.class);
225+
223226
switch (renderedAuthMethod) {
224227
case PASSWORD:
225228
session.setConfig("PreferredAuthentications", "password");
@@ -228,8 +231,7 @@ public ScriptOutput run(RunContext runContext) throws Exception {
228231
case PUBLIC_KEY:
229232
session.setConfig("PreferredAuthentications", "publickey");
230233
var privateKeyBytes = runContext.render(this.privateKey).as(String.class).orElseThrow().getBytes();
231-
var passphrase = runContext.render(this.privateKeyPassphrase).as(String.class);
232-
jsch.addIdentity("primary", privateKeyBytes, null, passphrase.map(String::getBytes).orElse(null));
234+
jsch.addIdentity("primary", privateKeyBytes, null, rPrivateKeyPassphrase.map(String::getBytes).orElse(null));
233235
break;
234236
case OPEN_SSH:
235237
var rOpenSSHConfigPath = runContext.render(openSSHConfigPath).as(String.class);
@@ -242,6 +244,9 @@ public ScriptOutput run(RunContext runContext) throws Exception {
242244
ConfigRepository configRepository = OpenSSHConfig.parseFile(configPath);
243245
jsch.setConfigRepository(configRepository);
244246
rPassword.ifPresent(session::setPassword);
247+
if (rPrivateKeyPassphrase.isPresent()) {
248+
session.setUserInfo(new BasicUserInfo(rPrivateKeyPassphrase.get()));
249+
}
245250
break;
246251
}
247252

@@ -303,6 +308,41 @@ public ScriptOutput run(RunContext runContext) throws Exception {
303308
}
304309
}
305310

311+
// Can be extended for Password AuthMethod as well
312+
@Slf4j
313+
private record BasicUserInfo(String passphrase) implements UserInfo {
314+
315+
@Override
316+
public String getPassphrase() {
317+
return passphrase;
318+
}
319+
320+
@Override
321+
public String getPassword() {
322+
return null;
323+
}
324+
325+
@Override
326+
public boolean promptPassword(String message) {
327+
return false;
328+
}
329+
330+
@Override
331+
public boolean promptPassphrase(String message) {
332+
return true;
333+
}
334+
335+
@Override
336+
public boolean promptYesNo(String message) {
337+
return false;
338+
}
339+
340+
@Override
341+
public void showMessage(String message) {
342+
log.debug(message);
343+
}
344+
}
345+
306346
private static class LogRunnable implements Runnable {
307347
private final InputStream inputStream;
308348

0 commit comments

Comments
 (0)