SshGenerator implements CredentialGenerator<SshCredentialValue> { @Override public SshCredentialValue generateCredential(final GenerationParameters p) { final SshGenerationParameters params = (SshGenerationParameters) p; try { final KeyPair keyPair = keyGenerator.generateKeyPair(params.getKeyLength()); final String sshComment = params.getSshComment(); final String sshCommentMessage = StringUtils.isEmpty(sshComment) ? "" : " " + sshComment; final String publicKey = CertificateFormatter.derOf((RSAPublicKey) keyPair.getPublic()) + sshCommentMessage; final String privateKey = CertificateFormatter.pemOf(keyPair.getPrivate()); return new SshCredentialValue(publicKey, privateKey, null); } catch (final Exception e) { throw new RuntimeException(e); } } @Autowired  SshGenerator(final RsaKeyPairGenerator keyGenerator); @Override SshCredentialValue generateCredential(final GenerationParameters p);  }
@Test public void generateCredential_shouldReturnAGeneratedCredential() throws Exception { final SshCredentialValue ssh = subject.generateCredential(new SshGenerationParameters()); verify(keyPairGeneratorMock).generateKeyPair(2048); assertThat(ssh.getPublicKey(), equalTo(CertificateFormatter.derOf((RSAPublicKey) keyPair.getPublic()))); assertThat(ssh.getPrivateKey(), equalTo(CertificateFormatter.pemOf(keyPair.getPrivate()))); }
@Test public void generateCredential_shouldUseTheProvidedKeyLength() throws Exception { final SshGenerationParameters sshGenerationParameters = new SshGenerationParameters(); sshGenerationParameters.setKeyLength(4096); subject.generateCredential(sshGenerationParameters); verify(keyPairGeneratorMock).generateKeyPair(4096); }
@Test public void generateCredential_shouldUseTheProvidedSSHComment() throws Exception { final SshGenerationParameters sshGenerationParameters = new SshGenerationParameters(); sshGenerationParameters.setSshComment("this is an ssh comment"); final SshCredentialValue ssh = subject.generateCredential(sshGenerationParameters); final String expectedPublicKey = CertificateFormatter.derOf((RSAPublicKey) keyPair.getPublic()) + " this is an ssh comment"; assertThat(ssh.getPublicKey(), equalTo(expectedPublicKey)); }