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;
}
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;
}
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!");
}
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);
}
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;
}
Aggregations