use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class KnoxCLI method getGatewayConfig.
private GatewayConfig getGatewayConfig() {
GatewayConfig result;
Configuration conf = getConf();
if (conf != null && conf instanceof GatewayConfig) {
result = (GatewayConfig) conf;
} else {
result = new GatewayConfigImpl();
}
return result;
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class KnoxCLI method initializeServices.
private void initializeServices(boolean persisting) throws ServiceLifecycleException {
GatewayConfig config = getGatewayConfig();
Map<String, String> options = new HashMap<>();
options.put(GatewayCommandLine.PERSIST_LONG, Boolean.toString(persisting));
if (master != null) {
options.put("master", master);
}
services.init(config, options);
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class AuditLoggingTest method testNoopFilter.
@Test
public /**
* One NoOp filter in chain. Single audit event with same with specified request URI is expected:
*
* action=access request_type=uri outcome=unavailable
*/
void testNoopFilter() throws ServletException, IOException, URISyntaxException {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.replay(config);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(request.getMethod()).andReturn(METHOD).anyTimes();
EasyMock.expect(request.getPathInfo()).andReturn(PATH).anyTimes();
EasyMock.expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
EasyMock.expect(request.getRemoteAddr()).andReturn(ADDRESS).anyTimes();
EasyMock.expect(request.getRemoteHost()).andReturn(HOST).anyTimes();
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn("Custom-Forwarded-For").anyTimes();
EasyMock.replay(request);
EasyMock.replay(context);
EasyMock.replay(gatewayConfig);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
FilterChain chain = EasyMock.createNiceMock(FilterChain.class);
EasyMock.replay(chain);
Filter filter = EasyMock.createNiceMock(Filter.class);
EasyMock.replay(filter);
GatewayFilter gateway = new GatewayFilter();
gateway.addFilter("path", "filter", filter, null, null);
gateway.init(config);
gateway.doFilter(request, response, chain);
gateway.destroy();
assertThat(CollectAppender.queue.size(), is(1));
Iterator<LoggingEvent> iterator = CollectAppender.queue.iterator();
LoggingEvent accessEvent = iterator.next();
verifyAuditEvent(accessEvent, CONTEXT_PATH + PATH, ResourceType.URI, Action.ACCESS, ActionOutcome.UNAVAILABLE, null, "Request method: GET");
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class AuditLoggingTest method testNoFiltersAudit.
@Test
public /**
* Empty filter chain. Two events with same correlation ID are expected:
*
* action=access request_type=uri outcome=unavailable
* action=access request_type=uri outcome=success message=Response status: 404
*/
void testNoFiltersAudit() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.replay(config);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(request.getMethod()).andReturn(METHOD).anyTimes();
EasyMock.expect(request.getPathInfo()).andReturn(PATH).anyTimes();
EasyMock.expect(request.getContextPath()).andReturn(CONTEXT_PATH).anyTimes();
EasyMock.expect(request.getRemoteAddr()).andReturn(ADDRESS).anyTimes();
EasyMock.expect(request.getRemoteHost()).andReturn(HOST).anyTimes();
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn("Custom-Forwarded-For").anyTimes();
EasyMock.replay(request);
EasyMock.replay(context);
EasyMock.replay(gatewayConfig);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
FilterChain chain = EasyMock.createNiceMock(FilterChain.class);
EasyMock.replay(chain);
Random rnd = new Random();
// Make number of total requests between 1-100
int numberTotalRequests = rnd.nextInt(99) + 1;
Set<Callable<Void>> callables = new HashSet<>(numberTotalRequests);
for (int i = 0; i < numberTotalRequests; i++) {
callables.add(() -> {
GatewayFilter gateway = new GatewayFilter();
gateway.init(config);
gateway.doFilter(request, response, chain);
gateway.destroy();
return null;
});
}
// Make number of concurrent requests between 1-10
int numberConcurrentRequests = rnd.nextInt(9) + 1;
LOG.info("Executing %d total requests with %d concurrently", numberTotalRequests, numberConcurrentRequests);
ExecutorService executor = Executors.newFixedThreadPool(numberConcurrentRequests);
executor.invokeAll(callables);
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
assertThat(executor.isTerminated(), is(true));
assertThat(CollectAppender.queue.size(), is(numberTotalRequests));
// Use a set to make sure to dedupe any requestIds to get only unique ones
Set<String> requestIds = new HashSet<>();
for (LoggingEvent accessEvent : CollectAppender.queue) {
verifyAuditEvent(accessEvent, CONTEXT_PATH + PATH, ResourceType.URI, Action.ACCESS, ActionOutcome.UNAVAILABLE, null, "Request method: GET");
CorrelationContext cc = (CorrelationContext) accessEvent.getMDC(Log4jCorrelationService.MDC_CORRELATION_CONTEXT_KEY);
// There are some events that do not have a CorrelationContext associated (ie: deploy)
if (cc != null) {
requestIds.add(cc.getRequestId());
}
}
// There should be a unique correlation id for each request
assertThat(requestIds.size(), is(numberTotalRequests));
}
use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.
the class GatewayFilterTest method testDefaultServicePathTopologyRequestAttribute.
@Test
public void testDefaultServicePathTopologyRequestAttribute() throws Exception {
FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
EasyMock.replay(config);
Topology topology = EasyMock.createNiceMock(Topology.class);
topology.setDefaultServicePath("test-role/");
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
ServletContext context = EasyMock.createNiceMock(ServletContext.class);
GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
EasyMock.expect(topology.getDefaultServicePath()).andReturn("test-role").anyTimes();
EasyMock.expect(request.getPathInfo()).andReturn("/test-path/test-resource").anyTimes();
EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
EasyMock.expect(context.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn("Custom-Forwarded-For").anyTimes();
EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer("http://host:8443/gateway/sandbox/test-path/test-resource/")).anyTimes();
EasyMock.expect(context.getAttribute("org.apache.knox.gateway.topology")).andReturn(topology).anyTimes();
EasyMock.replay(request);
EasyMock.replay(context);
EasyMock.replay(topology);
EasyMock.replay(gatewayConfig);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(response);
TestRoleFilter filter = new TestRoleFilter();
GatewayFilter gateway = new GatewayFilter();
gateway.addFilter("test-role/**/**", "test-filter", filter, null, "test-role");
gateway.init(config);
gateway.doFilter(request, response);
gateway.destroy();
assertThat((String) filter.defaultServicePath, is("test-role"));
assertThat((String) filter.url, is("http://host:8443/gateway/sandbox/test-role/test-path/test-resource"));
}
Aggregations