Skip to content

Commit a2ab99b

Browse files
committed
Update PR#5684: use char[] in authentication
1 parent e9a25ba commit a2ab99b

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

server/src/main/java/com/cloud/hypervisor/kvm/discoverer/LibvirtServerDiscoverer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ private void setupAgentSecurity(final Connection sshConnection, final String age
263263
final String privateKey = _configDao.getValue("ssh.privatekey");
264264
if (!SSHCmdHelper.acquireAuthorizedConnectionWithPublicKey(sshConnection, username, privateKey)) {
265265
s_logger.error("Failed to authenticate with ssh key");
266+
if (org.apache.commons.lang3.StringUtils.isEmpty(password)) {
267+
throw new DiscoveredWithErrorException("Authentication error with ssh private key");
268+
}
266269
if (!sshConnection.authenticateWithPassword(username, password)) {
267270
s_logger.error("Failed to authenticate with password");
268-
throw new DiscoveredWithErrorException("Authentication error");
271+
throw new DiscoveredWithErrorException("Authentication error with host password");
269272
}
270273
}
271274

utils/src/main/java/com/cloud/utils/ssh/SSHCmdHelper.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package com.cloud.utils.ssh;
2121

22-
import java.io.File;
23-
import java.io.FileWriter;
2422
import java.io.IOException;
2523
import java.io.InputStream;
2624

@@ -85,29 +83,15 @@ public static com.trilead.ssh2.Connection acquireAuthorizedConnection(String ip,
8583

8684
public static boolean acquireAuthorizedConnectionWithPublicKey(final com.trilead.ssh2.Connection sshConnection, final String username, final String privateKey) {
8785
if (StringUtils.isNotBlank(privateKey)) {
88-
File privateKeyFile = null;
8986
try {
90-
privateKeyFile = File.createTempFile("cloudstack-host-", null);
91-
FileWriter writer = new FileWriter(privateKeyFile.getAbsolutePath());
92-
writer.write(privateKey);
93-
writer.close();
94-
} catch (IOException e) {
95-
s_logger.warn("An exception occurred when create a tmp file and write private key to the tmp file");
96-
return false;
97-
}
98-
try {
99-
if (!sshConnection.authenticateWithPublicKey(username, privateKeyFile, null)) {
87+
if (!sshConnection.authenticateWithPublicKey(username, privateKey.toCharArray(), null)) {
10088
s_logger.warn("Failed to authenticate with ssh key");
10189
return false;
10290
}
10391
return true;
10492
} catch (IOException e) {
10593
s_logger.warn("An exception occurred when authenticate with ssh key");
10694
return false;
107-
} finally {
108-
if (privateKeyFile != null && privateKeyFile.exists()) {
109-
privateKeyFile.delete();
110-
}
11195
}
11296
}
11397
return false;

0 commit comments

Comments
 (0)