use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class GatewayServlet method createFilter.
private static GatewayFilter createFilter(InputStream stream, ServletContext servletContext) throws ServletException {
try {
GatewayFilter filter = null;
if (stream != null) {
try {
GatewayDescriptor descriptor = GatewayDescriptorFactory.load("xml", new InputStreamReader(stream));
filter = GatewayFactory.create(descriptor);
} finally {
stream.close();
}
}
GatewayConfig gatewayConfig = (GatewayConfig) servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
if (gatewayConfig.isMetricsEnabled()) {
GatewayServices gatewayServices = (GatewayServices) servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
MetricsService metricsService = gatewayServices.getService(GatewayServices.METRICS_SERVICE);
if (metricsService != null) {
GatewayFilter instrumentedFilter = metricsService.getInstrumented(filter);
if (instrumentedFilter != null) {
filter = instrumentedFilter;
}
}
}
return filter;
} catch (IOException e) {
throw new ServletException(e);
} catch (URISyntaxException e) {
throw new ServletException(e);
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class CMFKeystoreServiceTest method setup.
@Before
public void setup() {
try {
ks = new CMFKeystoreService(".", "ambari");
ks.setMasterService(new MasterService() {
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
// TODO Auto-generated method stub
}
public void start() throws ServiceLifecycleException {
// TODO Auto-generated method stub
}
public void stop() throws ServiceLifecycleException {
// TODO Auto-generated method stub
}
public char[] getMasterSecret() {
// TODO Auto-generated method stub
return "testmaster".toCharArray();
}
});
} catch (ServiceLifecycleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultDispatchTest method testUsingDefaultBufferSize.
@Test
public void testUsingDefaultBufferSize() throws URISyntaxException, IOException {
DefaultDispatch defaultDispatch = new DefaultDispatch();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(Boolean.TRUE).anyTimes();
EasyMock.expect(gatewayConfig.getHttpServerRequestBuffer()).andReturn(16384).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getQueryString()).andReturn("a=123").anyTimes();
EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
assertTrue("not buffering in the absence of delegation token", (httpEntity instanceof PartiallyRepeatableHttpEntity));
assertEquals(defaultDispatch.getReplayBufferSize(), 16);
assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), 16384);
// also test normal setter and getters
defaultDispatch.setReplayBufferSize(-1);
assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), -1);
assertEquals(defaultDispatch.getReplayBufferSize(), -1);
defaultDispatch.setReplayBufferSize(16);
assertEquals(defaultDispatch.getReplayBufferSizeInBytes(), 16384);
assertEquals(defaultDispatch.getReplayBufferSize(), 16);
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultDispatchTest method testCallToSecureClusterWithDelegationToken.
@Test
public void testCallToSecureClusterWithDelegationToken() throws URISyntaxException, IOException {
DefaultDispatch defaultDispatch = new DefaultDispatch();
ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(gatewayConfig.isHadoopKerberosSecured()).andReturn(true).anyTimes();
EasyMock.expect(servletContext.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
ServletInputStream inputStream = EasyMock.createNiceMock(ServletInputStream.class);
HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class);
EasyMock.expect(inboundRequest.getQueryString()).andReturn("delegation=123").anyTimes();
EasyMock.expect(inboundRequest.getInputStream()).andReturn(inputStream).anyTimes();
EasyMock.expect(inboundRequest.getServletContext()).andReturn(servletContext).anyTimes();
EasyMock.replay(gatewayConfig, servletContext, inboundRequest);
HttpEntity httpEntity = defaultDispatch.createRequestEntity(inboundRequest);
assertFalse("buffering in the presence of delegation token", (httpEntity instanceof PartiallyRepeatableHttpEntity));
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class DefaultDispatch method createRequestEntity.
protected HttpEntity createRequestEntity(HttpServletRequest request) throws IOException {
String contentType = request.getContentType();
int contentLength = request.getContentLength();
InputStream contentStream = request.getInputStream();
HttpEntity entity;
if (contentType == null) {
entity = new InputStreamEntity(contentStream, contentLength);
} else {
entity = new InputStreamEntity(contentStream, contentLength, ContentType.parse(contentType));
}
GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
if (config != null && config.isHadoopKerberosSecured()) {
// Check if delegation token is supplied in the request
boolean delegationTokenPresent = false;
String queryString = request.getQueryString();
if (queryString != null) {
delegationTokenPresent = queryString.startsWith("delegation=") || queryString.contains("&delegation=");
}
if (replayBufferSize < 0) {
replayBufferSize = config.getHttpServerRequestBuffer();
}
if (!delegationTokenPresent && replayBufferSize > 0) {
entity = new PartiallyRepeatableHttpEntity(entity, replayBufferSize);
}
}
return entity;
}
Aggregations