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