Search in sources :

Example 6 with UnauthenticatedException

use of io.cdap.cdap.security.spi.authentication.UnauthenticatedException in project cdap by caskdata.

the class ExploreDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!acceptsURL(url)) {
        return null;
    }
    ExploreConnectionParams params = ExploreConnectionParams.parseConnectionUrl(url);
    String authToken = getString(params, ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN, null);
    String namespace = getString(params, ExploreConnectionParams.Info.NAMESPACE, NamespaceId.DEFAULT.getNamespace());
    boolean sslEnabled = getBoolean(params, ExploreConnectionParams.Info.SSL_ENABLED, false);
    boolean verifySSLCert = getBoolean(params, ExploreConnectionParams.Info.VERIFY_SSL_CERT, true);
    ExploreClient exploreClient = new FixedAddressExploreClient(params.getHost(), params.getPort(), authToken, sslEnabled, verifySSLCert);
    try {
        exploreClient.ping();
    } catch (UnauthenticatedException e) {
        throw new SQLException("Cannot connect to " + url + ", not authenticated.", e);
    } catch (ServiceUnavailableException | ExploreException e) {
        throw new SQLException("Cannot connect to " + url + ", service not available.", e);
    }
    return new ExploreConnection(exploreClient, namespace, params);
}
Also used : ExploreClient(io.cdap.cdap.explore.client.ExploreClient) FixedAddressExploreClient(io.cdap.cdap.explore.client.FixedAddressExploreClient) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) SQLException(java.sql.SQLException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) FixedAddressExploreClient(io.cdap.cdap.explore.client.FixedAddressExploreClient) ExploreException(io.cdap.cdap.explore.service.ExploreException)

Example 7 with UnauthenticatedException

use of io.cdap.cdap.security.spi.authentication.UnauthenticatedException in project cdap by caskdata.

the class RESTClient method upload.

public HttpResponse upload(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes) throws IOException, UnauthenticatedException, DisconnectedException {
    HttpResponse response = HttpRequests.execute(HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(), clientConfig.getUploadRequestConfig());
    int responseCode = response.getResponseCode();
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new UnauthenticatedException("Unauthorized status code received from the server.");
        }
        throw new IOException(response.getResponseBodyAsString());
    }
    return response;
}
Also used : UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 8 with UnauthenticatedException

use of io.cdap.cdap.security.spi.authentication.UnauthenticatedException in project cdap by caskdata.

the class RuntimeServiceRoutingTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    mockRemoteAuthenticatorProvider = new MockRemoteAuthenticatorProvider();
    injector = Guice.createInjector(new ConfigModule(cConf), new PrivateModule() {

        @Override
        protected void configure() {
            bind(RemoteAuthenticator.class).toProvider(mockRemoteAuthenticatorProvider);
            expose(RemoteAuthenticator.class);
        }
    }, new LocalLocationModule(), new InMemoryDiscoveryModule(), new MessagingServerRuntimeModule().getInMemoryModules(), new AuthenticationContextModules().getNoOpModule(), new RuntimeServerModule() {

        @Override
        protected void bindRequestValidator() {
            bind(RuntimeRequestValidator.class).toInstance((programRunId, request) -> {
                String authHeader = request.headers().get(HttpHeaderNames.AUTHORIZATION);
                String expected = "Bearer " + Base64.getEncoder().encodeToString(Hashing.md5().hashString(programRunId.toString()).asBytes());
                if (!expected.equals(authHeader)) {
                    throw new UnauthenticatedException("Program run " + programRunId + " is not authorized");
                }
                return new ProgramRunInfo(ProgramRunStatus.COMPLETED, null);
            });
        }

        @Override
        protected void bindLogProcessor() {
            bind(RemoteExecutionLogProcessor.class).toInstance(payloads -> {
            });
        }
    }, new AbstractModule() {

        @Override
        protected void configure() {
            bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class);
        }
    });
    messagingService = injector.getInstance(MessagingService.class);
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    messagingService.createTopic(new TopicMetadata(NamespaceId.SYSTEM.topic("topic")));
    runtimeServer = injector.getInstance(RuntimeServer.class);
    runtimeServer.startAndWait();
    mockService = NettyHttpService.builder(MOCK_SERVICE).setHost(InetAddress.getLocalHost().getCanonicalHostName()).setHttpHandlers(new PingHandler(), new MockServiceHandler()).build();
    mockService.start();
    mockServiceCancellable = injector.getInstance(DiscoveryService.class).register(URIScheme.createDiscoverable(MOCK_SERVICE, mockService));
}
Also used : InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) RemoteAuthenticator(io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator) NoOpRemoteAuthenticator(io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator) PingHandler(io.cdap.cdap.gateway.handlers.PingHandler) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) NettyHttpService(io.cdap.http.NettyHttpService) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) MessagingService(io.cdap.cdap.messaging.MessagingService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) Service(com.google.common.util.concurrent.Service) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MessagingServerRuntimeModule(io.cdap.cdap.messaging.guice.MessagingServerRuntimeModule) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) AbstractModule(com.google.inject.AbstractModule) MessagingService(io.cdap.cdap.messaging.MessagingService) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) RuntimeServerModule(io.cdap.cdap.app.guice.RuntimeServerModule) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) PrivateModule(com.google.inject.PrivateModule) Before(org.junit.Before)

Example 9 with UnauthenticatedException

use of io.cdap.cdap.security.spi.authentication.UnauthenticatedException in project cdap by cdapio.

the class RESTClient method upload.

public HttpResponse upload(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes) throws IOException, UnauthenticatedException, DisconnectedException {
    HttpResponse response = HttpRequests.execute(HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(), clientConfig.getUploadRequestConfig());
    int responseCode = response.getResponseCode();
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new UnauthenticatedException("Unauthorized status code received from the server.");
        }
        throw new IOException(response.getResponseBodyAsString());
    }
    return response;
}
Also used : UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 10 with UnauthenticatedException

use of io.cdap.cdap.security.spi.authentication.UnauthenticatedException in project cdap by cdapio.

the class RuntimeServiceRoutingTest method beforeTest.

@Before
public void beforeTest() throws Exception {
    CConfiguration cConf = CConfiguration.create();
    cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().getAbsolutePath());
    mockRemoteAuthenticatorProvider = new MockRemoteAuthenticatorProvider();
    injector = Guice.createInjector(new ConfigModule(cConf), new PrivateModule() {

        @Override
        protected void configure() {
            bind(RemoteAuthenticator.class).toProvider(mockRemoteAuthenticatorProvider);
            expose(RemoteAuthenticator.class);
        }
    }, new LocalLocationModule(), new InMemoryDiscoveryModule(), new MessagingServerRuntimeModule().getInMemoryModules(), new AuthenticationContextModules().getNoOpModule(), new RuntimeServerModule() {

        @Override
        protected void bindRequestValidator() {
            bind(RuntimeRequestValidator.class).toInstance((programRunId, request) -> {
                String authHeader = request.headers().get(HttpHeaderNames.AUTHORIZATION);
                String expected = "Bearer " + Base64.getEncoder().encodeToString(Hashing.md5().hashString(programRunId.toString()).asBytes());
                if (!expected.equals(authHeader)) {
                    throw new UnauthenticatedException("Program run " + programRunId + " is not authorized");
                }
                return new ProgramRunInfo(ProgramRunStatus.COMPLETED, null);
            });
        }

        @Override
        protected void bindLogProcessor() {
            bind(RemoteExecutionLogProcessor.class).toInstance(payloads -> {
            });
        }
    }, new AbstractModule() {

        @Override
        protected void configure() {
            bind(MetricsCollectionService.class).to(NoOpMetricsCollectionService.class);
        }
    });
    messagingService = injector.getInstance(MessagingService.class);
    if (messagingService instanceof Service) {
        ((Service) messagingService).startAndWait();
    }
    messagingService.createTopic(new TopicMetadata(NamespaceId.SYSTEM.topic("topic")));
    runtimeServer = injector.getInstance(RuntimeServer.class);
    runtimeServer.startAndWait();
    mockService = NettyHttpService.builder(MOCK_SERVICE).setHost(InetAddress.getLocalHost().getCanonicalHostName()).setHttpHandlers(new PingHandler(), new MockServiceHandler()).build();
    mockService.start();
    mockServiceCancellable = injector.getInstance(DiscoveryService.class).register(URIScheme.createDiscoverable(MOCK_SERVICE, mockService));
}
Also used : InMemoryDiscoveryModule(io.cdap.cdap.common.guice.InMemoryDiscoveryModule) RemoteAuthenticator(io.cdap.cdap.security.spi.authenticator.RemoteAuthenticator) NoOpRemoteAuthenticator(io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator) PingHandler(io.cdap.cdap.gateway.handlers.PingHandler) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) ConfigModule(io.cdap.cdap.common.guice.ConfigModule) AuthenticationContextModules(io.cdap.cdap.security.auth.context.AuthenticationContextModules) NettyHttpService(io.cdap.http.NettyHttpService) NoOpMetricsCollectionService(io.cdap.cdap.common.metrics.NoOpMetricsCollectionService) MessagingService(io.cdap.cdap.messaging.MessagingService) DiscoveryService(org.apache.twill.discovery.DiscoveryService) Service(com.google.common.util.concurrent.Service) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MessagingServerRuntimeModule(io.cdap.cdap.messaging.guice.MessagingServerRuntimeModule) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) AbstractModule(com.google.inject.AbstractModule) MessagingService(io.cdap.cdap.messaging.MessagingService) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) RuntimeServerModule(io.cdap.cdap.app.guice.RuntimeServerModule) LocalLocationModule(io.cdap.cdap.common.guice.LocalLocationModule) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) PrivateModule(com.google.inject.PrivateModule) Before(org.junit.Before)

Aggregations

UnauthenticatedException (io.cdap.cdap.security.spi.authentication.UnauthenticatedException)12 IOException (java.io.IOException)8 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 HttpResponse (io.cdap.common.http.HttpResponse)4 Service (com.google.common.util.concurrent.Service)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 AbstractModule (com.google.inject.AbstractModule)2 PrivateModule (com.google.inject.PrivateModule)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 RuntimeServerModule (io.cdap.cdap.app.guice.RuntimeServerModule)2 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)2 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)2 ConfigModule (io.cdap.cdap.common.guice.ConfigModule)2 InMemoryDiscoveryModule (io.cdap.cdap.common.guice.InMemoryDiscoveryModule)2 LocalLocationModule (io.cdap.cdap.common.guice.LocalLocationModule)2 NoOpRemoteAuthenticator (io.cdap.cdap.common.internal.remote.NoOpRemoteAuthenticator)2 NoOpMetricsCollectionService (io.cdap.cdap.common.metrics.NoOpMetricsCollectionService)2 ExploreClient (io.cdap.cdap.explore.client.ExploreClient)2 FixedAddressExploreClient (io.cdap.cdap.explore.client.FixedAddressExploreClient)2 ExploreException (io.cdap.cdap.explore.service.ExploreException)2