public void testOrgDefautRegistrationToken() throws Exception {
    User user = UserTestUtils.findNewUser("testUser", "testOrg", true);
    Org orig = user.getOrg();
    orig.setName("org created by OrgFactory test: " + TestUtils.randomString());
    // build the channels set
    Channel channel1 = ChannelFactoryTest.createTestChannel(orig);
    flushAndEvict(channel1);
    orig.addOwnedChannel(channel1);
    orig = OrgFactory.save(orig);
    assertTrue(orig.getId().longValue() > 0);

    assertNull(orig.getToken());
    ActivationKey key = ActivationKeyTest.createTestActivationKey(user);
    // Token is hidden behind activation key so we have to look it up
    // manually:
    Token token = TokenFactory.lookupById(key.getId());
    orig.setToken(token);
    orig = OrgFactory.save(orig);
    Long origId = orig.getId();
    flushAndEvict(orig);

    Org lookup = OrgFactory.lookupById(origId);
    assertEquals(token.getId(), lookup.getToken().getId());
    lookup.setToken(null);
    flushAndEvict(lookup);

    lookup = OrgFactory.lookupById(origId);
    assertNull(lookup.getToken());
  }
  /** {@inheritDoc} */
  @Override
  public ActionForward handleDispatch(
      ListSessionSetHelper helper,
      ActionMapping mapping,
      ActionForm formIn,
      HttpServletRequest request,
      HttpServletResponse response) {
    RequestContext context = new RequestContext(request);
    ActivationKey key = context.lookupAndBindActivationKey();
    ConfigChannelListProcessor proc = new ConfigChannelListProcessor();
    Set<String> set = helper.getSet();

    for (String id : set) {
      Long ccid = Long.valueOf(id);
      ConfigChannel cc = ConfigurationFactory.lookupConfigChannelById(ccid);
      proc.remove(key.getConfigChannelsFor(context.getCurrentUser()), cc);
    }
    getStrutsDelegate()
        .saveMessage(
            "config_channels_to_unsubscribe.unsubscribe.success",
            new String[] {String.valueOf(set.size())},
            request);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(RequestContext.TOKEN_ID, key.getToken().getId().toString());
    StrutsDelegate strutsDelegate = getStrutsDelegate();
    return strutsDelegate.forwardParams(mapping.findForward("success"), params);
  }
 public void testCreateNewKeys() throws Exception {
   ActivationKey k = createTestActivationKey(user);
   Server s = k.getServer();
   for (int i = 0; i < 10; i++) {
     ActivationKey tk = createTestActivationKey(s.getCreator(), s);
     System.out.println("tk: " + tk.getKey());
   }
 }
 private void checkVirtEnt(ServerGroupType sgt, Channel virt, Channel tools) throws Exception {
   ActivationKey key = createActivationKey();
   key.addEntitlement(sgt);
   assertTrue(key.getChannels().contains(tools));
   assertTrue(key.getChannels().contains(virt));
   assertTrue(!key.getPackages().isEmpty());
   TokenPackage pkg = key.getPackages().iterator().next();
   assertEquals(ChannelManager.RHN_VIRT_HOST_PACKAGE_NAME, pkg.getPackageName().getName());
 }
 public void testLookupByServer() throws Exception {
   ActivationKey k = createTestActivationKey(user);
   Server s = k.getServer();
   createTestActivationKey(user, s);
   createTestActivationKey(user, s);
   createTestActivationKey(user, s);
   List keys = ActivationKeyFactory.lookupByServer(s);
   assertTrue(keys.size() == 4);
 }
  public void testAddGetKeys() throws Exception {

    ActivationKey k = createTestActivationKey(user);

    for (int i = 0; i < 5; i++) {
      Channel c = ChannelFactoryTest.createTestChannel(user);
      k.addChannel(c);
    }
    assertTrue(k.getChannels().size() == 5);
  }
 public void testKeyTrimming() throws Exception {
   ActivationKeyManager manager = ActivationKeyManager.getInstance();
   String keyName = " Test Space  ";
   ActivationKey k =
       manager.createNewActivationKey(user, keyName, "Cool Duplicate", null, null, false);
   assertEquals(
       ActivationKey.makePrefix(user.getOrg()) + keyName.trim().replace(" ", ""), k.getKey());
   String newKey = keyName + " FOO  ";
   manager.changeKey(newKey, k, user);
   assertNotNull(ActivationKey.makePrefix(user.getOrg()) + newKey.trim());
 }
 public void testDelete() throws Exception {
   user.addPermanentRole(RoleFactory.ACTIVATION_KEY_ADMIN);
   ActivationKey key = manager.createNewActivationKey(user, "Test");
   ActivationKey temp = manager.lookupByKey(key.getKey(), user);
   assertNotNull(temp);
   manager.remove(temp, user);
   try {
     temp = manager.lookupByKey(key.getKey(), user);
     String msg = "NUll lookup failed, because this object should exist!";
     fail(msg);
   } catch (Exception e) {
     // great!.. Exception for null lookpu is controvoersial but convenient..
   }
 }
  // See BZ: 191007
  public void testCreateWithCustomGroups() throws Exception {
    Server s =
        ServerFactoryTest.createTestServer(
            user, true, ServerConstants.getServerGroupTypeEnterpriseEntitled());
    ServerGroup testGroup = ServerGroupTestUtils.createManaged(user);
    s.getManagedGroups().add((ManagedServerGroup) testGroup);

    // Three, one for the server entitlement, one for the user permission to the
    // server, one as the testGroup.
    assertEquals(1, s.getManagedGroups().size());
    ActivationKey key = createTestActivationKey(user, s);
    assertNotNull(key);
    key = (ActivationKey) reload(key);
    assertNotNull(key.getId());
  }
  public void testLookupBySession() throws Exception {
    // Still have that weird error creating a test server
    // sometimes in hosted.
    ActivationKey k = createTestActivationKey(user);
    KickstartData ksdata = KickstartDataTest.createKickstartWithOptions(k.getOrg());
    KickstartFactory.saveKickstartData(ksdata);
    KickstartSession sess = KickstartSessionTest.createKickstartSession(ksdata, k.getCreator());
    KickstartFactory.saveKickstartSession(sess);
    k.setKickstartSession(sess);
    ActivationKeyFactory.save(k);
    k = (ActivationKey) reload(k);

    ActivationKey lookedUp = ActivationKeyFactory.lookupByKickstartSession(sess);
    assertNotNull(lookedUp);
  }
  /**
   * Create a one time activation key for use with a kickstart
   *
   * @param creator of the key
   * @param ksdata associated with the key
   * @param server being kickstarted (can be null)
   * @param session associated with the kickstart (NOT NULL)
   * @param note to add to key
   * @param usageLimit to apply to the key. null for unlimited.
   * @return ActivationKey that has been saved to the DB.
   */
  public static ActivationKey createKickstartActivationKey(
      User creator,
      KickstartData ksdata,
      Server server,
      KickstartSession session,
      Long usageLimit,
      String note) {

    // Now create ActivationKey
    ActivationKey key =
        ActivationKeyManager.getInstance().createNewReActivationKey(creator, server, note, session);
    key.addEntitlement(ServerConstants.getServerGroupTypeProvisioningEntitled());
    key.setDeployConfigs(false);
    key.setUsageLimit(usageLimit);
    if (KickstartVirtualizationType.paraHost()
        .equals(ksdata.getKickstartDefaults().getVirtualizationType())) {
      // we'll have to setup the key for virt
      key.addEntitlement(ServerConstants.getServerGroupTypeVirtualizationEntitled());
    }
    ActivationKeyFactory.save(key);

    // Add child channels to the key
    if (ksdata.getChildChannels() != null && ksdata.getChildChannels().size() > 0) {
      Iterator i = ksdata.getChildChannels().iterator();
      log.debug("Add the child Channels");
      while (i.hasNext()) {
        key.addChannel((Channel) i.next());
      }
    }

    log.debug("** Saving new token");
    ActivationKeyFactory.save(key);
    log.debug("** Saved new token: " + key.getId());
    return key;
  }
  public void testLookup() {
    // first lets just check on permissions...
    user.addPermanentRole(RoleFactory.ACTIVATION_KEY_ADMIN);
    final ActivationKey key = manager.createNewActivationKey(user, "Test");
    ActivationKey temp;
    // we make newuser
    // unfortunately satellite is NOT multiorg aware...
    // So we can't check on the org clause
    // so...
    User newUser =
        UserTestUtils.findNewUser("testUser2", "testOrg" + this.getClass().getSimpleName());
    try {
      manager.lookupByKey(key.getKey(), newUser);
      String msg =
          "Permission check failed :(.."
              + "Activation key should not have gotten found out"
              + " because the user does not have activation key admin role";

      fail(msg);
    } catch (Exception e) {
      // great!.. Exception for permission failure always welcome
    }
    try {
      temp = manager.lookupByKey(key.getKey() + "FOFOFOFOFOFOF", user);
      String msg = "NUll lookup failed, because this object should NOT exist!";
      fail(msg);
    } catch (Exception e) {
      // great!.. Exception for null lookpu is controvoersial but convenient..
    }
    temp = manager.lookupByKey(key.getKey(), user);
    assertNotNull(temp);
    assertEquals(user.getOrg(), temp.getOrg());
  }
  public void testConfigPermissions() throws Exception {
    UserTestUtils.addUserRole(user, RoleFactory.ACTIVATION_KEY_ADMIN);
    ActivationKey key = createActivationKey();

    // need a tools channel for config deploy
    Channel base = ChannelTestUtils.createBaseChannel(user);
    ChannelTestUtils.setupBaseChannelForVirtualization(user, base);

    key.setDeployConfigs(true);
    // Create a config channel
    ConfigChannel cc = ConfigTestUtils.createConfigChannel(user.getOrg());
    ConfigChannelListProcessor proc = new ConfigChannelListProcessor();
    proc.add(key.getConfigChannelsFor(user), cc);
    ActivationKeyFactory.save(key);
    assertTrue(key.getDeployConfigs());
    assertFalse(key.getChannels().isEmpty());
    assertFalse(key.getPackages().isEmpty());
    assertTrue(key.getConfigChannelsFor(user).contains(cc));
  }
  public void testKeyGeneration() throws Exception {

    ActivationKey k = createTestActivationKey(user);
    String note = k.getNote();
    String key = k.getKey();

    TestUtils.saveAndFlush(k);

    ActivationKey k2 = ActivationKeyFactory.lookupByKey(key);
    assertEquals(key, k2.getKey());
    assertEquals(note, k2.getNote());

    ActivationKey k3 = ActivationKeyFactory.lookupByKey(TestUtils.randomString());
    assertNull(k3);

    // Make sure we got the entitlements correct
    Server server = k2.getServer();
    assertEquals(1, server.getEntitlements().size());
    assertEquals(1, k2.getEntitlements().size());

    Entitlement e = server.getEntitlements().iterator().next();
    ServerGroupType t2 = k2.getEntitlements().iterator().next();
    assertEquals(e.getLabel(), t2.getLabel());

    // test out ActivationKeyManager.findByServer while we're here...
    ActivationKey k4 =
        ActivationKeyManager.getInstance().findByServer(server, user).iterator().next();
    assertNotNull(k4);
    assertEquals(key, k4.getKey());

    try {
      k3 = ActivationKeyManager.getInstance().findByServer(null, user).iterator().next();
      String msg =
          "Permission check failed :(.."
              + " Activation key should not have existed"
              + " for a server of 'null' id. An exception "
              + "should have been raised for this.";
      fail(msg);
    } catch (Exception ie) {
      // great!.. Exception for passing in invalid keys always welcome
    }

    User user1 = UserTestUtils.findNewUser("testuser", "testorg");
    Server server2 = ServerFactoryTest.createTestServer(user1);
    try {
      k3 = ActivationKeyManager.getInstance().findByServer(server2, user1).iterator().next();
      String msg =
          "Permission check failed :(.."
              + " Activation key should not have existed"
              + " for a server of the associated id. An exception "
              + "should have been raised for this.";
      fail(msg);
    } catch (Exception ie) {
      // great!.. Exception for passing in invalid keys always welcome
    }
  }
Esempio n. 15
0
  protected void updateCloneDetails(KickstartData cloned, User user, String newLabel) {

    cloned.setLabel(newLabel);
    cloned.setActive(this.isActive());
    cloned.setPostLog(this.getPostLog());
    cloned.setPreLog(this.getPreLog());
    cloned.setKsCfg(this.getKsCfg());
    cloned.setComments(this.getComments());
    cloned.setNonChrootPost(this.getNonChrootPost());
    cloned.setVerboseUp2date(this.getVerboseUp2date());
    cloned.setOrg(this.getOrg());
    cloned.setChildChannels(new HashSet<Channel>(this.getChildChannels()));
    cloned.setPartitionData(getPartitionData());
    copyKickstartCommands(getCommands(), cloned);

    // Gotta remember to create a new HashSet with
    // the other objects.  Otherwise hibernate will
    // complain that you are using the same collection
    // in two objects.
    if (this.getCryptoKeys() != null) {
      cloned.setCryptoKeys(new HashSet<CryptoKey>(this.getCryptoKeys()));
    }

    // NOTE: Make sure we *DONT* clone isOrgDefault
    cloned.setIsOrgDefault(Boolean.FALSE);
    cloned.setKernelParams(this.getKernelParams());
    if (this.getKickstartDefaults() != null) {
      cloned.setKickstartDefaults(this.getKickstartDefaults().deepCopy(cloned));
    }
    cloned.setOrg(this.getOrg());
    if (this.getKsPackages() != null) {
      for (KickstartPackage kp : this.getKsPackages()) {
        cloned.getKsPackages().add(kp.deepCopy(cloned));
      }
    }

    if (this.getPreserveFileLists() != null) {
      cloned.setPreserveFileLists(new HashSet<FileList>(this.getPreserveFileLists()));
    }

    if (this.getScripts() != null) {
      Iterator<KickstartScript> i = this.getScripts().iterator();
      while (i.hasNext()) {
        KickstartScript kss = i.next();
        KickstartScript ksscloned = kss.deepCopy(cloned);
        cloned.getScripts().add(ksscloned);
      }
    }

    // copy all of the non-session related kickstarts
    Set<Token> newTokens = new HashSet<Token>();
    if (this.getDefaultRegTokens() != null) {
      for (Token tok : this.getDefaultRegTokens()) {
        ActivationKey key = ActivationKeyFactory.lookupByToken(tok);
        if (key == null || key.getKickstartSession() == null) {
          newTokens.add(tok);
        }
      }
    }
    cloned.setDefaultRegTokens(newTokens);
    cloned.setNoBase(this.getNoBase());
    cloned.setIgnoreMissing(this.getIgnoreMissing());
  }
  public void testCreate() throws Exception {
    user.addPermanentRole(RoleFactory.ACTIVATION_KEY_ADMIN);
    String note = "Test";
    final ActivationKey key = manager.createNewActivationKey(user, note);
    assertEquals(user.getOrg(), key.getOrg());
    assertEquals(note, key.getNote());
    assertNotNull(key.getKey());
    Server server = ServerFactoryTest.createTestServer(user, true);

    final ActivationKey key1 = manager.createNewReActivationKey(user, server, note);
    assertEquals(server, key1.getServer());

    ActivationKey temp = manager.lookupByKey(key.getKey(), user);
    assertNotNull(temp);
    assertEquals(user.getOrg(), temp.getOrg());
    assertEquals(note, temp.getNote());

    String keyName = "I_RULE_THE_WORLD";
    Long usageLimit = new Long(1200);
    Channel baseChannel = ChannelTestUtils.createBaseChannel(user);

    final ActivationKey key2 =
        manager.createNewReActivationKey(
            user, server, keyName, note, usageLimit, baseChannel, true, null);

    temp = (ActivationKey) reload(key2);
    assertTrue(temp.getKey().endsWith(keyName));
    assertEquals(note, temp.getNote());
    assertEquals(usageLimit, temp.getUsageLimit());
    Set channels = new HashSet();
    channels.add(baseChannel);
    assertEquals(channels, temp.getChannels());

    // since universal default == true we have to
    // check if the user org has it..
    Token token = user.getOrg().getToken();
    assertEquals(channels, token.getChannels());
    assertEquals(usageLimit, token.getUsageLimit());
  }
 public void testNullServer() throws Exception {
   ActivationKey key = ActivationKeyFactory.createNewKey(user, TestUtils.randomString());
   assertNotNull(key.getEntitlements());
   assertTrue(key.getEntitlements().size() == 1);
 }