Search in sources :

Example 1 with UnauthenticatedException

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

the class RESTClient method execute.

public HttpResponse execute(HttpRequest request, int... allowedErrorCodes) throws IOException, UnauthenticatedException, UnauthorizedException {
    int currentTry = 0;
    HttpResponse response;
    int responseCode;
    boolean allowUnavailable = ArrayUtils.contains(allowedErrorCodes, HttpURLConnection.HTTP_UNAVAILABLE);
    do {
        onRequest(request, currentTry);
        response = HttpRequests.execute(request, clientConfig.getDefaultRequestConfig());
        responseCode = response.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_UNAVAILABLE || allowUnavailable) {
            // only retry if unavailable
            break;
        }
        currentTry++;
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            break;
        }
    } while (currentTry <= clientConfig.getUnavailableRetryLimit());
    onResponse(request, response, currentTry);
    if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        throw new UnauthenticatedException("Unauthorized status code received from the server.");
    }
    if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
        throw new UnauthorizedException(response.getResponseBodyAsString());
    }
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        throw new IOException(responseCode + ": " + response.getResponseBodyAsString());
    }
    return response;
}
Also used : UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 2 with UnauthenticatedException

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

the class CLIConfig method getSavedAccessToken.

@Nullable
private UserAccessToken getSavedAccessToken(ConnectionConfig connectionInfo) {
    File file = getAccessTokenFile(connectionInfo.getHostname());
    try (BufferedReader reader = Files.newReader(file, Charsets.UTF_8)) {
        UserAccessToken userAccessToken = GSON.fromJson(reader, UserAccessToken.class);
        if (userAccessToken == null) {
            return null;
        }
        checkConnection(clientConfig, connectionInfo, userAccessToken.getAccessToken());
        return userAccessToken;
    } catch (IOException | JsonSyntaxException | UnauthenticatedException | UnauthorizedException ignored) {
    // Fall through
    }
    return null;
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) BufferedReader(java.io.BufferedReader) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 3 with UnauthenticatedException

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

the class IntegrationTestBase method checkSystemServices.

protected void checkSystemServices() throws TimeoutException, InterruptedException {
    Callable<Boolean> cdapAvailable = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // first wait for all system services to be 'OK'
            if (!getMonitorClient().allSystemServicesOk()) {
                return false;
            }
            // For non-default namespaces, simply check that the dataset service is up with list().
            // If list() does not throw exception, which means the http request receives response
            // status HTTP_OK and dataset service is up, then check if default namespace exists, if so return true.
            List<NamespaceMeta> list = getNamespaceClient().list();
            if (!configuredNamespace.equals(NamespaceId.DEFAULT)) {
                return true;
            }
            // default namespace exists before integration test starts
            for (NamespaceMeta namespaceMeta : list) {
                if (namespaceMeta.getNamespaceId().equals(NamespaceId.DEFAULT)) {
                    return true;
                }
            }
            return false;
        }
    };
    String errorMessage = String.format("CDAP Services are not available. Retried for %s seconds.", SERVICE_CHECK_TIMEOUT_SECONDS);
    try {
        checkServicesWithRetry(cdapAvailable, errorMessage);
    } catch (Throwable e) {
        Throwable rootCause = Throwables.getRootCause(e);
        if (rootCause instanceof UnauthenticatedException) {
            // security is enabled, we need to get access token before checking system services
            try {
                accessToken = fetchAccessToken();
            } catch (IOException ex) {
                throw Throwables.propagate(ex);
            }
            checkServicesWithRetry(cdapAvailable, errorMessage);
        } else {
            throw Throwables.propagate(rootCause);
        }
    }
    LOG.info("CDAP Services are up and running!");
}
Also used : UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) IOException(java.io.IOException) Callable(java.util.concurrent.Callable)

Example 4 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 5 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)

Aggregations

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