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