Search in sources :

Example 1 with IdentityCertificate

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);
}
Also used : KeyPair(java.security.KeyPair) Calendar(java.util.Calendar) CertificateSerial(org.candlepin.model.CertificateSerial) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 2 with IdentityCertificate

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));
    }
}
Also used : CertificateDTO(org.candlepin.dto.manifest.v1.CertificateDTO) FileWriter(java.io.FileWriter) File(java.io.File) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 3 with IdentityCertificate

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));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 4 with IdentityCertificate

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));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 5 with IdentityCertificate

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));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Aggregations

IdentityCertificate (org.candlepin.model.IdentityCertificate)39 Consumer (org.candlepin.model.Consumer)25 Test (org.junit.Test)25 CertificateSerial (org.candlepin.model.CertificateSerial)16 Date (java.util.Date)14 Owner (org.candlepin.model.Owner)13 ConsumerType (org.candlepin.model.ConsumerType)10 File (java.io.File)8 ArrayList (java.util.ArrayList)8 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)7 ZipInputStream (java.util.zip.ZipInputStream)7 Principal (org.candlepin.auth.Principal)7 CandlepinQuery (org.candlepin.model.CandlepinQuery)7 List (java.util.List)6 Set (java.util.Set)6 KeyPair (org.candlepin.model.KeyPair)6 Rules (org.candlepin.model.Rules)6 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)6