-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDEC-436: Generate SSH key before instalation
- Loading branch information
Anatoliy Bazko
committed
Jan 16, 2016
1 parent
c45c1a4
commit 055234e
Showing
6 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
installation-manager-core/src/main/java/com/codenvy/im/utils/SshKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
installation-manager-core/src/test/java/com/codenvy/im/utils/SshKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters