Search in sources :

Example 11 with IseException

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

the class SyncUtils method makeTempDir.

File makeTempDir(String baseName) throws IOException {
    File baseDir = new File(config.getString(ConfigProperties.SYNC_WORK_DIR));
    if (!baseDir.exists() && !baseDir.mkdirs()) {
        throw new IseException("Unable to create base dir for sync: " + baseDir);
    }
    File tmp = File.createTempFile(baseName, Long.toString(System.nanoTime()), baseDir);
    if (!tmp.delete()) {
        throw new IOException("Could not delete temp file: " + tmp.getAbsolutePath());
    }
    if (!tmp.mkdirs()) {
        throw new IOException("Could not create temp directory: " + tmp.getAbsolutePath());
    }
    return (tmp);
}
Also used : IseException(org.candlepin.common.exceptions.IseException) IOException(java.io.IOException) File(java.io.File)

Example 12 with IseException

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

the class ConsumerResource method exportData.

/**
 * Retrieves a compressed file representation of a Consumer (manifest).
 *
 * @deprecated use GET /consumers/:consumer_uuid/export/async
 * @param response
 * @param consumerUuid
 * @param cdnLabel
 * @param webAppPrefix
 * @param apiUrl
 * @return the generated file archive.
 */
@Deprecated
@ApiOperation(notes = "Retrieves a Compressed File representation of a Consumer (manifest).", value = "Consumer Export (manifest)", response = File.class)
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 500, message = ""), @ApiResponse(code = 404, message = "") })
@Produces("application/zip")
@GET
@Path("{consumer_uuid}/export")
public File exportData(@Context HttpServletResponse response, @PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("cdn_label") String cdnLabel, @QueryParam("webapp_prefix") String webAppPrefix, @QueryParam("api_url") String apiUrl, @QueryParam("ext") @CandlepinParam(type = KeyValueParameter.class) @ApiParam(value = "Key/Value pairs to be passed to the extension adapter when generating a manifest", required = false, example = "ext=version:1.2.3&ext=extension_key:EXT1") List<KeyValueParameter> extensionArgs) {
    Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    if (ctype.isType(ConsumerTypeEnum.SHARE)) {
        throw new BadRequestException(i18n.tr("Can not export manifest of a share consumer"));
    }
    try {
        File archive = manifestManager.generateManifest(consumerUuid, cdnLabel, webAppPrefix, apiUrl, getExtensionParamMap(extensionArgs));
        response.addHeader("Content-Disposition", "attachment; filename=" + archive.getName());
        return archive;
    } catch (ExportCreationException e) {
        throw new IseException(i18n.tr("Unable to create export archive"), e);
    }
}
Also used : DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) IseException(org.candlepin.common.exceptions.IseException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ConsumerType(org.candlepin.model.ConsumerType) File(java.io.File) 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 13 with IseException

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

the class CrlResource method getCurrentCrl.

@ApiOperation(notes = "Retrieves the Certificate Revocation List", value = "getCurrentCrl", response = String.class)
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response getCurrentCrl(@Context Principal principal) throws CRLException {
    String filePath = getCrlFilePath();
    File crlFile = new File(filePath);
    try {
        this.crlFileUtil.syncCRLWithDB(crlFile);
        // Create an empty CRL if we didn't have anything to write
        if (!crlFile.exists() || crlFile.length() < 1) {
            pkiUtility.writePemEncoded(pkiUtility.createX509CRL(new LinkedList<>(), BigInteger.ZERO), new FileOutputStream(crlFile));
        }
        return Response.ok().entity(new FileInputStream(crlFile)).build();
    } catch (IOException e) {
        throw new IseException(e.getMessage(), e);
    }
}
Also used : IseException(org.candlepin.common.exceptions.IseException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 14 with IseException

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

the class VerifyAuthorizationFilter method runFilter.

@Override
public void runFilter(ContainerRequestContext requestContext) {
    HttpRequest request = ResteasyProviderFactory.getContextData(HttpRequest.class);
    Principal principal = (Principal) requestContext.getSecurityContext().getUserPrincipal();
    ResourceInfo resourceInfo = ResteasyProviderFactory.getContextData(ResourceInfo.class);
    Method method = resourceInfo.getResourceMethod();
    if (log.isDebugEnabled()) {
        log.debug("Authorization check for {} mapping to {}.{}", requestContext.getUriInfo().getPath(), method.getDeclaringClass().getName(), method.getName());
    }
    Map<Verify, Object> argMap = getArguments(request, method);
    // Couldn't find a match in Resteasy for method
    if (argMap.isEmpty()) {
        /* It would also be possible to get here if a super-admin only method
             * were inadvertently being filtered through this filter.  Normally the
             * AuthorizationFeature takes care of sending methods without any @Verify
             * annotations through the SuperAdminAuthorizationFilter */
        throw new IseException("Could not get parameters for " + method);
    }
    Access defaultAccess = getDefaultAccess(method);
    if (!hasAccess(argMap, principal, defaultAccess)) {
        denyAccess(principal, method);
    }
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) ResourceInfo(javax.ws.rs.container.ResourceInfo) IseException(org.candlepin.common.exceptions.IseException) Access(org.candlepin.auth.Access) Method(java.lang.reflect.Method) Verify(org.candlepin.auth.Verify) Principal(org.candlepin.auth.Principal)

Example 15 with IseException

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

the class VerifyAuthorizationFilter method hasAccess.

protected boolean hasAccess(Map<Verify, Object> argMap, Principal principal, Access defaultAccess) {
    boolean hasAccess = false;
    Owner owner = null;
    for (Map.Entry<Verify, Object> entry : argMap.entrySet()) {
        List<Persisted> accessedObjects = new ArrayList<>();
        Object obj = entry.getValue();
        Verify verify = entry.getKey();
        Class<? extends Persisted> verifyType = verify.value();
        accessedObjects.addAll(getAccessedEntities(verify, obj));
        Access requiredAccess = defaultAccess;
        if (verify.require() != Access.NONE) {
            requiredAccess = verify.require();
        }
        log.debug("Verifying {} access to {}: {}", requiredAccess, verifyType, obj);
        SubResource subResource = verify.subResource();
        for (Persisted entity : accessedObjects) {
            if (!principal.canAccess(entity, subResource, requiredAccess)) {
                hasAccess = false;
                break;
            }
            hasAccess = true;
            Owner entityOwner = ((EntityStore) storeFactory.getFor(verifyType)).getOwner(entity);
            if (entityOwner != null) {
                if (owner != null && !owner.equals(entityOwner)) {
                    log.error("Found entities from multiple orgs in a single request");
                    throw new IseException("Found entities from multiple orgs in a single request");
                }
                owner = entityOwner;
            }
        }
        // Stop all further checking with any authorization failure
        if (!hasAccess) {
            break;
        }
    }
    if (hasAccess && owner != null) {
        MDC.put("org", owner.getKey());
        if (owner.getLogLevel() != null) {
            MDC.put("orgLogLevel", owner.getLogLevel());
        }
    }
    return hasAccess;
}
Also used : SubResource(org.candlepin.auth.SubResource) Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) Access(org.candlepin.auth.Access) Persisted(org.candlepin.model.Persisted) IseException(org.candlepin.common.exceptions.IseException) Verify(org.candlepin.auth.Verify) LinkedHashMap(java.util.LinkedHashMap) ResourceLocatorMap(org.candlepin.resteasy.ResourceLocatorMap) Map(java.util.Map)

Aggregations

IseException (org.candlepin.common.exceptions.IseException)16 IOException (java.io.IOException)7 ApiOperation (io.swagger.annotations.ApiOperation)6 File (java.io.File)6 Produces (javax.ws.rs.Produces)6 BadRequestException (org.candlepin.common.exceptions.BadRequestException)5 ApiResponses (io.swagger.annotations.ApiResponses)4 Path (javax.ws.rs.Path)4 Consumer (org.candlepin.model.Consumer)4 Owner (org.candlepin.model.Owner)4 ConflictOverrides (org.candlepin.sync.ConflictOverrides)4 ImporterException (org.candlepin.sync.ImporterException)4 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 Verify (org.candlepin.auth.Verify)3 CandlepinException (org.candlepin.common.exceptions.CandlepinException)3 ExportCreationException (org.candlepin.sync.ExportCreationException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2