Search in sources :

Example 16 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ConsumerResourceTest method testCreateConsumerShouldFailOnMaxLengthOfName.

@Test
public void testCreateConsumerShouldFailOnMaxLengthOfName() {
    thrown.expect(BadRequestException.class);
    thrown.expectMessage(String.format("Name of the consumer " + "should be shorter than %d characters.", Consumer.MAX_LENGTH_OF_CONSUMER_NAME + 1));
    Owner owner = this.createOwner();
    ConsumerType ctype = this.mockConsumerType(new ConsumerType(ConsumerTypeEnum.SYSTEM));
    ConsumerTypeDTO ctypeDto = this.translator.translate(ctype, ConsumerTypeDTO.class);
    Consumer consumer = this.createConsumer(owner, ctype);
    consumer.setName(RandomStringUtils.randomAlphanumeric(Consumer.MAX_LENGTH_OF_CONSUMER_NAME + 1));
    ConsumerDTO consumerDto = this.translator.translate(consumer, ConsumerDTO.class);
    UserPrincipal up = mock(UserPrincipal.class);
    ConsumerResource consumerResource = createConsumerResource(mockOwnerCurator);
    when(up.canAccess(eq(owner), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
    consumerResource.create(consumerDto, up, null, owner.getKey(), null, false);
}
Also used : Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) ConsumerType(org.candlepin.model.ConsumerType) ConsumerTypeDTO(org.candlepin.dto.api.v1.ConsumerTypeDTO) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 17 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class UndoImportsJobTest method testUndoImport.

@Test
public void testUndoImport() throws JobExecutionException, IOException, ImporterException {
    // We need proper curators for this test
    this.poolManager = this.poolManagerBase;
    this.ownerCurator = super.ownerCurator;
    this.exportCurator = this.exportCuratorBase;
    this.undoImportsJob = new UndoImportsJob(this.i18n, this.ownerCurator, this.poolManager, this.subAdapter, this.exportCurator, this.importRecordCurator);
    // Create owner w/upstream consumer
    Owner owner1 = TestUtil.createOwner();
    Owner owner2 = TestUtil.createOwner();
    ConsumerType type = this.createConsumerType();
    UpstreamConsumer uc1 = new UpstreamConsumer("uc1", null, type, "uc1");
    UpstreamConsumer uc2 = new UpstreamConsumer("uc2", null, type, "uc2");
    this.ownerCurator.create(owner1);
    this.ownerCurator.create(owner2);
    owner1.setUpstreamConsumer(uc1);
    owner1.setUpstreamConsumer(uc2);
    this.ownerCurator.merge(owner1);
    this.ownerCurator.merge(owner2);
    // Create metadata
    ExporterMetadata metadata1 = new ExporterMetadata(ExporterMetadata.TYPE_PER_USER, new Date(), owner1);
    ExporterMetadata metadata2 = new ExporterMetadata(ExporterMetadata.TYPE_PER_USER, new Date(), owner2);
    this.exportCurator.create(metadata1);
    this.exportCurator.create(metadata2);
    // Create pools w/upstream pool IDs
    Pool pool1 = this.createPool("pool1", owner1, true, PoolType.NORMAL);
    Pool pool2 = this.createPool("pool2", owner1, true, PoolType.BONUS);
    Pool pool3 = this.createPool("pool3", owner1, false, PoolType.NORMAL);
    Pool pool4 = this.createPool("pool4", owner1, false, PoolType.BONUS);
    Pool pool5 = this.createPool("pool5", owner1, true, PoolType.ENTITLEMENT_DERIVED);
    Pool pool6 = this.createPool("pool6", owner1, false, PoolType.ENTITLEMENT_DERIVED);
    Pool pool7 = this.createPool("pool7", owner2, true, PoolType.NORMAL);
    Pool pool8 = this.createPool("pool8", owner2, true, PoolType.BONUS);
    Pool pool9 = this.createPool("pool9", owner2, true, PoolType.ENTITLEMENT_DERIVED);
    // Create an ueber certificate for the owner.
    UeberCertificate uebercert = ueberCertGenerator.generate(owner1.getKey(), this.setupAdminPrincipal("test_admin"));
    assertNotNull(uebercert);
    // Verify initial state
    assertEquals(Arrays.asList(pool1, pool2, pool3, pool4, pool5, pool6), this.poolManager.listPoolsByOwner(owner1).list());
    assertEquals(Arrays.asList(pool7, pool8, pool9), this.poolManager.listPoolsByOwner(owner2).list());
    assertEquals(metadata1, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner1));
    assertEquals(metadata2, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner2));
    assertEquals(0, this.importRecordCurator.findRecords(owner1).list().size());
    assertEquals(0, this.importRecordCurator.findRecords(owner2).list().size());
    // Execute job
    Principal principal = new UserPrincipal("JarJarBinks", null, true);
    this.jobDataMap.put(JobStatus.TARGET_TYPE, JobStatus.TargetType.OWNER);
    this.jobDataMap.put(JobStatus.TARGET_ID, owner1.getId());
    this.jobDataMap.put(UndoImportsJob.OWNER_KEY, owner1.getKey());
    this.jobDataMap.put(PinsetterJobListener.PRINCIPAL_KEY, principal);
    // since we locking owner we need start transaction
    beginTransaction();
    this.undoImportsJob.toExecute(this.jobContext);
    commitTransaction();
    // Verify deletions -- Ueber pools should not get deleted.
    assertEquals(Arrays.asList(pool3, pool4, pool5, pool6), this.poolManager.listPoolsByOwner(owner1).list());
    assertEquals(Arrays.asList(pool7, pool8, pool9), this.poolManager.listPoolsByOwner(owner2).list());
    assertNull(exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner1));
    assertEquals(metadata2, exportCurator.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, owner2));
    assertNull(owner1.getUpstreamConsumer());
    List<ImportRecord> records = this.importRecordCurator.findRecords(owner1).list();
    assertEquals(1, records.size());
    assertEquals(ImportRecord.Status.DELETE, records.get(0).getStatus());
    assertEquals(0, this.importRecordCurator.findRecords(owner2).list().size());
}
Also used : Owner(org.candlepin.model.Owner) UeberCertificate(org.candlepin.model.UeberCertificate) ExporterMetadata(org.candlepin.model.ExporterMetadata) Pool(org.candlepin.model.Pool) ConsumerType(org.candlepin.model.ConsumerType) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ImportRecord(org.candlepin.model.ImportRecord) Date(java.util.Date) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) Test(org.junit.Test)

Example 18 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ConsumerResource method setupOwner.

private Owner setupOwner(Principal principal, String ownerKey) {
    // has admin rights for. If more than one, we have to error out.
    if (ownerKey == null && (principal instanceof UserPrincipal)) {
        // check for this cast?
        List<String> ownerKeys = ((UserPrincipal) principal).getOwnerKeys();
        if (ownerKeys.size() != 1) {
            throw new BadRequestException(i18n.tr("You must specify an organization for new units."));
        }
        ownerKey = ownerKeys.get(0);
    }
    createOwnerIfNeeded(principal);
    Owner owner = ownerCurator.lookupByKey(ownerKey);
    if (owner == null) {
        throw new BadRequestException(i18n.tr("Organization {0} does not exist.", ownerKey));
    }
    // Check permissions for current principal on the owner:
    if ((principal instanceof UserPrincipal) && !principal.canAccess(owner, SubResource.CONSUMERS, Access.CREATE)) {
        log.warn("User {} does not have access to create consumers in org {}", principal.getPrincipalName(), owner.getKey());
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", owner.getKey()));
    }
    return owner;
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) UserPrincipal(org.candlepin.auth.UserPrincipal)

Example 19 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class ConsumerResource method createOwnerIfNeeded.

/*
     * During registration of new consumers we support an edge case where the
     * user service may have authenticated a username/password for an owner
     * which we have not yet created in the Candlepin database. If we detect
     * this during registration we need to create the new owner, and adjust the
     * principal that was created during authentication to carry it.
     */
// TODO: Re-evaluate if this is still an issue with the new membership
// scheme!
private void createOwnerIfNeeded(Principal principal) {
    if (!(principal instanceof UserPrincipal)) {
        // If this isn't a user principal we can't check for owners that may need to be created.
        return;
    }
    for (Owner owner : ((UserPrincipal) principal).getOwners()) {
        Owner existingOwner = ownerCurator.lookupByKey(owner.getKey());
        if (existingOwner == null) {
            log.info("Principal carries permission for owner that does not exist.");
            log.info("Creating new owner: {}", owner.getKey());
            existingOwner = ownerCurator.create(owner);
            poolManager.getRefresher(this.subAdapter, this.ownerAdapter).add(existingOwner).run();
        }
    }
}
Also used : Owner(org.candlepin.model.Owner) UserPrincipal(org.candlepin.auth.UserPrincipal)

Example 20 with UserPrincipal

use of org.candlepin.auth.UserPrincipal in project candlepin by candlepin.

the class OwnerInfoCuratorTest method setupOnlyMyConsumersPrincipal.

private User setupOnlyMyConsumersPrincipal() {
    Set<Permission> perms = new HashSet<>();
    User u = new User("MySystemsAdmin", "passwd");
    perms.add(new UsernameConsumersPermission(u, owner));
    Principal p = new UserPrincipal(u.getUsername(), perms, false);
    setupPrincipal(p);
    return u;
}
Also used : UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) UsernameConsumersPermission(org.candlepin.auth.permissions.UsernameConsumersPermission) Permission(org.candlepin.auth.permissions.Permission) UserPrincipal(org.candlepin.auth.UserPrincipal) Principal(org.candlepin.auth.Principal) UserPrincipal(org.candlepin.auth.UserPrincipal) HashSet(java.util.HashSet)

Aggregations

UserPrincipal (org.candlepin.auth.UserPrincipal)30 Test (org.junit.Test)18 Owner (org.candlepin.model.Owner)16 Principal (org.candlepin.auth.Principal)14 OwnerPermission (org.candlepin.auth.permissions.OwnerPermission)10 User (org.candlepin.model.User)10 Permission (org.candlepin.auth.permissions.Permission)9 HashSet (java.util.HashSet)7 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)7 ConsumerType (org.candlepin.model.ConsumerType)6 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)5 OwnerDTO (org.candlepin.dto.api.v1.OwnerDTO)4 Consumer (org.candlepin.model.Consumer)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)3 UsernameConsumersPermission (org.candlepin.auth.permissions.UsernameConsumersPermission)3 Role (org.candlepin.model.Role)3 ManifestFile (org.candlepin.sync.file.ManifestFile)3 TestUtil.createConsumerDTO (org.candlepin.test.TestUtil.createConsumerDTO)3