use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class DefaultIdentityCertServiceAdapter method generate.
private IdentityCertificate generate(Consumer consumer) throws GeneralSecurityException, IOException {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
Date startDate = cal.getTime();
Date endDate = this.endDateGenerator.apply(new Date());
CertificateSerial serial = new CertificateSerial(endDate);
// We need the sequence generated id before we create the EntitlementCertificate,
// otherwise we could have used cascading create
serialCurator.create(serial);
String dn = createDN(consumer);
IdentityCertificate identityCert = new IdentityCertificate();
KeyPair keyPair = keyPairCurator.getConsumerKeyPair(consumer);
X509Certificate x509cert = pki.createX509Certificate(dn, null, null, startDate, endDate, keyPair, BigInteger.valueOf(serial.getId()), consumer.getName());
identityCert.setCert(new String(pki.getPemEncoded(x509cert)));
identityCert.setKey(new String(pki.getPemEncoded(keyPair.getPrivate())));
identityCert.setSerial(serial);
consumer.setIdCert(identityCert);
return idCertCurator.create(identityCert);
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class Exporter method exportIdentityCertificate.
private void exportIdentityCertificate(File baseDir, Consumer consumer) throws IOException {
File idcertdir = new File(baseDir.getCanonicalPath(), "upstream_consumer");
idcertdir.mkdir();
IdentityCertificate cert = consumer.getIdCert();
File file = new File(idcertdir.getCanonicalPath(), cert.getSerial().getId() + ".json");
// paradigm dictates this should go in an exporter.export method
try (FileWriter writer = new FileWriter(file)) {
mapper.writeValue(writer, this.translator.translate(cert, CertificateDTO.class));
}
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class DefaultIdentityCertServiceAdapterTest method testGenerate.
@Test
public void testGenerate() throws GeneralSecurityException, IOException {
Consumer consumer = mock(Consumer.class);
when(consumer.getId()).thenReturn("42");
when(consumer.getUuid()).thenReturn(Util.generateUUID());
KeyPair kp = createKeyPair();
when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
when(idcur.find(consumer.getId())).thenReturn(null);
when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {
public CertificateSerial answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
CertificateSerial cs = (CertificateSerial) args[0];
cs.setId(42L);
return cs;
}
});
when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {
public IdentityCertificate answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
IdentityCertificate ic = (IdentityCertificate) args[0];
ic.setId("42");
return ic;
}
});
IdentityCertificate ic = dicsa.generateIdentityCert(consumer);
assertNotNull(ic);
assertEquals("priv", ic.getKey());
assertEquals("x509cert", ic.getCert());
assertNotNull(ic.getCertAsBytes());
assertNotNull(ic.getKeyAsBytes());
verify(consumer).setIdCert(ic);
verify(csc).create(any(CertificateSerial.class));
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class DefaultIdentityCertServiceAdapterTest method testRegenerateCallsDeletes.
@Test
public void testRegenerateCallsDeletes() throws GeneralSecurityException, IOException {
Consumer consumer = mock(Consumer.class);
IdentityCertificate mockic = mock(IdentityCertificate.class);
when(consumer.getIdCert()).thenReturn(mockic);
when(mockic.getId()).thenReturn("43");
when(idcur.find(mockic.getId())).thenReturn(mockic);
KeyPair kp = createKeyPair();
when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {
public CertificateSerial answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
CertificateSerial cs = (CertificateSerial) args[0];
cs.setId(42L);
return cs;
}
});
when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {
public IdentityCertificate answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
IdentityCertificate ic = (IdentityCertificate) args[0];
ic.setId("42");
return ic;
}
});
IdentityCertificate ic = dicsa.regenerateIdentityCert(consumer);
verify(consumer).setIdCert(null);
verify(idcur).delete(mockic);
assertNotSame(ic, mockic);
assertEquals("priv", ic.getKey());
assertEquals("x509cert", ic.getCert());
assertNotNull(ic.getCertAsBytes());
assertNotNull(ic.getKeyAsBytes());
verify(consumer).setIdCert(ic);
verify(csc).create(any(CertificateSerial.class));
}
use of org.candlepin.model.IdentityCertificate in project candlepin by candlepin.
the class DefaultIdentityCertServiceAdapterTest method testRegenerate.
@Test
public void testRegenerate() throws GeneralSecurityException, IOException {
Consumer consumer = mock(Consumer.class);
when(consumer.getId()).thenReturn("42L");
when(consumer.getUuid()).thenReturn(Util.generateUUID());
when(idcur.find(consumer.getId())).thenReturn(null);
KeyPair kp = createKeyPair();
when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {
public CertificateSerial answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
CertificateSerial cs = (CertificateSerial) args[0];
cs.setId(42L);
return cs;
}
});
when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {
public IdentityCertificate answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
IdentityCertificate ic = (IdentityCertificate) args[0];
ic.setId("42");
return ic;
}
});
IdentityCertificate ic = dicsa.regenerateIdentityCert(consumer);
assertNotNull(ic);
verify(consumer, never()).setIdCert(null);
verify(idcur, never()).delete(any(IdentityCertificate.class));
assertEquals("priv", ic.getKey());
assertEquals("x509cert", ic.getCert());
assertNotNull(ic.getCertAsBytes());
assertNotNull(ic.getKeyAsBytes());
verify(consumer).setIdCert(ic);
verify(csc).create(any(CertificateSerial.class));
}
Aggregations