Search in sources :

Example 1 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class ServiceRegistryFunctionsTest method setUp.

public void setUp(String username, Map<String, String> initParams) throws Exception {
    ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-nn-scheme://test-nn-host:411").anyTimes();
    EasyMock.expect(mockServiceRegistry.lookupServiceURL("test-cluster", "JOBTRACKER")).andReturn("test-jt-scheme://test-jt-host:511").anyTimes();
    GatewayServices mockGatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(mockGatewayServices.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(mockServiceRegistry).anyTimes();
    EasyMock.replay(mockServiceRegistry, mockGatewayServices);
    String descriptorUrl = getTestResource("rewrite.xml").toExternalForm();
    Log.setLog(new NoOpLogger());
    server = new ServletTester();
    server.setContextPath("/");
    server.getContext().addEventListener(new UrlRewriteServletContextListener());
    server.getContext().setInitParameter(UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl);
    server.getContext().setAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster");
    server.getContext().setAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices);
    FilterHolder setupFilter = server.addFilter(SetupFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    setupFilter.setFilter(new SetupFilter(username));
    FilterHolder rewriteFilter = server.addFilter(UrlRewriteServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
    if (initParams != null) {
        for (Map.Entry<String, String> entry : initParams.entrySet()) {
            rewriteFilter.setInitParameter(entry.getKey(), entry.getValue());
        }
    }
    rewriteFilter.setFilter(new UrlRewriteServletFilter());
    interactions = new ArrayQueue<MockInteraction>();
    ServletHolder servlet = server.addServlet(MockServlet.class, "/");
    servlet.setServlet(new MockServlet("mock-servlet", interactions));
    server.start();
    interaction = new MockInteraction();
    request = HttpTester.newRequest();
    response = null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) MockInteraction(org.apache.knox.test.mock.MockInteraction) ServletTester(org.eclipse.jetty.servlet.ServletTester) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) UrlRewriteServletContextListener(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener) MockServlet(org.apache.knox.test.mock.MockServlet) UrlRewriteServletFilter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter) NoOpLogger(org.apache.knox.test.log.NoOpLogger) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class GatewayServer method internalDeactivateTopology.

private synchronized void internalDeactivateTopology(Topology topology) {
    log.deactivatingTopology(topology.getName());
    String topoName = topology.getName();
    String topoPath = "/" + Urls.trimLeadingAndTrailingSlashJoin(config.getGatewayPath(), topoName);
    String topoPathSlash = topoPath + "/";
    ServiceRegistry sr = getGatewayServices().getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
    if (sr != null) {
        sr.removeClusterServices(topoName);
    }
    // Find all the deployed contexts we need to deactivate.
    List<WebAppContext> deactivate = new ArrayList<WebAppContext>();
    if (deployments != null) {
        for (WebAppContext app : deployments.values()) {
            String appPath = app.getContextPath();
            if (appPath.equals(topoPath) || appPath.startsWith(topoPathSlash)) {
                deactivate.add(app);
            }
        }
    }
    // Deactivate the required deployed contexts.
    for (WebAppContext context : deactivate) {
        String contextPath = context.getContextPath();
        deployments.remove(contextPath);
        contexts.removeHandler(context);
        try {
            context.stop();
        } catch (Exception e) {
            auditor.audit(Action.UNDEPLOY, topology.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
            log.failedToUndeployTopology(topology.getName(), e);
        }
    }
    deactivate.clear();
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ArrayList(java.util.ArrayList) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) DeploymentException(org.apache.knox.gateway.deploy.DeploymentException) URISyntaxException(java.net.URISyntaxException) KeyStoreException(java.security.KeyStoreException) ZipException(net.lingala.zip4j.exception.ZipException) ParseException(org.apache.commons.cli.ParseException) SAXException(org.xml.sax.SAXException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class GatewayWebsocketHandler method getMatchedBackendURL.

/**
 * This method looks at the context path and returns the backend websocket
 * url. If websocket url is found it is used as is, or we default to
 * ws://{host}:{port} which might or might not be right.
 *
 * @param
 * @return Websocket backend url
 */
private synchronized String getMatchedBackendURL(final String path) {
    final ServiceRegistry serviceRegistryService = services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE);
    final ServiceDefinitionRegistry serviceDefinitionService = services.getService(GatewayServices.SERVICE_DEFINITION_REGISTRY);
    /* Filter out the /cluster/topology to get the context we want */
    String[] pathInfo = path.split(REGEX_SPLIT_CONTEXT);
    final ServiceDefEntry entry = serviceDefinitionService.getMatchingService(pathInfo[1]);
    if (entry == null) {
        throw new RuntimeException(String.format("Cannot find service for the given path: %s", path));
    }
    /* Filter out /cluster/topology/service to get endpoint */
    String[] pathService = path.split(REGEX_SPLIT_SERVICE_PATH);
    final File servicesDir = new File(config.getGatewayServicesDir());
    final Set<ServiceDefinition> serviceDefs = ServiceDefinitionsLoader.getServiceDefinitions(servicesDir);
    /* URL used to connect to websocket backend */
    String backendURL = urlFromServiceDefinition(serviceDefs, serviceRegistryService, entry, path);
    StringBuffer backend = new StringBuffer();
    try {
        /* if we do not find websocket URL we default to HTTP */
        if (!StringUtils.containsAny(backendURL, WEBSOCKET_PROTOCOL_STRING, SECURE_WEBSOCKET_PROTOCOL_STRING)) {
            URL serviceUrl = new URL(backendURL);
            /* Use http host:port if ws url not configured */
            final String protocol = (serviceUrl.getProtocol() == "ws" || serviceUrl.getProtocol() == "wss") ? serviceUrl.getProtocol() : "ws";
            backend.append(protocol).append("://");
            backend.append(serviceUrl.getHost()).append(":");
            backend.append(serviceUrl.getPort()).append("/");
            backend.append(serviceUrl.getPath());
        } else {
            URI serviceUri = new URI(backendURL);
            backend.append(serviceUri);
            /* Avoid Zeppelin Regression - as this would require ambari changes and break current knox websocket use case*/
            if (!StringUtils.endsWith(backend.toString(), "/ws") && pathService.length > 0 && pathService[1] != null) {
                backend.append(pathService[1]);
            }
        }
        backendURL = backend.toString();
    } catch (MalformedURLException e) {
        LOG.badUrlError(e);
        throw new RuntimeException(e.toString());
    } catch (Exception e1) {
        LOG.failedCreatingWebSocket(e1);
        throw new RuntimeException(e1.toString());
    }
    return backendURL;
}
Also used : MalformedURLException(java.net.MalformedURLException) ServiceDefEntry(org.apache.knox.gateway.services.registry.ServiceDefEntry) URI(java.net.URI) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) File(java.io.File) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) ServiceDefinitionRegistry(org.apache.knox.gateway.services.registry.ServiceDefinitionRegistry)

Example 4 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralComplexTemplateValuesBugKnox394() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-with-complex-var.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${nameNode}/user/${user.name}/${examplesRoot}/apps/hive")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Example 5 with ServiceRegistry

use of org.apache.knox.gateway.services.registry.ServiceRegistry in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForValuesRelativeToServiceRegistry.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForValuesRelativeToServiceRegistry() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-relative.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    EasyMock.expect(servletRequest.getContentLength()).andReturn(-1).anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("test-scheme://test-host:42/workflow.xml")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Aggregations

ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)9 GatewayServices (org.apache.knox.gateway.services.GatewayServices)6 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 FilterConfig (javax.servlet.FilterConfig)3 ServletContext (javax.servlet.ServletContext)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)3 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)3 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)3 UrlRewriteServletEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment)3 UrlRewriteRequest (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest)3 MockServletInputStream (org.apache.knox.test.mock.MockServletInputStream)3 Test (org.junit.Test)3 Document (org.w3c.dom.Document)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UrlRewriteServletContextListener (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletContextListener)2 UrlRewriteServletFilter (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter)2