Search in sources :

Example 1 with BadRequestException

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

the class AuthenticationFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    log.debug("Authentication check for {}", requestContext.getUriInfo().getPath());
    HttpRequest httpRequest = ResteasyProviderFactory.getContextData(HttpRequest.class);
    ResourceInfo resourceInfo = ResteasyProviderFactory.getContextData(ResourceInfo.class);
    Method method = resourceInfo.getResourceMethod();
    SecurityHole hole = method.getAnnotation(SecurityHole.class);
    Principal principal = null;
    if (hole != null && hole.anon()) {
        principal = new NoAuthPrincipal();
    } else if (resourceInfo.getResourceClass().equals(ApiListingResource.class)) {
        log.debug("Swagger API request made; no principal required.");
        principal = new NoAuthPrincipal();
    } else {
        for (AuthProvider provider : providers) {
            principal = provider.getPrincipal(httpRequest);
            if (principal != null) {
                log.debug("Establishing principal with {}", provider.getClass().getName());
                break;
            }
        }
    }
    /* At this point, there is no provider that has given a valid principal,
         * so we use the NoAuthPrincipal here if it is allowed. */
    if (principal == null) {
        if (hole != null && hole.noAuth()) {
            log.debug("No auth allowed for resource; setting NoAuth principal");
            principal = new NoAuthPrincipal();
        } else if (!config.getBoolean(ConfigProperties.AUTH_OVER_HTTP) && !request.isSecure()) {
            throw new BadRequestException("Please use SSL when accessing protected resources");
        } else {
            throw new NotAuthorizedException("Invalid credentials.");
        }
    }
    SecurityContext securityContext = new CandlepinSecurityContext(principal);
    requestContext.setSecurityContext(securityContext);
    // Push the principal into the context for the PrincipalProvider to access directly
    ResteasyProviderFactory.pushContext(Principal.class, principal);
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) ResourceInfo(javax.ws.rs.container.ResourceInfo) SecurityHole(org.candlepin.common.auth.SecurityHole) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) SecurityContext(javax.ws.rs.core.SecurityContext) BadRequestException(org.candlepin.common.exceptions.BadRequestException) AuthProvider(org.candlepin.auth.AuthProvider) Method(java.lang.reflect.Method) NotAuthorizedException(org.candlepin.common.exceptions.NotAuthorizedException) Principal(org.candlepin.auth.Principal) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal)

Example 2 with BadRequestException

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

the class ResolverUtil method resolveSubscription.

public Subscription resolveSubscription(Subscription subscription) {
    // need to make sure it's not null.
    if (subscription == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(subscription.getOwner());
    subscription.setOwner(owner);
    // Ensure the specified product(s) exists for the given owner
    this.validateProductData(subscription.getProduct(), owner, false);
    this.validateProductData(subscription.getDerivedProduct(), owner, true);
    for (ProductData product : subscription.getProvidedProducts()) {
        this.validateProductData(product, owner, true);
    }
    for (ProductData product : subscription.getDerivedProvidedProducts()) {
        this.validateProductData(product, owner, true);
    }
    return subscription;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 3 with BadRequestException

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

the class ResolverUtil method validateProductData.

public void validateProductData(ProductData dto, Owner owner, boolean allowNull) {
    if (dto != null) {
        if (dto.getUuid() != null) {
            // UUID is set. Verify that product exists and matches the ID provided, if any
            Product product = this.productCurator.find(dto.getUuid());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the UUID \"{0}\"", dto.getUuid()));
            }
            dto.setId(product.getId());
        } else if (dto.getId() != null) {
            Product product = this.ownerProductCurator.getProductById(owner, dto.getId());
            if (product == null) {
                throw new NotFoundException(i18n.tr("Unable to find a product with the ID \"{0}\" for owner \"{1}\"", dto.getId(), owner.getKey()));
            }
        } else {
            throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
        }
    } else if (!allowNull) {
        throw new BadRequestException(i18n.tr("No product specified, or product lacks identifying information"));
    }
}
Also used : ProvidedProduct(org.candlepin.model.ProvidedProduct) Product(org.candlepin.model.Product) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 4 with BadRequestException

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

the class ResolverUtil method resolveSubscriptionAndProduct.

/**
 * used to resolve subscription but it resolves the product too.
 * currently used in hostedtest resources
 * @param subscription
 * @return the resolved subscription
 */
public Subscription resolveSubscriptionAndProduct(Subscription subscription) {
    // We just need to make sure it's not null.
    if (subscription == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(subscription.getOwner());
    subscription.setOwner(owner);
    subscription.setProduct(new ProductData(this.resolveProduct(owner, subscription.getProduct().getId())));
    if (subscription.getDerivedProduct() != null) {
        ProductData p = new ProductData(this.resolveProduct(owner, subscription.getDerivedProduct().getId()));
        subscription.setDerivedProduct(p);
    }
    HashSet<ProductData> providedProducts = new HashSet<>();
    for (ProductData product : subscription.getProvidedProducts()) {
        if (product != null) {
            providedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
        }
    }
    subscription.setProvidedProducts(providedProducts);
    HashSet<ProductData> derivedProvidedProducts = new HashSet<>();
    for (ProductData product : subscription.getDerivedProvidedProducts()) {
        if (product != null) {
            derivedProvidedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
        }
    }
    subscription.setDerivedProvidedProducts(derivedProvidedProducts);
    return subscription;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HashSet(java.util.HashSet)

Example 5 with BadRequestException

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

the class ResolverUtil method resolvePool.

public Pool resolvePool(Pool pool) {
    // doesn't (i.e. during creation). We just need to make sure it's not null.
    if (pool == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(pool.getOwner());
    pool.setOwner(owner);
    // Ensure the specified product(s) exists for the given owner
    pool.setProduct(this.resolveProduct(owner, pool.getProduct()));
    if (pool.getDerivedProduct() != null) {
        pool.setDerivedProduct(this.resolveProduct(owner, pool.getDerivedProduct()));
    }
    HashSet<Product> presolved = new HashSet<>();
    pool.populateAllTransientProvidedProducts(productCurator);
    for (ProvidedProduct product : pool.getProvidedProductDtos()) {
        // TODO: Maybe add UUID resolution as well?
        presolved.add(resolveProduct(owner, product.getProductId()));
    }
    pool.setProvidedProducts(presolved);
    presolved.clear();
    for (ProvidedProduct product : pool.getDerivedProvidedProductDtos()) {
        presolved.add(this.resolveProduct(owner, product.getProductId()));
    }
    pool.setDerivedProvidedProducts(presolved);
    return pool;
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ProvidedProduct(org.candlepin.model.ProvidedProduct) Product(org.candlepin.model.Product) ProvidedProduct(org.candlepin.model.ProvidedProduct) HashSet(java.util.HashSet)

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