Search in sources :

Example 1 with SecurityHole

use of org.candlepin.common.auth.SecurityHole 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 SecurityHole

use of org.candlepin.common.auth.SecurityHole in project candlepin by candlepin.

the class ConsumerResource method create.

@ApiOperation(notes = "Creates a Consumer. NOTE: Opening this method up " + "to everyone, as we have nothing we can reliably " + "verify in the method signature. Instead we have to " + "figure out what owner this consumer is destined for " + "(due to backward compatability with existing clients " + "which do not specify an owner during registration), " + "and then check the access to the specified owner in " + "the method itself.", value = "create")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@SecurityHole(noAuth = true)
@Transactional
public ConsumerDTO create(@ApiParam(name = "consumer", required = true) ConsumerDTO dto, @Context Principal principal, @QueryParam("username") String userName, @QueryParam("owner") String ownerKey, @QueryParam("activation_keys") String activationKeys, @QueryParam("identity_cert_creation") @DefaultValue("true") boolean identityCertCreation) throws BadRequestException {
    // fix for duplicate hypervisor/consumer problem
    Consumer consumer = null;
    if (ownerKey != null && dto.getFact("system_uuid") != null && !"true".equalsIgnoreCase(dto.getFact("virt.is_guest"))) {
        Owner owner = ownerCurator.lookupByKey(ownerKey);
        if (owner != null) {
            consumer = consumerCurator.getHypervisor(dto.getFact("system_uuid"), owner);
            if (consumer != null) {
                consumer.setIdCert(generateIdCert(consumer, false));
                this.updateConsumer(consumer.getUuid(), dto, principal);
                return translator.translate(consumer, ConsumerDTO.class);
            }
        }
    }
    if (consumer == null) {
        consumer = new Consumer();
    }
    if (dto.getUuid() != null) {
        consumer.setUuid(dto.getUuid());
    }
    consumer.setOwner(ownerCurator.lookupByKey(ownerKey));
    populateEntity(consumer, dto);
    if (dto.getType() == null) {
        throw new BadRequestException(i18n.tr("Unit type must be specified."));
    }
    ConsumerType ctype = this.consumerTypeCurator.lookupByLabel(dto.getType().getLabel());
    if (ctype == null) {
        throw new BadRequestException(i18n.tr("Invalid unit type: {0}", dto.getType().getLabel()));
    }
    return translator.translate(createConsumerFromDTO(dto, ctype, principal, userName, ownerKey, activationKeys, identityCertCreation), ConsumerDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) SecurityHole(org.candlepin.common.auth.SecurityHole) 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) Transactional(com.google.inject.persist.Transactional)

Example 3 with SecurityHole

use of org.candlepin.common.auth.SecurityHole in project candlepin by candlepin.

the class ContentOverrideResource method addContentOverrides.

/**
 * Adds a Content Override to a Principal
 *
 * @param info context to get the parent id
 * @param entries overrides to add or update
 *
 * @return a list of ContentOverride objects
 * @httpcode 404
 * @httpcode 200
 */
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@SecurityHole
public List<T> addContentOverrides(@Context UriInfo info, @Context Principal principal, List<ContentOverride> entries) {
    String parentId = info.getPathParameters().getFirst(this.getParentPath());
    Parent parent = this.verifyAndGetParent(parentId, principal, Access.ALL);
    contentOverrideValidator.validate(entries);
    for (ContentOverride entry : entries) {
        contentOverrideCurator.addOrUpdate(parent, entry);
    }
    return contentOverrideCurator.getList(parent);
}
Also used : ContentOverride(org.candlepin.model.ContentOverride) SecurityHole(org.candlepin.common.auth.SecurityHole) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT) Transactional(com.google.inject.persist.Transactional)

Example 4 with SecurityHole

use of org.candlepin.common.auth.SecurityHole in project candlepin by candlepin.

the class ContentOverrideResource method deleteContentOverrides.

/**
 * Removes a Content Override from a Principal
 *
 * @param info context to get the parent id
 * @param entries overrides to remove to remove
 *
 * @return a list of ContentOverride objects
 * @httpcode 404
 * @httpcode 200
 */
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@SecurityHole
public List<T> deleteContentOverrides(@Context UriInfo info, @Context Principal principal, List<ContentOverride> entries) {
    String parentId = info.getPathParameters().getFirst(this.getParentPath());
    Parent parent = this.verifyAndGetParent(parentId, principal, Access.ALL);
    if (entries.size() == 0) {
        contentOverrideCurator.removeByParent(parent);
    } else {
        for (ContentOverride entry : entries) {
            String label = entry.getContentLabel();
            if (StringUtils.isBlank(label)) {
                contentOverrideCurator.removeByParent(parent);
            } else {
                String name = entry.getName();
                if (StringUtils.isBlank(name)) {
                    contentOverrideCurator.removeByContentLabel(parent, entry.getContentLabel());
                } else {
                    contentOverrideCurator.removeByName(parent, entry.getContentLabel(), name);
                }
            }
        }
    }
    return contentOverrideCurator.getList(parent);
}
Also used : ContentOverride(org.candlepin.model.ContentOverride) DELETE(javax.ws.rs.DELETE) SecurityHole(org.candlepin.common.auth.SecurityHole) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(com.google.inject.persist.Transactional)

Example 5 with SecurityHole

use of org.candlepin.common.auth.SecurityHole in project candlepin by candlepin.

the class AdminResource method initialize.

@GET
@Produces({ MediaType.TEXT_PLAIN })
@Path("init")
@SecurityHole(noAuth = true)
@ApiOperation(notes = "Initializes the Candlepin database. Currently this just" + " creates the admin user for standalone deployments using the" + " default user service adapter. It must be called once after" + " candlepin is installed, repeat calls are not required, but" + " will be harmless. The String returned is the description if" + " the db was or already is initialized.", value = "initialize")
public String initialize() {
    log.debug("Called initialize()");
    log.info("Initializing Candlepin database.");
    // the default user service adapter, and no other users exist already:
    if (userService instanceof DefaultUserServiceAdapter && userCurator.getUserCount() == 0) {
        // Push the system principal so we can create all these entries as a
        // superuser:
        ResteasyProviderFactory.pushContext(Principal.class, new SystemPrincipal());
        log.info("Creating default super admin.");
        User defaultAdmin = new User("admin", "admin", true);
        userService.createUser(defaultAdmin);
        return "Initialized!";
    } else {
        // Any other user service adapter and we really have nothing to do:
        return "Already initialized.";
    }
}
Also used : DefaultUserServiceAdapter(org.candlepin.service.impl.DefaultUserServiceAdapter) User(org.candlepin.model.User) SystemPrincipal(org.candlepin.auth.SystemPrincipal) Path(javax.ws.rs.Path) SecurityHole(org.candlepin.common.auth.SecurityHole) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SecurityHole (org.candlepin.common.auth.SecurityHole)12 Produces (javax.ws.rs.Produces)10 ApiOperation (io.swagger.annotations.ApiOperation)8 GET (javax.ws.rs.GET)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Path (javax.ws.rs.Path)5 Transactional (com.google.inject.persist.Transactional)4 Consumes (javax.ws.rs.Consumes)4 BadRequestException (org.candlepin.common.exceptions.BadRequestException)4 Owner (org.candlepin.model.Owner)4 Product (org.candlepin.model.Product)3 ApiListingResource (io.swagger.jaxrs.listing.ApiListingResource)2 Method (java.lang.reflect.Method)2 POST (javax.ws.rs.POST)2 Consumer (org.candlepin.model.Consumer)2 ContentOverride (org.candlepin.model.ContentOverride)2 ProductCertificate (org.candlepin.model.ProductCertificate)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1