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());
}
}
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;
}
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;
}
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"));
}
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));
}
Aggregations