Search in sources :

Example 21 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayServicesContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    GatewayServices gs = GatewayServer.getGatewayServices();
    sce.getServletContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, gs);
    String topologyName = (String) sce.getServletContext().getAttribute("org.apache.knox.gateway.gateway.cluster");
    TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
    Topology topology = getTopology(ts, topologyName);
    sce.getServletContext().setAttribute("org.apache.knox.gateway.topology", topology);
}
Also used : Topology(org.apache.knox.gateway.topology.Topology) TopologyService(org.apache.knox.gateway.services.topology.TopologyService)

Example 22 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class ServiceTestResource method serviceTest.

@GET
@Produces({ APPLICATION_XML, APPLICATION_JSON })
public ServiceTestWrapper serviceTest(@QueryParam("username") String username, @QueryParam("password") String password) {
    List<ServiceTest> tests = new ArrayList<>();
    List<String> messages = new ArrayList<>();
    String authString;
    GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    SSLContext ctx = null;
    CloseableHttpClient client;
    String id = getTopologyName();
    Topology topology = getTopology(id);
    // Create Authorization String
    if (username != null && password != null) {
        authString = "Basic " + Base64.encodeAsString((username + ":" + password).getBytes());
    } else if (request.getHeader("Authorization") != null) {
        authString = request.getHeader("Authorization");
    } else {
        authString = null;
    }
    // Attempt to build SSL context for HTTP client.
    try {
        ctx = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
    } catch (Exception e) {
        messages.add(e.getMessage());
    }
    // Initialize the HTTP client
    if (ctx == null) {
        client = HttpClients.createDefault();
    } else {
        client = HttpClients.custom().setSslcontext(ctx).build();
    }
    if (topology != null) {
        for (Service s : topology.getServices()) {
            List<String> urls = getServiceTestURLs(config, s.getRole(), topology);
            // Make sure we handle a case where no URLs are found.
            if (urls.size() <= 0) {
                ServiceTest test = new ServiceTest(s);
                test.setMessage("This service did not contain any test URLs");
            }
            for (String url : urls) {
                HttpGet req = new HttpGet();
                ServiceTest test = new ServiceTest(s, url);
                if (authString != null) {
                    req.setHeader("Authorization", authString);
                } else {
                    messages.add("No credentials provided. Expect HTTP 401 responses.");
                }
                try {
                    req.setURI(new URIBuilder(url).build());
                    CloseableHttpResponse res = client.execute(req);
                    String contentLength = "Content-Length:" + res.getEntity().getContentLength();
                    String contentType = (res.getEntity().getContentType() != null) ? res.getEntity().getContentType().toString() : "No-contenttype";
                    test.setResponseContent(contentLength + "," + contentType);
                    test.setHttpCode(res.getStatusLine().getStatusCode());
                    res.close();
                } catch (IOException e) {
                    messages.add("Exception: " + e.getMessage());
                    test.setMessage(e.getMessage());
                } catch (URISyntaxException e) {
                    test.setMessage(e.getMessage());
                } catch (Exception e) {
                    messages.add(e.getMessage());
                    test.setMessage(e.getMessage());
                } finally {
                    req.releaseConnection();
                    tests.add(test);
                }
            }
        }
    } else {
        messages.add("Topology " + id + " not found");
    }
    try {
        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ServiceTestWrapper stw = new ServiceTestWrapper();
    stw.setTests(tests);
    stw.setMessages(messages);
    return stw;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) Service(org.apache.knox.gateway.topology.Service) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) SSLContext(javax.net.ssl.SSLContext) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 23 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayAdminTopologyFuncTest method testDeployTopology.

@Test(timeout = TestUtils.LONG_TIMEOUT)
public void testDeployTopology() throws Exception {
    LOG_ENTER();
    Topology testTopology = createTestTopology();
    String user = "guest";
    String password = "guest-password";
    String url = gatewayUrl + "/" + testTopology.getName() + "/test-service-path/test-service-resource";
    GatewayServices srvs = GatewayServer.getGatewayServices();
    TopologyService ts = srvs.getService(GatewayServices.TOPOLOGY_SERVICE);
    try {
        ts.stopMonitor();
        assertThat(testTopology, not(nullValue()));
        assertThat(testTopology.getName(), is("test-topology"));
        given().auth().preemptive().basic("admin", "admin-password").header("Accept", MediaType.APPLICATION_JSON).then().statusCode(HttpStatus.SC_OK).body(containsString("ServerVersion")).when().get(gatewayUrl + "/admin/api/v1/version");
        given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_NOT_FOUND).when().get(url);
        ts.deployTopology(testTopology);
        given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_OK).contentType("text/plain").body(is("test-service-response")).when().get(url).getBody();
        ts.deleteTopology(testTopology);
        given().auth().preemptive().basic(user, password).then().statusCode(HttpStatus.SC_NOT_FOUND).when().get(url);
    } finally {
        ts.startMonitor();
    }
    LOG_EXIT();
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) Topology(org.apache.knox.gateway.topology.Topology) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Test(org.junit.Test)

Example 24 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayAdminTopologyFuncTest method createTestTopology.

private Topology createTestTopology() {
    Topology topology = new Topology();
    topology.setName("test-topology");
    try {
        topology.setUri(new URI(gatewayUrl + "/" + topology.getName()));
    } catch (URISyntaxException ex) {
        assertThat(topology.getUri(), not(nullValue()));
    }
    Provider identityProvider = new Provider();
    identityProvider.setName("Default");
    identityProvider.setRole("identity-assertion");
    identityProvider.setEnabled(true);
    Provider AuthenicationProvider = new Provider();
    AuthenicationProvider.setName("ShiroProvider");
    AuthenicationProvider.setRole("authentication");
    AuthenicationProvider.setEnabled(true);
    Param ldapMain = new Param();
    ldapMain.setName("main.ldapRealm");
    ldapMain.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapRealm");
    Param ldapGroupContextFactory = new Param();
    ldapGroupContextFactory.setName("main.ldapGroupContextFactory");
    ldapGroupContextFactory.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapContextFactory");
    Param ldapRealmContext = new Param();
    ldapRealmContext.setName("main.ldapRealm.contextFactory");
    ldapRealmContext.setValue("$ldapGroupContextFactory");
    Param ldapURL = new Param();
    ldapURL.setName("main.ldapRealm.contextFactory.url");
    ldapURL.setValue(driver.getLdapUrl());
    Param ldapUserTemplate = new Param();
    ldapUserTemplate.setName("main.ldapRealm.userDnTemplate");
    ldapUserTemplate.setValue("uid={0},ou=people,dc=hadoop,dc=apache,dc=org");
    Param authcBasic = new Param();
    authcBasic.setName("urls./**");
    authcBasic.setValue("authcBasic");
    AuthenicationProvider.addParam(ldapGroupContextFactory);
    AuthenicationProvider.addParam(ldapMain);
    AuthenicationProvider.addParam(ldapRealmContext);
    AuthenicationProvider.addParam(ldapURL);
    AuthenicationProvider.addParam(ldapUserTemplate);
    AuthenicationProvider.addParam(authcBasic);
    Service testService = new Service();
    testService.setRole("test-service-role");
    topology.addProvider(AuthenicationProvider);
    topology.addProvider(identityProvider);
    topology.addService(testService);
    topology.setTimestamp(System.nanoTime());
    return topology;
}
Also used : Param(org.apache.knox.gateway.topology.Param) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Provider(org.apache.knox.gateway.topology.Provider)

Example 25 with Topology

use of org.apache.knox.gateway.topology.Topology in project knox by apache.

the class GatewayAdminTopologyFuncTest method testDeleteTopology.

@Test(timeout = TestUtils.LONG_TIMEOUT)
public void testDeleteTopology() throws ClassNotFoundException {
    LOG_ENTER();
    Topology test = createTestTopology();
    String username = "admin";
    String password = "admin-password";
    String url = clusterUrl + "/api/v1/topologies/" + test.getName();
    GatewayServices gs = GatewayServer.getGatewayServices();
    TopologyService ts = gs.getService(GatewayServices.TOPOLOGY_SERVICE);
    ts.deployTopology(test);
    given().auth().preemptive().basic(username, password).header("Accept", MediaType.APPLICATION_JSON).then().statusCode(HttpStatus.SC_OK).contentType(MediaType.APPLICATION_JSON).when().get(url);
    given().auth().preemptive().basic(username, password).then().statusCode(HttpStatus.SC_OK).contentType(MediaType.APPLICATION_JSON).when().delete(url);
    given().auth().preemptive().basic(username, password).then().statusCode(HttpStatus.SC_NO_CONTENT).when().get(url);
    LOG_EXIT();
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) Topology(org.apache.knox.gateway.topology.Topology) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Test(org.junit.Test)

Aggregations

Topology (org.apache.knox.gateway.topology.Topology)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)23 Service (org.apache.knox.gateway.topology.Service)22 File (java.io.File)20 Provider (org.apache.knox.gateway.topology.Provider)20 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)17 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)10 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)9 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 IOException (java.io.IOException)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 URL (java.net.URL)7 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)7 Application (org.apache.knox.gateway.topology.Application)7 Param (org.apache.knox.gateway.topology.Param)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 Document (org.w3c.dom.Document)7 Digester (org.apache.commons.digester3.Digester)6