Search in sources :

Example 11 with GatewayConfig

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

the class SimpleDescriptorHandlerTest method testSimpleDescriptorHandler.

/**
 * KNOX-1006
 *
 * N.B. This test depends on the PropertiesFileServiceDiscovery extension being configured:
 *             org.apache.knox.gateway.topology.discovery.test.extension.PropertiesFileServiceDiscovery
 */
@Test
public void testSimpleDescriptorHandler() throws Exception {
    final String type = "PROPERTIES_FILE";
    final String clusterName = "dummy";
    // Create a properties file to be the source of service discovery details for this test
    final File discoveryConfig = File.createTempFile(getClass().getName() + "_discovery-config", ".properties");
    final String address = discoveryConfig.getAbsolutePath();
    final Properties DISCOVERY_PROPERTIES = new Properties();
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".name", clusterName);
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".NAMENODE", "hdfs://namenodehost:8020");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".JOBTRACKER", "rpc://jobtrackerhostname:8050");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".WEBHDFS", "http://webhdfshost:1234");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".WEBHCAT", "http://webhcathost:50111/templeton");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".OOZIE", "http://ooziehost:11000/oozie");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".WEBHBASE", "http://webhbasehost:1234");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".HIVE", "http://hivehostname:10001/clipath");
    DISCOVERY_PROPERTIES.setProperty(clusterName + ".RESOURCEMANAGER", "http://remanhost:8088/ws");
    try {
        DISCOVERY_PROPERTIES.store(new FileOutputStream(discoveryConfig), null);
    } catch (FileNotFoundException e) {
        fail(e.getMessage());
    }
    final Map<String, List<String>> serviceURLs = new HashMap<>();
    serviceURLs.put("NAMENODE", null);
    serviceURLs.put("JOBTRACKER", null);
    serviceURLs.put("WEBHDFS", null);
    serviceURLs.put("WEBHCAT", null);
    serviceURLs.put("OOZIE", null);
    serviceURLs.put("WEBHBASE", null);
    serviceURLs.put("HIVE", null);
    serviceURLs.put("RESOURCEMANAGER", null);
    serviceURLs.put("AMBARIUI", Collections.singletonList("http://c6401.ambari.apache.org:8080"));
    serviceURLs.put("KNOXSSO", null);
    // Write the externalized provider config to a temp file
    File providerConfig = new File(System.getProperty("java.io.tmpdir"), "ambari-cluster-policy.xml");
    FileUtils.write(providerConfig, TEST_PROVIDER_CONFIG);
    File topologyFile = null;
    try {
        File destDir = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
        Map<String, Map<String, String>> serviceParameters = new HashMap<>();
        Map<String, String> knoxssoParams = new HashMap<>();
        knoxssoParams.put("knoxsso.cookie.secure.only", "true");
        knoxssoParams.put("knoxsso.token.ttl", "100000");
        serviceParameters.put("KNOXSSO", knoxssoParams);
        GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.replay(gc);
        // Mock out the simple descriptor
        SimpleDescriptor testDescriptor = EasyMock.createNiceMock(SimpleDescriptor.class);
        EasyMock.expect(testDescriptor.getName()).andReturn("mysimpledescriptor").anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryAddress()).andReturn(address).anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryType()).andReturn(type).anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryUser()).andReturn(null).anyTimes();
        EasyMock.expect(testDescriptor.getProviderConfig()).andReturn(providerConfig.getAbsolutePath()).anyTimes();
        EasyMock.expect(testDescriptor.getClusterName()).andReturn(clusterName).anyTimes();
        List<SimpleDescriptor.Service> serviceMocks = new ArrayList<>();
        for (String serviceName : serviceURLs.keySet()) {
            SimpleDescriptor.Service svc = EasyMock.createNiceMock(SimpleDescriptor.Service.class);
            EasyMock.expect(svc.getName()).andReturn(serviceName).anyTimes();
            EasyMock.expect(svc.getVersion()).andReturn("WEBHDFS".equals(serviceName) ? "2.4.0" : null).anyTimes();
            EasyMock.expect(svc.getURLs()).andReturn(serviceURLs.get(serviceName)).anyTimes();
            EasyMock.expect(svc.getParams()).andReturn(serviceParameters.get(serviceName)).anyTimes();
            EasyMock.replay(svc);
            serviceMocks.add(svc);
        }
        EasyMock.expect(testDescriptor.getServices()).andReturn(serviceMocks).anyTimes();
        EasyMock.replay(testDescriptor);
        // Invoke the simple descriptor handler
        Map<String, File> files = SimpleDescriptorHandler.handle(gc, testDescriptor, // simple desc co-located with provider config
        providerConfig.getParentFile(), destDir);
        topologyFile = files.get("topology");
        // Validate the resulting topology descriptor
        assertTrue(topologyFile.exists());
        // Validate the topology descriptor's correctness
        TopologyValidator validator = new TopologyValidator(topologyFile.getAbsolutePath());
        if (!validator.validateTopology()) {
            throw new SAXException(validator.getErrorString());
        }
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        // Parse the topology descriptor
        Document topologyXml = XmlUtils.readXml(topologyFile);
        // KNOX-1105 Mark generated topology files
        assertThat("Expected the \"generated\" marker element in the topology XML, with value of \"true\".", topologyXml, hasXPath("/topology/generated", is("true")));
        // Validate the provider configuration
        Node gatewayNode = (Node) xpath.compile("/topology/gateway").evaluate(topologyXml, XPathConstants.NODE);
        ProviderConfiguration testProviderConfiguration = ProviderConfigurationParser.parseXML(new ByteArrayInputStream(TEST_PROVIDER_CONFIG.getBytes()));
        validateGeneratedProviderConfiguration(testProviderConfiguration, gatewayNode);
        // Validate the service declarations
        Map<String, List<String>> topologyServiceURLs = new HashMap<>();
        NodeList serviceNodes = (NodeList) xpath.compile("/topology/service").evaluate(topologyXml, XPathConstants.NODESET);
        for (int serviceNodeIndex = 0; serviceNodeIndex < serviceNodes.getLength(); serviceNodeIndex++) {
            Node serviceNode = serviceNodes.item(serviceNodeIndex);
            // Validate the role
            Node roleNode = (Node) xpath.compile("role/text()").evaluate(serviceNode, XPathConstants.NODE);
            assertNotNull(roleNode);
            String role = roleNode.getNodeValue();
            // Validate the explicit version for the WEBHDFS service
            if ("WEBHDFS".equals(role)) {
                Node versionNode = (Node) xpath.compile("version/text()").evaluate(serviceNode, XPathConstants.NODE);
                assertNotNull(versionNode);
                String version = versionNode.getNodeValue();
                assertEquals("2.4.0", version);
            }
            // Validate the URLs
            NodeList urlNodes = (NodeList) xpath.compile("url/text()").evaluate(serviceNode, XPathConstants.NODESET);
            for (int urlNodeIndex = 0; urlNodeIndex < urlNodes.getLength(); urlNodeIndex++) {
                Node urlNode = urlNodes.item(urlNodeIndex);
                assertNotNull(urlNode);
                String url = urlNode.getNodeValue();
                // If the service should have a URL (some don't require it)
                if (serviceURLs.containsKey(role)) {
                    assertNotNull("Declared service should have a URL.", url);
                    if (!topologyServiceURLs.containsKey(role)) {
                        topologyServiceURLs.put(role, new ArrayList<>());
                    }
                    // Add it for validation later
                    topologyServiceURLs.get(role).add(url);
                }
            }
            // If params were declared in the descriptor, then validate them in the resulting topology file
            Map<String, String> params = serviceParameters.get(role);
            if (params != null) {
                NodeList paramNodes = (NodeList) xpath.compile("param").evaluate(serviceNode, XPathConstants.NODESET);
                for (int paramNodeIndex = 0; paramNodeIndex < paramNodes.getLength(); paramNodeIndex++) {
                    Node paramNode = paramNodes.item(paramNodeIndex);
                    String paramName = (String) xpath.compile("name/text()").evaluate(paramNode, XPathConstants.STRING);
                    String paramValue = (String) xpath.compile("value/text()").evaluate(paramNode, XPathConstants.STRING);
                    assertTrue(params.keySet().contains(paramName));
                    assertEquals(params.get(paramName), paramValue);
                }
            }
        }
        assertEquals("Unexpected number of service declarations.", (serviceURLs.size() - 1), topologyServiceURLs.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        providerConfig.delete();
        discoveryConfig.delete();
        if (topologyFile != null) {
            topologyFile.delete();
        }
    }
}
Also used : HashMap(java.util.HashMap) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) XPath(javax.xml.xpath.XPath) Matchers.hasXPath(org.hamcrest.Matchers.hasXPath) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) TopologyValidator(org.apache.knox.gateway.topology.validation.TopologyValidator) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with GatewayConfig

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

the class SimpleDescriptorHandlerTest method testInvalidServiceURLFromDiscovery.

/**
 * KNOX-1006
 *
 * Verify the behavior of the SimpleDescriptorHandler when service discovery fails to produce a valid URL for
 * a service.
 *
 * N.B. This test depends on the PropertiesFileServiceDiscovery extension being configured:
 *             org.apache.knox.gateway.topology.discovery.test.extension.PropertiesFileServiceDiscovery
 */
@Test
public void testInvalidServiceURLFromDiscovery() throws Exception {
    final String CLUSTER_NAME = "myproperties";
    // Configure the PropertiesFile Service Discovery implementation for this test
    final String DEFAULT_VALID_SERVICE_URL = "http://localhost:9999/thiswillwork";
    Properties serviceDiscoverySourceProps = new Properties();
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".NAMENODE", DEFAULT_VALID_SERVICE_URL.replace("http", "hdfs"));
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".JOBTRACKER", DEFAULT_VALID_SERVICE_URL.replace("http", "rpc"));
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".WEBHDFS", DEFAULT_VALID_SERVICE_URL);
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".WEBHCAT", DEFAULT_VALID_SERVICE_URL);
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".OOZIE", DEFAULT_VALID_SERVICE_URL);
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".WEBHBASE", DEFAULT_VALID_SERVICE_URL);
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".HIVE", "{SCHEME}://localhost:10000/");
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".RESOURCEMANAGER", DEFAULT_VALID_SERVICE_URL);
    serviceDiscoverySourceProps.setProperty(CLUSTER_NAME + ".AMBARIUI", DEFAULT_VALID_SERVICE_URL);
    File serviceDiscoverySource = File.createTempFile("service-discovery", ".properties");
    serviceDiscoverySourceProps.store(new FileOutputStream(serviceDiscoverySource), "Test Service Discovery Source");
    // Prepare a mock SimpleDescriptor
    final String type = "PROPERTIES_FILE";
    final String address = serviceDiscoverySource.getAbsolutePath();
    final Map<String, List<String>> serviceURLs = new HashMap<>();
    serviceURLs.put("NAMENODE", null);
    serviceURLs.put("JOBTRACKER", null);
    serviceURLs.put("WEBHDFS", null);
    serviceURLs.put("WEBHCAT", null);
    serviceURLs.put("OOZIE", null);
    serviceURLs.put("WEBHBASE", null);
    serviceURLs.put("HIVE", null);
    serviceURLs.put("RESOURCEMANAGER", null);
    serviceURLs.put("AMBARIUI", Collections.singletonList("http://c6401.ambari.apache.org:8080"));
    // Write the externalized provider config to a temp file
    File providerConfig = writeProviderConfig("ambari-cluster-policy.xml", TEST_PROVIDER_CONFIG);
    File topologyFile = null;
    try {
        File destDir = (new File(".")).getCanonicalFile();
        GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.replay(gc);
        // Mock out the simple descriptor
        SimpleDescriptor testDescriptor = EasyMock.createNiceMock(SimpleDescriptor.class);
        EasyMock.expect(testDescriptor.getName()).andReturn("mysimpledescriptor").anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryAddress()).andReturn(address).anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryType()).andReturn(type).anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryUser()).andReturn(null).anyTimes();
        EasyMock.expect(testDescriptor.getProviderConfig()).andReturn(providerConfig.getAbsolutePath()).anyTimes();
        EasyMock.expect(testDescriptor.getClusterName()).andReturn(CLUSTER_NAME).anyTimes();
        List<SimpleDescriptor.Service> serviceMocks = new ArrayList<>();
        for (String serviceName : serviceURLs.keySet()) {
            SimpleDescriptor.Service svc = EasyMock.createNiceMock(SimpleDescriptor.Service.class);
            EasyMock.expect(svc.getName()).andReturn(serviceName).anyTimes();
            EasyMock.expect(svc.getURLs()).andReturn(serviceURLs.get(serviceName)).anyTimes();
            EasyMock.replay(svc);
            serviceMocks.add(svc);
        }
        EasyMock.expect(testDescriptor.getServices()).andReturn(serviceMocks).anyTimes();
        EasyMock.replay(testDescriptor);
        // Invoke the simple descriptor handler
        Map<String, File> files = SimpleDescriptorHandler.handle(gc, testDescriptor, // simple desc co-located with provider config
        providerConfig.getParentFile(), destDir);
        topologyFile = files.get("topology");
        // Validate the resulting topology descriptor
        assertTrue(topologyFile.exists());
        // Validate the topology descriptor's correctness
        TopologyValidator validator = new TopologyValidator(topologyFile.getAbsolutePath());
        if (!validator.validateTopology()) {
            throw new SAXException(validator.getErrorString());
        }
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        // Parse the topology descriptor
        Document topologyXml = XmlUtils.readXml(topologyFile);
        // Validate the provider configuration
        Node gatewayNode = (Node) xpath.compile("/topology/gateway").evaluate(topologyXml, XPathConstants.NODE);
        ProviderConfiguration testProviderConfiguration = ProviderConfigurationParser.parseXML(new ByteArrayInputStream(TEST_PROVIDER_CONFIG.getBytes()));
        validateGeneratedProviderConfiguration(testProviderConfiguration, gatewayNode);
        // Validate the service declarations
        List<String> topologyServices = new ArrayList<>();
        Map<String, List<String>> topologyServiceURLs = new HashMap<>();
        NodeList serviceNodes = (NodeList) xpath.compile("/topology/service").evaluate(topologyXml, XPathConstants.NODESET);
        for (int serviceNodeIndex = 0; serviceNodeIndex < serviceNodes.getLength(); serviceNodeIndex++) {
            Node serviceNode = serviceNodes.item(serviceNodeIndex);
            Node roleNode = (Node) xpath.compile("role/text()").evaluate(serviceNode, XPathConstants.NODE);
            assertNotNull(roleNode);
            String role = roleNode.getNodeValue();
            topologyServices.add(role);
            NodeList urlNodes = (NodeList) xpath.compile("url/text()").evaluate(serviceNode, XPathConstants.NODESET);
            for (int urlNodeIndex = 0; urlNodeIndex < urlNodes.getLength(); urlNodeIndex++) {
                Node urlNode = urlNodes.item(urlNodeIndex);
                assertNotNull(urlNode);
                String url = urlNode.getNodeValue();
                assertNotNull("Every declared service should have a URL.", url);
                if (!topologyServiceURLs.containsKey(role)) {
                    topologyServiceURLs.put(role, new ArrayList<>());
                }
                topologyServiceURLs.get(role).add(url);
            }
        }
        // There should not be a service element for HIVE, since it had no valid URLs
        assertEquals("Unexpected number of service declarations.", serviceURLs.size() - 1, topologyServices.size());
        assertFalse("The HIVE service should have been omitted from the generated topology.", topologyServices.contains("HIVE"));
        assertEquals("Unexpected number of service URLs.", serviceURLs.size() - 1, topologyServiceURLs.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        serviceDiscoverySource.delete();
        providerConfig.delete();
        if (topologyFile != null) {
            topologyFile.delete();
        }
    }
}
Also used : HashMap(java.util.HashMap) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) XPath(javax.xml.xpath.XPath) Matchers.hasXPath(org.hamcrest.Matchers.hasXPath) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) TopologyValidator(org.apache.knox.gateway.topology.validation.TopologyValidator) File(java.io.File) Test(org.junit.Test)

Example 13 with GatewayConfig

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

the class GatewayGlobalConfigTest method testForUpdatedDeploymentDir.

@Test
public void testForUpdatedDeploymentDir() {
    String homeDirName = getHomeDirName("conf-demo/conf/gateway-site.xml");
    System.setProperty(GatewayConfigImpl.GATEWAY_HOME_VAR, homeDirName);
    System.setProperty(GatewayConfigImpl.GATEWAY_DATA_HOME_VAR, homeDirName);
    GatewayConfig config = new GatewayConfigImpl();
    assertTrue(("target/test").equalsIgnoreCase(config.getGatewayDeploymentDir()));
}
Also used : GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 14 with GatewayConfig

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

the class GatewayGlobalConfigTest method testStacksServicesDir.

@Test
public void testStacksServicesDir() {
    System.clearProperty(GatewayConfigImpl.GATEWAY_HOME_VAR);
    GatewayConfig config = new GatewayConfigImpl();
    assertThat(config.getGatewayServicesDir(), Matchers.endsWith("data" + File.separator + "services"));
    String homeDirName = getHomeDirName("conf-demo/conf/gateway-site.xml");
    System.setProperty(GatewayConfigImpl.GATEWAY_HOME_VAR, homeDirName);
    config = new GatewayConfigImpl();
    assertEquals("target/test", config.getGatewayServicesDir());
}
Also used : GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 15 with GatewayConfig

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

the class GatewayGlobalConfigTest method testDataDirSetAsBothSystemPropertyAndConfig.

/**
 * When data dir is set at both system property and configuration level , then system property
 * value should be considered
 */
@Test
public void testDataDirSetAsBothSystemPropertyAndConfig() {
    String homeDirName = getHomeDirName("conf-demo/conf/gateway-site.xml");
    System.setProperty(GatewayConfigImpl.GATEWAY_HOME_VAR, homeDirName);
    System.setProperty(GatewayConfigImpl.GATEWAY_DATA_HOME_VAR, homeDirName + File.separator + "DataDirSystemProperty");
    GatewayConfig config = new GatewayConfigImpl();
    assertTrue((homeDirName + File.separator + "DataDirSystemProperty").equalsIgnoreCase(config.getGatewayDataDir()));
}
Also used : GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) 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