private static SSHPublicKey createSSHPublicKey(Location keysDir, String sshUser) throws IOException { try (InputStream is = keysDir.append(Constants.RuntimeMonitor.PUBLIC_KEY).getInputStream()) { return new SSHPublicKey(sshUser, new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8)); } }
private static SSHPublicKey createSSHPublicKey(Location keysDir, String sshUser) throws IOException { try (InputStream is = keysDir.append(Constants.RuntimeMonitor.PUBLIC_KEY).getInputStream()) { return new SSHPublicKey(sshUser, new String(ByteStreams.toByteArray(is), StandardCharsets.UTF_8)); } }
@Override public SSHKeyPair generate(String user, int bits) throws KeyException { JSch jsch = new JSch(); try { KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits); ByteArrayOutputStream bos = new ByteArrayOutputStream(); keyPair.writePublicKey(bos, user); SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8)); bos.reset(); keyPair.writePrivateKey(bos); byte[] privateKey = bos.toByteArray(); return new SSHKeyPair(publicKey, () -> privateKey); } catch (JSchException e) { throw new KeyException("Failed to generate ssh key pair", e); } }
@Override public SSHKeyPair generate(String user, int bits) throws KeyException { JSch jsch = new JSch(); try { KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits); ByteArrayOutputStream bos = new ByteArrayOutputStream(); keyPair.writePublicKey(bos, user); SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8)); bos.reset(); keyPair.writePrivateKey(bos); byte[] privateKey = bos.toByteArray(); return new SSHKeyPair(publicKey, () -> privateKey); } catch (JSchException e) { throw new KeyException("Failed to generate ssh key pair", e); } }
/** * Create the conf from a property map while also performing validation. */ public static RemoteHadoopConf fromProperties(Map<String, String> properties) { String host = getString(properties, "host"); String user = getString(properties, "user"); String privateKey = getString(properties, "sshKey"); String initializationAction = getString(properties, "initializationAction"); SSHKeyPair keyPair = new SSHKeyPair(new SSHPublicKey(user, ""), () -> privateKey.getBytes(StandardCharsets.UTF_8)); return new RemoteHadoopConf(keyPair, host, initializationAction); }