@Override public void setSSHKeyPair(SSHKeyPair keyPair) { if (keysDir == null) { throw new IllegalStateException("Setting of key pair is not allowed. " + "It can only be called during the Provisioner.createCluster cycle"); } this.sshKeyPair = keyPair; // Save the ssh key pair try { Location publicKeyFile = keysDir.append(Constants.RuntimeMonitor.PUBLIC_KEY); try (OutputStream os = publicKeyFile.getOutputStream()) { os.write(keyPair.getPublicKey().getKey().getBytes(StandardCharsets.UTF_8)); } Location privateKeyFile = keysDir.append(Constants.RuntimeMonitor.PRIVATE_KEY); try (OutputStream os = privateKeyFile.getOutputStream("600")) { os.write(keyPair.getPrivateKeySupplier().get()); } } catch (IOException e) { throw new RuntimeException("Failed to save the ssh key pair", e); } }
/** * Creates a {@link SSHSession} to the given host. * * @param host hostname to ssh to * @return a new {@link SSHSession} * @throws IOException if failed to create a new session to the host */ default SSHSession createSSHSession(SSHKeyPair keyPair, String host) throws IOException { return createSSHSession(keyPair.getPublicKey().getUser(), keyPair.getPrivateKeySupplier(), host, 22, Collections.emptyMap()); }
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 Cluster execute(Cluster cluster) throws Exception { Cluster nextCluster = provisioner.createCluster(provisionerContext); // Add the ssh user property to the resulting Cluster if SSHKeyPair is present in the context return provisionerContext.getSSHContext().getSSHKeyPair() .map(sshKeyPair -> { Map<String, String> properties = new HashMap<>(nextCluster.getProperties()); properties.put(Constants.RuntimeMonitor.SSH_USER, sshKeyPair.getPublicKey().getUser()); return new Cluster(nextCluster.getName(), nextCluster.getStatus(), nextCluster.getNodes(), properties); }) .orElse(nextCluster); } }
@Override public void setSSHKeyPair(SSHKeyPair keyPair) { if (keysDir == null) { throw new IllegalStateException("Setting of key pair is not allowed. " + "It can only be called during the Provisioner.createCluster cycle"); } this.sshKeyPair = keyPair; // Save the ssh key pair try { Location publicKeyFile = keysDir.append(Constants.RuntimeMonitor.PUBLIC_KEY); try (OutputStream os = publicKeyFile.getOutputStream()) { os.write(keyPair.getPublicKey().getKey().getBytes(StandardCharsets.UTF_8)); } Location privateKeyFile = keysDir.append(Constants.RuntimeMonitor.PRIVATE_KEY); try (OutputStream os = privateKeyFile.getOutputStream("600")) { os.write(keyPair.getPrivateKeySupplier().get()); } } catch (IOException e) { throw new RuntimeException("Failed to save the ssh key pair", e); } }
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 Cluster execute(Cluster cluster) throws Exception { Cluster nextCluster = provisioner.createCluster(provisionerContext); // Add the ssh user property to the resulting Cluster if SSHKeyPair is present in the context return provisionerContext.getSSHContext().getSSHKeyPair() .map(sshKeyPair -> { Map<String, String> properties = new HashMap<>(nextCluster.getProperties()); properties.put(Constants.RuntimeMonitor.SSH_USER, sshKeyPair.getPublicKey().getUser()); return new Cluster(nextCluster.getName(), nextCluster.getStatus(), nextCluster.getNodes(), properties); }) .orElse(nextCluster); } }
ec2.importKeyPair(new ImportKeyPairRequest(name, emrConf.getPublicKey().getKey()));
@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); } }
/** * Creates a {@link SSHConfig} for ssh to the master node of the given {@link Cluster}. * * @param cluster the cluster information * @param keysDir the {@link Location} that contains the ssh keys * @return a {@link SSHConfig} */ private SSHConfig createSSHConfig(Cluster cluster, Location keysDir) { // Loads the SSH keys SSHKeyPair sshKeyPair = createSSHKeyPair(keysDir, cluster); Node masterNode = cluster.getNodes().stream() .filter(node -> node.getType() == Node.Type.MASTER) .findFirst().orElseThrow( () -> new IllegalArgumentException("Missing master node information for the cluster " + cluster.getName())); // Creates and return the twill preparer return SSHConfig.builder(masterNode.getIpAddress()) .setUser(sshKeyPair.getPublicKey().getUser()) .setPrivateKeySupplier(sshKeyPair.getPrivateKeySupplier()) .build(); } }
@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); } }
/** * Creates a {@link SSHConfig} for ssh to the master node of the given {@link Cluster}. * * @param cluster the cluster information * @param keysDir the {@link Location} that contains the ssh keys * @return a {@link SSHConfig} */ private SSHConfig createSSHConfig(Cluster cluster, Location keysDir) { // Loads the SSH keys SSHKeyPair sshKeyPair = createSSHKeyPair(keysDir, cluster); Node masterNode = cluster.getNodes().stream() .filter(node -> node.getType() == Node.Type.MASTER) .findFirst().orElseThrow( () -> new IllegalArgumentException("Missing master node information for the cluster " + cluster.getName())); // Creates and return the twill preparer return SSHConfig.builder(masterNode.getIpAddress()) .setUser(sshKeyPair.getPublicKey().getUser()) .setPrivateKeySupplier(sshKeyPair.getPrivateKeySupplier()) .build(); } }
/** * 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); }