Search in sources :

Example 6 with DeploymentContext

use of org.apache.knox.gateway.deploy.DeploymentContext in project knox by apache.

the class HaProviderDeploymentContributorTest method testServiceLevelParamOverrides_NoProviderParams.

/**
 * Simple test verifying that HaProvider service params specified ONLY at the service level works.
 */
@Test
public void testServiceLevelParamOverrides_NoProviderParams() 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", "");
    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("maxRetryAttempts", "6");
    testRoleOneParams.put("retrySleep", "60");
    testRoleOneParams.put("maxFailoverAttempts", "8");
    testRoleOneParams.put("failoverSleep", "80");
    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, 80, 8, 60, 6, "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)

Example 7 with DeploymentContext

use of org.apache.knox.gateway.deploy.DeploymentContext in project knox by apache.

the class JerseyDeploymentContributorTest method testDeploymentContributors.

@Test
public void testDeploymentContributors() throws Exception {
    JerseyDispatchDeploymentContributor providerContributor = new JerseyDispatchDeploymentContributor();
    assertThat(providerContributor.getRole(), is("pivot"));
    assertThat(providerContributor.getName(), is("jersey"));
    MockJerseyService serviceContributor = new MockJerseyService();
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-archive");
    Topology topology = new Topology();
    topology.setName("test-topology");
    Provider provider = new Provider();
    provider.setRole("pivot");
    provider.setName("jersey");
    provider.setEnabled(true);
    topology.addProvider(provider);
    GatewayDescriptor descriptor = GatewayDescriptorFactory.create();
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes();
    EasyMock.expect(context.getGatewayDescriptor()).andReturn(descriptor).anyTimes();
    context.contributeFilter(EasyMock.<Service>isA(Service.class), EasyMock.<ResourceDescriptor>isA(ResourceDescriptor.class), EasyMock.<String>isA(String.class), EasyMock.<String>isA(String.class), EasyMock.<List>isA(List.class));
    EasyMock.expectLastCall().andDelegateTo(new MockDeploymentContext(context, providerContributor, provider)).anyTimes();
    EasyMock.replay(context);
    // Just make sure they don't blow up.
    providerContributor.initializeContribution(context);
    serviceContributor.initializeContribution(context);
    Service service = new Service();
    service.setRole("test-service-role");
    service.setName("test-service-name");
    service.addUrl("http://test-service-host:777/test-service-path");
    // This should end up calling providerContributor.contributeFilter
    serviceContributor.contributeService(context, service);
    ResourceDescriptor resource = context.getGatewayDescriptor().resources().get(0);
    // Just make sure they don't blow up.
    serviceContributor.finalizeContribution(context);
    providerContributor.finalizeContribution(context);
    /*
    GatewayDescriptorFactory.store( descriptor, "xml", new PrintWriter( System.out ) );
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <gateway>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
      <resource>
        <role>test-service-role</role>
        <pattern>test-service/**?**</pattern>
        <filter>
          <role>dispatch</role>
          <name>jersey</name>
          <class>org.glassfish.jersey.servlet.ServletContainer</class>
          <param>
            <name>jersey.config.server.provider.packages</name>
            <value>test-package-1;test-package-2</value>
          </param>
        </filter>
      </resource>
    </gateway>
    */
    List<ResourceDescriptor> resources = context.getGatewayDescriptor().resources();
    assertThat(resources.size(), is(2));
    resource = resources.get(0);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/?**"));
    List<FilterDescriptor> filters = resource.filters();
    assertThat(filters.size(), is(1));
    FilterDescriptor filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    List<FilterParamDescriptor> params = filter.params();
    assertThat(params.size(), is(1));
    FilterParamDescriptor param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
    resource = resources.get(1);
    assertThat(resource.role(), is("test-service-role"));
    assertThat(resource.pattern(), is("test-service/**?**"));
    filters = resource.filters();
    assertThat(filters.size(), is(1));
    filter = filters.get(0);
    assertThat(filter.role(), is("pivot"));
    assertThat(filter.name(), is("jersey"));
    assertThat(filter.impl(), is("org.glassfish.jersey.servlet.ServletContainer"));
    params = filter.params();
    assertThat(params.size(), is(1));
    param = params.get(0);
    assertThat(param.name(), is("jersey.config.server.provider.packages"));
    assertThat(param.value(), is("test-package-1;test-package-2"));
}
Also used : GatewayDescriptor(org.apache.knox.gateway.descriptor.GatewayDescriptor) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) 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) FilterDescriptor(org.apache.knox.gateway.descriptor.FilterDescriptor) List(java.util.List) ResourceDescriptor(org.apache.knox.gateway.descriptor.ResourceDescriptor) Test(org.junit.Test)

Example 8 with DeploymentContext

use of org.apache.knox.gateway.deploy.DeploymentContext in project knox by apache.

the class HostmapDeploymentContributorTest method testDeployment.

@Test
public void testDeployment() throws IOException {
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test-acrhive");
    UrlRewriteRulesDescriptorImpl rewriteRules = new UrlRewriteRulesDescriptorImpl();
    Map<String, String> providerParams = new HashMap<>();
    providerParams.put("test-host-external", "test-host-internal");
    Provider provider = new Provider();
    provider.setEnabled(true);
    provider.setName("hostmap");
    provider.setParams(providerParams);
    DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class);
    EasyMock.expect(context.getDescriptor("rewrite")).andReturn(rewriteRules).anyTimes();
    EasyMock.expect(context.getWebArchive()).andReturn(webArchive).anyTimes();
    EasyMock.replay(context);
    HostmapDeploymentContributor contributor = new HostmapDeploymentContributor();
    assertThat(contributor.getRole(), is("hostmap"));
    assertThat(contributor.getName(), is("static"));
    // Just make sure it doesn't blow up.
    contributor.contributeFilter(null, null, null, null, null);
    // Just make sure it doesn't blow up.
    contributor.initializeContribution(context);
    contributor.contributeProvider(context, provider);
    HostmapFunctionDescriptor funcDesc = rewriteRules.getFunction("hostmap");
    assertThat(funcDesc.config(), is("/WEB-INF/hostmap.txt"));
    Node node = webArchive.get("/WEB-INF/hostmap.txt");
    String asset = IOUtils.toString(node.getAsset().openStream());
    assertThat(asset, containsString("test-host-external=test-host-internal"));
    // Just make sure it doesn't blow up.
    contributor.finalizeContribution(context);
}
Also used : DeploymentContext(org.apache.knox.gateway.deploy.DeploymentContext) UrlRewriteRulesDescriptorImpl(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRulesDescriptorImpl) HashMap(java.util.HashMap) HostmapFunctionDescriptor(org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) Node(org.jboss.shrinkwrap.api.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Provider(org.apache.knox.gateway.topology.Provider) Test(org.junit.Test)

Example 9 with DeploymentContext

use of org.apache.knox.gateway.deploy.DeploymentContext 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 10 with DeploymentContext

use of org.apache.knox.gateway.deploy.DeploymentContext 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)

Aggregations

DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)10 Provider (org.apache.knox.gateway.topology.Provider)10 Test (org.junit.Test)10 Topology (org.apache.knox.gateway.topology.Topology)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 HashMap (java.util.HashMap)8 Service (org.apache.knox.gateway.topology.Service)6 HashSet (java.util.HashSet)4 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)4 GatewayServices (org.apache.knox.gateway.services.GatewayServices)3 AliasService (org.apache.knox.gateway.services.security.AliasService)3 CryptoService (org.apache.knox.gateway.services.security.CryptoService)3 DefaultCryptoService (org.apache.knox.gateway.services.security.impl.DefaultCryptoService)3 FilterDescriptor (org.apache.knox.gateway.descriptor.FilterDescriptor)2 FilterParamDescriptor (org.apache.knox.gateway.descriptor.FilterParamDescriptor)2 ResourceDescriptor (org.apache.knox.gateway.descriptor.ResourceDescriptor)2 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1