Search in sources :

Example 16 with EntitlementCertificate

use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.

the class EntitlementCertificateGenerator method regenerateCertificatesOf.

/**
 * Regenerates the certificates for the specified entitlement.
 *
 * @param entitlement
 *  The entitlement for which to regenerate certificates
 *
 * @param lazy
 *  Whether or not to generate the certificate immediately, or mark it dirty and allow it to be
 *  regenerated on-demand
 */
@Transactional
public void regenerateCertificatesOf(Entitlement entitlement, boolean lazy) {
    if (lazy) {
        log.info("Marking certificates dirty for entitlement: {}", entitlement);
        entitlement.setDirty(true);
        return;
    }
    log.debug("Revoking entitlementCertificates of: {}", entitlement);
    Entitlement tempE = new Entitlement();
    tempE.setCertificates(entitlement.getCertificates());
    entitlement.setCertificates(null);
    // below call creates new certificates and saves it to the backend.
    try {
        EntitlementCertificate generated = this.generateEntitlementCertificate(entitlement.getPool(), entitlement);
        entitlement.setDirty(false);
        this.entitlementCurator.merge(entitlement);
        for (EntitlementCertificate ec : tempE.getCertificates()) {
            log.debug("Deleting entitlementCertificate: #{}", ec.getId());
            this.entitlementCertificateCurator.delete(ec);
        }
        // send entitlement changed event.
        this.eventSink.queueEvent(this.eventFactory.entitlementChanged(entitlement));
        log.debug("Generated entitlementCertificate: #{}", generated.getId());
    } catch (CertificateSizeException cse) {
        entitlement.setCertificates(tempE.getCertificates());
        log.warn("The certificate cannot be regenerated at this time: {}", cse.getMessage());
    }
}
Also used : EntitlementCertificate(org.candlepin.model.EntitlementCertificate) CertificateSizeException(org.candlepin.util.CertificateSizeException) Entitlement(org.candlepin.model.Entitlement) Transactional(com.google.inject.persist.Transactional)

Example 17 with EntitlementCertificate

use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.

the class EntitlementTranslatorTest method initSourceObject.

@Override
protected Entitlement initSourceObject() {
    Entitlement source = new Entitlement();
    source.setId("ent-id");
    source.setQuantity(1);
    source.setDeletedFromPool(false);
    source.setOwner(this.ownerTranslatorTest.initSourceObject());
    source.setPool(this.poolTranslatorTest.initSourceObject());
    HashSet<EntitlementCertificate> certs = new HashSet<>();
    EntitlementCertificate entCert = new EntitlementCertificate();
    entCert.setId("ent-cert-id");
    entCert.setEntitlement(source);
    entCert.setKey("ent-cert-key");
    entCert.setCert("ent-cert-cert");
    entCert.setSerial(new CertificateSerial());
    certs.add(entCert);
    source.setCertificates(certs);
    Consumer consumer = new Consumer();
    consumer.setUuid("consumer-uuid");
    source.setConsumer(consumer);
    source.setEndDate(new Date());
    source.setStartDate(new Date());
    return source;
}
Also used : EntitlementCertificate(org.candlepin.model.EntitlementCertificate) Consumer(org.candlepin.model.Consumer) CertificateSerial(org.candlepin.model.CertificateSerial) Entitlement(org.candlepin.model.Entitlement) Date(java.util.Date) HashSet(java.util.HashSet)

Example 18 with EntitlementCertificate

use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.

the class StubEntitlementCertServiceAdapter method generateEntitlementCerts.

@Override
public Map<String, EntitlementCertificate> generateEntitlementCerts(Consumer consumer, Map<String, PoolQuantity> poolQuantityMap, Map<String, Entitlement> entitlements, Map<String, Product> products, boolean save) throws GeneralSecurityException, IOException {
    Map<String, EntitlementCertificate> result = new HashMap<>();
    for (Entry<String, Entitlement> entry : entitlements.entrySet()) {
        Entitlement entitlement = entry.getValue();
        Product product = products.get(entry.getKey());
        log.debug("Generating entitlement cert for:");
        log.debug("   consumer: " + consumer.getUuid());
        log.debug("   product: " + product.getUuid());
        log.debug("   end date: " + entitlement.getEndDate());
        EntitlementCertificate cert = new EntitlementCertificate();
        CertificateSerial serial = new CertificateSerial(entitlement.getEndDate());
        serialCurator.create(serial);
        cert.setSerial(serial);
        cert.setKeyAsBytes(("---- STUB KEY -----" + Math.random()).getBytes());
        cert.setCertAsBytes(("---- STUB CERT -----" + Math.random()).getBytes());
        cert.setEntitlement(entitlement);
        entitlement.getCertificates().add(cert);
        log.debug("Generated cert: " + serial.getId());
        log.debug("Key: " + cert.getKey());
        log.debug("Cert: " + cert.getCert());
        if (save) {
            cert = entCertCurator.create(cert);
        }
        result.put(entry.getKey(), cert);
    }
    return result;
}
Also used : EntitlementCertificate(org.candlepin.model.EntitlementCertificate) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) CertificateSerial(org.candlepin.model.CertificateSerial) Entitlement(org.candlepin.model.Entitlement)

Example 19 with EntitlementCertificate

use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.

the class DefaultEntitlementCertServiceAdapterTest method testDetachedEntitlementDataNotAddedToCertV1.

@Test
public void testDetachedEntitlementDataNotAddedToCertV1() throws Exception {
    KeyPair keyPair = new BouncyCastlePKIUtility(null, null, null).generateNewKeyPair();
    when(keyPairCurator.getConsumerKeyPair(any(Consumer.class))).thenReturn(keyPair);
    when(mockedPKI.getPemEncoded(any(X509Certificate.class))).thenReturn("".getBytes());
    when(mockedPKI.getPemEncoded(any(Key.class))).thenReturn("".getBytes());
    final CertificateSerial serial = mock(CertificateSerial.class);
    when(serial.getId()).thenReturn(1L);
    pool.setId("poolId");
    doAnswer(new Answer<Map<String, CertificateSerial>>() {

        @Override
        public Map<String, CertificateSerial> answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Map<String, CertificateSerial> map = (Map<String, CertificateSerial>) args[0];
            map.put("poolId", serial);
            return null;
        }
    }).when(serialCurator).saveOrUpdateAll(anyMap());
    EntitlementCertificate cert = certServiceAdapter.generateEntitlementCert(entitlement, product);
    assertTrue(!cert.getCert().contains("ENTITLEMENT DATA"));
}
Also used : KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) EntitlementCertificate(org.candlepin.model.EntitlementCertificate) CertificateSerial(org.candlepin.model.CertificateSerial) Matchers.anyString(org.mockito.Matchers.anyString) BouncyCastlePKIUtility(org.candlepin.pki.impl.BouncyCastlePKIUtility) X509Certificate(java.security.cert.X509Certificate) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) Key(java.security.Key) Test(org.junit.Test)

Example 20 with EntitlementCertificate

use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.

the class EntitlementCertificateGeneratorTest method testNonLazyRegnerateForEnvironmentContent.

@Test
public void testNonLazyRegnerateForEnvironmentContent() throws Exception {
    Environment environment = new Environment();
    List<Entitlement> entitlements = this.generateEntitlements();
    HashMap<String, EntitlementCertificate> ecMap = new HashMap<>();
    for (Entitlement entitlement : entitlements) {
        ecMap.put(entitlement.getPool().getId(), new EntitlementCertificate());
    }
    CandlepinQuery<Entitlement> cqmock = mock(CandlepinQuery.class);
    when(cqmock.iterator()).thenReturn(entitlements.iterator());
    when(this.mockEntitlementCurator.listByEnvironment(environment)).thenReturn(cqmock);
    when(this.mockEntCertAdapter.generateEntitlementCerts(any(Consumer.class), any(Map.class), any(Map.class), any(Map.class), eq(true))).thenReturn(ecMap);
    this.ecGenerator.regenerateCertificatesOf(environment, Arrays.asList("c1", "c2", "c4"), false);
    assertFalse(entitlements.get(0).isDirty());
    assertFalse(entitlements.get(1).isDirty());
    assertFalse(entitlements.get(2).isDirty());
    verify(this.mockEntCertAdapter, times(2)).generateEntitlementCerts(any(Consumer.class), this.poolQuantityMapCaptor.capture(), this.entMapCaptor.capture(), this.productMapCaptor.capture(), eq(true));
    verify(this.mockEventSink, times(2)).queueEvent(any(Event.class));
}
Also used : EntitlementCertificate(org.candlepin.model.EntitlementCertificate) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) Environment(org.candlepin.model.Environment) Event(org.candlepin.audit.Event) Entitlement(org.candlepin.model.Entitlement) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

EntitlementCertificate (org.candlepin.model.EntitlementCertificate)29 Entitlement (org.candlepin.model.Entitlement)15 Consumer (org.candlepin.model.Consumer)14 CertificateSerial (org.candlepin.model.CertificateSerial)11 Date (java.util.Date)10 HashSet (java.util.HashSet)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 Owner (org.candlepin.model.Owner)8 Pool (org.candlepin.model.Pool)8 Product (org.candlepin.model.Product)7 Map (java.util.Map)5 Event (org.candlepin.audit.Event)4 Certificate (org.candlepin.model.Certificate)3 PoolQuantity (org.candlepin.model.PoolQuantity)3 KeyPair (java.security.KeyPair)2 X509Certificate (java.security.cert.X509Certificate)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 CertificateDTO (org.candlepin.dto.api.v1.CertificateDTO)2