Search in sources :

Example 1 with SimpleDescriptor

use of org.apache.knox.gateway.topology.simple.SimpleDescriptor in project knox by apache.

the class DefaultTopologyService method init.

@Override
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
    String gatewayConfDir = config.getGatewayConfDir();
    if (gatewayConfDir != null) {
        System.setProperty(ServiceDiscovery.CONFIG_DIR_PROPERTY, gatewayConfDir);
    }
    try {
        listeners = new HashSet<>();
        topologies = new HashMap<>();
        topologiesDirectory = calculateAbsoluteTopologiesDir(config);
        File configDirectory = calculateAbsoluteConfigDir(config);
        descriptorsDirectory = new File(configDirectory, "descriptors");
        sharedProvidersDirectory = new File(configDirectory, "shared-providers");
        // Add support for conf/topologies
        initListener(topologiesDirectory, this, this);
        // Add support for conf/descriptors
        descriptorsMonitor = new DescriptorsMonitor(config, topologiesDirectory, aliasService);
        initListener(descriptorsDirectory, descriptorsMonitor, descriptorsMonitor);
        log.monitoringDescriptorChangesInDirectory(descriptorsDirectory.getAbsolutePath());
        // Add support for conf/shared-providers
        SharedProviderConfigMonitor spm = new SharedProviderConfigMonitor(descriptorsMonitor, descriptorsDirectory);
        initListener(sharedProvidersDirectory, spm, spm);
        log.monitoringProviderConfigChangesInDirectory(sharedProvidersDirectory.getAbsolutePath());
        // For all the descriptors currently in the descriptors dir at start-up time, determine if topology regeneration
        // is required.
        // This happens prior to the start-up loading of the topologies.
        String[] descriptorFilenames = descriptorsDirectory.list();
        if (descriptorFilenames != null) {
            for (String descriptorFilename : descriptorFilenames) {
                if (DescriptorsMonitor.isDescriptorFile(descriptorFilename)) {
                    String topologyName = FilenameUtils.getBaseName(descriptorFilename);
                    File existingDescriptorFile = getExistingFile(descriptorsDirectory, topologyName);
                    // If there isn't a corresponding topology file, or if the descriptor has been modified since the
                    // corresponding topology file was generated, then trigger generation of one
                    File matchingTopologyFile = getExistingFile(topologiesDirectory, topologyName);
                    if (matchingTopologyFile == null || matchingTopologyFile.lastModified() < existingDescriptorFile.lastModified()) {
                        descriptorsMonitor.onFileChange(existingDescriptorFile);
                    } else {
                        // If regeneration is NOT required, then we at least need to report the provider configuration
                        // reference relationship (KNOX-1144)
                        String normalizedDescriptorPath = FilenameUtils.normalize(existingDescriptorFile.getAbsolutePath());
                        // Parse the descriptor to determine the provider config reference
                        SimpleDescriptor sd = SimpleDescriptorFactory.parse(normalizedDescriptorPath);
                        if (sd != null) {
                            File referencedProviderConfig = getExistingFile(sharedProvidersDirectory, FilenameUtils.getBaseName(sd.getProviderConfig()));
                            if (referencedProviderConfig != null) {
                                List<String> references = descriptorsMonitor.getReferencingDescriptors(referencedProviderConfig.getAbsolutePath());
                                if (!references.contains(normalizedDescriptorPath)) {
                                    references.add(normalizedDescriptorPath);
                                }
                            }
                        }
                    }
                }
            }
        }
        // Initialize the remote configuration monitor, if it has been configured
        remoteMonitor = RemoteConfigurationMonitorFactory.get(config);
    } catch (IOException | SAXException io) {
        throw new ServiceLifecycleException(io.getMessage());
    }
}
Also used : ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException) SimpleDescriptor(org.apache.knox.gateway.topology.simple.SimpleDescriptor) SAXException(org.xml.sax.SAXException) File(java.io.File)

Example 2 with SimpleDescriptor

use of org.apache.knox.gateway.topology.simple.SimpleDescriptor in project knox by apache.

the class SimpleDescriptorHandlerFuncTest method testSimpleDescriptorHandlerQueryStringCredentialAliasCreation.

/**
 * KNOX-1136
 * <p>
 * Test that a credential store is created, and a encryptQueryString alias is defined, with a password that is not
 * random (but is derived from the master secret and the topology name).
 * <p>
 * N.B. This test depends on the NoOpServiceDiscovery extension being configured in META-INF/services
 */
@Test
public void testSimpleDescriptorHandlerQueryStringCredentialAliasCreation() throws Exception {
    final String testMasterSecret = "mysecret";
    final String discoveryType = "NO_OP";
    final String clusterName = "dummy";
    final Map<String, List<String>> serviceURLs = new HashMap<>();
    serviceURLs.put("RESOURCEMANAGER", Collections.singletonList("http://myhost:1234/resource"));
    File testRootDir = TestUtils.createTempDir(getClass().getSimpleName());
    File testConfDir = new File(testRootDir, "conf");
    File testProvDir = new File(testConfDir, "shared-providers");
    File testTopoDir = new File(testConfDir, "topologies");
    File testDeployDir = new File(testConfDir, "deployments");
    // Write the externalized provider config to a temp file
    File providerConfig = new File(testProvDir, "ambari-cluster-policy.xml");
    FileUtils.write(providerConfig, TEST_PROVIDER_CONFIG);
    File topologyFile = null;
    try {
        File destDir = new File(System.getProperty("java.io.tmpdir")).getCanonicalFile();
        // Mock out the simple descriptor
        SimpleDescriptor testDescriptor = EasyMock.createNiceMock(SimpleDescriptor.class);
        EasyMock.expect(testDescriptor.getName()).andReturn("mysimpledescriptor").anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryAddress()).andReturn(null).anyTimes();
        EasyMock.expect(testDescriptor.getDiscoveryType()).andReturn(discoveryType).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.getURLs()).andReturn(serviceURLs.get(serviceName)).anyTimes();
            EasyMock.expect(svc.getParams()).andReturn(Collections.emptyMap()).anyTimes();
            EasyMock.replay(svc);
            serviceMocks.add(svc);
        }
        EasyMock.expect(testDescriptor.getServices()).andReturn(serviceMocks).anyTimes();
        EasyMock.replay(testDescriptor);
        // Try setting up enough of the GatewayServer to support the test...
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        InetSocketAddress gatewayAddress = new InetSocketAddress(0);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(testTopoDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayDeploymentDir()).andReturn(testDeployDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayAddress()).andReturn(gatewayAddress).anyTimes();
        EasyMock.expect(config.getGatewayPortMappings()).andReturn(Collections.emptyMap()).anyTimes();
        EasyMock.replay(config);
        // Setup the Gateway Services
        GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
        // Master Service
        MasterService ms = EasyMock.createNiceMock(MasterService.class);
        EasyMock.expect(ms.getMasterSecret()).andReturn(testMasterSecret.toCharArray()).anyTimes();
        EasyMock.replay(ms);
        EasyMock.expect(gatewayServices.getService("MasterService")).andReturn(ms).anyTimes();
        // Keystore Service
        KeystoreService ks = EasyMock.createNiceMock(KeystoreService.class);
        EasyMock.expect(ks.isCredentialStoreForClusterAvailable(testDescriptor.getName())).andReturn(false).once();
        ks.createCredentialStoreForCluster(testDescriptor.getName());
        EasyMock.expectLastCall().once();
        KeyStore credStore = EasyMock.createNiceMock(KeyStore.class);
        EasyMock.expect(ks.getCredentialStoreForCluster(testDescriptor.getName())).andReturn(credStore).anyTimes();
        EasyMock.replay(ks);
        EasyMock.expect(gatewayServices.getService(GatewayServices.KEYSTORE_SERVICE)).andReturn(ks).anyTimes();
        // Alias Service
        AliasService as = EasyMock.createNiceMock(AliasService.class);
        // Captures for validating the alias creation for a generated topology
        Capture<String> capturedCluster = EasyMock.newCapture();
        Capture<String> capturedAlias = EasyMock.newCapture();
        Capture<String> capturedPwd = EasyMock.newCapture();
        as.addAliasForCluster(capture(capturedCluster), capture(capturedAlias), capture(capturedPwd));
        EasyMock.expectLastCall().anyTimes();
        EasyMock.replay(as);
        EasyMock.expect(gatewayServices.getService(GatewayServices.ALIAS_SERVICE)).andReturn(as).anyTimes();
        // Topology Service
        TopologyService ts = EasyMock.createNiceMock(TopologyService.class);
        ts.addTopologyChangeListener(anyObject());
        EasyMock.expectLastCall().anyTimes();
        ts.reloadTopologies();
        EasyMock.expectLastCall().anyTimes();
        EasyMock.expect(ts.getTopologies()).andReturn(Collections.emptyList()).anyTimes();
        EasyMock.replay(ts);
        EasyMock.expect(gatewayServices.getService(GatewayServices.TOPOLOGY_SERVICE)).andReturn(ts).anyTimes();
        EasyMock.replay(gatewayServices);
        // Start a GatewayService with the GatewayServices mock
        GatewayServer server = GatewayServer.startGateway(config, gatewayServices);
        // Invoke the simple descriptor handler, which will also create the credential store
        // (because it doesn't exist) and the encryptQueryString alias
        Map<String, File> files = SimpleDescriptorHandler.handle(config, testDescriptor, providerConfig.getParentFile(), destDir);
        topologyFile = files.get("topology");
        // Validate the AliasService interaction
        assertEquals("Unexpected cluster name for the alias (should be the topology name).", testDescriptor.getName(), capturedCluster.getValue());
        assertEquals("Unexpected alias name.", "encryptQueryString", capturedAlias.getValue());
        assertEquals("Unexpected alias value (should be master secret + topology name.", testMasterSecret + testDescriptor.getName(), capturedPwd.getValue());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        FileUtils.forceDelete(testRootDir);
        if (topologyFile != null) {
            topologyFile.delete();
        }
    }
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) AliasService(org.apache.knox.gateway.services.security.AliasService) MasterService(org.apache.knox.gateway.services.security.MasterService) KeystoreService(org.apache.knox.gateway.services.security.KeystoreService) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) MasterService(org.apache.knox.gateway.services.security.MasterService) KeyStore(java.security.KeyStore) SimpleDescriptor(org.apache.knox.gateway.topology.simple.SimpleDescriptor) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) ArrayList(java.util.ArrayList) List(java.util.List) KeystoreService(org.apache.knox.gateway.services.security.KeystoreService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Aggregations

File (java.io.File)2 SimpleDescriptor (org.apache.knox.gateway.topology.simple.SimpleDescriptor)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyStore (java.security.KeyStore)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1 GatewayServices (org.apache.knox.gateway.services.GatewayServices)1 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)1 AliasService (org.apache.knox.gateway.services.security.AliasService)1 KeystoreService (org.apache.knox.gateway.services.security.KeystoreService)1 MasterService (org.apache.knox.gateway.services.security.MasterService)1 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)1 Test (org.junit.Test)1 SAXException (org.xml.sax.SAXException)1