Search in sources :

Example 11 with TopologyService

use of org.apache.knox.gateway.services.topology.TopologyService 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 12 with TopologyService

use of org.apache.knox.gateway.services.topology.TopologyService 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)

Example 13 with TopologyService

use of org.apache.knox.gateway.services.topology.TopologyService 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)

Example 14 with TopologyService

use of org.apache.knox.gateway.services.topology.TopologyService in project knox by apache.

the class ServiceTestResource method getTopology.

public Topology getTopology(@PathParam("id") String id) {
    GatewayServices services = (GatewayServices) request.getServletContext().getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE);
    GatewayConfig config = (GatewayConfig) request.getServletContext().getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE);
    TopologyService ts = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    for (Topology t : ts.getTopologies()) {
        if (t.getName().equals(id)) {
            try {
                t.setUri(new URI(buildURI(t, config, request)));
            } catch (URISyntaxException se) {
                t.setUri(null);
            }
            return t;
        }
    }
    return null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) Topology(org.apache.knox.gateway.topology.Topology) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) TopologyService(org.apache.knox.gateway.services.topology.TopologyService)

Example 15 with TopologyService

use of org.apache.knox.gateway.services.topology.TopologyService in project knox by apache.

the class BadUrlTest method setupGatewayConfig.

/**
 * Initialize the configs and components required for this test.
 *
 * @param backend
 * @throws IOException
 */
private static void setupGatewayConfig(final String backend) throws IOException {
    services = new DefaultGatewayServices();
    topoDir = createDir();
    URL serviceUrl = ClassLoader.getSystemResource("websocket-services");
    final File descriptor = new File(topoDir, "websocket.xml");
    final FileOutputStream stream = new FileOutputStream(descriptor);
    createKnoxTopology(backend).toStream(stream);
    stream.close();
    final TestTopologyListener topoListener = new TestTopologyListener();
    final Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gatewayConfig.getGatewayTopologyDir()).andReturn(topoDir.toString()).anyTimes();
    EasyMock.expect(gatewayConfig.getGatewayProvidersConfigDir()).andReturn(topoDir.getAbsolutePath() + "/shared-providers").anyTimes();
    EasyMock.expect(gatewayConfig.getGatewayDescriptorsDir()).andReturn(topoDir.getAbsolutePath() + "/descriptors").anyTimes();
    EasyMock.expect(gatewayConfig.getGatewayServicesDir()).andReturn(serviceUrl.getFile()).anyTimes();
    EasyMock.expect(gatewayConfig.getEphemeralDHKeySize()).andReturn("2048").anyTimes();
    EasyMock.expect(gatewayConfig.getGatewaySecurityDir()).andReturn(topoDir.toString()).anyTimes();
    /* Websocket configs */
    EasyMock.expect(gatewayConfig.isWebsocketEnabled()).andReturn(true).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketMaxTextMessageSize()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_SIZE).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketMaxBinaryMessageSize()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_SIZE).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketMaxTextMessageBufferSize()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_MAX_TEXT_MESSAGE_BUFFER_SIZE).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketMaxBinaryMessageBufferSize()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_MAX_BINARY_MESSAGE_BUFFER_SIZE).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketInputBufferSize()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_INPUT_BUFFER_SIZE).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketAsyncWriteTimeout()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_ASYNC_WRITE_TIMEOUT).anyTimes();
    EasyMock.expect(gatewayConfig.getWebsocketIdleTimeout()).andReturn(GatewayConfigImpl.DEFAULT_WEBSOCKET_IDLE_TIMEOUT).anyTimes();
    EasyMock.expect(gatewayConfig.getRemoteRegistryConfigurationNames()).andReturn(Collections.emptyList()).anyTimes();
    EasyMock.replay(gatewayConfig);
    try {
        services.init(gatewayConfig, options);
    } catch (ServiceLifecycleException e) {
        e.printStackTrace();
    }
    DeploymentFactory.setGatewayServices(services);
    final TopologyService monitor = services.getService(GatewayServices.TOPOLOGY_SERVICE);
    monitor.addTopologyChangeListener(topoListener);
    monitor.reloadTopologies();
}
Also used : HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) File(java.io.File) URL(java.net.URL) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) TopologyService(org.apache.knox.gateway.services.topology.TopologyService)

Aggregations

TopologyService (org.apache.knox.gateway.services.topology.TopologyService)23 GatewayServices (org.apache.knox.gateway.services.GatewayServices)17 Path (javax.ws.rs.Path)12 File (java.io.File)11 Produces (javax.ws.rs.Produces)9 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)7 GET (javax.ws.rs.GET)6 Response (javax.ws.rs.core.Response)6 ArrayList (java.util.ArrayList)5 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)5 Topology (org.apache.knox.gateway.topology.Topology)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 HashMap (java.util.HashMap)4 FileOutputStream (java.io.FileOutputStream)3 URL (java.net.URL)3 Consumes (javax.ws.rs.Consumes)3 DELETE (javax.ws.rs.DELETE)3 PUT (javax.ws.rs.PUT)3 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)3