Search in sources :

Example 31 with GatewayConfig

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

the class CryptoServiceTest method testCryptoServiceDES.

@Test
public void testCryptoServiceDES() throws Exception {
    GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(config.getAlgorithm()).andReturn("DES");
    EasyMock.expect(config.getPBEAlgorithm()).andReturn("PBKDF2WithHmacSHA1");
    EasyMock.expect(config.getSaltSize()).andReturn("16");
    EasyMock.expect(config.getIterationCount()).andReturn("65536");
    EasyMock.expect(config.getKeyLength()).andReturn("128");
    EasyMock.expect(config.getTransformation()).andReturn("DES");
    EasyMock.replay(config);
    // password to create key - same Encryptor
    String queryString = "url=http://localhost:50070/api/v1/blahblah";
    ConfigurableEncryptor aes0 = new ConfigurableEncryptor("password");
    aes0.init(config);
    cs.init(config, new HashMap<String, String>());
    EncryptionResult result0 = cs.encryptForCluster("Test", "encrypt_url", queryString.getBytes("UTF8"));
    byte[] decrypted0 = cs.decryptForCluster("Test", "encrypt_url", result0.cipher, result0.iv, result0.salt);
    assertEquals(queryString, new String(decrypted0, "UTF8"));
    assertEquals(queryString.getBytes("UTF8").length, decrypted0.length);
    assertEquals(queryString.getBytes("UTF8").length, new String(decrypted0, "UTF8").toCharArray().length);
}
Also used : GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) ConfigurableEncryptor(org.apache.knox.gateway.services.security.impl.ConfigurableEncryptor) Test(org.junit.Test)

Example 32 with GatewayConfig

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

the class TopologiesResource method getTopology.

@GET
@Produces({ APPLICATION_JSON, APPLICATION_XML })
@Path(SINGLE_TOPOLOGY_API_PATH)
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 (org.apache.knox.gateway.topology.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 BeanConverter.getTopology(t);
        }
    }
    return null;
}
Also used : GatewayServices(org.apache.knox.gateway.services.GatewayServices) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 33 with GatewayConfig

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

the class TopologyResourceTest method testTopologyURLMethods.

@Test
public void testTopologyURLMethods() {
    // Note: had to change method signature due to these tests. Changed methods to public and added
    // HttpServletRequest argument.
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Host, host);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Server, server);
    Topology t = EasyMock.createNiceMock(Topology.class);
    EasyMock.expect(t.getName()).andReturn(topologyName).anyTimes();
    GatewayConfig conf = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(conf.getGatewayPath()).andReturn(gatewayPath).anyTimes();
    EasyMock.replay(request, t, conf);
    TopologiesResource res = new TopologiesResource();
    String href = res.buildHref(t, request);
    String uri = res.buildURI(t, conf, request);
    assertThat(uri, containsString(proto));
    assertThat(uri, containsString(host));
    assertThat(uri, containsString(server));
    assertThat(uri, containsString(port));
    assertThat(uri, containsString(topologyName));
    assert (uri.equals(expectedURI));
    assertThat(href, containsString(proto));
    assertThat(href, containsString(host));
    assertThat(href, containsString(server));
    assertThat(href, containsString(port));
    assertThat(href, containsString(fullContext));
    assert (href.equals(expectedHref));
    // Test 2 - No Protocol Header
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Host, host);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Server, server);
    EasyMock.replay(request);
    String test2URI = expectedURI.replace(proto, reqProto);
    String test2href = expectedHref.replace(proto, reqProto);
    assert (res.buildURI(t, conf, request).equals(test2URI));
    assert (res.buildHref(t, request).equals(test2href));
    // Test 3 - No port in host Header
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Host, server);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Server, server);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    EasyMock.replay(request);
    assert (res.buildURI(t, conf, request).equals(expectedURI));
    assert (res.buildHref(t, request).equals(expectedHref));
    // Test 4 - server & no host Header
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Server, server);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    EasyMock.replay(request);
    assert (res.buildURI(t, conf, request).equals(expectedURI));
    assert (res.buildHref(t, request).equals(expectedHref));
    // Test 5 - no server & no host Header
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    EasyMock.replay(request);
    String test5URI = expectedURI.replace(server, reqServer);
    String test5href = expectedHref.replace(server, reqServer);
    assertEquals(res.buildURI(t, conf, request), test5URI);
    assertEquals(res.buildHref(t, request), test5href);
    // Test 6 - no port, no server & no host Header
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Context, fullContext);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    EasyMock.replay(request);
    String test6URI = expectedURI.replace(server, reqServer).replace(port, reqPort);
    String test6href = expectedHref.replace(server, reqServer).replace(port, reqPort);
    assertEquals(res.buildURI(t, conf, request), (test6URI));
    assertEquals(res.buildHref(t, request), (test6href));
    // Test 7 - No Context
    EasyMock.reset(request);
    setDefaultExpectations(request);
    setMockRequestHeader(request, X_Forwarded_Host, host);
    setMockRequestHeader(request, X_Forwarded_Port, port);
    setMockRequestHeader(request, X_Forwarded_Proto, proto);
    setMockRequestHeader(request, X_Forwarded_Server, server);
    EasyMock.replay(request);
    String test7URI = expectedURI.replace(startContext, "");
    String test7href = expectedHref.replace(startContext, "");
    assertEquals(res.buildURI(t, conf, request), test7URI);
    assertEquals(res.buildHref(t, request), test7href);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Topology(org.apache.knox.gateway.topology.Topology) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 34 with GatewayConfig

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

the class DefaultRemoteConfigurationRegistriesTest method mockGatewayConfig.

/**
 * Create a mock GatewayConfig based on the specified test properties.
 *
 * @param testProperties The test properties to set on the config
 */
private GatewayConfig mockGatewayConfig(Map<String, Properties> testProperties) {
    // Mock gateway config
    GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
    List<String> configNames = new ArrayList<>();
    for (String registryName : testProperties.keySet()) {
        configNames.add(registryName);
        String propertyValueString = "";
        Properties props = testProperties.get(registryName);
        Enumeration names = props.propertyNames();
        while (names.hasMoreElements()) {
            String propertyName = (String) names.nextElement();
            propertyValueString += propertyName + "=" + props.get(propertyName);
            if (names.hasMoreElements()) {
                propertyValueString += ";";
            }
        }
        EasyMock.expect(gc.getRemoteRegistryConfiguration(registryName)).andReturn(propertyValueString).anyTimes();
    }
    EasyMock.expect(gc.getRemoteRegistryConfigurationNames()).andReturn(configNames).anyTimes();
    EasyMock.replay(gc);
    return gc;
}
Also used : Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) Properties(java.util.Properties) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig)

Example 35 with GatewayConfig

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

the class RemoteConfigurationRegistryClientServiceTest method testMultipleUnsecuredZooKeeperWithSimpleRegistryConfig.

/**
 * Test multiple configurations for an unsecured remote registry.
 */
@Test
public void testMultipleUnsecuredZooKeeperWithSimpleRegistryConfig() throws Exception {
    final String REGISTRY_CLIENT_NAME_1 = "zkclient1";
    final String REGISTRY_CLIENT_NAME_2 = "zkclient2";
    final String PRINCIPAL = null;
    final String PWD = null;
    final String CRED_ALIAS = null;
    // Configure and start a secure ZK cluster
    TestingCluster zkCluster = setupAndStartSecureTestZooKeeper(PRINCIPAL, PWD);
    try {
        // Create the setup client for the test cluster, and initialize the test znodes
        CuratorFramework setupClient = initializeTestClientAndZNodes(zkCluster, PRINCIPAL);
        // Mock configuration
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        final String registryConfigValue1 = GatewayConfig.REMOTE_CONFIG_REGISTRY_TYPE + "=" + ZooKeeperClientService.TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_ADDRESS + "=" + zkCluster.getConnectString();
        EasyMock.expect(config.getRemoteRegistryConfiguration(REGISTRY_CLIENT_NAME_1)).andReturn(registryConfigValue1).anyTimes();
        final String registryConfigValue2 = GatewayConfig.REMOTE_CONFIG_REGISTRY_TYPE + "=" + ZooKeeperClientService.TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_ADDRESS + "=" + zkCluster.getConnectString();
        EasyMock.expect(config.getRemoteRegistryConfiguration(REGISTRY_CLIENT_NAME_2)).andReturn(registryConfigValue2).anyTimes();
        EasyMock.expect(config.getRemoteRegistryConfigurationNames()).andReturn(Arrays.asList(REGISTRY_CLIENT_NAME_1, REGISTRY_CLIENT_NAME_2)).anyTimes();
        EasyMock.replay(config);
        // Create the client service instance
        RemoteConfigurationRegistryClientService clientService = RemoteConfigurationRegistryClientServiceFactory.newInstance(config);
        assertEquals("Wrong registry client service type.", clientService.getClass(), CuratorClientService.class);
        clientService.setAliasService(null);
        clientService.init(config, null);
        clientService.start();
        RemoteConfigurationRegistryClient client1 = clientService.get(REGISTRY_CLIENT_NAME_1);
        assertNotNull(client1);
        RemoteConfigurationRegistryClient client2 = clientService.get(REGISTRY_CLIENT_NAME_2);
        assertNotNull(client2);
        doTestZooKeeperClient(setupClient, REGISTRY_CLIENT_NAME_1, clientService, false);
        doTestZooKeeperClient(setupClient, REGISTRY_CLIENT_NAME_2, clientService, false);
    } finally {
        zkCluster.stop();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) RemoteConfigurationRegistryClient(org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClient) RemoteConfigurationRegistryClientService(org.apache.knox.gateway.services.config.client.RemoteConfigurationRegistryClientService) 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