DefaultEntitlementCertServiceAdapter extends BaseEntitlementCertServiceAdapter { public Set<X509ExtensionWrapper> prepareV3Extensions() { Set<X509ExtensionWrapper> result = v3extensionUtil.getExtensions(); X509ExtensionWrapper typeExtension = new X509ExtensionWrapper(OIDUtil.REDHAT_OID + "." + OIDUtil.TOPLEVEL_NAMESPACES.get(OIDUtil.ENTITLEMENT_TYPE_KEY), false, "Basic"); result.add(typeExtension); return result; } @Inject  DefaultEntitlementCertServiceAdapter(PKIUtility pki,
        X509ExtensionUtil extensionUtil,
        X509V3ExtensionUtil v3extensionUtil,
        EntitlementCertificateCurator entCertCurator,
        KeyPairCurator keyPairCurator,
        CertificateSerialCurator serialCurator,
        OwnerCurator ownerCurator,
        EntitlementCurator entCurator, I18n i18n,
        Configuration config,
        ProductCurator productCurator,
        ConsumerTypeCurator consumerTypeCurator,
        EnvironmentCurator environmentCurator); @Override EntitlementCertificate generateEntitlementCert(Entitlement entitlement, Product product); @Override Map<String, EntitlementCertificate> generateEntitlementCerts(Consumer consumer,
        Map<String, PoolQuantity> poolQuantities, Map<String, Entitlement> entitlements,
        Map<String, Product> products, boolean save); X509Certificate createX509Certificate(Consumer consumer, Owner owner, Pool pool,
        Entitlement ent, Product product, Set<Product> products,
        List<org.candlepin.model.dto.Product> productModels, BigInteger serialNumber,
        KeyPair keyPair, boolean useContentPrefix); Set<X509ExtensionWrapper> prepareV1Extensions(Set<Product> products,
        Pool pool, Consumer consumer, Integer quantity, String contentPrefix,
        Map<String, EnvironmentContent> promotedContent); Set<X509ExtensionWrapper> prepareV3Extensions(); Set<X509ByteExtensionWrapper> prepareV3ByteExtensions(Product sku,
        List<org.candlepin.model.dto.Product> productModels, String contentPrefix,
        Map<String, EnvironmentContent> promotedContent); String cleanUpPrefix(String contentPrefix); List<Long> listEntitlementSerialIds(Consumer consumer);  }
@Test public void testPrepareV3EntitlementData() throws IOException { Set<Product> products = new HashSet<>(); products.add(product); consumer.setFact("system.certificate_version", X509V3ExtensionUtil.CERT_VERSION); consumer.setFact("uname.machine", "x86_64"); Product product = pool.getProduct(); product.setAttribute(Product.Attributes.WARNING_PERIOD, "20"); product.setAttribute(Product.Attributes.SOCKETS, "4"); product.setAttribute(Product.Attributes.RAM, "8"); product.setAttribute(Product.Attributes.CORES, "4"); product.setAttribute(Product.Attributes.MANAGEMENT_ENABLED, "true"); product.setAttribute(Product.Attributes.STACKING_ID, "45678"); pool.setAttribute(Product.Attributes.VIRT_ONLY, "true"); product.setAttribute(Product.Attributes.SUPPORT_LEVEL, "slevel"); product.setAttribute(Product.Attributes.SUPPORT_TYPE, "stype"); pool.setAccountNumber("account1"); pool.setContractNumber("contract1"); pool.setOrderNumber("order1"); for (ProductContent pc : product.getProductContent()) { pc.setEnabled(false); } Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6")); assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), (X509V3ExtensionUtil.CERT_VERSION)); byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); assertEquals(data.get("consumer"), "test-consumer"); assertEquals(data.get("quantity"), 10); Map<String, Object> subs = (Map<String, Object>) data.get("subscription"); assertEquals(subs.get("sku"), subscription.getProduct().getId()); assertEquals(subs.get("name"), subscription.getProduct().getName()); assertEquals(subs.get("warning"), 20); assertEquals(subs.get("sockets"), 4); assertEquals(subs.get("ram"), 8); assertEquals(subs.get("cores"), 4); assertTrue((Boolean) subs.get("management")); assertEquals(subs.get("stacking_id"), "45678"); assertTrue((Boolean) subs.get("virt_only")); Map<String, Object> service = (Map<String, Object>) subs.get("service"); assertEquals(service.get("level"), "slevel"); assertEquals(service.get("type"), "stype"); Map<String, Object> order = (Map<String, Object>) data.get("order"); assertEquals(order.get("number"), pool.getOrderNumber()); assertEquals(((Integer) order.get("quantity")).intValue(), (long) subscription.getQuantity()); assertNotNull(order.get("start")); assertNotNull(order.get("end")); List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products"); List<Map<String, Object>> contents; for (Map<String, Object> prod : prods) { assertEquals(prod.get("id"), product.getId()); assertEquals(prod.get("name"), product.getName()); assertEquals(prod.get("version"), product.getAttributeValue(Product.Attributes.VERSION)); String arch = product.hasAttribute(Product.Attributes.ARCHITECTURE) ? product.getAttributeValue(Product.Attributes.ARCHITECTURE) : ""; StringTokenizer st = new StringTokenizer(arch, ","); while (st.hasMoreElements()) { assertTrue(((List) prod.get("architectures")).contains(st.nextElement())); } contents = (List<Map<String, Object>>) prod.get("content"); for (Map<String, Object> cont : contents) { assertEquals(cont.get("id"), CONTENT_ID); assertEquals(cont.get("name"), CONTENT_NAME); assertEquals(cont.get("type"), CONTENT_TYPE); assertEquals(cont.get("label"), CONTENT_LABEL); assertEquals(cont.get("vendor"), CONTENT_VENDOR); assertEquals(cont.get("gpg_url"), CONTENT_GPG_URL); assertEquals(cont.get("path"), "prefix" + CONTENT_URL); assertFalse((Boolean) cont.get("enabled")); assertEquals(cont.get("metadata_expire"), 3200); List<String> arches = new ArrayList<>(); arches.add(ARCH_LABEL); assertEquals(cont.get("arches"), arches); String rTags = content.getRequiredTags(); st = new StringTokenizer(rTags, ","); while (st.hasMoreElements()) { assertTrue(((List) cont.get("required_tags")).contains(st.nextElement())); } } } }
@Test public void testPrepareV3EntitlementDataNoConsumerArch() throws IOException { Set<Product> products = new HashSet<>(); products.add(product); setupEntitlements(null, X509V3ExtensionUtil.CERT_VERSION); Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products"); List<Map<String, Object>> contents; for (Map<String, Object> prod : prods) { String arch = product.hasAttribute(Product.Attributes.ARCHITECTURE) ? product.getAttributeValue(Product.Attributes.ARCHITECTURE) : ""; StringTokenizer st = new StringTokenizer(arch, ","); while (st.hasMoreElements()) { assertTrue(((List) prod.get("architectures")).contains(st.nextElement())); } contents = (List<Map<String, Object>>) prod.get("content"); for (Map<String, Object> cont : contents) { assertEquals(cont.get("id"), CONTENT_ID); assertEquals(cont.get("path"), "prefix" + CONTENT_URL); assertFalse((Boolean) cont.get("enabled")); List<String> arches = new ArrayList<>(); arches.add(ARCH_LABEL); assertEquals(cont.get("arches"), arches); } } }
@Test public void testPrepareV3EntitlementDataNoContentArch() throws IOException { Set<Product> products = new HashSet<>(); Product inheritedArchProduct = TestUtil.createProduct("12345", "a product"); inheritedArchProduct.setAttribute(Product.Attributes.VERSION, "version"); inheritedArchProduct.setAttribute(Product.Attributes.VARIANT, "variant"); inheritedArchProduct.setAttribute(Product.Attributes.TYPE, "SVC"); inheritedArchProduct.setAttribute(Product.Attributes.ARCHITECTURE, ARCH_LABEL); inheritedArchProduct.addContent(noArchContent, false); products.add(inheritedArchProduct); setupEntitlements(ARCH_LABEL, X509V3ExtensionUtil.CERT_VERSION); Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6")); assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), (X509V3ExtensionUtil.CERT_VERSION)); byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products"); List<Map<String, Object>> contents; for (Map<String, Object> prod : prods) { String arch = product.hasAttribute(Product.Attributes.ARCHITECTURE) ? product.getAttributeValue(Product.Attributes.ARCHITECTURE) : ""; StringTokenizer st = new StringTokenizer(arch, ","); while (st.hasMoreElements()) { assertTrue(((List) prod.get("architectures")).contains(st.nextElement())); } contents = (List<Map<String, Object>>) prod.get("content"); for (Map<String, Object> cont : contents) { List<String> arches = new ArrayList<>(); arches.add(ARCH_LABEL); assertEquals(cont.get("arches"), arches); } } }
@Test public void testPrepareV3EntitlementDataNoCompatibleArch() throws IOException { Set<Product> products = new HashSet<>(); Product wrongArchProduct = TestUtil.createProduct("12345", "a product"); wrongArchProduct.setAttribute(Product.Attributes.VERSION, "version"); wrongArchProduct.setAttribute(Product.Attributes.VARIANT, "variant"); wrongArchProduct.setAttribute(Product.Attributes.TYPE, "SVC"); wrongArchProduct.setAttribute(Product.Attributes.ARCHITECTURE, "ALL"); String wrongArches = "s390x,s390,ppc64,ia64"; Content wrongArchContent = createContent(CONTENT_NAME, CONTENT_ID, CONTENT_LABEL, CONTENT_TYPE, CONTENT_VENDOR, CONTENT_URL, CONTENT_GPG_URL, wrongArches); wrongArchProduct.addContent(wrongArchContent, false); products.clear(); products.add(wrongArchProduct); setupEntitlements(ARCH_LABEL, X509V3ExtensionUtil.CERT_VERSION); Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6")); assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), (X509V3ExtensionUtil.CERT_VERSION)); byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products"); List<Map<String, Object>> contents; for (Map<String, Object> prod : prods) { String arch = wrongArchProduct.hasAttribute(Product.Attributes.ARCHITECTURE) ? wrongArchProduct.getAttributeValue(Product.Attributes.ARCHITECTURE) : ""; StringTokenizer st = new StringTokenizer(arch, ","); while (st.hasMoreElements()) { assertTrue(((List) prod.get("architectures")).contains(st.nextElement())); } contents = (List<Map<String, Object>>) prod.get("content"); assertTrue(contents.isEmpty()); } }
@Test public void testPrepareV3EntitlementDataForDefaults() throws IOException { Set<Product> products = new HashSet<>(); products.add(product); consumer.setFact("system.certificate_version", X509V3ExtensionUtil.CERT_VERSION); consumer.setFact("uname.machine", "x86_64"); subscription.getProduct().setAttribute(Product.Attributes.WARNING_PERIOD, "0"); subscription.getProduct().setAttribute(Product.Attributes.MANAGEMENT_ENABLED, "false"); entitlement.getPool().setAttribute(Product.Attributes.VIRT_ONLY, "false"); for (ProductContent pc : product.getProductContent()) { pc.setEnabled(true); } Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6")); assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), (X509V3ExtensionUtil.CERT_VERSION)); byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); assertEquals(data.get("consumer"), "test-consumer"); Map<String, Object> subs = (Map<String, Object>) data.get("subscription"); assertNull(subs.get("warning")); assertNull(subs.get("management")); assertNull(subs.get("virt_only")); List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products"); for (Map<String, Object> prod : prods) { List<Map<String, Object>> contents = (List<Map<String, Object>>) prod.get("content"); for (Map<String, Object> cont : contents) { assertNull(cont.get("enabled")); } } }
@Test public void testPrepareV3EntitlementDataForBooleans() throws IOException { Set<Product> products = new HashSet<>(); products.add(product); consumer.setUuid("test-consumer"); consumer.setFact("system.certificate_version", X509V3ExtensionUtil.CERT_VERSION); consumer.setFact("uname.machine", "x86_64"); pool.getProduct().setAttribute(Product.Attributes.MANAGEMENT_ENABLED, "1"); entitlement.getPool().setAttribute(Product.Attributes.VIRT_ONLY, "1"); Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions(); Map<String, X509ExtensionWrapper> map = new HashMap<>(); for (X509ExtensionWrapper ext : extensions) { map.put(ext.getOid(), ext); } assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6")); assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), (X509V3ExtensionUtil.CERT_VERSION)); byte[] payload = v3extensionUtil.createEntitlementDataPayload( getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity()); String stringValue; try { stringValue = processPayload(payload); } catch (Exception e) { throw new RuntimeException(e); } Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue , Map.class); assertEquals(data.get("consumer"), "test-consumer"); Map<String, Object> subs = (Map<String, Object>) data.get("subscription"); assertTrue((Boolean) subs.get("management")); assertTrue((Boolean) subs.get("virt_only")); }