Search in sources :

Example 41 with Topology

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

the class ShiroDeploymentContributorTest method testDeployment.

@Test
public void testDeployment() throws IOException {
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-archive");
    Map<String, String> providerParams = new HashMap<>();
    Provider provider = new Provider();
    provider.setEnabled(true);
    provider.setName("shiro");
    provider.setParams(providerParams);
    Topology topology = new Topology();
    topology.setName("Sample");
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.expect(context.getWebAppDescriptor()).andReturn(Descriptors.create(WebAppDescriptor.class)).anyTimes();
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    EasyMock.replay(context);
    AliasService as = EasyMock.createNiceMock(AliasService.class);
    CryptoService cryptoService = new DefaultCryptoService();
    ((DefaultCryptoService) cryptoService).setAliasService(as);
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService).anyTimes();
    ShiroDeploymentContributor contributor = new ShiroDeploymentContributor();
    assertThat(contributor.getRole(), is("authentication"));
    assertThat(contributor.getName(), is("ShiroProvider"));
    // Just make sure it doesn't blow up.
    contributor.initializeContribution(context);
    contributor.contributeProvider(context, provider);
    // Just make sure it doesn't blow up.
    contributor.finalizeContribution(context);
    assertThat(context.getWebAppDescriptor().getOrCreateSessionConfig().getOrCreateCookieConfig().isHttpOnly(), is(true));
    assertThat(context.getWebAppDescriptor().getOrCreateSessionConfig().getOrCreateCookieConfig().isSecure(), is(true));
}
Also used : DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) GatewayServices(org.apache.knox.gateway.services.GatewayServices) AliasService(org.apache.knox.gateway.services.security.AliasService) HashMap(java.util.HashMap) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) CryptoService(org.apache.knox.gateway.services.security.CryptoService) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Topology(org.apache.knox.gateway.topology.Topology) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 42 with Topology

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

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_MultipleMixed.

@Test
public void testServiceLevelParamOverrides_MultipleMixed() throws Exception {
    // Define some provider params
    Map<String, String> providerParams = new HashMap<>();
    // Specify a subset of the possible HaProvider-level params for TestRoleOne
    providerParams.put("TestRoleOne", "enabled=true;maxRetryAttempts=1;retrySleep=10;maxFailoverAttempts=2;failoverSleep=20");
    // Specify all the possible params at the HaProvider level for TestRoleTwo
    providerParams.put("TestRoleTwo", "enabled=false;" + "maxRetryAttempts=3;" + "retrySleep=30;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleTwo;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
    Provider testHaProvider = createHaProvider(providerParams);
    // Define the topology content (e.g., services)
    Collection<Service> topologyServices = new HashSet<>();
    // A service with no param overrides
    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(Collections.emptyMap()).anyTimes();
    EasyMock.replay(testRoleOneService);
    topologyServices.add(testRoleOneService);
    // Override all the possible params in the TestRoleTwo service level
    Map<String, String> testRoleTwoParams = new HashMap<>();
    testRoleTwoParams.put("enabled", "true");
    testRoleTwoParams.put("maxRetryAttempts", "6");
    testRoleTwoParams.put("retrySleep", "60");
    testRoleTwoParams.put("maxFailoverAttempts", "8");
    testRoleTwoParams.put("failoverSleep", "80");
    testRoleTwoParams.put("zookeeperNamespace", "testRoleTwoOverride");
    testRoleTwoParams.put("zookeeperEnsemble", "http://host3:2181,http://host4:2181");
    Service testRoleTwoService = EasyMock.createNiceMock(Service.class);
    EasyMock.expect(testRoleTwoService.getRole()).andReturn("TestRoleTwo").anyTimes();
    EasyMock.expect(testRoleTwoService.getName()).andReturn("TestRoleTwoService").anyTimes();
    EasyMock.expect(testRoleTwoService.getParams()).andReturn(testRoleTwoParams).anyTimes();
    EasyMock.replay(testRoleTwoService);
    topologyServices.add(testRoleTwoService);
    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, testHaProvider);
    HaDescriptor descriptor = context.getDescriptor("ha.provider.descriptor");
    assertNotNull(descriptor);
    assertEquals(2, descriptor.getServiceConfigs().size());
    // Validate the service with no-overrides, checking that the provider-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleOne"), true, 20, 2, 10, 1, null, null);
    // Validate the service with all-overrides, checking that the service-level defaults are applied
    validateServiceHaConfig(descriptor.getServiceConfig("TestRoleTwo"), true, 80, 8, 60, 6, "testRoleTwoOverride", "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)

Example 43 with Topology

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

the class GatewayFilterTest method testDefaultServicePathTopologyRequestAttribute.

@Test
public void testDefaultServicePathTopologyRequestAttribute() throws Exception {
    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.replay(config);
    Topology topology = EasyMock.createNiceMock(Topology.class);
    topology.setDefaultServicePath("test-role/");
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
    EasyMock.expect(topology.getDefaultServicePath()).andReturn("test-role").anyTimes();
    EasyMock.expect(request.getPathInfo()).andReturn("/test-path/test-resource").anyTimes();
    EasyMock.expect(request.getServletContext()).andReturn(context).anyTimes();
    EasyMock.expect(context.getAttribute(GatewayConfig.GATEWAY_CONFIG_ATTRIBUTE)).andReturn(gatewayConfig).anyTimes();
    EasyMock.expect(gatewayConfig.getHeaderNameForRemoteAddress()).andReturn("Custom-Forwarded-For").anyTimes();
    EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer("http://host:8443/gateway/sandbox/test-path/test-resource/")).anyTimes();
    EasyMock.expect(context.getAttribute("org.apache.knox.gateway.topology")).andReturn(topology).anyTimes();
    EasyMock.replay(request);
    EasyMock.replay(context);
    EasyMock.replay(topology);
    EasyMock.replay(gatewayConfig);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(response);
    TestRoleFilter filter = new TestRoleFilter();
    GatewayFilter gateway = new GatewayFilter();
    gateway.addFilter("test-role/**/**", "test-filter", filter, null, "test-role");
    gateway.init(config);
    gateway.doFilter(request, response);
    gateway.destroy();
    assertThat((String) filter.defaultServicePath, is("test-role"));
    assertThat((String) filter.url, is("http://host:8443/gateway/sandbox/test-role/test-path/test-resource"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Topology(org.apache.knox.gateway.topology.Topology) AbstractGatewayFilter(org.apache.knox.gateway.filter.AbstractGatewayFilter) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 44 with Topology

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

the class DefaultTopologyService method deployTopology.

public void deployTopology(Topology t) {
    try {
        File temp = new File(topologiesDirectory.getAbsolutePath() + "/" + t.getName() + ".xml.temp");
        Package topologyPkg = Topology.class.getPackage();
        String pkgName = topologyPkg.getName();
        String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";
        Map<String, Object> properties = new HashMap<>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
        JAXBContext jc = JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(), properties);
        Marshaller mr = jc.createMarshaller();
        mr.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        mr.marshal(t, temp);
        File topology = new File(topologiesDirectory.getAbsolutePath() + "/" + t.getName() + ".xml");
        if (!temp.renameTo(topology)) {
            FileUtils.forceDelete(temp);
            throw new IOException("Could not rename temp file");
        }
        // This code will check if the topology is valid, and retrieve the errors if it is not.
        TopologyValidator validator = new TopologyValidator(topology.getAbsolutePath());
        if (!validator.validateTopology()) {
            throw new SAXException(validator.getErrorString());
        }
    } catch (JAXBException e) {
        auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
        log.failedToDeployTopology(t.getName(), e);
    } catch (IOException io) {
        auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
        log.failedToDeployTopology(t.getName(), io);
    } catch (SAXException sx) {
        auditor.audit(Action.DEPLOY, t.getName(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
        log.failedToDeployTopology(t.getName(), sx);
    }
    reloadTopologies();
}
Also used : Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) TopologyValidator(org.apache.knox.gateway.topology.validation.TopologyValidator) File(java.io.File)

Example 45 with Topology

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

the class DefaultTopologyService method loadTopologies.

private Map<File, Topology> loadTopologies(File directory) {
    Map<File, Topology> map = new HashMap<>();
    if (directory.isDirectory() && directory.canRead()) {
        File[] existingTopologies = directory.listFiles(this);
        if (existingTopologies != null) {
            for (File file : existingTopologies) {
                try {
                    Topology loadTopology = loadTopology(file);
                    if (null != loadTopology) {
                        map.put(file, loadTopology);
                    } else {
                        auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
                        log.failedToLoadTopology(file.getAbsolutePath());
                    }
                } catch (IOException e) {
                    // Maybe it makes sense to throw exception
                    auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
                    log.failedToLoadTopology(file.getAbsolutePath(), e);
                } catch (SAXException e) {
                    // Maybe it makes sense to throw exception
                    auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
                    log.failedToLoadTopology(file.getAbsolutePath(), e);
                } catch (Exception e) {
                    // Maybe it makes sense to throw exception
                    auditor.audit(Action.LOAD, file.getAbsolutePath(), ResourceType.TOPOLOGY, ActionOutcome.FAILURE);
                    log.failedToLoadTopology(file.getAbsolutePath(), e);
                }
            }
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Topology(org.apache.knox.gateway.topology.Topology) IOException(java.io.IOException) File(java.io.File) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) SAXException(org.xml.sax.SAXException)

Aggregations

Topology (org.apache.knox.gateway.topology.Topology)52 Test (org.junit.Test)36 HashMap (java.util.HashMap)23 Service (org.apache.knox.gateway.topology.Service)22 File (java.io.File)20 Provider (org.apache.knox.gateway.topology.Provider)20 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)17 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)10 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)9 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 IOException (java.io.IOException)8 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)8 URL (java.net.URL)7 TopologyService (org.apache.knox.gateway.services.topology.TopologyService)7 Application (org.apache.knox.gateway.topology.Application)7 Param (org.apache.knox.gateway.topology.Param)7 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)7 Document (org.w3c.dom.Document)7 Digester (org.apache.commons.digester3.Digester)6