Search in sources :

Example 11 with Provider

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

the class HaProviderDeploymentContributorTest method testProviderLevelParams.

/**
 * Basically, a backward-compatibility test to ensure that HaProvider service params specified ONLY at the provider
 * level still work.
 */
@Test
public void testProviderLevelParams() 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;" + "retrySleep=50;" + "maxFailoverAttempts=4;" + "failoverSleep=40;" + "zookeeperNamespace=testRoleOne;" + "zookeeperEnsemble=http://host1:2181,http://host2:2181");
    Provider haProvider = 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);
    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"), false, 40, 4, 50, 5, "testRoleOne", "http://host1:2181,http://host2: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 12 with Provider

use of org.apache.knox.gateway.topology.Provider 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 13 with Provider

use of org.apache.knox.gateway.topology.Provider 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 14 with Provider

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

the class ProviderPropertyInterpreter method interpret.

@Override
public void interpret(String token, String value) throws InterpretException {
    int dotPosition = token.indexOf(DOT);
    if (dotPosition == -1) {
        throw new InterpretException(gatewayResources.unsupportedPropertyTokenError(token));
    }
    String providerRole = token.substring(0, dotPosition);
    if (providerRole != null && providerRole.isEmpty()) {
        providerRole = null;
    }
    String nextToken = token.substring(dotPosition + 1);
    dotPosition = nextToken.indexOf(DOT);
    if (dotPosition == -1) {
        throw new InterpretException(gatewayResources.unsupportedPropertyTokenError(token));
    }
    String providerName = nextToken.substring(0, dotPosition);
    if (providerName != null && providerName.isEmpty()) {
        providerName = null;
    }
    nextToken = nextToken.substring(dotPosition + 1);
    Provider provider = topology.getProvider(providerRole, providerName);
    if (provider == null) {
        provider = new Provider();
        provider.setName(providerName);
        provider.setRole(providerRole);
        topology.addProvider(provider);
    }
    if (PROVIDER_ENABLED.equalsIgnoreCase(nextToken)) {
        provider.setEnabled(Boolean.valueOf(value));
    } else {
        dotPosition = nextToken.indexOf(DOT);
        if (dotPosition != -1) {
            String aggregator = nextToken.substring(0, dotPosition);
            nextToken = nextToken.substring(dotPosition + 1);
            if (AGGREGATOR_PARAM.equalsIgnoreCase(aggregator)) {
                new ProviderParameterPropertyInterpreter(provider).interpret(nextToken, value);
            } else {
                throw new InterpretException(gatewayResources.unsupportedPropertyTokenError(token));
            }
        } else {
            throw new InterpretException(gatewayResources.unsupportedPropertyTokenError(token));
        }
    }
}
Also used : Provider(org.apache.knox.gateway.topology.Provider)

Example 15 with Provider

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

the class GatewayAdminTopologyFuncTest method createTestTopology.

private Topology createTestTopology() {
    Topology topology = new Topology();
    topology.setName("test-topology");
    try {
        topology.setUri(new URI(gatewayUrl + "/" + topology.getName()));
    } catch (URISyntaxException ex) {
        assertThat(topology.getUri(), not(nullValue()));
    }
    Provider identityProvider = new Provider();
    identityProvider.setName("Default");
    identityProvider.setRole("identity-assertion");
    identityProvider.setEnabled(true);
    Provider AuthenicationProvider = new Provider();
    AuthenicationProvider.setName("ShiroProvider");
    AuthenicationProvider.setRole("authentication");
    AuthenicationProvider.setEnabled(true);
    Param ldapMain = new Param();
    ldapMain.setName("main.ldapRealm");
    ldapMain.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapRealm");
    Param ldapGroupContextFactory = new Param();
    ldapGroupContextFactory.setName("main.ldapGroupContextFactory");
    ldapGroupContextFactory.setValue("org.apache.knox.gateway.shirorealm.KnoxLdapContextFactory");
    Param ldapRealmContext = new Param();
    ldapRealmContext.setName("main.ldapRealm.contextFactory");
    ldapRealmContext.setValue("$ldapGroupContextFactory");
    Param ldapURL = new Param();
    ldapURL.setName("main.ldapRealm.contextFactory.url");
    ldapURL.setValue(driver.getLdapUrl());
    Param ldapUserTemplate = new Param();
    ldapUserTemplate.setName("main.ldapRealm.userDnTemplate");
    ldapUserTemplate.setValue("uid={0},ou=people,dc=hadoop,dc=apache,dc=org");
    Param authcBasic = new Param();
    authcBasic.setName("urls./**");
    authcBasic.setValue("authcBasic");
    AuthenicationProvider.addParam(ldapGroupContextFactory);
    AuthenicationProvider.addParam(ldapMain);
    AuthenicationProvider.addParam(ldapRealmContext);
    AuthenicationProvider.addParam(ldapURL);
    AuthenicationProvider.addParam(ldapUserTemplate);
    AuthenicationProvider.addParam(authcBasic);
    Service testService = new Service();
    testService.setRole("test-service-role");
    topology.addProvider(AuthenicationProvider);
    topology.addProvider(identityProvider);
    topology.addService(testService);
    topology.setTimestamp(System.nanoTime());
    return topology;
}
Also used : Param(org.apache.knox.gateway.topology.Param) TopologyService(org.apache.knox.gateway.services.topology.TopologyService) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Provider(org.apache.knox.gateway.topology.Provider)

Aggregations

Provider (org.apache.knox.gateway.topology.Provider)30 Topology (org.apache.knox.gateway.topology.Topology)20 Test (org.junit.Test)20 Service (org.apache.knox.gateway.topology.Service)17 HashMap (java.util.HashMap)14 DeploymentContext (org.apache.knox.gateway.deploy.DeploymentContext)10 File (java.io.File)9 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)9 Param (org.apache.knox.gateway.topology.Param)7 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)6 GatewayTestConfig (org.apache.knox.gateway.GatewayTestConfig)5 DefaultGatewayServices (org.apache.knox.gateway.services.DefaultGatewayServices)5 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)5 URL (java.net.URL)4 HashSet (java.util.HashSet)4 Digester (org.apache.commons.digester3.Digester)4 HaDescriptor (org.apache.knox.gateway.ha.provider.HaDescriptor)4 TopologyBuilder (org.apache.knox.gateway.topology.builder.TopologyBuilder)4 FilterParamDescriptor (org.apache.knox.gateway.descriptor.FilterParamDescriptor)3 GatewayServices (org.apache.knox.gateway.services.GatewayServices)3