Skip to content

Commit

Permalink
CDEC-436: Generate SSH key before instalation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Bazko committed Jan 16, 2016
1 parent c45c1a4 commit 055234e
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class Config {
public static final String NODE_SSH_USER_PRIVATE_KEY_PROPERTY = "node_ssh_user_private_key";
public static final String SYSTEM_LDAP_PASSWORD = "system_ldap_password";

public static final String PUBLIC_KEY = "public_key";
public static final String PRIVATE_KEY = "private_key";

public static final String ADDITIONAL_BUILDERS = "additional_builders";
public static final String ADDITIONAL_RUNNERS = "additional_runners";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.codenvy.im.artifacts.InstallManagerArtifact;
import com.codenvy.im.commands.SimpleCommand;
import com.codenvy.im.utils.HttpTransport;
import com.codenvy.im.utils.SshKey;
import com.codenvy.im.utils.Version;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
Expand Down Expand Up @@ -471,6 +472,8 @@ public Map<String, String> prepareInstallProperties(@Nullable String configFile,
if (installType == InstallType.MULTI_SERVER) {
setSSHAccessProperties(properties);
}
setSshKeyParts(properties);

} else { // update
if (binaries != null) {
properties = loadConfigProperties(binaries, installType);
Expand Down Expand Up @@ -527,6 +530,15 @@ protected void setSSHAccessProperties(Map<String, String> properties) throws IOE
properties.put(Config.NODE_SSH_USER_PRIVATE_KEY, sshKey); // set private key of ssh user
}

/**
* Generates and sets private and public parts of the ssh key.
*/
protected void setSshKeyParts(Map<String, String> properties) throws IOException {
SshKey sshKey = new SshKey();
properties.put(Config.PUBLIC_KEY, sshKey.getPublicPart());
properties.put(Config.PRIVATE_KEY, sshKey.getPrivatePart().replace("\n", "\\n"));
}

protected String readSSHKey(Path pathToIdRsa) throws IOException {
if (!exists(pathToIdRsa)) {
throw new RuntimeException("SSH private key not found: " + pathToIdRsa.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* CODENVY CONFIDENTIAL
* __________________
*
* [2012] - [2016] Codenvy, S.A.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Codenvy S.A. and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Codenvy S.A.
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Codenvy S.A..
*/
package com.codenvy.im.utils;

import com.codenvy.im.commands.CommandException;
import com.codenvy.im.commands.SimpleCommand;

import org.eclipse.che.commons.annotation.Nullable;

import java.io.IOException;
import java.math.BigInteger;
import java.security.SecureRandom;

import static com.codenvy.im.commands.SimpleCommand.createCommand;
import static java.lang.String.format;

/**
* Generates ssh key.
*
* @author Anatoliy Bazko
*/
public class SshKey {
private static final String GENERATE_STRATEGY = "ssh-keygen -q -P '' -t rsa -f %s";

private String privatePart;
private String publicPart;

public SshKey() throws IOException {
try {
generate();
} catch (CommandException e) {
invalidateKey();
throw new IOException("Can't generate ssh key", e);
}
}

private void generate() throws CommandException {
String file = new BigInteger(120, new SecureRandom()).toString();

SimpleCommand command = createCommand(format(GENERATE_STRATEGY, file));
command.execute();

command = createCommand(format("cat %s", file));
privatePart = command.execute();

command = createCommand(format("cat %s.pub", file));
publicPart = command.execute();

command = createCommand(format("rm %1$s; rm %1$s.pub", file));
command.execute();
}

/**
* @return private part of the ssh key or null if key is invalid due to some reason
*/
@Nullable
public String getPrivatePart() {
return privatePart;
}

/**
* @return public part of the ssh key or null if key is invalid due to some reason
*/
@Nullable
public String getPublicPart() {
return publicPart;
}

private void invalidateKey() {
privatePart = null;
publicPart = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,10 @@ public void testPrepareInstallPropertiesLoadPropertiesFromConfigInstallUseCase()
ArtifactFactory.createArtifact(CDECArtifact.NAME),
Version.valueOf("3.1.0"),
true);
assertEquals(actualProperties.size(), 1);
assertEquals(actualProperties.size(), 3);
assertEquals(actualProperties.get("a"), "b");
assertTrue(actualProperties.containsKey(Config.PRIVATE_KEY));
assertTrue(actualProperties.containsKey(Config.PUBLIC_KEY));
}

@Test
Expand All @@ -440,8 +442,10 @@ public void testPrepareInstallPropertiesLoadDefaultPropertiesInstallUseCase() th
ArtifactFactory.createArtifact(CDECArtifact.NAME),
Version.valueOf("3.1.0"),
true);
assertEquals(actualProperties.size(), 1);
assertEquals(actualProperties.size(), 3);
assertEquals(actualProperties.get("a"), "b");
assertTrue(actualProperties.containsKey(Config.PRIVATE_KEY));
assertTrue(actualProperties.containsKey(Config.PUBLIC_KEY));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.codenvy.im.utils;

/*
* CODENVY CONFIDENTIAL
* __________________
*
* [2012] - [2016] Codenvy, S.A.
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Codenvy S.A. and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Codenvy S.A.
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Codenvy S.A..
*/

import org.testng.annotations.Test;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

/**
* @author Anatoliy Bazko
*/
public class SshKeyTest {

@Test
public void testGenerate() throws Exception {
SshKey sshKey = new SshKey();

String privatePart = sshKey.getPrivatePart();
String publicPart = sshKey.getPublicPart();

assertNotNull(privatePart);
assertTrue(privatePart.contains("-----BEGIN RSA PRIVATE KEY-----"));

assertNotNull(publicPart);
assertTrue(publicPart.startsWith("ssh-rsa"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ env=prod
target_server=prod
on_prem=true
version=4.0.0-beta-10-SNAPSHOT
private_key=-----BEGIN RSA PRIVATE KEY-----\nMIIEoQIBAAKCAQEA6L+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FdyR6qIYN05CfE0C827rehfR\nN/OavcSSLDTsXr/Y5Kj2w08s6zzi/vZPIKxJ5zYRvDMy4Ztvd7dlc8DOfcl1drVR\nL0UqSUvWqpdKMPeELiDdpoddEfVD1d9DQT+TQGrlcrF/TnRHXkLIEXEHWrdlbXjJ\n4wep0tDiGM005dtO9yUCgYEA4vBQUBXw0pMgCE6Dot4Abunh9s1C+Aq1koPFq9ql\ntaCCloNGD4Fm4y9my20e/JCGy97cMGPMPSIuCqXcyNZMZSlJbNvgW3CwA+eziSNq\n8y/zRwdFA2olsJBb+XW6lBegUpGL2stlS6UA4r8qrcPrf93rGrFA8XAKk+ybOG+e\ntBMCgYBasPLiZZ+/rg/Y1hjopPBcYhbTjeKl353YAHFb29F3hgKjYgcoIwtfyeB7\nsIJ3geiIVu2sg8Qr4yiAhiDBNHPqrQ83yEi2ri/qzDT+oswNzzrT2QQ2gD8fwH5D\ni3AZhvggK0Q9bIst/QeUxofJdCKvfxSWb66MdyA2G2dSwIZ+Jg==\n-----END RSA PRIVATE KEY-----
public_key=ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6L+RRE/F5YWusbM5XUEx/5EYZAxMnp1MDH3THSnBg7a5jdYWfIh6ogxujVBdOnQiH0bGOHTqcCXvXvHsEBuZs5OLioCGNzuX60B9h5qg2YSKv6OlG0W4IPlL2xlRMMOkQkJuh8fKpvfaf5Vfr8TuuQjQijVJUckzmOHh438QginYk2eItrHDU0nTRPL5pvHQOGqAy0lO/AiEYHS/rSEWZoXDUEXNomi8qrQHmCax17bFe1y4XqgGHytVPkwnRilWxA2+S78T9wUGK12gzkIaOiDRQUt7WlfSAdqkL1XU+nxBjkhPWDstcsrWAFIt/8LKI24k9XBLz5jyNfaD2VvbHw== admin@codenvy-dev.com
private_key=
public_key=

##############################
# http / https configuration
Expand Down

0 comments on commit 055234e

Please sign in to comment.