Search in sources :

Example 26 with Provider

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

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

the class BeanPropertyTopologyBuilder method build.

public Topology build() {
    Topology topology = new Topology();
    topology.setName(name);
    topology.setDefaultServicePath(defaultService);
    topology.setGenerated(isGenerated);
    for (Provider provider : providers) {
        topology.addProvider(provider);
    }
    for (Service service : services) {
        topology.addService(service);
    }
    for (Application application : applications) {
        topology.addApplication(application);
    }
    return topology;
}
Also used : Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Application(org.apache.knox.gateway.topology.Application) Provider(org.apache.knox.gateway.topology.Provider)

Example 28 with Provider

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

the class DeploymentFactoryFuncTest method testSimpleTopology.

@Test(timeout = MEDIUM_TIMEOUT)
public void testSimpleTopology() throws IOException, SAXException, ParserConfigurationException, URISyntaxException, TransformerException {
    LOG_ENTER();
    GatewayConfig config = new GatewayTestConfig();
    // Testing without x-forwarded headers filter
    ((GatewayTestConfig) config).setXForwardedEnabled(false);
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    ((GatewayTestConfig) config).setGatewayHomeDir(gatewayDir.getAbsolutePath());
    File deployDir = new File(config.getGatewayDeploymentDir());
    deployDir.mkdirs();
    DefaultGatewayServices srvcs = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        DeploymentFactory.setGatewayServices(srvcs);
        srvcs.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    Topology topology = new Topology();
    topology.setName("test-cluster");
    Service service = new Service();
    service.setRole("WEBHDFS");
    service.addUrl("http://localhost:50070/webhdfs");
    topology.addService(service);
    Provider provider = new Provider();
    provider.setRole("authentication");
    provider.setName("ShiroProvider");
    provider.setEnabled(true);
    Param param = new Param();
    param.setName("contextConfigLocation");
    param.setValue("classpath:app-context-security.xml");
    provider.addParam(param);
    topology.addProvider(provider);
    Provider asserter = new Provider();
    asserter.setRole("identity-assertion");
    asserter.setName("Default");
    asserter.setEnabled(true);
    topology.addProvider(asserter);
    Provider authorizer = new Provider();
    authorizer.setRole("authorization");
    authorizer.setName("AclsAuthz");
    authorizer.setEnabled(true);
    topology.addProvider(authorizer);
    EnterpriseArchive war = DeploymentFactory.createDeployment(config, topology);
    // File dir = new File( System.getProperty( "user.dir" ) );
    // File file = war.as( ExplodedExporter.class ).exportExploded( dir, "test-cluster.war" );
    Document web = XmlUtils.readXml(war.get("%2F/WEB-INF/web.xml").getAsset().openStream());
    assertThat(web, hasXPath("/web-app"));
    assertThat(web, hasXPath("/web-app/servlet"));
    assertThat(web, hasXPath("/web-app/servlet/servlet-name"));
    assertThat(web, hasXPath("/web-app/servlet/servlet-name", equalTo("test-cluster-knox-gateway-servlet")));
    assertThat(web, hasXPath("/web-app/servlet/servlet-class", equalTo("org.apache.knox.gateway.GatewayServlet")));
    assertThat(web, hasXPath("/web-app/servlet/init-param/param-name", equalTo("gatewayDescriptorLocation")));
    assertThat(web, hasXPath("/web-app/servlet/init-param/param-value", equalTo("/WEB-INF/gateway.xml")));
    assertThat(web, hasXPath("/web-app/servlet-mapping/servlet-name", equalTo("test-cluster-knox-gateway-servlet")));
    assertThat(web, hasXPath("/web-app/servlet-mapping/url-pattern", equalTo("/*")));
    Document gateway = XmlUtils.readXml(war.get("%2F/WEB-INF/gateway.xml").getAsset().openStream());
    assertThat(gateway, hasXPath("/gateway/resource[1]/pattern", equalTo("/webhdfs/v1/?**")));
    // assertThat( gateway, hasXPath( "/gateway/resource[1]/target", equalTo( "http://localhost:50070/webhdfs/v1/?{**}" ) ) );
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[1]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[1]/class", equalTo("org.apache.knox.gateway.filter.ResponseCookieFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/class", equalTo("org.apache.shiro.web.servlet.ShiroFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[3]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[3]/class", equalTo("org.apache.knox.gateway.filter.ShiroSubjectIdentityAdapter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/role", equalTo("rewrite")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/class", equalTo("org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[5]/role", equalTo("identity-assertion")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[5]/class", equalTo("org.apache.knox.gateway.identityasserter.filter.IdentityAsserterFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/role", equalTo("authorization")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/name", equalTo("AclsAuthz")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/class", equalTo("org.apache.knox.gateway.filter.AclsAuthorizationFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[7]/role", equalTo("dispatch")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[7]/name", equalTo("webhdfs")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[7]/class", equalTo("org.apache.knox.gateway.dispatch.GatewayDispatchFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/pattern", equalTo("/webhdfs/v1/**?**")));
    // assertThat( gateway, hasXPath( "/gateway/resource[2]/target", equalTo( "http://localhost:50070/webhdfs/v1/{path=**}?{**}" ) ) );
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[1]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[1]/class", equalTo("org.apache.knox.gateway.filter.ResponseCookieFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[2]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[2]/class", equalTo("org.apache.shiro.web.servlet.ShiroFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[3]/role", equalTo("authentication")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[3]/class", equalTo("org.apache.knox.gateway.filter.ShiroSubjectIdentityAdapter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[4]/role", equalTo("rewrite")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[4]/class", equalTo("org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[5]/role", equalTo("identity-assertion")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[5]/class", equalTo("org.apache.knox.gateway.identityasserter.filter.IdentityAsserterFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/role", equalTo("authorization")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/name", equalTo("AclsAuthz")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[6]/class", equalTo("org.apache.knox.gateway.filter.AclsAuthorizationFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[7]/role", equalTo("dispatch")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[7]/name", equalTo("webhdfs")));
    assertThat(gateway, hasXPath("/gateway/resource[2]/filter[7]/class", equalTo("org.apache.knox.gateway.dispatch.GatewayDispatchFilter")));
    LOG_EXIT();
}
Also used : EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) HashMap(java.util.HashMap) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Document(org.w3c.dom.Document) GatewayTestConfig(org.apache.knox.gateway.GatewayTestConfig) Provider(org.apache.knox.gateway.topology.Provider) Param(org.apache.knox.gateway.topology.Param) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 29 with Provider

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

the class DeploymentFactoryFuncTest method testGenericProviderDeploymentContributor.

@Test(timeout = MEDIUM_TIMEOUT)
public void testGenericProviderDeploymentContributor() throws ParserConfigurationException, SAXException, IOException, TransformerException {
    LOG_ENTER();
    GatewayConfig config = new GatewayTestConfig();
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    ((GatewayTestConfig) config).setGatewayHomeDir(gatewayDir.getAbsolutePath());
    File deployDir = new File(config.getGatewayDeploymentDir());
    deployDir.mkdirs();
    // ((GatewayTestConfig) config).setDeploymentDir( "clusters" );
    DefaultGatewayServices srvcs = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        DeploymentFactory.setGatewayServices(srvcs);
        srvcs.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    Topology topology = new Topology();
    topology.setName("test-cluster");
    Service service = new Service();
    service.setRole("WEBHDFS");
    service.addUrl("http://localhost:50070/test-service-url");
    topology.addService(service);
    Provider provider = new Provider();
    provider.setRole("federation");
    provider.setName("HeaderPreAuth");
    provider.setEnabled(true);
    Param param = new Param();
    param.setName("filter");
    param.setValue("org.opensource.ExistingFilter");
    provider.addParam(param);
    param = new Param();
    param.setName("test-param-name");
    param.setValue("test-param-value");
    provider.addParam(param);
    topology.addProvider(provider);
    EnterpriseArchive war = DeploymentFactory.createDeployment(config, topology);
    Document gateway = XmlUtils.readXml(war.get("%2F/WEB-INF/gateway.xml").getAsset().openStream());
    // dump( gateway );
    // by default the first filter will be the X-Forwarded header filter
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[1]/role", equalTo("xforwardedheaders")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[1]/name", equalTo("XForwardedHeaderFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[1]/class", equalTo("org.apache.knox.gateway.filter.XForwardedHeaderFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/role", equalTo("federation")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/name", equalTo("HeaderPreAuth")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/class", equalTo("org.apache.knox.gateway.preauth.filter.HeaderPreAuthFederationFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/param[1]/name", equalTo("filter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/param[1]/value", equalTo("org.opensource.ExistingFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/param[2]/name", equalTo("test-param-name")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/param[2]/value", equalTo("test-param-value")));
    // testing for the adding of missing identity assertion provider - since it isn't explicitly added above
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/role", equalTo("identity-assertion")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/name", equalTo("Default")));
    LOG_EXIT();
}
Also used : EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) HashMap(java.util.HashMap) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) Document(org.w3c.dom.Document) GatewayTestConfig(org.apache.knox.gateway.GatewayTestConfig) Provider(org.apache.knox.gateway.topology.Provider) Param(org.apache.knox.gateway.topology.Param) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 30 with Provider

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

the class DeploymentFactoryFuncTest method testInvalidGenericProviderDeploymentContributor.

@Test(timeout = LONG_TIMEOUT)
public void testInvalidGenericProviderDeploymentContributor() throws ParserConfigurationException, SAXException, IOException, TransformerException {
    LOG_ENTER();
    GatewayConfig config = new GatewayTestConfig();
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    ((GatewayTestConfig) config).setGatewayHomeDir(gatewayDir.getAbsolutePath());
    File deployDir = new File(config.getGatewayDeploymentDir());
    deployDir.mkdirs();
    DefaultGatewayServices srvcs = new DefaultGatewayServices();
    Map<String, String> options = new HashMap<>();
    options.put("persist-master", "false");
    options.put("master", "password");
    try {
        DeploymentFactory.setGatewayServices(srvcs);
        srvcs.init(config, options);
    } catch (ServiceLifecycleException e) {
        // I18N not required.
        e.printStackTrace();
    }
    Topology topology = new Topology();
    topology.setName("test-cluster");
    Service service = new Service();
    service.setRole("WEBHDFS");
    service.addUrl("http://localhost:50070/test-service-url");
    topology.addService(service);
    Provider provider = new Provider();
    provider.setRole("authentication");
    provider.setName("generic");
    provider.setEnabled(true);
    // = new ProviderParam();
    Param param;
    // Missing filter param.
    // param.setName( "filter" );
    // param.setValue( "org.opensource.ExistingFilter" );
    // provider.addParam( param );
    param = new Param();
    param.setName("test-param-name");
    param.setValue("test-param-value");
    provider.addParam(param);
    topology.addProvider(provider);
    Enumeration<Appender> appenders = NoOpAppender.setUp();
    try {
        DeploymentFactory.createDeployment(config, topology);
        fail("Should have throws IllegalArgumentException");
    } catch (DeploymentException e) {
    // Expected.
    } finally {
        NoOpAppender.tearDown(appenders);
    }
    LOG_EXIT();
}
Also used : NoOpAppender(org.apache.knox.test.log.NoOpAppender) Appender(org.apache.log4j.Appender) HashMap(java.util.HashMap) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) GatewayTestConfig(org.apache.knox.gateway.GatewayTestConfig) Provider(org.apache.knox.gateway.topology.Provider) Param(org.apache.knox.gateway.topology.Param) DefaultGatewayServices(org.apache.knox.gateway.services.DefaultGatewayServices) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

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