From 6e693725d646729368953fd065e405787e7e476e Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 19 Dec 2009 14:47:01 -0800 Subject: [PATCH] Fix reading the $site_path/etc/ssh_host_key in serialized form Change-Id: I9c3898d38fc5d2298a6863a45ff75e1512b055e3 Signed-off-by: Shawn O. Pearce --- .../google/gerrit/sshd/HostKeyProvider.java | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/HostKeyProvider.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/HostKeyProvider.java index 5e10f420ff..f901a776ac 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/HostKeyProvider.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/HostKeyProvider.java @@ -22,6 +22,7 @@ import com.google.inject.ProvisionException; import org.apache.sshd.common.KeyPairProvider; import org.apache.sshd.common.keyprovider.FileKeyPairProvider; import org.apache.sshd.common.util.SecurityUtils; +import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; import java.io.File; import java.util.ArrayList; @@ -37,33 +38,42 @@ class HostKeyProvider implements Provider { @Override public KeyPairProvider get() { - final File anyKey = site.ssh_key; + final File objKey = site.ssh_key; final File rsaKey = site.ssh_rsa; final File dsaKey = site.ssh_dsa; - final List keys = new ArrayList(2); + final List stdKeys = new ArrayList(2); if (rsaKey.exists()) { - keys.add(rsaKey.getAbsolutePath()); + stdKeys.add(rsaKey.getAbsolutePath()); } if (dsaKey.exists()) { - keys.add(dsaKey.getAbsolutePath()); + stdKeys.add(dsaKey.getAbsolutePath()); } - if (anyKey.exists() && !keys.isEmpty()) { - // If both formats of host key exist, we don't know which format - // should be authoritative. Complain and abort. - // - keys.add(anyKey.getAbsolutePath()); - throw new ProvisionException("Multiple host keys exist: " + keys); - } + if (objKey.exists()) { + if (stdKeys.isEmpty()) { + SimpleGeneratorHostKeyProvider p = new SimpleGeneratorHostKeyProvider(); + p.setPath(objKey.getAbsolutePath()); + return p; - if (keys.isEmpty()) { - throw new ProvisionException("No SSH keys under " + site.etc_dir); + } else { + // Both formats of host key exist, we don't know which format + // should be authoritative. Complain and abort. + // + stdKeys.add(objKey.getAbsolutePath()); + throw new ProvisionException("Multiple host keys exist: " + stdKeys); + } + + } else { + if (stdKeys.isEmpty()) { + throw new ProvisionException("No SSH keys under " + site.etc_dir); + } + if (!SecurityUtils.isBouncyCastleRegistered()) { + throw new ProvisionException("Bouncy Castle Crypto not installed;" + + " needed to read server host keys: " + stdKeys + ""); + } + return new FileKeyPairProvider(stdKeys + .toArray(new String[stdKeys.size()])); } - if (!SecurityUtils.isBouncyCastleRegistered()) { - throw new ProvisionException("Bouncy Castle Crypto not installed;" - + " needed to read server host keys: " + keys + ""); - } - return new FileKeyPairProvider(keys.toArray(new String[keys.size()])); } }