ContentAccessManager { @Transactional public ContentAccessCertificate getCertificate(Consumer consumer) throws GeneralSecurityException, IOException { Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId()); if (!owner.isContentAccessEnabled() || !this.consumerIsCertV3Capable(consumer)) { return null; } ContentAccessCertificate existing = consumer.getContentAccessCert(); ContentAccessCertificate result = new ContentAccessCertificate(); String pem = ""; if (existing != null && existing.getSerial().getExpiration().getTime() < (new Date()).getTime()) { consumer.setContentAccessCert(null); contentAccessCertificateCurator.delete(existing); existing = null; } if (existing == null) { Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, -1); Date startDate = cal.getTime(); cal.add(Calendar.YEAR, 1); Date endDate = cal.getTime(); CertificateSerial serial = new CertificateSerial(endDate); serialCurator.create(serial); KeyPair keyPair = keyPairCurator.getConsumerKeyPair(consumer); byte[] pemEncodedKeyPair = pki.getPemEncoded(keyPair.getPrivate()); X509Certificate x509Cert = createX509Certificate(consumer, owner, BigInteger.valueOf(serial.getId()), keyPair, startDate, endDate); existing = new ContentAccessCertificate(); existing.setSerial(serial); existing.setKeyAsBytes(pemEncodedKeyPair); existing.setConsumer(consumer); log.info("Setting PEM encoded cert."); pem = new String(this.pki.getPemEncoded(x509Cert)); existing.setCert(pem); consumer.setContentAccessCert(existing); contentAccessCertificateCurator.create(existing); consumer = consumerCurator.merge(consumer); } else { pem = existing.getCert(); } Environment env = this.environmentCurator.getConsumerEnvironment(consumer); OwnerEnvContentAccess oeca = ownerEnvContentAccessCurator .getContentAccess(owner.getId(), env == null ? null : env.getId()); if (oeca == null) { String contentJson = createPayloadAndSignature(owner, env); oeca = new OwnerEnvContentAccess(owner, env, contentJson); oeca = ownerEnvContentAccessCurator.saveOrUpdate(oeca); } pem += oeca.getContentJson(); result.setCert(pem); result.setCreated(existing.getCreated()); result.setUpdated(existing.getUpdated()); result.setId(existing.getId()); result.setConsumer(existing.getConsumer()); result.setKey(existing.getKey()); result.setSerial(existing.getSerial()); return result; } @Inject  ContentAccessManager(
        Configuration config,
        PKIUtility pki,
        X509V3ExtensionUtil v3extensionUtil,
        ContentAccessCertificateCurator contentAccessCertificateCurator,
        KeyPairCurator keyPairCurator,
        CertificateSerialCurator serialCurator,
        OwnerCurator ownerCurator,
        OwnerEnvContentAccessCurator ownerEnvContentAccessCurator,
        ConsumerCurator consumerCurator,
        ConsumerTypeCurator consumerTypeCurator,
        EnvironmentCurator environmentCurator,
        ContentAccessCertificateCurator contentAccessCertCurator,
        OwnerProductCurator ownerProductCurator,
        EventSink eventSink); @Transactional ContentAccessCertificate getCertificate(Consumer consumer); boolean hasCertChangedSince(Consumer consumer, Date date); @Transactional void removeContentAccessCert(Consumer consumer); @Transactional Owner updateOwnerContentAccess(Owner owner, String updatedList, String updatedMode); @Transactional void refreshOwnerForContentAccess(Owner owner);  }
@Test public void testContentPrefixShouldBeOmittedInHosted() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "false"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); String expectedPrefix = ""; ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); verify(this.x509V3ExtensionUtil, times(1)).mapProduct(any(Product.class), any(Product.class), eq(expectedPrefix), any(Map.class), any(Consumer.class), any(Pool.class), any(Set.class)); }
@Test public void testContainerContentPathShouldUseOwnerKeyInHosted() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "false"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); String expectedPath = "/sca/" + owner.getKey(); ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); this.verifyContainerContentPath(expectedPath); }
@Test public void testContentPrefixesShouldBeUsedInStandalone() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "true"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); String expectedPrefix = "/" + owner.getKey(); ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); verify(this.x509V3ExtensionUtil, times(1)).mapProduct(any(Product.class), any(Product.class), eq(expectedPrefix), any(Map.class), any(Consumer.class), any(Pool.class), any(Set.class)); }
@Test public void testContainerContentPathShouldBeContentPrefixInStandalone() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "true"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); String expectedPath = "/" + owner.getKey(); ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); verify(this.x509V3ExtensionUtil, times(1)).mapProduct(any(Product.class), any(Product.class), eq(expectedPath), any(Map.class), any(Consumer.class), any(Pool.class), any(Set.class)); this.verifyContainerContentPath(expectedPath); }
@Test public void testContentPrefixIncludesEnvironmentWhenPresent() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "true"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); Environment environment = this.mockEnvironment(owner, consumer, content); String expectedPrefix = "/" + owner.getKey() + "/" + environment.getName(); ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); verify(this.x509V3ExtensionUtil, times(1)).mapProduct(any(Product.class), any(Product.class), eq(expectedPrefix), any(Map.class), any(Consumer.class), any(Pool.class), any(Set.class)); this.verifyContainerContentPath(expectedPrefix); }
@Test public void testContentPrefixEncoding() throws Exception { this.config.setProperty(ConfigProperties.STANDALONE, "true"); Owner owner = this.mockOwner(); Consumer consumer = this.mockConsumer(owner); Content content = this.mockContent(owner); Product product = this.mockProduct(owner, content); Pool pool = this.mockPool(product); Entitlement entitlement = this.mockEntitlement(owner, consumer, pool); Environment environment = this.mockEnvironment(owner, consumer, content); owner.setKey("org! #$%&'()*+,/123:;=?@[]\"-.<>\\^_`{|}~£円"); environment.setName("test environment #1"); String expectedPrefix = "/org%21+%23%24%25%26%27%28%29*%2B%2C%2F123%3A%3B%3D%3F%40%5B%5D%22" + "-.%3C%3E%5C%5E_%60%7B%7C%7D%7E%C2%A3%E5%86%86/test+environment+%231"; ContentAccessManager manager = this.createManager(); manager.getCertificate(consumer); verify(this.x509V3ExtensionUtil, times(1)).mapProduct(any(Product.class), any(Product.class), eq(expectedPrefix), any(Map.class), any(Consumer.class), any(Pool.class), any(Set.class)); }