use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class HttpsEnablerTest method testServer.
/**
* Private method to verify https connection.
*
* @param useTrustStore {@code true} to have the client use a trust store that contains the certificate of the server
* @param trustAll {@code true} to have the client trust any https server
*/
private void testServer(boolean useTrustStore, boolean trustAll) throws Exception {
String ksPass = "xyz";
KeyStore keyStore = KeyStores.generatedCertKeyStore(1, ksPass);
// Start the http server
NettyHttpService httpService = new HttpsEnabler().setKeyStore(keyStore, ksPass::toCharArray).enable(NettyHttpService.builder("test").setHttpHandlers(new PingHandler())).build();
httpService.start();
try {
// Verify that it can be hit with HTTPS
InetSocketAddress address = httpService.getBindAddress();
URL url = new URL(String.format("https://%s:%d/ping", address.getHostName(), address.getPort()));
HttpsEnabler enabler = new HttpsEnabler().setTrustAll(trustAll);
// Optionally validates the server
if (useTrustStore) {
enabler.setTrustStore(KeyStores.createTrustStore(keyStore));
}
HttpsURLConnection urlConn = enabler.enable((HttpsURLConnection) url.openConnection());
Assert.assertEquals(200, urlConn.getResponseCode());
} finally {
httpService.stop();
}
}
use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class UGIProviderTest method testRemoteUGIProvider.
@Test
public void testRemoteUGIProvider() throws Exception {
// Starts a mock server to handle remote UGI requests
final NettyHttpService httpService = NettyHttpService.builder("remoteUGITest").setHttpHandlers(new UGIProviderTestHandler()).build();
httpService.start();
setKeytabDir(localKeytabDirPath.getAbsolutePath());
OwnerAdmin ownerAdmin = getOwnerAdmin();
// add an owner for stream
ownerAdmin.add(aliceEntity, aliceKerberosPrincipalId);
try {
InMemoryDiscoveryService discoveryService = new InMemoryDiscoveryService();
discoveryService.register(new Discoverable(Constants.Service.APP_FABRIC_HTTP, httpService.getBindAddress()));
RemoteClientFactory remoteClientFactory = new RemoteClientFactory(discoveryService, new DefaultInternalAuthenticator(new AuthenticationTestContext()));
RemoteUGIProvider ugiProvider = new RemoteUGIProvider(cConf, locationFactory, ownerAdmin, remoteClientFactory);
ImpersonationRequest aliceImpRequest = new ImpersonationRequest(aliceEntity, ImpersonatedOpType.OTHER);
UGIWithPrincipal aliceUGIWithPrincipal = ugiProvider.getConfiguredUGI(aliceImpRequest);
// Shouldn't be a kerberos UGI
Assert.assertFalse(aliceUGIWithPrincipal.getUGI().hasKerberosCredentials());
// Validate the credentials
Token<? extends TokenIdentifier> token = aliceUGIWithPrincipal.getUGI().getCredentials().getToken(new Text("entity"));
Assert.assertArrayEquals(aliceEntity.toString().getBytes(StandardCharsets.UTF_8), token.getIdentifier());
Assert.assertArrayEquals(aliceEntity.toString().getBytes(StandardCharsets.UTF_8), token.getPassword());
Assert.assertEquals(new Text("entity"), token.getKind());
Assert.assertEquals(new Text("service"), token.getService());
token = aliceUGIWithPrincipal.getUGI().getCredentials().getToken(new Text("opType"));
Assert.assertArrayEquals(aliceImpRequest.getImpersonatedOpType().toString().getBytes(StandardCharsets.UTF_8), token.getIdentifier());
Assert.assertArrayEquals(aliceImpRequest.getImpersonatedOpType().toString().getBytes(StandardCharsets.UTF_8), token.getPassword());
Assert.assertEquals(new Text("opType"), token.getKind());
Assert.assertEquals(new Text("service"), token.getService());
// Fetch it again, it should return the same UGI due to caching
Assert.assertSame(aliceUGIWithPrincipal, ugiProvider.getConfiguredUGI(aliceImpRequest));
// Invalid the cache and fetch it again. A different UGI should be returned
ugiProvider.invalidCache();
Assert.assertNotSame(aliceUGIWithPrincipal, ugiProvider.getConfiguredUGI(aliceImpRequest));
} finally {
httpService.stop();
}
// cleanup
ownerAdmin.delete(aliceEntity);
}
use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class AuthorizationHandlerTest method testDisabled.
private void testDisabled(CConfiguration cConf, FeatureDisabledException.Feature feature, String configSetting) throws Exception {
final InMemoryAccessController accessController = new InMemoryAccessController();
NettyHttpService service = new CommonNettyHttpServiceBuilder(cConf, getClass().getSimpleName()).setHttpHandlers(new AuthorizationHandler(accessController, new AccessControllerInstantiator(cConf, FACTORY) {
@Override
public AccessController get() {
return accessController;
}
}, cConf, new MasterAuthenticationContext())).build();
service.start();
try {
final AuthorizationClient client = new AuthorizationClient(ClientConfig.builder().setConnectionConfig(ConnectionConfig.builder().setHostname(service.getBindAddress().getHostName()).setPort(service.getBindAddress().getPort()).setSSLEnabled(false).build()).build());
final NamespaceId ns1 = Ids.namespace("ns1");
final Role admins = new Role("admins");
// Test that the right exception is thrown when any Authorization REST API is called with authorization disabled
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.grant(Authorizable.fromEntityId(ns1), admin, ImmutableSet.of(StandardPermission.GET));
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.revoke(Authorizable.fromEntityId(ns1), admin, ImmutableSet.of(StandardPermission.GET));
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.revoke(Authorizable.fromEntityId(ns1));
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.listGrants(admin);
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.addRoleToPrincipal(admins, admin);
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.removeRoleFromPrincipal(admins, admin);
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.createRole(admins);
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.dropRole(admins);
}
}, feature, configSetting);
verifyFeatureDisabled(new DisabledFeatureCaller() {
@Override
public void call() throws Exception {
client.listAllRoles();
}
}, feature, configSetting);
} finally {
service.stop();
}
}
use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class SpillableBodyConsumerTest method testPost.
private void testPost(String body, int bufferLimit) throws Exception {
NettyHttpService httpService = NettyHttpService.builder("test").setHttpHandlers(new TestHandler(bufferLimit)).build();
httpService.start();
try {
InetSocketAddress addr = httpService.getBindAddress();
URL url = new URL(String.format("http://%s:%d/post", addr.getHostName(), addr.getPort()));
HttpResponse response = HttpRequests.execute(io.cdap.common.http.HttpRequest.post(url).withBody(body).build(), new HttpRequestConfig(1000, 10000000));
Assert.assertEquals(200, response.getResponseCode());
Assert.assertEquals(body, response.getResponseBodyAsString());
} finally {
httpService.stop();
}
}
use of io.cdap.http.NettyHttpService in project cdap by caskdata.
the class HttpHandlerGeneratorTest method testHttpHeaders.
@Test
public void testHttpHeaders() throws Exception {
HttpHandlerFactory factory = new HttpHandlerFactory("/prefix", TransactionControl.IMPLICIT);
HttpHandler httpHandler = factory.createHttpHandler(TypeToken.of(MyHttpHandler.class), new AbstractDelegatorContext<MyHttpHandler>() {
@Override
protected MyHttpHandler createHandler() {
return new MyHttpHandler();
}
}, new NoopMetricsContext());
NettyHttpService service = NettyHttpService.builder("test-headers").setHttpHandlers(httpHandler).build();
service.start();
try {
InetSocketAddress bindAddress = service.getBindAddress();
// Make a request with headers that the response should carry first value for each header name
HttpURLConnection urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/prefix/p2/echo/firstHeaders", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.addRequestProperty("k1", "v1");
urlConn.addRequestProperty("k1", "v2");
urlConn.addRequestProperty("k2", "v2");
Assert.assertEquals(200, urlConn.getResponseCode());
Map<String, List<String>> headers = urlConn.getHeaderFields();
Assert.assertEquals(ImmutableList.of("v1"), headers.get("k1"));
Assert.assertEquals(ImmutableList.of("v2"), headers.get("k2"));
// Make a request with headers that the response should carry all values for each header name
urlConn = (HttpURLConnection) new URL(String.format("http://%s:%d/prefix/p2/echo/allHeaders", bindAddress.getHostName(), bindAddress.getPort())).openConnection();
urlConn.addRequestProperty("k1", "v1");
urlConn.addRequestProperty("k1", "v2");
urlConn.addRequestProperty("k1", "v3");
urlConn.addRequestProperty("k2", "v2");
Assert.assertEquals(200, urlConn.getResponseCode());
headers = urlConn.getHeaderFields();
// URLConnection always reverse the ordering of the header values.
Assert.assertEquals(ImmutableList.of("v3", "v2", "v1"), headers.get("k1"));
Assert.assertEquals(ImmutableList.of("v2"), headers.get("k2"));
} finally {
service.stop();
}
}
Aggregations