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);
}
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());
}
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;
}
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();
}
}
}
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;
}
Aggregations