use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.
the class ManifestEntitlementRulesTest method postEntitlement.
@Test
public void postEntitlement() {
Consumer c = this.createMockConsumer(true);
Owner o = mock(Owner.class);
PoolManager pm = mock(PoolManager.class);
Entitlement e = mock(Entitlement.class);
Pool pool = mock(Pool.class);
Product product = mock(Product.class);
when(e.getPool()).thenReturn(pool);
when(e.getConsumer()).thenReturn(c);
when(pool.getProductId()).thenReturn("testProd");
when(product.getAttributes()).thenReturn(new HashMap<>());
when(pool.getAttributes()).thenReturn(new HashMap<>());
Map<String, Entitlement> entitlements = new HashMap<>();
entitlements.put("pool", e);
Map<String, PoolQuantity> poolQuantityMap = new HashMap<>();
poolQuantityMap.put("pool", new PoolQuantity(pool, 1));
enforcer.postEntitlement(pm, c, o, entitlements, null, false, poolQuantityMap);
}
use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.
the class EntitlementCertificateGeneratorTest method testGenerateEntitlementCertificates.
@Test
public void testGenerateEntitlementCertificates() throws GeneralSecurityException, IOException {
this.ecGenerator = new EntitlementCertificateGenerator(this.mockEntCertCurator, this.mockEntCertAdapter, this.mockEntitlementCurator, this.mockPoolCurator, this.mockEventSink, this.mockEventFactory, this.mockProductCurator);
Consumer consumer = mock(Consumer.class);
Product product = mock(Product.class);
Entitlement entitlement = mock(Entitlement.class);
Pool pool = mock(Pool.class);
Map<String, Product> products = new HashMap<>();
products.put("Taylor", product);
Map<String, Entitlement> entitlements = new HashMap<>();
entitlements.put("Taylor", entitlement);
Map<String, PoolQuantity> poolQuantities = new HashMap<>();
poolQuantities.put("Taylor", new PoolQuantity(pool, 1));
ecGenerator.generateEntitlementCertificates(consumer, products, poolQuantities, entitlements, true);
verify(mockEntCertAdapter).generateEntitlementCerts(eq(consumer), eq(poolQuantities), eq(entitlements), eq(products), eq(true));
}
use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.
the class PoolManagerTest method testEntitleByProductsEmptyArray.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testEntitleByProductsEmptyArray() throws Exception {
Product product = TestUtil.createProduct();
List<Pool> pools = new ArrayList<>();
Pool pool1 = TestUtil.createPool(product);
pools.add(pool1);
Date now = new Date();
ValidationResult result = mock(ValidationResult.class);
// Setup an installed product for the consumer, we'll make the bind request
// with no products specified, so this should get used instead:
String[] installedPids = new String[] { product.getUuid() };
ComplianceStatus mockCompliance = new ComplianceStatus(now);
mockCompliance.addNonCompliantProduct(installedPids[0]);
when(complianceRules.getStatus(any(Consumer.class), any(Date.class), any(Boolean.class))).thenReturn(mockCompliance);
Page page = mock(Page.class);
when(page.getPageData()).thenReturn(pools);
when(mockPoolCurator.listAvailableEntitlementPools(any(Consumer.class), any(String.class), anyString(), anyString(), eq(now), any(PoolFilterBuilder.class), any(PageRequest.class), anyBoolean(), anyBoolean(), anyBoolean(), any(Date.class))).thenReturn(page);
CandlepinQuery mockQuery = mock(CandlepinQuery.class);
when(mockPoolCurator.listAllByIds(any(List.class))).thenReturn(mockQuery);
when(mockQuery.iterator()).thenReturn(Arrays.asList(pool1).listIterator());
when(enforcerMock.preEntitlement(any(Consumer.class), any(Pool.class), anyInt(), any(CallerType.class))).thenReturn(result);
when(result.isSuccessful()).thenReturn(true);
List<PoolQuantity> bestPools = new ArrayList<>();
bestPools.add(new PoolQuantity(pool1, 1));
when(autobindRules.selectBestPools(any(Consumer.class), any(String[].class), any(List.class), any(ComplianceStatus.class), any(String.class), any(Set.class), eq(false))).thenReturn(bestPools);
// Make the call but provide a null array of product IDs (simulates healing):
ConsumerType ctype = this.mockConsumerType(TestUtil.createConsumerType());
Consumer consumer = TestUtil.createConsumer(ctype, owner);
AutobindData data = AutobindData.create(consumer, owner).on(now);
manager.entitleByProducts(data);
verify(autobindRules).selectBestPools(any(Consumer.class), eq(installedPids), any(List.class), eq(mockCompliance), any(String.class), any(Set.class), eq(false));
}
use of org.candlepin.model.PoolQuantity 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;
}
use of org.candlepin.model.PoolQuantity in project candlepin by candlepin.
the class DefaultEntitlementCertServiceAdapter method generateEntitlementCert.
// NOTE: we use entitlement here, but it version does not...
// NOTE: we can get consumer from entitlement.getConsumer()
@Override
public EntitlementCertificate generateEntitlementCert(Entitlement entitlement, Product product) throws GeneralSecurityException, IOException {
Map<String, Entitlement> entitlements = new HashMap<>();
entitlements.put(entitlement.getPool().getId(), entitlement);
Map<String, PoolQuantity> poolQuantities = new HashMap<>();
poolQuantities.put(entitlement.getPool().getId(), new PoolQuantity(entitlement.getPool(), entitlement.getQuantity()));
Map<String, Product> products = new HashMap<>();
products.put(entitlement.getPool().getId(), product);
Map<String, EntitlementCertificate> result = generateEntitlementCerts(entitlement.getConsumer(), poolQuantities, entitlements, products, true);
return result.get(entitlement.getPool().getId());
}
Aggregations