Search in sources :

Example 31 with UnauthorizedException

use of co.cask.cdap.security.spi.authorization.UnauthorizedException in project cdap by caskdata.

the class DatasetInstanceService method ensureAccess.

/**
   * Ensures that the logged-in user has a {@link Action privilege} on the specified dataset instance.
   *
   * @param datasetId the {@link DatasetId} to check for privileges
   * @throws UnauthorizedException if the logged in user has no {@link Action privileges} on the specified dataset
   */
private void ensureAccess(DatasetId datasetId) throws Exception {
    Principal principal = authenticationContext.getPrincipal();
    Predicate<EntityId> filter = authorizationEnforcer.createFilter(principal);
    if (!filter.apply(datasetId)) {
        throw new UnauthorizedException(principal, datasetId);
    }
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) Principal(co.cask.cdap.proto.security.Principal)

Example 32 with UnauthorizedException

use of co.cask.cdap.security.spi.authorization.UnauthorizedException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method resetLogLevels.

private void resetLogLevels(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
    ProgramType programType = getProgramType(type);
    if (programType == null) {
        throw new BadRequestException("Invalid program type provided");
    }
    try {
        Set<String> loggerNames = parseBody(request, SET_STRING_TYPE);
        lifecycleService.resetProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), loggerNames == null ? Collections.<String>emptySet() : loggerNames, component, runId);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid JSON in body");
    } catch (SecurityException e) {
        throw new UnauthorizedException("Unauthorized to reset the log levels");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 33 with UnauthorizedException

use of co.cask.cdap.security.spi.authorization.UnauthorizedException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method updateLogLevels.

private void updateLogLevels(HttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String type, String programName, @Nullable String component, String runId) throws Exception {
    ProgramType programType = getProgramType(type);
    if (programType == null) {
        throw new BadRequestException("Invalid program type provided");
    }
    try {
        // we are decoding the body to Map<String, String> instead of Map<String, LogEntry.Level> here since Gson will
        // serialize invalid enum values to null, which is allowed for log level, instead of throw an Exception.
        lifecycleService.updateProgramLogLevels(new ApplicationId(namespace, appName, appVersion).program(programType, programName), transformLogLevelsMap(decodeArguments(request)), component, runId);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Invalid JSON in body");
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e.getMessage());
    } catch (SecurityException e) {
        throw new UnauthorizedException("Unauthorized to update the log levels");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 34 with UnauthorizedException

use of co.cask.cdap.security.spi.authorization.UnauthorizedException in project cdap by caskdata.

the class DatasetServiceClient method getInstance.

@Nullable
public DatasetMeta getInstance(String instanceName, @Nullable Iterable<? extends EntityId> owners) throws DatasetManagementException {
    String query = "";
    if (owners != null) {
        Set<String> ownerParams = Sets.newHashSet();
        for (EntityId owner : owners) {
            ownerParams.add("owner=" + owner.toString());
        }
        query = ownerParams.isEmpty() ? "" : "?" + Joiner.on("&").join(ownerParams);
    }
    HttpResponse response = doGet("datasets/" + instanceName + query);
    if (HttpResponseStatus.NOT_FOUND.code() == response.getResponseCode()) {
        return null;
    }
    if (HttpResponseStatus.FORBIDDEN.code() == response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to get dataset instance %s, details: %s", instanceName, response), new UnauthorizedException(response.getResponseBodyAsString()));
    }
    if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Cannot retrieve dataset instance %s info, details: %s", instanceName, response));
    }
    return GSON.fromJson(response.getResponseBodyAsString(), DatasetMeta.class);
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(co.cask.common.http.HttpResponse) Nullable(javax.annotation.Nullable)

Example 35 with UnauthorizedException

use of co.cask.cdap.security.spi.authorization.UnauthorizedException in project cdap by caskdata.

the class StreamAdminTest method testConfigAndTruncate.

@Test
public void testConfigAndTruncate() throws Exception {
    StreamAdmin streamAdmin = getStreamAdmin();
    StreamId stream = FOO_NAMESPACE.stream("stream");
    grantAndAssertSuccess(stream, USER, EnumSet.of(Action.ADMIN));
    streamAdmin.create(stream);
    Assert.assertTrue(streamAdmin.exists(stream));
    writeEvent(stream);
    // Getting config / properties should work
    streamAdmin.getConfig(stream);
    streamAdmin.getProperties(stream);
    // Now revoke access to the user to the stream and to the namespace
    revokeAndAssertSuccess(stream, USER, EnumSet.allOf(Action.class));
    streamAdmin.getConfig(stream);
    try {
        streamAdmin.getProperties(stream);
        Assert.fail("User should not be able to get the properties.");
    } catch (UnauthorizedException e) {
    // expected
    }
    // read action should be enough to get the stream config
    grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.READ));
    streamAdmin.getConfig(stream);
    StreamProperties properties = streamAdmin.getProperties(stream);
    try {
        streamAdmin.updateConfig(stream, properties);
        Assert.fail("User should not be able to update the config with just READ permissions.");
    } catch (UnauthorizedException e) {
    // expected
    }
    // This call bypasses the stream handler and thus authorization is not checked for this call and so write
    // to stream will succeed. It is done so that we can check and perform truncate call.
    writeEvent(stream);
    grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.WRITE));
    writeEvent(stream);
    try {
        streamAdmin.updateConfig(stream, properties);
        Assert.fail("User should not be able to update the config with just READ and WRITE permissions.");
    } catch (UnauthorizedException e) {
    // expected
    }
    try {
        streamAdmin.truncate(stream);
        Assert.fail("User should not be able to truncate the stream without ADMIN permission.");
    } catch (UnauthorizedException e) {
    // expected
    }
    try {
        streamAdmin.drop(stream);
        Assert.fail("User should not be able to drop the stream without ADMIN permission.");
    } catch (UnauthorizedException e) {
    // expdcted
    }
    grantAndAssertSuccess(stream, USER, ImmutableSet.of(Action.ADMIN));
    streamAdmin.updateConfig(stream, properties);
    streamAdmin.truncate(stream);
    Assert.assertEquals(0, getStreamSize(stream));
    streamAdmin.drop(stream);
    revokeAndAssertSuccess(stream, USER, EnumSet.of(Action.ADMIN));
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) Action(co.cask.cdap.proto.security.Action) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) StreamProperties(co.cask.cdap.proto.StreamProperties) Test(org.junit.Test)

Aggregations

UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)49 Test (org.junit.Test)18 IOException (java.io.IOException)15 EntityId (co.cask.cdap.proto.id.EntityId)13 Principal (co.cask.cdap.proto.security.Principal)13 Action (co.cask.cdap.proto.security.Action)12 BadRequestException (co.cask.cdap.common.BadRequestException)11 ApplicationId (co.cask.cdap.proto.id.ApplicationId)10 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 JsonSyntaxException (com.google.gson.JsonSyntaxException)9 ExecutionException (java.util.concurrent.ExecutionException)9 NotFoundException (co.cask.cdap.common.NotFoundException)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)7 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)6 ConflictException (co.cask.cdap.common.ConflictException)6 StreamId (co.cask.cdap.proto.id.StreamId)6 ArtifactAlreadyExistsException (co.cask.cdap.common.ArtifactAlreadyExistsException)5 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)5 WriteConflictException (co.cask.cdap.internal.app.runtime.artifact.WriteConflictException)5 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)5