Search in sources :

Example 56 with GatewayConfig

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

the class DefaultTopologyServiceTest method testConfigurationCRUDAPI.

/**
 * KNOX-1039
 */
@Test
public void testConfigurationCRUDAPI() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    topologyDir.mkdirs();
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    try {
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService ts = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        ts.init(config, c);
        ts.addTopologyChangeListener(topoListener);
        ts.reloadTopologies();
        java.lang.reflect.Field dmField = ts.getClass().getDeclaredField("descriptorsMonitor");
        dmField.setAccessible(true);
        DefaultTopologyService.DescriptorsMonitor dm = (DefaultTopologyService.DescriptorsMonitor) dmField.get(ts);
        final String simpleDescName = "six.json";
        final String provConfOne = "provider-config-one.xml";
        final String provConfTwo = "ambari-cluster-policy.xml";
        // "Deploy" the referenced provider configs first
        boolean isDeployed = ts.deployProviderConfiguration(provConfOne, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/provider-config-one.xml").toURI())));
        assertTrue(isDeployed);
        File provConfOneFile = new File(sharedProvidersDir, provConfOne);
        assertTrue(provConfOneFile.exists());
        isDeployed = ts.deployProviderConfiguration(provConfTwo, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml").toURI())));
        assertTrue(isDeployed);
        File provConfTwoFile = new File(sharedProvidersDir, provConfTwo);
        assertTrue(provConfTwoFile.exists());
        // Validate the provider configurations known by the topology service
        Collection<File> providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertEquals(2, providerConfigurations.size());
        assertTrue(providerConfigurations.contains(provConfOneFile));
        assertTrue(providerConfigurations.contains(provConfTwoFile));
        // "Deploy" the simple descriptor, which depends on provConfOne
        isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-six.json").toURI())));
        assertTrue(isDeployed);
        File simpleDesc = new File(descriptorsDir, simpleDescName);
        assertTrue(simpleDesc.exists());
        // Validate the simple descriptors known by the topology service
        Collection<File> descriptors = ts.getDescriptors();
        assertNotNull(descriptors);
        assertEquals(1, descriptors.size());
        assertTrue(descriptors.contains(simpleDesc));
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is recorded
        dm.onFileChange(simpleDesc);
        // Attempt to delete the referenced provConfOne
        assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
        // Overwrite the simple descriptor with content that changes the provider config reference to provConfTwo
        isDeployed = ts.deployDescriptor(simpleDescName, FileUtils.readFileToString(new File(ClassLoader.getSystemResource("org/apache/knox/gateway/topology/file/simple-descriptor-five.json").toURI())));
        assertTrue(isDeployed);
        assertTrue(simpleDesc.exists());
        ts.getProviderConfigurations();
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
        dm.onFileChange(simpleDesc);
        // Attempt to delete the referenced provConfOne
        assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfOne)));
        // Re-validate the provider configurations known by the topology service
        providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertEquals(1, providerConfigurations.size());
        assertFalse(providerConfigurations.contains(provConfOneFile));
        assertTrue(providerConfigurations.contains(provConfTwoFile));
        // Attempt to delete the referenced provConfTwo
        assertFalse("Should not be able to delete a provider configuration that is referenced by one or more descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
        // Delete the referencing simple descriptor
        assertTrue(ts.deleteDescriptor(FilenameUtils.getBaseName(simpleDescName)));
        assertFalse(simpleDesc.exists());
        // Re-validate the simple descriptors known by the topology service
        descriptors = ts.getDescriptors();
        assertNotNull(descriptors);
        assertTrue(descriptors.isEmpty());
        // "Notice" the simple descriptor, so the provider configuration dependency relationship is updated
        dm.onFileDelete(simpleDesc);
        // Attempt to delete the referenced provConfTwo
        assertTrue("Should be able to delete the provider configuration, now that it's not referenced by any descriptors", ts.deleteProviderConfiguration(FilenameUtils.getBaseName(provConfTwo)));
        // Re-validate the provider configurations known by the topology service
        providerConfigurations = ts.getProviderConfigurations();
        assertNotNull(providerConfigurations);
        assertTrue(providerConfigurations.isEmpty());
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 57 with GatewayConfig

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

the class DefaultTopologyServiceTest method testTopologiesUpdateFromProviderConfigChange.

/**
 * KNOX-1014
 *
 * Test the lifecycle relationship between provider configuration files, simple descriptors, and topology files.
 *
 * N.B. This test depends on the DummyServiceDiscovery extension being configured:
 *        org.apache.knox.gateway.topology.discovery.test.extension.DummyServiceDiscovery
 */
@Test
public void testTopologiesUpdateFromProviderConfigChange() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    topologyDir.mkdirs();
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    try {
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService ts = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        ts.init(config, c);
        ts.addTopologyChangeListener(topoListener);
        ts.reloadTopologies();
        java.lang.reflect.Field dmField = ts.getClass().getDeclaredField("descriptorsMonitor");
        dmField.setAccessible(true);
        DefaultTopologyService.DescriptorsMonitor dm = (DefaultTopologyService.DescriptorsMonitor) dmField.get(ts);
        // Write out the referenced provider configs first
        createFile(sharedProvidersDir, "provider-config-one.xml", "org/apache/knox/gateway/topology/file/provider-config-one.xml", System.currentTimeMillis());
        // Create the simple descriptor, which depends on provider-config-one.xml
        File simpleDesc = createFile(descriptorsDir, "six.json", "org/apache/knox/gateway/topology/file/simple-descriptor-six.json", System.currentTimeMillis());
        // "Notice" the simple descriptor change, and generate a topology based on it
        dm.onFileChange(simpleDesc);
        // Load the generated topology
        ts.reloadTopologies();
        Collection<Topology> topologies = ts.getTopologies();
        assertThat(topologies.size(), is(1));
        Iterator<Topology> iterator = topologies.iterator();
        Topology topology = iterator.next();
        assertFalse("The Shiro provider is disabled in provider-config-one.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
        // Overwrite the referenced provider configuration with a different ShiroProvider config, and check that the
        // changes are propagated to the associated topology
        File providerConfig = createFile(sharedProvidersDir, "provider-config-one.xml", "org/apache/knox/gateway/topology/file/ambari-cluster-policy.xml", System.currentTimeMillis());
        // "Notice" the simple descriptor change as a result of the referenced config change
        dm.onFileChange(simpleDesc);
        // Load the generated topology
        ts.reloadTopologies();
        topologies = ts.getTopologies();
        assertFalse(topologies.isEmpty());
        topology = topologies.iterator().next();
        assertTrue("The Shiro provider is enabled in ambari-cluster-policy.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
        // Delete the provider configuration, and make sure that the associated topology file is unaffected.
        // The topology file should not be affected because the simple descriptor handling will fail to resolve the
        // referenced provider configuration.
        // Delete the file
        providerConfig.delete();
        // The provider config deletion will trigger a descriptor change notification
        dm.onFileChange(simpleDesc);
        ts.reloadTopologies();
        topologies = ts.getTopologies();
        assertFalse(topologies.isEmpty());
        assertTrue("The Shiro provider is enabled in ambari-cluster-policy.xml", topology.getProvider("authentication", "ShiroProvider").isEnabled());
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) Topology(org.apache.knox.gateway.topology.Topology) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 58 with GatewayConfig

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

the class PropertiesFileServiceDiscoveryTest method testPropertiesFileServiceDiscovery.

@Test
public void testPropertiesFileServiceDiscovery() throws Exception {
    ServiceDiscovery sd = ServiceDiscoveryFactory.get("PROPERTIES_FILE");
    assertNotNull(sd);
    GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.replay(gc);
    String discoveryAddress = this.getClass().getName() + "__test-discovery-source.properties";
    File discoverySource = new File(discoveryAddress);
    try {
        config.store(new FileOutputStream(discoverySource), "Test discovery source for PropertiesFileServiceDiscovery");
        ServiceDiscovery.Cluster c = sd.discover(gc, new DefaultServiceDiscoveryConfig(discoverySource.getAbsolutePath()), "mycluster");
        assertNotNull(c);
        for (String name : clusterProperties.keySet()) {
            assertEquals(clusterProperties.get(name), c.getServiceURLs(name.split("\\.")[1]).get(0));
        }
    } finally {
        discoverySource.delete();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 59 with GatewayConfig

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

the class GatewayFilter method getRemoteAddress.

private String getRemoteAddress(ServletRequest servletRequest) {
    GatewayConfig gatewayConfig = (GatewayConfig) servletRequest.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    String addrHeaderName = gatewayConfig.getHeaderNameForRemoteAddress();
    String addr = ((HttpServletRequest) servletRequest).getHeader(addrHeaderName);
    if (addr == null || addr.trim().isEmpty()) {
        addr = servletRequest.getRemoteAddr();
    }
    return addr;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 60 with GatewayConfig

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

the class GatewayServer method main.

public static void main(String[] args) {
    try {
        configureLogging();
        logSysProps();
        CommandLine cmd = GatewayCommandLine.parse(args);
        if (cmd.hasOption(GatewayCommandLine.HELP_LONG)) {
            GatewayCommandLine.printHelp();
        } else if (cmd.hasOption(GatewayCommandLine.VERSION_LONG)) {
            printVersion();
        } else if (cmd.hasOption(GatewayCommandLine.REDEPLOY_LONG)) {
            redeployTopologies(cmd.getOptionValue(GatewayCommandLine.REDEPLOY_LONG));
        } else {
            buildProperties = loadBuildProperties();
            services = instantiateGatewayServices();
            if (services == null) {
                log.failedToInstantiateGatewayServices();
            }
            GatewayConfig config = new GatewayConfigImpl();
            if (config.isHadoopKerberosSecured()) {
                configureKerberosSecurity(config);
            }
            Map<String, String> options = new HashMap<>();
            options.put(GatewayCommandLine.PERSIST_LONG, Boolean.toString(cmd.hasOption(GatewayCommandLine.PERSIST_LONG)));
            services.init(config, options);
            if (!cmd.hasOption(GatewayCommandLine.NOSTART_LONG)) {
                startGateway(config, services);
            }
        }
    } catch (ParseException e) {
        log.failedToParseCommandLine(e);
        GatewayCommandLine.printHelp();
    } catch (Exception e) {
        log.failedToStartGateway(e);
        // Make sure the process exits.
        System.exit(1);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ParseException(org.apache.commons.cli.ParseException) GatewayConfigImpl(org.apache.knox.gateway.config.impl.GatewayConfigImpl) 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) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

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