Search in sources :

Example 46 with PoolQuantity

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);
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) PoolManager(org.candlepin.controller.PoolManager) Test(org.junit.Test)

Example 47 with PoolQuantity

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));
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Example 48 with PoolQuantity

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));
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) ArrayList(java.util.ArrayList) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Page(org.candlepin.common.paging.Page) CandlepinQuery(org.candlepin.model.CandlepinQuery) Matchers.anyString(org.mockito.Matchers.anyString) CallerType(org.candlepin.policy.js.entitlement.Enforcer.CallerType) ValidationResult(org.candlepin.policy.ValidationResult) Date(java.util.Date) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Matchers.anyList(org.mockito.Matchers.anyList) AutobindData(org.candlepin.resource.dto.AutobindData) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) ConsumerType(org.candlepin.model.ConsumerType) Test(org.junit.Test)

Example 49 with PoolQuantity

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;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Owner(org.candlepin.model.Owner) KeyPair(java.security.KeyPair) EntitlementCertificate(org.candlepin.model.EntitlementCertificate) HashMap(java.util.HashMap) CertificateSerial(org.candlepin.model.CertificateSerial) Product(org.candlepin.model.Product) EnvironmentContent(org.candlepin.model.EnvironmentContent) X509Certificate(java.security.cert.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 50 with PoolQuantity

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());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) EntitlementCertificate(org.candlepin.model.EntitlementCertificate) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Entitlement(org.candlepin.model.Entitlement)

Aggregations

PoolQuantity (org.candlepin.model.PoolQuantity)75 Pool (org.candlepin.model.Pool)65 Test (org.junit.Test)52 Entitlement (org.candlepin.model.Entitlement)34 LinkedList (java.util.LinkedList)30 Product (org.candlepin.model.Product)28 HashMap (java.util.HashMap)27 ArrayList (java.util.ArrayList)21 Consumer (org.candlepin.model.Consumer)12 List (java.util.List)11 ConsumerType (org.candlepin.model.ConsumerType)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Date (java.util.Date)8 Set (java.util.Set)8 HashSet (java.util.HashSet)7 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)7 ValidationResult (org.candlepin.policy.ValidationResult)7 Subscription (org.candlepin.model.dto.Subscription)6 Matchers.anyLong (org.mockito.Matchers.anyLong)6 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)5