Search in sources :

Example 46 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.

the class ConsumerResource method createConsumerFromDTO.

public Consumer createConsumerFromDTO(ConsumerDTO consumer, ConsumerType type, Principal principal, String userName, String ownerKey, String activationKeys, boolean identityCertCreation) throws BadRequestException {
    // API:registerConsumer
    Set<String> keyStrings = splitKeys(activationKeys);
    // Only let NoAuth principals through if there are activation keys to consider:
    if ((principal instanceof NoAuthPrincipal) && keyStrings.isEmpty()) {
        throw new ForbiddenException(i18n.tr("Insufficient permissions"));
    }
    validateOnKeyStrings(keyStrings, ownerKey, userName);
    Owner owner = setupOwner(principal, ownerKey);
    // Raise an exception if none of the keys specified exist for this owner.
    List<ActivationKey> keys = checkActivationKeys(principal, owner, keyStrings);
    userName = setUserName(consumer, principal, userName);
    checkConsumerName(consumer);
    validateViaConsumerType(consumer, type, keys, owner, userName, principal);
    if (type.isType(ConsumerTypeEnum.SHARE)) {
        // Share consumers do not need identity certificates so refuse to create them.
        identityCertCreation = false;
        validateShareConsumer(consumer, principal, keys);
        // if there exists a share consumer between the two orgs, return it.
        Consumer existingShareConsumer = consumerCurator.getSharingConsumer(owner, consumer.getRecipientOwnerKey());
        if (existingShareConsumer != null) {
            return existingShareConsumer;
        }
        consumer.setAutoheal(false);
    } else {
        // this is the default
        consumer.setAutoheal(true);
        if (StringUtils.isNotEmpty(consumer.getRecipientOwnerKey())) {
            throw new BadRequestException(i18n.tr("Only share consumers can specify recipient owners"));
        }
    }
    if (consumer.getServiceLevel() == null) {
        consumer.setServiceLevel("");
    }
    // Sanitize the inbound facts
    this.sanitizeConsumerFacts(consumer);
    // If no service level was specified, and the owner has a default set, use it:
    if (consumer.getServiceLevel().equals("") && owner.getDefaultServiceLevel() != null && !type.isType(ConsumerTypeEnum.SHARE)) {
        consumer.setServiceLevel(owner.getDefaultServiceLevel());
    }
    Consumer consumerToCreate = new Consumer();
    consumerToCreate.setOwner(owner);
    populateEntity(consumerToCreate, consumer);
    consumerToCreate.setType(type);
    if (!type.isType(ConsumerTypeEnum.SHARE)) {
        consumerToCreate.setCanActivate(subAdapter.canActivateSubscription(consumerToCreate));
    }
    HypervisorId hvsrId = consumerToCreate.getHypervisorId();
    if (hvsrId != null && hvsrId.getHypervisorId() != null && !hvsrId.getHypervisorId().isEmpty()) {
        // If a hypervisorId is supplied, make sure the consumer and owner are correct
        hvsrId.setConsumer(consumerToCreate);
        hvsrId.setOwner(owner);
    }
    updateCapabilities(consumerToCreate, null);
    logNewConsumerDebugInfo(consumerToCreate, keys, type);
    validateContentAccessMode(consumerToCreate, owner);
    consumerBindUtil.validateServiceLevel(owner.getId(), consumerToCreate.getServiceLevel());
    try {
        Date createdDate = consumerToCreate.getCreated();
        Date lastCheckIn = consumerToCreate.getLastCheckin();
        // create sets created to current time.
        consumerToCreate = consumerCurator.create(consumerToCreate);
        // If we sent in a created date, we want it persisted at the update below
        if (createdDate != null) {
            consumerToCreate.setCreated(createdDate);
        }
        if (lastCheckIn != null) {
            log.info("Creating with specific last check-in time: {}", lastCheckIn);
            consumerToCreate.setLastCheckin(lastCheckIn);
        }
        if (identityCertCreation) {
            IdentityCertificate idCert = generateIdCert(consumerToCreate, false);
            consumerToCreate.setIdCert(idCert);
        }
        sink.emitConsumerCreated(consumerToCreate);
        if (keys.size() > 0) {
            consumerBindUtil.handleActivationKeys(consumerToCreate, keys, owner.isAutobindDisabled());
        }
        // Don't allow complianceRules to update entitlementStatus, because we're about to perform
        // an update unconditionally.
        complianceRules.getStatus(consumerToCreate, null, false, false);
        consumerCurator.update(consumerToCreate);
        log.info("Consumer {} created in org {}", consumerToCreate.getUuid(), consumerToCreate.getOwnerId());
        return consumerToCreate;
    } catch (CandlepinException ce) {
        // If it is one of ours, rethrow it.
        throw ce;
    } catch (Exception e) {
        log.error("Problem creating unit:", e);
        throw new BadRequestException(i18n.tr("Problem creating unit {0}", consumer));
    }
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Owner(org.candlepin.model.Owner) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Date(java.util.Date) GeneralSecurityException(java.security.GeneralSecurityException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) IseException(org.candlepin.common.exceptions.IseException) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PropertyValidationException(org.candlepin.util.PropertyValidationException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HypervisorId(org.candlepin.model.HypervisorId) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 47 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.

the class ConsumerResource method bind.

@ApiOperation(notes = "If a pool ID is specified, we know we're binding to that exact pool. " + "Specifying an entitle date in this case makes no sense and will throw an " + "error. If a list of product IDs are specified, we attempt to auto-bind to" + " subscriptions which will provide those products. An optional date can be" + " specified allowing the consumer to get compliant for some date in the " + "future. If no date is specified we assume the current date. If neither a " + "pool nor an ID is specified, this is a healing request. The path is similar " + "to the bind by products, but in this case we use the installed products on " + "the consumer, and their current compliant status, to determine which product" + " IDs should be requested. The entitle date is used the same as with bind by " + "products. The response will contain a list of Entitlement objects if async is" + " false, or a JobDetail object if async is true.", value = "Bind Entitlements")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 403, message = "Binds Entitlements"), @ApiResponse(code = 404, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{consumer_uuid}/entitlements")
@SuppressWarnings("checkstyle:indentation")
public Response bind(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("pool") @Verify(value = Pool.class, nullable = true, subResource = SubResource.ENTITLEMENTS) String poolIdString, @QueryParam("product") String[] productIds, @QueryParam("quantity") Integer quantity, @QueryParam("email") String email, @QueryParam("email_locale") String emailLocale, @QueryParam("async") @DefaultValue("false") boolean async, @QueryParam("entitle_date") String entitleDateStr, @QueryParam("from_pool") List<String> fromPools) {
    /* NOTE: This method should NEVER be provided with a POST body.
           While technically that change would be backwards compatible,
           there are older clients which erroneously provide an empty string
           as a post body and hence result in a serialization error.
           ref: BZ: 1502807
         */
    // TODO: really should do this in a before we get to this call
    // so the method takes in a real Date object and not just a String.
    Date entitleDate = ResourceDateParser.parseDateString(entitleDateStr);
    // Verify consumer exists:
    Consumer consumer = consumerCurator.verifyAndLookupConsumerWithEntitlements(consumerUuid);
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    log.debug("Consumer (post verify): {}", consumer);
    // Check that only one query param was set, and some other validations
    validateBindArguments(poolIdString, quantity, productIds, fromPools, entitleDate, consumer, async);
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    try {
        // I hate double negatives, but if they have accepted all
        // terms, we want comeToTerms to be true.
        long subTermsStart = System.currentTimeMillis();
        if (!ctype.isType(ConsumerTypeEnum.SHARE) && subAdapter.hasUnacceptedSubscriptionTerms(owner)) {
            return Response.serverError().build();
        }
        log.debug("Checked if consumer has unaccepted subscription terms in {}ms", (System.currentTimeMillis() - subTermsStart));
    } catch (CandlepinException e) {
        log.debug(e.getMessage());
        throw e;
    }
    if (poolIdString != null && quantity == null) {
        Pool pool = poolManager.find(poolIdString);
        quantity = pool != null ? consumerBindUtil.getQuantityToBind(pool, consumer) : 1;
    }
    // 
    if (async) {
        JobDetail detail = null;
        if (poolIdString != null) {
            detail = EntitlerJob.bindByPool(poolIdString, consumer, owner.getKey(), quantity);
        } else {
            detail = EntitleByProductsJob.bindByProducts(productIds, consumer, entitleDate, fromPools, owner.getKey());
        }
        // events will be triggered by the job
        return Response.status(Response.Status.OK).type(MediaType.APPLICATION_JSON).entity(detail).build();
    }
    // 
    // otherwise we do what we do today.
    // 
    List<Entitlement> entitlements = null;
    if (poolIdString != null) {
        entitlements = entitler.bindByPoolQuantity(consumer, poolIdString, quantity);
    } else {
        try {
            AutobindData autobindData = AutobindData.create(consumer, owner).on(entitleDate).forProducts(productIds).withPools(fromPools);
            entitlements = entitler.bindByProducts(autobindData);
        } catch (AutobindDisabledForOwnerException e) {
            throw new BadRequestException(i18n.tr("Ignoring request to auto-attach. " + "It is disabled for org \"{0}\".", owner.getKey()));
        }
    }
    List<EntitlementDTO> entitlementDTOs = null;
    if (entitlements != null) {
        entitlementDTOs = new ArrayList<>();
        for (Entitlement ent : entitlements) {
            // we need to supply the compliance type for the pools
            // the method in this class does not do quantity
            addCalculatedAttributes(ent);
            entitlementDTOs.add(this.translator.translate(ent, EntitlementDTO.class));
        }
    }
    // Trigger events:
    entitler.sendEvents(entitlements);
    return Response.status(Response.Status.OK).type(MediaType.APPLICATION_JSON).entity(entitlementDTOs).build();
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) Owner(org.candlepin.model.Owner) Date(java.util.Date) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) JobDetail(org.quartz.JobDetail) EntitlementDTO(org.candlepin.dto.api.v1.EntitlementDTO) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Pool(org.candlepin.model.Pool) AutobindData(org.candlepin.resource.dto.AutobindData) ConsumerType(org.candlepin.model.ConsumerType) Entitlement(org.candlepin.model.Entitlement) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 48 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.

the class ConsumerResource method validateShareConsumer.

/**
 * Ensure that certain fields remain unset when creating a share consumer.
 * @param consumer the consumer to validate
 * @param principal the principal performing the operation
 * @param keys any provided activation keys
 * @throws BadRequestException if any validations fail
 */
private void validateShareConsumer(ConsumerDTO consumer, Principal principal, List<ActivationKey> keys) throws BadRequestException {
    if (keys.size() > 0) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot be used with activation keys"));
    }
    if (StringUtils.isNotBlank(consumer.getServiceLevel())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a service level"));
    }
    if (StringUtils.isNotBlank(consumer.getReleaseVersion())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a release version"));
    }
    if (CollectionUtils.isNotEmpty(consumer.getInstalledProducts())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have installed products"));
    }
    if (StringUtils.isNotBlank(consumer.getContentAccessMode())) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a content access mode"));
    }
    if (consumer.getGuestIds() != null && !consumer.getGuestIds().isEmpty()) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have guest IDs"));
    }
    if (consumer.getHypervisorId() != null) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot have a hypervisor ID"));
    }
    if (consumer.getRecipientOwnerKey() == null) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" must specify a recipient org key"));
    }
    if (consumer.isGuest()) {
        throw new BadRequestException(i18n.tr("A unit type of \"share\" cannot be a virtual guest"));
    }
    String recipient = consumer.getRecipientOwnerKey();
    Owner recipientOwner = ownerCurator.lookupByKey(recipient);
    if (recipientOwner == null) {
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", recipient));
    }
    // Check permissions for current principal on the recipient owner
    if (!principal.canAccess(recipientOwner, SubResource.ENTITLEMENTS, Access.CREATE)) {
        log.warn("User {} does not have access to create shares to org {}", principal.getPrincipalName(), recipient);
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", recipient));
    }
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException)

Example 49 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.

the class ConsumerResource method getContentAccessBody.

@ApiOperation(notes = "Retrieves the body of the Content Access Certificate for the Consumer", value = "getContentAccessBody", response = String.class)
@ApiResponses({ @ApiResponse(code = 404, message = ""), @ApiResponse(code = 304, message = "") })
@GET
@Path("{consumer_uuid}/accessible_content")
@Produces(MediaType.APPLICATION_JSON)
public Response getContentAccessBody(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @HeaderParam("If-Modified-Since") @DefaultValue("Thu, 01 Jan 1970 00:00:00 GMT") @DateFormat({ "EEE, dd MMM yyyy HH:mm:ss z" }) Date since) {
    log.debug("Getting content access certificate for consumer: {}", consumerUuid);
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    if (ctype.isType(ConsumerTypeEnum.SHARE)) {
        throw new BadRequestException(i18n.tr("Content access body can not be requested for a share consumer"));
    }
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    String cam = owner.getContentAccessMode();
    if (!ContentAccessCertServiceAdapter.ORG_ENV_ACCESS_MODE.equals(cam)) {
        throw new BadRequestException(i18n.tr("Content access mode does not allow this request."));
    }
    if (!contentAccessCertService.hasCertChangedSince(consumer, since)) {
        return Response.status(Response.Status.NOT_MODIFIED).entity("Not modified since date supplied.").build();
    }
    ContentAccessListing result = new ContentAccessListing();
    try {
        ContentAccessCertificate cac = contentAccessCertService.getCertificate(consumer);
        if (cac == null) {
            throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate"));
        }
        String cert = cac.getCert();
        String certificate = cert.substring(0, cert.indexOf("-----BEGIN ENTITLEMENT DATA-----\n"));
        String json = cert.substring(cert.indexOf("-----BEGIN ENTITLEMENT DATA-----\n"));
        List<String> pieces = new ArrayList<>();
        pieces.add(certificate);
        pieces.add(json);
        result.setContentListing(cac.getSerial().getId(), pieces);
        result.setLastUpdate(cac.getUpdated());
    } catch (IOException ioe) {
        throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate"), ioe);
    } catch (GeneralSecurityException gse) {
        throw new BadRequestException(i18n.tr("Cannot retrieve content access certificate", gse));
    }
    return Response.ok(result, MediaType.APPLICATION_JSON).build();
}
Also used : Owner(org.candlepin.model.Owner) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) GeneralSecurityException(java.security.GeneralSecurityException) ContentAccessListing(org.candlepin.resource.dto.ContentAccessListing) ArrayList(java.util.ArrayList) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ContentAccessCertificate(org.candlepin.model.ContentAccessCertificate) IOException(java.io.IOException) ConsumerType(org.candlepin.model.ConsumerType) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 50 with BadRequestException

use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.

the class ConsumerResource method updateConsumer.

// While this is a PUT, we are treating it as a PATCH until this operation
// becomes more prevalent. We only update the portions of the consumer that appear
// to be set.
@ApiOperation(notes = "Updates a Consumer", value = "updateConsumer")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("{consumer_uuid}")
@Transactional
@UpdateConsumerCheckIn
public void updateConsumer(@PathParam("consumer_uuid") @Verify(Consumer.class) String uuid, @ApiParam(name = "consumer", required = true) ConsumerDTO dto, @Context Principal principal) {
    Consumer toUpdate = consumerCurator.verifyAndLookupConsumer(uuid);
    dto.setUuid(uuid);
    // Sanitize the inbound facts before applying the update
    this.sanitizeConsumerFacts(dto);
    ConsumerType toUpdateType = this.consumerTypeCurator.getConsumerType(toUpdate);
    if (toUpdateType.isType(ConsumerTypeEnum.SHARE)) {
        validateShareConsumerUpdate(toUpdate, dto, principal);
    }
    GuestMigration guestMigration = migrationProvider.get();
    guestMigration.buildMigrationManifest(dto, toUpdate);
    if (performConsumerUpdates(dto, toUpdate, guestMigration)) {
        try {
            if (guestMigration.isMigrationPending()) {
                guestMigration.migrate();
            } else {
                consumerCurator.update(toUpdate);
            }
        } catch (CandlepinException ce) {
            // If it is one of ours, rethrow it.
            throw ce;
        } catch (Exception e) {
            log.error("Problem updating unit:", e);
            throw new BadRequestException(i18n.tr("Problem updating unit {0}", dto));
        }
    }
}
Also used : GuestMigration(org.candlepin.resource.util.GuestMigration) CandlepinException(org.candlepin.common.exceptions.CandlepinException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) GeneralSecurityException(java.security.GeneralSecurityException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) IseException(org.candlepin.common.exceptions.IseException) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PropertyValidationException(org.candlepin.util.PropertyValidationException) Path(javax.ws.rs.Path) UpdateConsumerCheckIn(org.candlepin.auth.UpdateConsumerCheckIn) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT) Transactional(com.google.inject.persist.Transactional)

Aggregations

BadRequestException (org.candlepin.common.exceptions.BadRequestException)69 ApiOperation (io.swagger.annotations.ApiOperation)38 Produces (javax.ws.rs.Produces)38 ApiResponses (io.swagger.annotations.ApiResponses)36 Owner (org.candlepin.model.Owner)33 Path (javax.ws.rs.Path)28 Consumer (org.candlepin.model.Consumer)27 Consumes (javax.ws.rs.Consumes)24 NotFoundException (org.candlepin.common.exceptions.NotFoundException)21 POST (javax.ws.rs.POST)15 ConsumerType (org.candlepin.model.ConsumerType)15 Transactional (com.google.inject.persist.Transactional)14 DeletedConsumer (org.candlepin.model.DeletedConsumer)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 GET (javax.ws.rs.GET)13 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)11 PUT (javax.ws.rs.PUT)9 IseException (org.candlepin.common.exceptions.IseException)9 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)9