@Test
  public void hypervisorCheckInCreatesNewConsumer() throws Exception {
    Owner owner = new Owner("admin");

    Map<String, List<GuestId>> hostGuestMap = new HashMap<String, List<GuestId>>();
    hostGuestMap.put("test-host", Arrays.asList(new GuestId("GUEST_A"), new GuestId("GUEST_B")));

    when(consumerCurator.findByUuid(eq("test-host"))).thenReturn(null);
    when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
    when(principal.canAccess(eq(owner), eq(Access.ALL))).thenReturn(true);
    when(consumerTypeCurator.lookupByLabel(eq(ConsumerTypeEnum.HYPERVISOR.getLabel())))
        .thenReturn(hypervisorType);
    when(idCertService.generateIdentityCert(any(Consumer.class)))
        .thenReturn(new IdentityCertificate());

    HypervisorCheckInResult result =
        hypervisorResource.hypervisorCheckIn(hostGuestMap, principal, owner.getKey());

    Set<Consumer> created = result.getCreated();
    assertEquals(1, created.size());

    Consumer c1 = created.iterator().next();
    assertEquals("test-host", c1.getUuid());
    assertEquals(2, c1.getGuestIds().size());
    assertEquals("GUEST_A", c1.getGuestIds().get(0).getGuestId());
    assertEquals("GUEST_B", c1.getGuestIds().get(1).getGuestId());
    assertEquals("x86_64", c1.getFact("uname.machine"));
    assertEquals("hypervisor", c1.getType().getLabel());
  }
Esempio n. 2
0
 @Override
 public PoolHelper postEntitlement(Consumer consumer, PoolHelper postEntHelper, Entitlement ent) {
   if (consumer.getType().isManifest()) {
     return manifestEnforcer.postEntitlement(consumer, postEntHelper, ent);
   }
   return jsEnforcer.postEntitlement(consumer, postEntHelper, ent);
 }
Esempio n. 3
0
  @Before
  public void createObjects() {
    beginTransaction();

    prod1 = TestUtil.createProduct();
    prod2 = TestUtil.createProduct();
    productCurator.create(prod1);
    productCurator.create(prod2);
    owner = new Owner("testowner");
    ownerCurator.create(owner);

    Set<ProvidedProduct> providedProducts = new HashSet<ProvidedProduct>();
    ProvidedProduct providedProduct = new ProvidedProduct(prod2.getId(), prod2.getName());
    providedProducts.add(providedProduct);

    pool = TestUtil.createPool(owner, prod1, providedProducts, 1000);
    providedProduct.setPool(pool);
    poolCurator.create(pool);
    owner = pool.getOwner();

    consumer = TestUtil.createConsumer(owner);

    productCurator.create(prod1);
    poolCurator.create(pool);
    consumerTypeCurator.create(consumer.getType());
    consumerCurator.create(consumer);

    commitTransaction();
  }
Esempio n. 4
0
  @Test
  public void exportConsumer() throws ExportCreationException, IOException {
    config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
    config.setProperty(ConfigProperties.PREFIX_WEBURL, "localhost:8443/weburl");
    config.setProperty(ConfigProperties.PREFIX_APIURL, "localhost:8443/apiurl");
    Rules mrules = mock(Rules.class);
    Consumer consumer = mock(Consumer.class);
    Principal principal = mock(Principal.class);

    when(mrules.getRules()).thenReturn("foobar");
    when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes());
    when(rc.getRules()).thenReturn(mrules);
    when(pprov.get()).thenReturn(principal);
    when(principal.getUsername()).thenReturn("testUser");

    // specific to this test
    IdentityCertificate idcert = new IdentityCertificate();
    idcert.setSerial(new CertificateSerial(10L, new Date()));
    idcert.setKey("euh0876puhapodifbvj094");
    idcert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
    idcert.setCreated(new Date());
    idcert.setUpdated(new Date());
    when(consumer.getIdCert()).thenReturn(idcert);

    KeyPair keyPair = createKeyPair();
    when(consumer.getKeyPair()).thenReturn(keyPair);
    when(pki.getPemEncoded(keyPair.getPrivateKey())).thenReturn("privateKey".getBytes());
    when(pki.getPemEncoded(keyPair.getPublicKey())).thenReturn("publicKey".getBytes());
    when(consumer.getUuid()).thenReturn("8auuid");
    when(consumer.getName()).thenReturn("consumer_name");
    when(consumer.getType()).thenReturn(new ConsumerType(ConsumerTypeEnum.CANDLEPIN));

    // FINALLY test this badboy
    Exporter e =
        new Exporter(
            ctc,
            me,
            ce,
            cte,
            re,
            ece,
            ecsa,
            pe,
            psa,
            pce,
            ec,
            ee,
            pki,
            config,
            exportRules,
            pprov,
            dvc,
            dve,
            cdnc,
            cdne);
    File export = e.getFullExport(consumer);

    verifyContent(export, "export/consumer.json", new VerifyConsumer("consumer.json"));
  }
  private boolean shouldGenerateV3(Entitlement entitlement) {
    Consumer consumer = entitlement.getConsumer();

    if (consumer.getType().isManifest()) {
      for (ConsumerCapability capability : consumer.getCapabilities()) {
        if ("cert_v3".equals(capability.getName())) {
          return true;
        }
      }
      return false;
    } else if (consumer.getType().getLabel().equals(ConsumerTypeEnum.HYPERVISOR.getLabel())) {
      // Hypervisors in this context don't use content, so allow v3
      return true;
    } else {
      String entitlementVersion = consumer.getFact("system.certificate_version");
      return entitlementVersion != null && entitlementVersion.startsWith("3.");
    }
  }
Esempio n. 6
0
  @Override
  public ValidationResult preEntitlement(
      Consumer consumer, Pool entitlementPool, Integer quantity) {

    if (consumer.getType().isManifest()) {
      return manifestEnforcer.preEntitlement(consumer, entitlementPool, quantity);
    }

    return jsEnforcer.preEntitlement(consumer, entitlementPool, quantity);
  }
  @Test(expected = BadRequestException.class)
  public void testCreatePersonConsumerWithActivationKey() {
    Consumer c = mock(Consumer.class);
    Owner o = mock(Owner.class);
    ActivationKey ak = mock(ActivationKey.class);
    NoAuthPrincipal nap = mock(NoAuthPrincipal.class);
    ActivationKeyCurator akc = mock(ActivationKeyCurator.class);
    OwnerCurator oc = mock(OwnerCurator.class);
    ConsumerTypeCurator ctc = mock(ConsumerTypeCurator.class);
    ConsumerContentOverrideCurator ccoc = mock(ConsumerContentOverrideCurator.class);

    ConsumerType cType = new ConsumerType(ConsumerTypeEnum.PERSON);
    when(ak.getId()).thenReturn("testKey");
    when(o.getKey()).thenReturn("testOwner");
    when(akc.lookupForOwner(eq("testKey"), eq(o))).thenReturn(ak);
    when(oc.lookupByKey(eq("testOwner"))).thenReturn(o);
    when(c.getType()).thenReturn(cType);
    when(c.getName()).thenReturn("testConsumer");
    when(ctc.lookupByLabel(eq("person"))).thenReturn(cType);

    ConsumerResource cr =
        new ConsumerResource(
            null,
            ctc,
            null,
            null,
            null,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            oc,
            akc,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    cr.create(c, nap, null, "testOwner", "testKey", true);
  }
Esempio n. 8
0
  /*
   * Distributors should always get suggested=1, increment=1
   */
  @Test
  public void testInstanceBasedOnDistributor() {
    Consumer dist = TestUtil.createConsumer(owner);
    dist.getType().setManifest(true);
    dist.setFact(IS_VIRT, "false");
    dist.setFact(SOCKET_FACT, "4");
    pool.getProduct().setAttribute(SOCKET_ATTRIBUTE, "2");
    pool.getProduct().setAttribute(INSTANCE_ATTRIBUTE, "2");

    SuggestedQuantity suggested = quantityRules.getSuggestedQuantity(pool, dist, new Date());
    assertEquals(new Long(1), suggested.getSuggested());
    assertEquals(new Long(1), suggested.getIncrement());
  }
  @Test(expected = NotFoundException.class)
  public void testNullPerson() {
    Consumer c = mock(Consumer.class);
    Owner o = mock(Owner.class);
    UserServiceAdapter usa = mock(UserServiceAdapter.class);
    UserPrincipal up = mock(UserPrincipal.class);
    OwnerCurator oc = mock(OwnerCurator.class);
    ConsumerTypeCurator ctc = mock(ConsumerTypeCurator.class);
    ConsumerType cType = new ConsumerType(ConsumerTypeEnum.PERSON);

    when(o.getKey()).thenReturn("testOwner");
    when(oc.lookupByKey(eq("testOwner"))).thenReturn(o);
    when(c.getType()).thenReturn(cType);
    when(c.getName()).thenReturn("testConsumer");
    when(ctc.lookupByLabel(eq("person"))).thenReturn(cType);
    when(up.canAccess(eq(o), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
    // usa.findByLogin() will return null by default no need for a when

    ConsumerResource cr =
        new ConsumerResource(
            null,
            ctc,
            null,
            null,
            null,
            null,
            null,
            i18n,
            null,
            null,
            null,
            null,
            usa,
            null,
            null,
            oc,
            null,
            null,
            null,
            null,
            null,
            null,
            new CandlepinCommonTestConfig(),
            null,
            null,
            null,
            consumerBindUtil,
            productCurator,
            null);
    cr.create(c, up, null, "testOwner", null, true);
  }
Esempio n. 10
0
  @Before
  public void createEnforcer() throws Exception {
    MockitoAnnotations.initMocks(this);

    when(config.getInt(eq(ConfigProperties.PRODUCT_CACHE_MAX))).thenReturn(100);
    productCache = new ProductCache(config, productAdapter);

    owner = createOwner();
    ownerCurator.create(owner);

    consumer = TestUtil.createConsumer(owner);
    consumerTypeCurator.create(consumer.getType());
    consumerCurator.create(consumer);

    BufferedReader reader =
        new BufferedReader(
            new InputStreamReader(getClass().getResourceAsStream("/rules/test-rules.js")));
    StringBuilder builder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      builder.append(line + "\n");
    }
    reader.close();

    Rules rules = mock(Rules.class);
    when(rules.getRules()).thenReturn(builder.toString());
    when(rulesCurator.getRules()).thenReturn(rules);
    when(rulesCurator.getUpdated()).thenReturn(TestDateUtil.date(2010, 1, 1));

    JsRunner jsRules = new JsRunnerProvider(rulesCurator).get();

    enforcer =
        new EntitlementRules(
            new DateSourceForTesting(2010, 1, 1),
            jsRules,
            productCache,
            i18n,
            config,
            consumerCurator,
            poolCurator);
  }