use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.
the class EntitlementCertificateGeneratorTest method testNonLazyRegenerationForProductById.
@Test
public void testNonLazyRegenerationForProductById() throws Exception {
Owner owner = TestUtil.createOwner("test-owner", "Test Owner");
Consumer consumer = TestUtil.createConsumer(owner);
Product product = TestUtil.createProduct();
Pool pool = TestUtil.createPool(owner, product);
Entitlement entitlement = TestUtil.createEntitlement(owner, consumer, pool, null);
Set<Entitlement> entitlements = new HashSet<>();
entitlements.add(entitlement);
pool.setEntitlements(entitlements);
HashMap<String, EntitlementCertificate> ecMap = new HashMap<>();
ecMap.put(pool.getId(), new EntitlementCertificate());
when(this.mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), eq(owner), eq(product.getId()), any(Date.class))).thenReturn(Arrays.asList(pool));
when(this.mockEntCertAdapter.generateEntitlementCerts(any(Consumer.class), any(Map.class), any(Map.class), any(Map.class), eq(true))).thenReturn(ecMap);
this.ecGenerator.regenerateCertificatesOf(owner, product.getId(), false);
assertFalse(entitlement.isDirty());
verify(this.mockEntCertAdapter, times(1)).generateEntitlementCerts(any(Consumer.class), this.poolQuantityMapCaptor.capture(), this.entMapCaptor.capture(), this.productMapCaptor.capture(), eq(true));
verify(this.mockEventSink, times(1)).queueEvent(any(Event.class));
}
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 EntitlementImporter method importObject.
public Subscription importObject(ObjectMapper mapper, Reader reader, Owner owner, Map<String, ProductDTO> productsById, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
EntitlementDTO entitlementDTO = mapper.readValue(reader, EntitlementDTO.class);
Entitlement entitlement = new Entitlement();
populateEntity(entitlement, entitlementDTO);
Subscription subscription = new Subscription();
log.debug("Building subscription for owner: {}", owner);
log.debug("Using pool from entitlement: {}", entitlement.getPool());
// Now that we no longer store Subscriptions in the on-site database, we need to
// manually give the subscription a downstream ID. Note that this may later be
// overwritten by reconciliation code if it determines this Subscription
// should replace and existing one.
subscription.setId(Util.generateDbUUID());
subscription.setUpstreamPoolId(entitlement.getPool().getId());
subscription.setUpstreamEntitlementId(entitlement.getId());
subscription.setUpstreamConsumerId(consumerUuid);
subscription.setOwner(owner);
subscription.setStartDate(entitlement.getStartDate());
subscription.setEndDate(entitlement.getEndDate());
subscription.setAccountNumber(entitlement.getPool().getAccountNumber());
subscription.setContractNumber(entitlement.getPool().getContractNumber());
subscription.setOrderNumber(entitlement.getPool().getOrderNumber());
subscription.setQuantity(entitlement.getQuantity().longValue());
for (Branding b : entitlement.getPool().getBranding()) {
subscription.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
}
String cdnLabel = meta.getCdnLabel();
if (!StringUtils.isBlank(cdnLabel)) {
Cdn cdn = cdnCurator.lookupByLabel(cdnLabel);
if (cdn != null) {
subscription.setCdn(cdn);
}
}
ProductDTO productDTO = this.findProduct(productsById, entitlement.getPool().getProductId());
subscription.setProduct(this.translator.translate(productDTO, ProductData.class));
// Add any sub product data to the subscription.
if (entitlement.getPool().getDerivedProductId() != null) {
productDTO = this.findProduct(productsById, entitlement.getPool().getDerivedProductId());
subscription.setDerivedProduct(this.translator.translate(productDTO, ProductData.class));
}
associateProvidedProducts(productsById, entitlement, subscription);
Set<EntitlementCertificate> certs = entitlement.getCertificates();
// subscriptions have one cert
int entcnt = 0;
for (EntitlementCertificate cert : certs) {
++entcnt;
CertificateSerial cs = new CertificateSerial();
cs.setCollected(cert.getSerial().isCollected());
cs.setExpiration(cert.getSerial().getExpiration());
cs.setUpdated(cert.getSerial().getUpdated());
cs.setCreated(cert.getSerial().getCreated());
SubscriptionsCertificate sc = new SubscriptionsCertificate();
sc.setKey(cert.getKey());
sc.setCertAsBytes(cert.getCertAsBytes());
sc.setSerial(cs);
subscription.setCertificate(sc);
}
if (entcnt > 1) {
log.error("More than one entitlement cert found for subscription");
}
return subscription;
}
use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.
the class EntitlementImporter method populateEntity.
/**
* Populates the specified entity with data from the provided DTO.
*
* @param entity
* The entity instance to populate
*
* @param dto
* The DTO containing the data with which to populate the entity
*
* @throws IllegalArgumentException
* if either entity or dto are null
*/
@SuppressWarnings("checkstyle:methodlength")
private void populateEntity(Entitlement entity, EntitlementDTO dto) {
if (entity == null) {
throw new IllegalArgumentException("the entitlement model entity is null");
}
if (dto == null) {
throw new IllegalArgumentException("the entitlement dto is null");
}
if (dto.getId() != null) {
entity.setId(dto.getId());
}
if (dto.getQuantity() != null) {
entity.setQuantity(dto.getQuantity());
}
if (dto.getUpdated() != null) {
entity.setUpdated(dto.getUpdated());
}
if (dto.getCreated() != null) {
entity.setCreated(dto.getCreated());
}
if (dto.getStartDate() != null) {
entity.setStartDate(dto.getStartDate());
}
if (dto.getEndDate() != null) {
entity.setEndDate(dto.getEndDate());
}
if (dto.getPool() != null) {
PoolDTO poolDTO = dto.getPool();
Pool poolEntity = new Pool();
if (poolDTO.getId() != null) {
poolEntity.setId(poolDTO.getId());
}
if (poolDTO.getQuantity() != null) {
poolEntity.setQuantity(poolDTO.getQuantity());
}
if (poolDTO.isActiveSubscription() != null) {
poolEntity.setActiveSubscription(poolDTO.isActiveSubscription());
}
if (poolDTO.isCreatedByShare() != null) {
poolEntity.setCreatedByShare(poolDTO.isCreatedByShare());
}
if (poolDTO.hasSharedAncestor() != null) {
poolEntity.setHasSharedAncestor(poolDTO.hasSharedAncestor());
}
if (poolDTO.getRestrictedToUsername() != null) {
poolEntity.setRestrictedToUsername(poolDTO.getRestrictedToUsername());
}
if (poolDTO.getConsumed() != null) {
poolEntity.setConsumed(poolDTO.getConsumed());
}
if (poolDTO.getExported() != null) {
poolEntity.setExported(poolDTO.getExported());
}
if (poolDTO.getShared() != null) {
poolEntity.setShared(poolDTO.getShared());
}
if (poolDTO.getStackId() != null && poolDTO.getSourceStackId() != null) {
SourceStack sourceStack = new SourceStack();
sourceStack.setId(poolDTO.getStackId());
sourceStack.setSourceStackId(poolDTO.getSourceStackId());
poolEntity.setSourceStack(sourceStack);
}
if (poolDTO.getProductId() != null) {
poolEntity.setProductId(poolDTO.getProductId());
}
if (poolDTO.getDerivedProductId() != null) {
poolEntity.setDerivedProductId(poolDTO.getDerivedProductId());
}
if (poolDTO.getStartDate() != null) {
poolEntity.setStartDate(poolDTO.getStartDate());
}
if (poolDTO.getEndDate() != null) {
poolEntity.setEndDate(poolDTO.getEndDate());
}
if (poolDTO.getCreated() != null) {
poolEntity.setCreated(poolDTO.getCreated());
}
if (poolDTO.getUpdated() != null) {
poolEntity.setUpdated(poolDTO.getUpdated());
}
if (poolDTO.getAccountNumber() != null) {
poolEntity.setAccountNumber(poolDTO.getAccountNumber());
}
if (poolDTO.getOrderNumber() != null) {
poolEntity.setOrderNumber(poolDTO.getOrderNumber());
}
if (poolDTO.getContractNumber() != null) {
poolEntity.setContractNumber(poolDTO.getContractNumber());
}
if (poolDTO.getOwner() != null) {
Owner ownerEntity = new Owner();
populateEntity(ownerEntity, poolDTO.getOwner());
poolEntity.setOwner(ownerEntity);
}
if (poolDTO.getUpstreamPoolId() != null) {
poolEntity.setUpstreamPoolId(poolDTO.getUpstreamPoolId());
}
if (poolDTO.getUpstreamConsumerId() != null) {
poolEntity.setUpstreamConsumerId(poolDTO.getUpstreamConsumerId());
}
if (poolDTO.getUpstreamEntitlementId() != null) {
poolEntity.setUpstreamEntitlementId(poolDTO.getUpstreamEntitlementId());
}
if (poolDTO.getSourceEntitlement() != null) {
EntitlementDTO sourceEntitlementDTO = poolDTO.getSourceEntitlement();
poolEntity.setSourceEntitlement(findEntitlement(sourceEntitlementDTO.getId()));
}
if (poolDTO.getSubscriptionSubKey() != null) {
poolEntity.setSubscriptionSubKey(poolDTO.getSubscriptionSubKey());
}
if (poolDTO.getSubscriptionId() != null) {
poolEntity.setSubscriptionId(poolDTO.getSubscriptionId());
}
if (poolDTO.getAttributes() != null) {
if (poolDTO.getAttributes().isEmpty()) {
poolEntity.setAttributes(Collections.emptyMap());
} else {
poolEntity.setAttributes(poolDTO.getAttributes());
}
}
if (poolDTO.getCalculatedAttributes() != null) {
if (poolDTO.getCalculatedAttributes().isEmpty()) {
poolEntity.setCalculatedAttributes(Collections.emptyMap());
} else {
poolEntity.setCalculatedAttributes(poolDTO.getCalculatedAttributes());
}
}
if (poolDTO.getProductAttributes() != null) {
if (poolDTO.getProductAttributes().isEmpty()) {
poolEntity.setProductAttributes(Collections.emptyMap());
} else {
poolEntity.setProductAttributes(poolDTO.getProductAttributes());
}
}
if (poolDTO.getDerivedProductAttributes() != null) {
if (poolDTO.getDerivedProductAttributes().isEmpty()) {
poolEntity.setDerivedProductAttributes(Collections.emptyMap());
} else {
poolEntity.setDerivedProductAttributes(poolDTO.getDerivedProductAttributes());
}
}
if (poolDTO.getBranding() != null) {
if (poolDTO.getBranding().isEmpty()) {
poolEntity.setBranding(Collections.emptySet());
} else {
Set<Branding> branding = new HashSet<>();
for (BrandingDTO brandingDTO : poolDTO.getBranding()) {
if (brandingDTO != null) {
Branding brandingEntity = new Branding(brandingDTO.getProductId(), brandingDTO.getType(), brandingDTO.getName());
brandingEntity.setId(brandingDTO.getId());
brandingEntity.setCreated(brandingDTO.getCreated());
brandingEntity.setUpdated(brandingDTO.getUpdated());
branding.add(brandingEntity);
}
}
poolEntity.setBranding(branding);
}
}
if (poolDTO.getProvidedProducts() != null) {
if (poolDTO.getProvidedProducts().isEmpty()) {
poolEntity.setProvidedProductDtos(Collections.emptySet());
} else {
Set<ProvidedProduct> providedProducts = new HashSet<>();
for (PoolDTO.ProvidedProductDTO ppDTO : poolDTO.getProvidedProducts()) {
if (ppDTO != null) {
ProvidedProduct providedProduct = new ProvidedProduct();
providedProduct.setProductId(ppDTO.getProductId());
providedProduct.setProductName(ppDTO.getProductName());
providedProducts.add(providedProduct);
}
}
poolEntity.setProvidedProductDtos(providedProducts);
}
}
if (poolDTO.getDerivedProvidedProducts() != null) {
if (poolDTO.getDerivedProvidedProducts().isEmpty()) {
poolEntity.setDerivedProvidedProductDtos(Collections.emptySet());
} else {
Set<ProvidedProduct> derivedProvidedProducts = new HashSet<>();
for (PoolDTO.ProvidedProductDTO dppDTO : poolDTO.getDerivedProvidedProducts()) {
if (dppDTO != null) {
ProvidedProduct derivedProvidedProduct = new ProvidedProduct();
derivedProvidedProduct.setProductId(dppDTO.getProductId());
derivedProvidedProduct.setProductName(dppDTO.getProductName());
derivedProvidedProducts.add(derivedProvidedProduct);
}
}
poolEntity.setDerivedProvidedProductDtos(derivedProvidedProducts);
}
}
entity.setPool(poolEntity);
}
if (dto.getCertificates() != null) {
if (dto.getCertificates().isEmpty()) {
entity.setCertificates(Collections.emptySet());
} else {
Set<EntitlementCertificate> entityCerts = new HashSet<>();
for (CertificateDTO dtoCert : dto.getCertificates()) {
if (dtoCert != null) {
EntitlementCertificate entityCert = new EntitlementCertificate();
entityCert.setId(dtoCert.getId());
entityCert.setKey(dtoCert.getKey());
entityCert.setCert(dtoCert.getCert());
entityCert.setCreated(dtoCert.getCreated());
entityCert.setUpdated(dtoCert.getUpdated());
if (dtoCert.getSerial() != null) {
CertificateSerialDTO dtoSerial = dtoCert.getSerial();
CertificateSerial entitySerial = new CertificateSerial();
entitySerial.setId(dtoSerial.getId());
entitySerial.setCollected(dtoSerial.isCollected());
entitySerial.setExpiration(dtoSerial.getExpiration());
entitySerial.setRevoked(dtoSerial.isRevoked());
entitySerial.setSerial(dtoSerial.getSerial() != null ? dtoSerial.getSerial().longValueExact() : null);
entitySerial.setCreated(dtoSerial.getCreated());
entitySerial.setUpdated(dtoSerial.getUpdated());
entityCert.setSerial(entitySerial);
}
entityCerts.add(entityCert);
}
}
entity.setCertificates(entityCerts);
}
}
}
use of org.candlepin.model.EntitlementCertificate in project candlepin by candlepin.
the class DefaultEntitlementCertServiceAdapter method doEntitlementCertGeneration.
/**
* @param entitlements a map of entitlements indexed by pool ids to generate
* the certs of
* @param productMap a map of respective products indexed by pool id
* @throws IOException
* @throws GeneralSecurityException
* @return entitlementCerts the respective entitlement certs indexed by pool
* id
*/
private Map<String, EntitlementCertificate> doEntitlementCertGeneration(Consumer consumer, Map<String, Product> productMap, Map<String, PoolQuantity> poolQuantities, Map<String, Entitlement> entitlements, boolean save) throws GeneralSecurityException, IOException {
Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
log.debug("Generating entitlement cert for entitlements");
KeyPair keyPair = keyPairCurator.getConsumerKeyPair(consumer);
byte[] pemEncodedKeyPair = pki.getPemEncoded(keyPair.getPrivate());
Map<String, CertificateSerial> serialMap = new HashMap<>();
for (Entry<String, PoolQuantity> entry : poolQuantities.entrySet()) {
// No need to persist the cert serial here as the IDs are generated on object creation.
serialMap.put(entry.getKey(), new CertificateSerial(entry.getValue().getPool().getEndDate()));
}
Map<String, EntitlementCertificate> entitlementCerts = new HashMap<>();
for (Entry<String, PoolQuantity> entry : poolQuantities.entrySet()) {
Pool pool = entry.getValue().getPool();
Entitlement ent = entitlements.get(entry.getKey());
CertificateSerial serial = serialMap.get(entry.getKey());
Product product = productMap.get(entry.getKey());
log.info("Generating entitlement cert for pool: {} quantity: {} entitlement id: {}", pool, ent.getQuantity(), ent.getId());
Set<Product> products = new HashSet<>(productCurator.getPoolProvidedProductsCached(pool));
// If creating a certificate for a distributor, we need
// to add any derived products as well so that their content
// is available in the upstream certificate.
products.addAll(getDerivedProductsForDistributor(pool, consumer));
products.add(product);
Map<String, EnvironmentContent> promotedContent = getPromotedContent(consumer);
String contentPrefix = getContentPrefix(consumer, owner, true);
log.info("Creating X509 cert for product: {}", product);
log.debug("Provided products: {}", products);
List<org.candlepin.model.dto.Product> productModels = v3extensionUtil.createProducts(product, products, contentPrefix, promotedContent, consumer, pool);
X509Certificate x509Cert = createX509Certificate(consumer, owner, pool, ent, product, products, productModels, BigInteger.valueOf(serial.getId()), keyPair, true);
log.debug("Getting PEM encoded cert.");
String pem = new String(this.pki.getPemEncoded(x509Cert));
if (shouldGenerateV3(consumer)) {
log.debug("Generating v3 entitlement data");
byte[] payloadBytes = v3extensionUtil.createEntitlementDataPayload(productModels, consumer, pool, ent.getQuantity());
String payload = "-----BEGIN ENTITLEMENT DATA-----\n";
payload += Util.toBase64(payloadBytes);
payload += "-----END ENTITLEMENT DATA-----\n";
byte[] bytes = pki.getSHA256WithRSAHash(new ByteArrayInputStream(payloadBytes));
String signature = "-----BEGIN RSA SIGNATURE-----\n";
signature += Util.toBase64(bytes);
signature += "-----END RSA SIGNATURE-----\n";
pem += payload + signature;
}
// Build a skeleton cert as part of the entitlement processing.
EntitlementCertificate cert = new EntitlementCertificate();
cert.setKeyAsBytes(pemEncodedKeyPair);
cert.setCert(pem);
if (save) {
cert.setEntitlement(ent);
}
if (log.isDebugEnabled()) {
log.debug("Generated cert serial number: {}", serial.getId());
log.debug("Key: {}", cert.getKey());
log.debug("Cert: {}", cert.getCert());
}
entitlementCerts.put(entry.getKey(), cert);
}
// Serials need to be saved before the certs.
log.debug("Persisting new certificate serials");
serialCurator.saveOrUpdateAll(serialMap);
// certs with their serials and add them to the entitlements.
for (Entry<String, PoolQuantity> entry : poolQuantities.entrySet()) {
CertificateSerial nextSerial = serialMap.get(entry.getKey());
if (nextSerial == null) {
// This should never happen, but checking to be safe.
throw new RuntimeException("Certificate serial not found for entitlement during cert generation.");
}
EntitlementCertificate nextCert = entitlementCerts.get(entry.getKey());
if (nextCert == null) {
// This should never happen, but checking to be safe.
throw new RuntimeException("Entitlement certificate not found for entitlement during cert generation");
}
nextCert.setSerial(nextSerial);
if (save) {
Entitlement ent = entitlements.get(entry.getKey());
ent.getCertificates().add(nextCert);
}
}
if (save) {
log.info("Persisting certs.");
entCertCurator.saveOrUpdateAll(entitlementCerts.values(), false, false);
}
return entitlementCerts;
}
Aggregations