Search in sources :

Example 1 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class TopologyRulesModuleTest method testParseSimpleTopologyXmlInHadoopFormat.

@Test
public void testParseSimpleTopologyXmlInHadoopFormat() throws IOException, SAXException, URISyntaxException {
    Digester digester = loader.newDigester();
    String name = "org/apache/knox/gateway/topology/xml/simple-topology-ambari-format.conf";
    URL url = ClassLoader.getSystemResource(name);
    assertThat("Failed to find URL for resource " + name, url, notNullValue());
    File file = new File(url.getFile());
    TopologyBuilder topologyBuilder = digester.parse(url);
    Topology topology = topologyBuilder.build();
    assertThat("Failed to parse resource " + name, topology, notNullValue());
    topology.setTimestamp(file.lastModified());
    assertThat(topology.getName(), is("topology2"));
    assertThat(topology.getTimestamp(), is(file.lastModified()));
    assertThat(topology.getServices().size(), is(4));
    assertThat(topology.getProviders().size(), is(2));
    Service webhdfsService = topology.getService("WEBHDFS", null, null);
    assertThat(webhdfsService, notNullValue());
    assertThat(webhdfsService.getRole(), is("WEBHDFS"));
    assertThat(webhdfsService.getName(), nullValue());
    assertThat(webhdfsService.getUrls().size(), is(2));
    assertThat(webhdfsService.getUrls(), hasItem("http://host1:50070/webhdfs"));
    assertThat(webhdfsService.getUrls(), hasItem("http://host2:50070/webhdfs"));
    Service webhcatService = topology.getService("WEBHCAT", null, null);
    assertThat(webhcatService, notNullValue());
    assertThat(webhcatService.getRole(), is("WEBHCAT"));
    assertThat(webhcatService.getName(), nullValue());
    assertThat(webhcatService.getUrls().size(), is(1));
    assertThat(webhcatService.getUrls(), hasItem("http://host:50111/templeton"));
    Service oozieService = topology.getService("OOZIE", null, null);
    assertThat(oozieService, notNullValue());
    assertThat(oozieService.getRole(), is("OOZIE"));
    assertThat(oozieService.getName(), nullValue());
    assertThat(webhcatService.getUrls().size(), is(1));
    assertThat(oozieService.getUrls(), hasItem("http://host:11000/oozie"));
    Service hiveService = topology.getService("HIVE", null, null);
    assertThat(hiveService, notNullValue());
    assertThat(hiveService.getRole(), is("HIVE"));
    assertThat(hiveService.getName(), nullValue());
    assertThat(webhcatService.getUrls().size(), is(1));
    assertThat(hiveService.getUrls(), hasItem("http://host:10000"));
    Provider authenticationProvider = topology.getProvider("authentication", "ShiroProvider");
    assertThat(authenticationProvider, notNullValue());
    assertThat(authenticationProvider.isEnabled(), is(true));
    assertThat(authenticationProvider.getRole(), is("authentication"));
    assertThat(authenticationProvider.getName(), is("ShiroProvider"));
    assertThat(authenticationProvider.getParams().size(), is(5));
    assertThat(authenticationProvider.getParams().get("main.ldapRealm.contextFactory.url"), is("ldap://localhost:33389"));
    Provider identityAssertionProvider = topology.getProvider("identity-assertion", "Default");
    assertThat(identityAssertionProvider, notNullValue());
    assertThat(identityAssertionProvider.isEnabled(), is(false));
    assertThat(identityAssertionProvider.getRole(), is("identity-assertion"));
    assertThat(identityAssertionProvider.getName(), is("Default"));
    assertThat(identityAssertionProvider.getParams().size(), is(2));
    assertThat(identityAssertionProvider.getParams().get("name"), is("user.name"));
}
Also used : TopologyBuilder(org.apache.knox.gateway.topology.builder.TopologyBuilder) Digester(org.apache.commons.digester3.Digester) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) File(java.io.File) URL(java.net.URL) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 2 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class TopologyRulesModuleTest method testParseServiceParamsInKnoxFormat.

@Test
public void testParseServiceParamsInKnoxFormat() throws IOException, SAXException {
    Digester digester = loader.newDigester();
    String name = "org/apache/knox/gateway/topology/xml/service-param-topology-knox-format.xml";
    URL url = ClassLoader.getSystemResource(name);
    assertThat("Failed to find URL for resource " + name, url, notNullValue());
    File file = new File(url.getFile());
    TopologyBuilder topologyBuilder = digester.parse(url);
    Topology topology = topologyBuilder.build();
    assertThat("Failed to parse resource " + name, topology, notNullValue());
    topology.setTimestamp(file.lastModified());
    assertThat(topology.getName(), is("test-topology-name"));
    assertThat(topology.getTimestamp(), is(file.lastModified()));
    assertThat(topology.getServices().size(), is(1));
    Provider provider = topology.getProviders().iterator().next();
    assertThat(provider, notNullValue());
    assertThat(provider.getRole(), is("test-provider-role"));
    assertThat(provider.getName(), is("test-provider-name"));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-1"), is("test-provider-param-value-1")));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-2"), is("test-provider-param-value-2")));
    Service service = topology.getServices().iterator().next();
    assertThat(service, notNullValue());
    assertThat(service.getRole(), is("test-service-role"));
    assertThat(service.getUrls().size(), is(2));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host1:42/test-service-path"));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host2:42/test-service-path"));
    assertThat(service.getName(), is("test-service-name"));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-1"), is("test-service-param-value-1")));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-2"), is("test-service-param-value-2")));
    assertThat(service.getVersion(), is(new Version(1, 0, 0)));
}
Also used : TopologyBuilder(org.apache.knox.gateway.topology.builder.TopologyBuilder) Version(org.apache.knox.gateway.topology.Version) Digester(org.apache.commons.digester3.Digester) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) File(java.io.File) URL(java.net.URL) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 3 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class DeploymentFactoryTest method test_validateNoAppsWithRootUrlsInServicesTopology.

@Test(timeout = TestUtils.SHORT_TIMEOUT)
public void test_validateNoAppsWithRootUrlsInServicesTopology() {
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(null);
    Topology topology = new Topology();
    topology.setName("test-topology");
    DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
    Service service;
    Application application;
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    topology.addApplication(application);
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
    topology = new Topology();
    topology.setName("test-topology");
    service = new Service();
    service.setName("test-service");
    service.setRole("test-service");
    topology.addService(service);
    application = new Application();
    application.setName("test-application");
    application.addUrl("/test-application");
    application.addUrl("/");
    topology.addApplication(application);
    try {
        DeploymentFactory.validateNoAppsWithRootUrlsInServicesTopology(topology);
        fail("Expected DeploymentException");
    } catch (DeploymentException e) {
    // Expected.
    }
}
Also used : Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Test(org.junit.Test)

Example 4 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class ServiceDefinitionDeploymentContributorTest method testServiceAttributeUseTwoWaySSLParamOverride.

/**
 * Test that service param useTwoWaySsl in topologies overrides the corresponding custom dispatch property.
 */
@Test
public void testServiceAttributeUseTwoWaySSLParamOverride() throws Exception {
    final String TEST_SERVICE_ROLE = "Test";
    final String USE_TWO_WAY_SSL_PARAM = "useTwoWaySsl";
    UrlRewriteRulesDescriptor clusterRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(clusterRules);
    UrlRewriteRulesDescriptor svcRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class);
    EasyMock.replay(svcRules);
    ServiceDefinition svcDef = EasyMock.createNiceMock(ServiceDefinition.class);
    EasyMock.expect(svcDef.getRole()).andReturn(TEST_SERVICE_ROLE).anyTimes();
    List<Route> svcRoutes = new ArrayList<>();
    Route route = EasyMock.createNiceMock(Route.class);
    List<Rewrite> filters = new ArrayList<>();
    EasyMock.expect(route.getRewrites()).andReturn(filters).anyTimes();
    svcRoutes.add(route);
    EasyMock.replay(route);
    EasyMock.expect(svcDef.getRoutes()).andReturn(svcRoutes).anyTimes();
    CustomDispatch cd = EasyMock.createNiceMock(CustomDispatch.class);
    EasyMock.expect(cd.getClassName()).andReturn("TestDispatch").anyTimes();
    EasyMock.expect(cd.getHaClassName()).andReturn("TestHADispatch").anyTimes();
    EasyMock.expect(cd.getHaContributorName()).andReturn(null).anyTimes();
    // Let useTwoWaySsl be FALSE by default
    EasyMock.expect(cd.getUseTwoWaySsl()).andReturn(false).anyTimes();
    EasyMock.replay(cd);
    EasyMock.expect(svcDef.getDispatch()).andReturn(cd).anyTimes();
    EasyMock.replay(svcDef);
    ServiceDefinitionDeploymentContributor sddc = new ServiceDefinitionDeploymentContributor(svcDef, svcRules);
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getDescriptor("rewrite")).andReturn(clusterRules).anyTimes();
    GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(gc.isXForwardedEnabled()).andReturn(false).anyTimes();
    EasyMock.expect(gc.isCookieScopingToPathEnabled()).andReturn(false).anyTimes();
    EasyMock.replay(gc);
    EasyMock.expect(context.getGatewayConfig()).andReturn(gc).anyTimes();
    // Configure the HaProvider
    Topology topology = EasyMock.createNiceMock(Topology.class);
    List<Provider> providers = new ArrayList<>();
    Provider haProvider = EasyMock.createNiceMock(Provider.class);
    EasyMock.expect(haProvider.getRole()).andReturn("ha").anyTimes();
    EasyMock.expect(haProvider.isEnabled()).andReturn(true).anyTimes();
    Map<String, String> providerParams = new HashMap<>();
    providerParams.put(TEST_SERVICE_ROLE, "whatever");
    EasyMock.expect(haProvider.getParams()).andReturn(providerParams).anyTimes();
    EasyMock.replay(haProvider);
    providers.add(haProvider);
    EasyMock.expect(topology.getProviders()).andReturn(providers).anyTimes();
    EasyMock.replay(topology);
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    TestGatewayDescriptor gd = new TestGatewayDescriptor();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(gd).anyTimes();
    EasyMock.replay(context);
    // Configure the service with the useTwoWaySsl param to OVERRIDE the value in the service definition
    Service service = EasyMock.createNiceMock(Service.class);
    Map<String, String> svcParams = new HashMap<>();
    svcParams.put(USE_TWO_WAY_SSL_PARAM, "true");
    EasyMock.expect(service.getParams()).andReturn(svcParams).anyTimes();
    EasyMock.replay(service);
    sddc.contributeService(context, service);
    List<ResourceDescriptor> resources = gd.resources();
    assertEquals(1, gd.resources().size());
    ResourceDescriptor res = gd.resources().get(0);
    assertNotNull(res);
    List<FilterDescriptor> filterList = res.filters();
    assertEquals(1, filterList.size());
    FilterDescriptor f = filterList.get(0);
    assertNotNull(f);
    assertEquals("dispatch", f.role());
    List<FilterParamDescriptor> fParams = f.params();
    assertNotNull(fParams);
    // Collect the values of filter params named useTwoWaySsl
    List<String> useTwoWaySslFilterParamValues = new ArrayList<>();
    for (FilterParamDescriptor param : fParams) {
        if (param.name().equals(USE_TWO_WAY_SSL_PARAM)) {
            useTwoWaySslFilterParamValues.add(param.value());
        }
    }
    assertEquals("Expected only a single filter param named " + USE_TWO_WAY_SSL_PARAM, 1, useTwoWaySslFilterParamValues.size());
    assertEquals("Expected the service param to override the service definition value for " + USE_TWO_WAY_SSL_PARAM, "true", useTwoWaySslFilterParamValues.get(0));
}
Also used : CustomDispatch(org.apache.knox.gateway.service.definition.CustomDispatch) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) FilterParamDescriptor(org.apache.knox.gateway.descriptor.FilterParamDescriptor) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) Rewrite(org.apache.knox.gateway.service.definition.Rewrite) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Example 5 with Service

use of org.apache.knox.gateway.topology.Service in project knox by apache.

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_SubsetProviderParams.

/**
 * Verify a mixture of provider-level params and service-level params.
 */
@Test
public void testServiceLevelParamOverrides_SubsetProviderParams() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleOne", "enabled=false;" + "maxRetryAttempts=5;" + "maxFailoverAttempts=4;" + "failoverSleep=40");
    Provider haProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // Specify all the possible params in the TestRoleOne service level
    Map<String, String> testRoleOneParams = new HashMap<>();
    testRoleOneParams.put("enabled", "true");
    testRoleOneParams.put("retrySleep", "60");
    testRoleOneParams.put("zookeeperNamespace", "testRoleOneOverride");
    testRoleOneParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    // A service with all the params overriden
    Service testRoleOneService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleOneService.getRole()).andReturn("TestRoleOne").anyTimes();
    EasyMock.expect(testRoleOneService.getName()).andReturn("TestRoleOneService").anyTimes();
    EasyMock.expect(testRoleOneService.getParams()).andReturn(testRoleOneParams).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    Topology topology = EasyMock.createNiceMock(Topology.class);
    EasyMock.expect(topology.getServices()).andReturn(topologyServices).anyTimes();
    EasyMock.replay(topology);
    WebArchive war = EasyMock.createNiceMock(WebArchive.class);
    EasyMock.replay(war);
    DeploymentContext context = new DescriptorCaptureDeploymentContext(topology, war);
    // Invoke the contributor
    HaProviderDeploymentContributor haPDC = new HaProviderDeploymentContributor();
    haPDC.contributeProvider(context, haProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(1, descriptor.getServiceConfigs().size());
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 40, 4, 60, 5, "testRoleOneOverride", "http://host3:2181,http://host4:2181");
}
Also used : HashMap(java.util.HashMap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Provider(org.apache.knox.gateway.topology.Provider) DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) HaDescriptor(org.apache.knox.gateway.ha.provider.HaDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Service (org.apache.knox.gateway.topology.Service)25 Topology (org.apache.knox.gateway.topology.Topology)22 Test (org.junit.Test)18 Provider (org.apache.knox.gateway.topology.Provider)17 HashMap (java.util.HashMap)13 File (java.io.File)11 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)9 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)7 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)7 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)7 Param (org.apache.knox.gateway.topology.Param)7 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)6 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)6 URL (java.net.URL)5 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)5 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)5 Document (org.w3c.dom.Document)5 HashSet (java.util.HashSet)4 Digester (org.apache.commons.digester3.Digester)4 TopologyBuilder (org.apache.knox.gateway.topology.builder.TopologyBuilder)4