Search in sources :

Example 26 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class GatewayFilterTest method testNoFilters.

@Test
public void testNoFilters() throws ServletException, IOException {
    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.getPathInfo()).andReturn("source").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);
    GatewayFilter gateway = new GatewayFilter();
    gateway.init(config);
    gateway.doFilter(request, response, chain);
    gateway.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 27 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class GatewayFilterTest method testTargetServiceRoleRequestAttribute.

@Test
public void testTargetServiceRoleRequestAttribute() 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.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();
    request.setAttribute(AbstractGatewayFilter.TARGET_SERVICE_ROLE, "test-role");
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(request.getAttribute(AbstractGatewayFilter.TARGET_SERVICE_ROLE)).andReturn("test-role").anyTimes();
    EasyMock.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(gatewayConfig);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(response);
    TestRoleFilter filter = new TestRoleFilter();
    GatewayFilter gateway = new GatewayFilter();
    gateway.addFilter("test-path/**", "test-filter", filter, null, "test-role");
    gateway.init(config);
    gateway.doFilter(request, response);
    gateway.destroy();
    assertThat((String) filter.role, is("test-role"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 28 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class TopologiesResource method getTopologies.

@GET
@Produces({ APPLICATION_JSON, APPLICATION_XML })
@Path(TOPOLOGIES_API_PATH)
public SimpleTopologyWrapper getTopologies() {
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    ArrayList<SimpleTopology> st = new ArrayList<SimpleTopology>();
    GatewayConfig conf = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    for (org.apache.knox.gateway.topology.Topology t : ts.getTopologies()) {
        st.add(getSimpleTopology(t, conf));
    }
    Collections.sort(st, new TopologyComparator());
    SimpleTopologyWrapper stw = new SimpleTopologyWrapper();
    for (SimpleTopology t : st) {
        stw.topologies.add(t);
    }
    return stw;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) ArrayList(java.util.ArrayList) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 29 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class DefaultServiceDefinitionRegistryTest method matchSimplePattern.

@Test
public void matchSimplePattern() throws Exception {
    DefaultServiceDefinitionRegistry registry = new DefaultServiceDefinitionRegistry();
    GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
    URL url = ClassLoader.getSystemResource("services");
    EasyMock.expect(config.getGatewayServicesDir()).andReturn(new File(url.getFile()).getAbsolutePath()).anyTimes();
    EasyMock.replay(config);
    registry.init(config, null);
    ServiceDefEntry entry = registry.getMatchingService("/foo/somepath");
    assertThat(entry.getRole(), is("FOO"));
    entry = registry.getMatchingService("/bar/?somepath");
    assertThat(entry.getRole(), is("BAR"));
}
Also used : DefaultServiceDefinitionRegistry(org.apache.knox.gateway.services.registry.impl.DefaultServiceDefinitionRegistry) File(java.io.File) URL(java.net.URL) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 30 with GatewayConfig

use of org.apache.knox.gateway.config.GatewayConfig in project knox by apache.

the class CryptoServiceTest method testConfigurableEncryptor.

@Test
public void testConfigurableEncryptor() throws Exception {
    GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(config.getAlgorithm()).andReturn("AES");
    EasyMock.expect(config.getPBEAlgorithm()).andReturn("PBKDF2WithHmacSHA1");
    EasyMock.expect(config.getSaltSize()).andReturn("16");
    EasyMock.expect(config.getIterationCount()).andReturn("65536");
    EasyMock.expect(config.getKeyLength()).andReturn("128");
    EasyMock.expect(config.getTransformation()).andReturn("AES/CBC/PKCS5Padding");
    EasyMock.replay(config);
    // password to create key - same Encryptor
    ConfigurableEncryptor aes = new ConfigurableEncryptor("Test");
    aes.init(config);
    EncryptionResult result = aes.encrypt("larry".getBytes("UTF8"));
    byte[] decrypted = aes.decrypt(result.salt, result.iv, result.cipher);
    assertEquals(new String(decrypted, "UTF8"), "larry");
    // password to create key - different Encryptor
    ConfigurableEncryptor aes2 = new ConfigurableEncryptor("Test");
    aes2.init(config);
    decrypted = aes2.decrypt(result.salt, result.iv, result.cipher);
    assertEquals(new String(decrypted, "UTF8"), "larry");
    // password to create key resolved from alias - same Encryptor
    ConfigurableEncryptor aes3 = new ConfigurableEncryptor(new String(as.getPasswordFromAliasForCluster("test", "encrypt_url")));
    aes3.init(config);
    result = aes3.encrypt("larry".getBytes("UTF8"));
    decrypted = aes3.decrypt(result.salt, result.iv, result.cipher);
    assertEquals(new String(decrypted, "UTF8"), "larry");
    // password to create key resolved from alias - different Encryptor
    ConfigurableEncryptor aes4 = new ConfigurableEncryptor(new String(as.getPasswordFromAliasForCluster("test", "encrypt_url")));
    aes4.init(config);
    decrypted = aes4.decrypt(result.salt, result.iv, result.cipher);
    assertEquals(new String(decrypted, "UTF8"), "larry");
}
Also used : GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) ConfigurableEncryptor(org.apache.knox.gateway.services.security.impl.ConfigurableEncryptor) Test(org.junit.Test)

Aggregations

GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)90 Test (org.junit.Test)67 File (java.io.File)31 HashMap (java.util.HashMap)24 GatewayConfigImpl (org.apache.knox.gateway.config.impl.GatewayConfigImpl)19 Topology (org.apache.knox.gateway.topology.Topology)17 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 AliasService (org.apache.knox.gateway.services.security.AliasService)12 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)11 IOException (java.io.IOException)9 Service (org.apache.knox.gateway.topology.Service)9 Document (org.w3c.dom.Document)9 ArrayList (java.util.ArrayList)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 MasterService (org.apache.knox.gateway.services.security.MasterService)8 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)8 KeystoreService (org.apache.knox.gateway.services.security.KeystoreService)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 List (java.util.List)6