Search in sources :

Example 1 with Route

use of org.apache.knox.gateway.service.definition.Route 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 2 with Route

use of org.apache.knox.gateway.service.definition.Route in project knox by apache.

the class ApplicationDeploymentContributor method loadServiceDefinition.

private static ServiceDefinition loadServiceDefinition(Application application, File file) throws JAXBException, FileNotFoundException, IOException {
    ServiceDefinition definition;
    if (!file.exists()) {
        definition = new ServiceDefinition();
        definition.setName(application.getName());
        List<Route> routes = new ArrayList<Route>(1);
        Route route;
        route = new Route();
        route.setPath("/?**");
        routes.add(route);
        route = new Route();
        route.setPath("/**?**");
        routes.add(route);
        definition.setRoutes(routes);
    } else {
        JAXBContext context = JAXBContext.newInstance(ServiceDefinition.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        try (FileInputStream inputStream = new FileInputStream(file)) {
            definition = (ServiceDefinition) unmarshaller.unmarshal(inputStream);
        }
    }
    return definition;
}
Also used : ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) FileInputStream(java.io.FileInputStream)

Example 3 with Route

use of org.apache.knox.gateway.service.definition.Route in project knox by apache.

the class ApplicationDeploymentContributor method contributeResources.

private void contributeResources(DeploymentContext context, Service service) {
    Map<String, String> filterParams = new HashMap<>();
    List<Route> bindings = serviceDefinition.getRoutes();
    for (Route binding : bindings) {
        List<Rewrite> filters = binding.getRewrites();
        if (filters != null && !filters.isEmpty()) {
            filterParams.clear();
            for (Rewrite filter : filters) {
                filterParams.put(filter.getTo(), filter.getApply());
            }
        }
        try {
            contributeResource(context, service, binding, filterParams);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
Also used : HashMap(java.util.HashMap) Rewrite(org.apache.knox.gateway.service.definition.Rewrite) URISyntaxException(java.net.URISyntaxException) Route(org.apache.knox.gateway.service.definition.Route)

Example 4 with Route

use of org.apache.knox.gateway.service.definition.Route in project knox by apache.

the class ServiceDefinitionDeploymentContributor method contributeResources.

private void contributeResources(DeploymentContext context, Service service) {
    Map<String, String> filterParams = new HashMap<>();
    List<Route> bindings = serviceDefinition.getRoutes();
    for (Route binding : bindings) {
        List<Rewrite> filters = binding.getRewrites();
        if (filters != null && !filters.isEmpty()) {
            filterParams.clear();
            for (Rewrite filter : filters) {
                filterParams.put(filter.getTo(), filter.getApply());
            }
        }
        try {
            contributeResource(context, service, binding, filterParams);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
Also used : HashMap(java.util.HashMap) Rewrite(org.apache.knox.gateway.service.definition.Rewrite) URISyntaxException(java.net.URISyntaxException) Route(org.apache.knox.gateway.service.definition.Route)

Example 5 with Route

use of org.apache.knox.gateway.service.definition.Route in project knox by apache.

the class DefaultServiceDefinitionRegistry method init.

@Override
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
    String stacks = config.getGatewayServicesDir();
    File stacksDir = new File(stacks);
    Set<ServiceDefinition> serviceDefinitions = ServiceDefinitionsLoader.getServiceDefinitions(stacksDir);
    for (ServiceDefinition serviceDefinition : serviceDefinitions) {
        List<Route> routes = serviceDefinition.getRoutes();
        for (Route route : routes) {
            try {
                Template template = Parser.parseTemplate(route.getPath());
                addServiceDefEntry(template, serviceDefinition);
            } catch (URISyntaxException e) {
                LOG.failedToParsePath(route.getPath(), e);
            }
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) File(java.io.File) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) Template(org.apache.knox.gateway.util.urltemplate.Template)

Aggregations

Route (org.apache.knox.gateway.service.definition.Route)5 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Rewrite (org.apache.knox.gateway.service.definition.Rewrite)3 ServiceDefinition (org.apache.knox.gateway.service.definition.ServiceDefinition)3 ArrayList (java.util.ArrayList)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 JAXBContext (javax.xml.bind.JAXBContext)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)1 FilterDescriptor (org.apache.knox.gateway.descriptor.FilterDescriptor)1 FilterParamDescriptor (org.apache.knox.gateway.descriptor.FilterParamDescriptor)1 ResourceDescriptor (org.apache.knox.gateway.descriptor.ResourceDescriptor)1 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)1 CustomDispatch (org.apache.knox.gateway.service.definition.CustomDispatch)1 Provider (org.apache.knox.gateway.topology.Provider)1 Service (org.apache.knox.gateway.topology.Service)1 Topology (org.apache.knox.gateway.topology.Topology)1