use of org.cloudfoundry.credhub.domain.CertificateGenerationParameters in project credhub by cloudfoundry-incubator.
the class CertificateGeneratorTest method whenTheCADoesNotHaveAPrivateKey_itThrowsAnException.
@Test
public void whenTheCADoesNotHaveAPrivateKey_itThrowsAnException() throws Exception {
CertificateGenerationRequestParameters parameters = new CertificateGenerationRequestParameters();
parameters.setCaName("/ca-without-private-key");
parameters.setKeyLength(2048);
parameters.setSelfSigned(false);
CertificateCredentialValue caWithoutPrivateKey = mock(CertificateCredentialValue.class);
when(certificateAuthorityService.findActiveVersion("/ca-without-private-key")).thenReturn(caWithoutPrivateKey);
when(caWithoutPrivateKey.getPrivateKey()).thenReturn(null);
when(keyGenerator.generateKeyPair(anyInt())).thenReturn(rootCaKeyPair);
try {
subject.generateCredential(new CertificateGenerationParameters(parameters));
fail("Should throw exception");
} catch (ParameterizedValidationException e) {
assertThat(e.getMessage(), equalTo("error.ca_missing_private_key"));
}
}
use of org.cloudfoundry.credhub.domain.CertificateGenerationParameters in project credhub by cloudfoundry-incubator.
the class CertificateGeneratorTest method beforeEach.
@Before
public void beforeEach() throws Exception {
TestHelper.getBouncyCastleProvider();
keyGenerator = mock(LibcryptoRsaKeyPairGenerator.class);
signedCertificateGenerator = mock(SignedCertificateGenerator.class);
certificateAuthorityService = mock(CertificateAuthorityService.class);
permissionCheckingService = mock(PermissionCheckingService.class);
userContext = mock(UserContext.class);
subject = new CertificateGenerator(keyGenerator, signedCertificateGenerator, certificateAuthorityService);
when(permissionCheckingService.hasPermission(anyString(), anyString(), any())).thenReturn(true);
fakeKeyPairGenerator = new FakeKeyPairGenerator();
rootCaDn = new X500Name("O=foo,ST=bar,C=root");
signeeDn = new X500Name("O=foo,ST=bar,C=mars");
rootCaKeyPair = fakeKeyPairGenerator.generate();
X509CertificateHolder caX509CertHolder = makeCert(rootCaKeyPair, rootCaKeyPair.getPrivate(), rootCaDn, rootCaDn, true);
rootCaX509Certificate = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(caX509CertHolder);
rootCa = new CertificateCredentialValue(null, CertificateFormatter.pemOf(rootCaX509Certificate), CertificateFormatter.pemOf(rootCaKeyPair.getPrivate()), null);
generationParameters = new CertificateGenerationRequestParameters();
generationParameters.setOrganization("foo");
generationParameters.setState("bar");
generationParameters.setCaName("my-ca-name");
generationParameters.setCountry("mars");
generationParameters.setDuration(365);
inputParameters = new CertificateGenerationParameters(generationParameters);
}
use of org.cloudfoundry.credhub.domain.CertificateGenerationParameters in project credhub by cloudfoundry-incubator.
the class CertificateGeneratorTest method whenCAExists_andItIsARootCA_aValidChildCertificateIsGeneratedWithTheProvidedKeyLength.
@Test
public void whenCAExists_andItIsARootCA_aValidChildCertificateIsGeneratedWithTheProvidedKeyLength() throws Exception {
final KeyPair childCertificateKeyPair = setupKeyPair();
setupMocksForRootCA(childCertificateKeyPair);
generationParameters.setKeyLength(4096);
CertificateGenerationParameters params = new CertificateGenerationParameters(generationParameters);
when(signedCertificateGenerator.getSignedByIssuer(childCertificateKeyPair, params, rootCaX509Certificate, rootCaKeyPair.getPrivate())).thenReturn(childX509Certificate);
CertificateCredentialValue certificate = subject.generateCredential(params);
assertThat(certificate, notNullValue());
verify(keyGenerator, times(1)).generateKeyPair(4096);
}
use of org.cloudfoundry.credhub.domain.CertificateGenerationParameters in project credhub by cloudfoundry-incubator.
the class CertificateGeneratorTest method whenSelfSignIsTrue_itGeneratesAValidSelfSignedCertificate.
@Test
public void whenSelfSignIsTrue_itGeneratesAValidSelfSignedCertificate() throws Exception {
final X509Certificate certificate = new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(generateX509SelfSignedCert());
generationParameters.setCaName(null);
generationParameters.setSelfSigned(true);
inputParameters = new CertificateGenerationParameters(generationParameters);
when(keyGenerator.generateKeyPair(anyInt())).thenReturn(rootCaKeyPair);
when(signedCertificateGenerator.getSelfSigned(rootCaKeyPair, inputParameters)).thenReturn(certificate);
CertificateCredentialValue certificateCredential = subject.generateCredential(inputParameters);
assertThat(certificateCredential.getPrivateKey(), equalTo(CertificateFormatter.pemOf(rootCaKeyPair.getPrivate())));
assertThat(certificateCredential.getCertificate(), equalTo(CertificateFormatter.pemOf(certificate)));
assertThat(certificateCredential.getCa(), equalTo(CertificateFormatter.pemOf(certificate)));
verify(signedCertificateGenerator, times(1)).getSelfSigned(rootCaKeyPair, inputParameters);
}
Aggregations