Search in sources :

Example 1 with PropertiesConfigurationStore

use of org.carapaceproxy.configstore.PropertiesConfigurationStore in project carapaceproxy by diennea.

the class BasicStandardEndpointMapperTest method testCustomAndDebbugingHeaders.

@Test
public void testCustomAndDebbugingHeaders() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    stubFor(get(urlEqualTo("/index2.html")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    stubFor(get(urlEqualTo("/index3.html")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder())) {
        Properties configuration = new Properties();
        configuration.put("backend.1.id", "b1");
        configuration.put("backend.1.host", "localhost");
        configuration.put("backend.1.port", backend1.port() + "");
        configuration.put("backend.1.enabled", "true");
        configuration.put("backend.2.id", "b2");
        configuration.put("backend.2.host", "localhost");
        configuration.put("backend.2.port", backend1.port() + "");
        configuration.put("backend.2.enabled", "true");
        configuration.put("director.1.id", "d1");
        configuration.put("director.1.backends", "b1");
        configuration.put("director.1.enabled", "true");
        configuration.put("director.2.id", "d2");
        configuration.put("director.2.backends", "b2");
        configuration.put("director.2.enabled", "true");
        configuration.put("listener.1.host", "0.0.0.0");
        configuration.put("listener.1.port", "1425");
        configuration.put("listener.1.ssl", "false");
        configuration.put("listener.1.enabled", "true");
        configuration.put("route.1.id", "r1");
        configuration.put("route.1.enabled", "true");
        configuration.put("route.1.match", "request.uri ~ \".*index\\.html\"");
        configuration.put("route.1.action", "addHeaders");
        configuration.put("action.1.id", "addHeaders");
        configuration.put("action.1.enabled", "true");
        configuration.put("action.1.type", "cache");
        configuration.put("action.1.director", "d1");
        configuration.put("action.1.headers", "h1,h2,h5,h6");
        configuration.put("route.2.id", "r2");
        configuration.put("route.2.enabled", "true");
        configuration.put("route.2.match", "request.uri ~ \".*index2\\.html\"");
        configuration.put("route.2.action", "addHeader2");
        configuration.put("action.2.id", "addHeader2");
        configuration.put("action.2.enabled", "true");
        configuration.put("action.2.type", "proxy");
        configuration.put("action.2.director", "d2");
        configuration.put("action.2.headers", "h2,h3,h4");
        configuration.put("route.3.id", "r3");
        configuration.put("route.3.enabled", "true");
        configuration.put("route.3.match", "request.uri ~ \".*index3\\.html\"");
        configuration.put("route.3.action", "serve-static");
        configuration.put("action.3.id", "serve-static");
        configuration.put("action.3.enabled", "true");
        configuration.put("action.3.type", "static");
        configuration.put("action.3.file", CLASSPATH_RESOURCE + "/test-static-page.html");
        configuration.put("action.3.code", "200");
        configuration.put("action.3.headers", "h1");
        // Custom headers
        configuration.put("header.1.id", "h1");
        configuration.put("header.1.name", "custom-header-1");
        configuration.put("header.1.value", "header-1-value; header-1-value2;header-1-value3");
        configuration.put("header.2.id", "h2");
        configuration.put("header.2.name", "custom-header-2");
        configuration.put("header.2.value", "header-2-value");
        configuration.put("header.3.id", "h3");
        // test headers merge (default add-mode)
        configuration.put("header.3.name", "custom-header-3");
        configuration.put("header.3.value", "header-3-overridden-value");
        configuration.put("header.4.id", "h4");
        // test headers merge
        configuration.put("header.4.name", "custom-header-3");
        configuration.put("header.4.value", "header-4-overridden-value");
        configuration.put("header.5.id", "h5");
        // test reset headers
        configuration.put("header.5.name", "Content-Type");
        configuration.put("header.5.value", "text/custom-text");
        configuration.put("header.5.mode", "set");
        configuration.put("header.6.id", "h6");
        // test remove headers
        configuration.put("header.6.name", "Transfer-Encoding");
        configuration.put("header.6.mode", "remove");
        // To enable debugging header "Mapping-Path"
        configuration.put("mapper.debug", "true");
        configuration.put("mapper.debug.name", "DebugHeaderCustomName");
        PropertiesConfigurationStore config = new PropertiesConfigurationStore(configuration);
        server.configureAtBoot(config);
        server.start();
        int port = server.getLocalPort();
        {
            URLConnection conn = new URL("http://localhost:" + port + "/index.html").openConnection();
            assertEquals("header-1-value; header-1-value2;header-1-value3", conn.getHeaderField("custom-header-1"));
            assertEquals("header-2-value", conn.getHeaderField("custom-header-2"));
            // header mode-set
            assertEquals("text/custom-text", conn.getHeaderField("Content-Type"));
            assertFalse(conn.getHeaderFields().toString().contains("text/html"));
            // header mode-remove: for this action doesn't exist
            assertNull(conn.getHeaderField("Transfer-Encoding"));
            // debugging header "Routing-Path"
            assertEquals("r1;addHeaders;d1;b1", conn.getHeaderField("DebugHeaderCustomName"));
        }
        {
            URLConnection conn = new URL("http://localhost:" + port + "/index2.html").openConnection();
            assertNull(conn.getHeaderField("custom-header-1"));
            assertEquals("header-2-value", conn.getHeaderField("custom-header-2"));
            // in this action is text/html as normal
            assertTrue(conn.getHeaderFields().toString().contains("text/html"));
            // custom-header-3 values have been merged (default mode-add)
            assertTrue(conn.getHeaderFields().toString().contains("header-4-overridden-value"));
            assertTrue(conn.getHeaderFields().toString().contains("header-3-overridden-value"));
            // in this action still exists
            assertNotNull(conn.getHeaderField("Transfer-Encoding"));
            // debugging header "Routing-Path"
            assertEquals("r2;addHeader2;d2;b2", conn.getHeaderField("DebugHeaderCustomName"));
        }
        {
            URLConnection conn = new URL("http://localhost:" + port + "/index3.html").openConnection();
            assertEquals("header-1-value; header-1-value2;header-1-value3", conn.getHeaderField("custom-header-1"));
        }
    }
}
Also used : HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) Properties(java.util.Properties) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL) Test(org.junit.Test)

Example 2 with PropertiesConfigurationStore

use of org.carapaceproxy.configstore.PropertiesConfigurationStore in project carapaceproxy by diennea.

the class BasicStandardEndpointMapperTest method testRouteErrors.

@Test
public void testRouteErrors() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder())) {
        Properties configuration = new Properties();
        configuration.put("listener.1.host", "0.0.0.0");
        configuration.put("listener.1.port", "1425");
        configuration.put("listener.1.ssl", "false");
        configuration.put("listener.1.enabled", "true");
        configuration.put("backend.1.id", "backend");
        configuration.put("backend.1.host", "localhost");
        configuration.put("backend.1.port", backend1.port() + "");
        configuration.put("backend.1.enabled", "true");
        configuration.put("director.1.id", "director");
        configuration.put("director.1.backends", "backend");
        configuration.put("director.1.enabled", "true");
        // unreachable backend -> expected Internal Error
        configuration.put("backend.2.id", "backend-down");
        configuration.put("backend.2.host", "localhost-down");
        configuration.put("backend.2.port", backend1.port() + "");
        configuration.put("backend.2.enabled", "true");
        configuration.put("director.2.id", "director-down");
        configuration.put("director.2.backends", "backend-down");
        configuration.put("director.2.enabled", "true");
        // custom headers
        configuration.put("header.1.id", "h-custom-error");
        configuration.put("header.1.name", "h-custom-error");
        configuration.put("header.1.value", "h-custom-error-value; h-custom-error-value2;h-custom-error-value3");
        configuration.put("header.2.id", "h-working-one");
        configuration.put("header.2.name", "h-working-one");
        configuration.put("header.2.value", "h-working-one-value; h-working-one-value2;h-working-one-value3");
        configuration.put("header.3.id", "h-custom-global-error");
        configuration.put("header.3.name", "h-custom-global-error");
        configuration.put("header.3.value", "h-custom-global-error-value; h-custom-global-error-value2;h-custom-global-error-value3");
        configuration.put("action.0.id", "to-backend-down");
        configuration.put("action.0.enabled", "true");
        configuration.put("action.0.type", "cache");
        configuration.put("action.0.director", "director-down");
        // actions
        configuration.put("action.1.id", "custom-error");
        configuration.put("action.1.enabled", "true");
        configuration.put("action.1.type", "static");
        configuration.put("action.1.file", CLASSPATH_RESOURCE + "/test-static-page.html");
        configuration.put("action.1.code", "555");
        configuration.put("action.1.headers", "h-custom-error");
        configuration.put("action.2.id", "working-one");
        configuration.put("action.2.enabled", "true");
        configuration.put("action.2.type", "cache");
        configuration.put("action.2.director", "director");
        configuration.put("action.2.headers", "h-working-one");
        // global-custom error (Not Found)
        configuration.put("action.3.id", "custom-global-error");
        configuration.put("action.3.enabled", "true");
        configuration.put("action.3.type", "static");
        configuration.put("action.3.file", CLASSPATH_RESOURCE + "/test-static-page.html");
        configuration.put("action.3.code", "444");
        configuration.put("action.3.headers", "h-custom-global-error");
        configuration.put("default.action.notfound", "custom-global-error");
        // route-custom error (Internal Errror)
        configuration.put("route.1.id", "route-custom-error");
        configuration.put("route.1.enabled", "true");
        configuration.put("route.1.match", "request.uri ~ \".*custom-error.*\"");
        configuration.put("route.1.action", "to-backend-down");
        configuration.put("route.1.erroraction", "custom-error");
        // working-one
        configuration.put("route.2.id", "working-one");
        configuration.put("route.2.enabled", "true");
        configuration.put("route.2.match", "request.uri ~ \".*index.html.*\"");
        configuration.put("route.2.action", "working-one");
        PropertiesConfigurationStore config = new PropertiesConfigurationStore(configuration);
        BackendHealthManager bhMan = mock(BackendHealthManager.class);
        when(bhMan.isAvailable(eq("localhost:" + backend1.port()))).thenReturn(true);
        // simulate unreachable backend -> expected 500 error
        when(bhMan.isAvailable(eq("localhost-down:" + backend1.port()))).thenReturn(false);
        server.setBackendHealthManager(bhMan);
        server.configureAtBoot(config);
        server.start();
        Thread.sleep(5_000);
        // route-custom error (Internal Errror)
        {
            HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:" + server.getLocalPort() + "/custom-error.html").openConnection();
            assertEquals("h-custom-error-value; h-custom-error-value2;h-custom-error-value3", conn.getHeaderField("h-custom-error"));
            assertEquals(555, conn.getResponseCode());
        }
        // working one
        {
            HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:" + server.getLocalPort() + "/index.html").openConnection();
            assertEquals("h-working-one-value; h-working-one-value2;h-working-one-value3", conn.getHeaderField("h-working-one"));
            assertEquals(200, conn.getResponseCode());
        }
        // global-custom error (Not Found)
        {
            HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:" + server.getLocalPort() + "/index2.html").openConnection();
            assertEquals("h-custom-global-error-value; h-custom-global-error-value2;h-custom-global-error-value3", conn.getHeaderField("h-custom-global-error"));
            assertEquals(444, conn.getResponseCode());
        }
    }
}
Also used : HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) HttpURLConnection(java.net.HttpURLConnection) BackendHealthManager(org.carapaceproxy.server.backends.BackendHealthManager) Properties(java.util.Properties) URL(java.net.URL) Test(org.junit.Test)

Example 3 with PropertiesConfigurationStore

use of org.carapaceproxy.configstore.PropertiesConfigurationStore in project carapaceproxy by diennea.

the class BasicStandardEndpointMapperTest method testServeACMEChallengeToken.

@Test
public void testServeACMEChallengeToken() throws Exception {
    try (HttpProxyServer server = new HttpProxyServer(null, tmpDir.newFolder())) {
        final String tokenName = "test-token";
        final String tokenData = "test-token-data-content";
        DynamicCertificatesManager dynamicCertificateManager = mock(DynamicCertificatesManager.class);
        when(dynamicCertificateManager.getChallengeToken(matches(tokenName))).thenReturn(tokenData);
        server.setDynamicCertificatesManager(dynamicCertificateManager);
        Properties configuration = new Properties();
        configuration.put("backend.1.id", "foo");
        configuration.put("backend.1.host", "localhost");
        configuration.put("backend.1.port", backend1.port() + "");
        configuration.put("backend.1.enabled", "true");
        configuration.put("director.1.id", "*");
        configuration.put("director.1.backends", "*");
        configuration.put("director.1.enabled", "true");
        configuration.put("listener.1.host", "0.0.0.0");
        configuration.put("listener.1.port", "1425");
        configuration.put("listener.1.ssl", "false");
        configuration.put("listener.1.enabled", "true");
        configuration.put("route.10.id", "default");
        configuration.put("route.10.enabled", "true");
        configuration.put("route.10.match", "all");
        configuration.put("route.10.action", "proxy-all");
        configuration.put("action.1.id", "serve-static");
        configuration.put("action.1.enabled", "true");
        configuration.put("action.1.type", "static");
        configuration.put("action.1.file", CLASSPATH_RESOURCE + "/test-static-page.html");
        configuration.put("action.1.code", "200");
        configuration.put("route.8.id", "static-page");
        configuration.put("route.8.enabled", "true");
        configuration.put("route.8.match", "request.uri ~ \".*index.*\"");
        configuration.put("route.8.action", "serve-static");
        PropertiesConfigurationStore config = new PropertiesConfigurationStore(configuration);
        server.configureAtBoot(config);
        server.start();
        Thread.sleep(5_000);
        // Test existent token
        String url = "http://localhost:" + server.getLocalPort() + "/.well-known/acme-challenge/" + tokenName;
        String s = IOUtils.toString(new URL(url).toURI(), "utf-8");
        assertEquals(tokenData, s);
        // Test not existent token
        try {
            url = "http://localhost:" + server.getLocalPort() + "/.well-known/acme-challenge/not-existent-token";
            IOUtils.toString(new URL(url).toURI(), "utf-8");
            fail();
        } catch (Throwable t) {
            assertTrue(t instanceof FileNotFoundException);
        }
    }
}
Also used : HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) DynamicCertificatesManager(org.carapaceproxy.server.certificates.DynamicCertificatesManager) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) FileNotFoundException(java.io.FileNotFoundException) Properties(java.util.Properties) URL(java.net.URL) Test(org.junit.Test)

Example 4 with PropertiesConfigurationStore

use of org.carapaceproxy.configstore.PropertiesConfigurationStore in project carapaceproxy by diennea.

the class ForceBackendTest method test.

@Test
public void test() throws Exception {
    stubFor(get(urlEqualTo("/index.html?thedirector=director-2")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    stubFor(get(urlEqualTo("/index.html?thebackend=backend-b")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/html").withBody("it <b>works</b> !!")));
    int backendPort = backend1.port();
    StandardEndpointMapper mapper = new StandardEndpointMapper();
    Properties properties = new Properties();
    properties.put("mapper.forcedirector.parameter", "thedirector");
    properties.put("mapper.forcebackend.parameter", "thebackend");
    mapper.configure(new PropertiesConfigurationStore(properties));
    assertEquals("thedirector", mapper.getForceDirectorParameter());
    assertEquals("thebackend", mapper.getForceBackendParameter());
    mapper.addBackend(new BackendConfiguration("backend-a", "localhost", backendPort, "/"));
    mapper.addBackend(new BackendConfiguration("backend-b", "localhost", backendPort, "/"));
    mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a"));
    mapper.addDirector(new DirectorConfiguration("director-2").addBackend("backend-b"));
    mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1));
    mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*")));
    try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
        server.start();
        int port = server.getLocalPort();
        {
            // proxy on director 2
            String s = IOUtils.toString(new URL("http://localhost:" + port + "/index.html?thedirector=director-2").toURI(), "utf-8");
            assertEquals("it <b>works</b> !!", s);
        }
        {
            // proxy on backend 2
            String s = IOUtils.toString(new URL("http://localhost:" + port + "/index.html?thebackend=backend-b").toURI(), "utf-8");
            assertEquals("it <b>works</b> !!", s);
        }
    }
}
Also used : HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) RouteConfiguration(org.carapaceproxy.server.config.RouteConfiguration) DirectorConfiguration(org.carapaceproxy.server.config.DirectorConfiguration) RegexpRequestMatcher(org.carapaceproxy.server.mapper.requestmatcher.RegexpRequestMatcher) Properties(java.util.Properties) ActionConfiguration(org.carapaceproxy.server.config.ActionConfiguration) BackendConfiguration(org.carapaceproxy.server.config.BackendConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 5 with PropertiesConfigurationStore

use of org.carapaceproxy.configstore.PropertiesConfigurationStore in project carapaceproxy by diennea.

the class UnreachableBackendTest method testNonHttpResponseThenClose.

@Test
public void testNonHttpResponseThenClose() throws Exception {
    stubFor(get(urlEqualTo("/index.html")).willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)));
    int dummyport = wireMockRule.port();
    TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache);
    EndpointKey key = new EndpointKey("localhost", dummyport);
    try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) {
        Properties properties = new Properties();
        server.configureAtBoot(new PropertiesConfigurationStore(properties));
        server.addListener(new NetworkListenerConfiguration("localhost", 0));
        server.setMapper(mapper);
        server.start();
        int port = server.getLocalPort();
        try (RawHttpClient client = new RawHttpClient("localhost", port)) {
            RawHttpClient.HttpResponse resp = client.executeRequest("GET /index.html HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
            String s = resp.toString();
            System.out.println("s:" + s);
            assertEquals("HTTP/1.1 500 Internal Server Error\r\n", resp.getStatusLine());
            assertEquals("<html>\n" + "    <body>\n" + "        An internal error occurred\n" + "    </body>        \n" + "</html>\n", resp.getBodyString());
        }
        assertTrue(server.getBackendHealthManager().isAvailable(key.getHostPort()));
        assertThat((int) ProxyRequestsManager.PENDING_REQUESTS_GAUGE.get(), is(0));
    }
}
Also used : RawHttpClient(org.carapaceproxy.utils.RawHttpClient) TestEndpointMapper(org.carapaceproxy.utils.TestEndpointMapper) HttpProxyServer(org.carapaceproxy.core.HttpProxyServer) PropertiesConfigurationStore(org.carapaceproxy.configstore.PropertiesConfigurationStore) EndpointKey(org.carapaceproxy.client.EndpointKey) Properties(java.util.Properties) NetworkListenerConfiguration(org.carapaceproxy.server.config.NetworkListenerConfiguration) Test(org.junit.Test)

Aggregations

PropertiesConfigurationStore (org.carapaceproxy.configstore.PropertiesConfigurationStore)30 Properties (java.util.Properties)26 HttpProxyServer (org.carapaceproxy.core.HttpProxyServer)23 Test (org.junit.Test)21 URL (java.net.URL)9 ConfigurationStore (org.carapaceproxy.configstore.ConfigurationStore)9 File (java.io.File)7 ConfigurationNotValidException (org.carapaceproxy.server.config.ConfigurationNotValidException)6 IOException (java.io.IOException)4 EndpointKey (org.carapaceproxy.client.EndpointKey)4 NullGroupMembershipHandler (org.carapaceproxy.cluster.impl.NullGroupMembershipHandler)4 CertificateData (org.carapaceproxy.configstore.CertificateData)4 NetworkListenerConfiguration (org.carapaceproxy.server.config.NetworkListenerConfiguration)4 UserRealm (org.carapaceproxy.user.UserRealm)4 TestEndpointMapper (org.carapaceproxy.utils.TestEndpointMapper)4 FileInputStream (java.io.FileInputStream)3 HttpURLConnection (java.net.HttpURLConnection)3 KeyPair (java.security.KeyPair)3 X509Certificate (java.security.cert.X509Certificate)3 Parameters (junitparams.Parameters)3