Search in sources :

Example 16 with Provider

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

the class DeploymentFactoryFuncTest method testWebXmlGeneration.

@Test(timeout = LONG_TIMEOUT)
public void testWebXmlGeneration() throws IOException, SAXException, ParserConfigurationException, URISyntaxException {
    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/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);
    Provider ha = new Provider();
    ha.setRole("ha");
    ha.setName("HaProvider");
    ha.setEnabled(true);
    topology.addProvider(ha);
    for (int i = 0; i < 10; i++) {
        createAndTestDeployment(config, topology);
    }
    LOG_EXIT();
}
Also used : 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)

Example 17 with Provider

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

the class DeploymentFactoryFuncTest method testServiceAnonAuth.

/**
 * Test the case where topology has federation provider configured
 * and service uses anonymous authentication in which case we should
 * add AnonymousFilter to the filter chain.
 * @since 1.1.0
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 * @throws URISyntaxException
 * @throws TransformerException
 */
@Test(timeout = MEDIUM_TIMEOUT)
public void testServiceAnonAuth() throws IOException, SAXException, ParserConfigurationException, URISyntaxException, TransformerException {
    LOG_ENTER();
    final GatewayConfig config = new GatewayTestConfig();
    ((GatewayTestConfig) config).setXForwardedEnabled(false);
    final File targetDir = new File(System.getProperty("user.dir"), "target");
    final File gatewayDir = new File(targetDir, "gateway-home-" + UUID.randomUUID());
    gatewayDir.mkdirs();
    ((GatewayTestConfig) config).setGatewayHomeDir(gatewayDir.getAbsolutePath());
    final File deployDir = new File(config.getGatewayDeploymentDir());
    deployDir.mkdirs();
    final DefaultGatewayServices srvcs = new DefaultGatewayServices();
    final 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();
    }
    final Topology federationTopology = new Topology();
    final Topology authenticationTopology = new Topology();
    federationTopology.setName("test-cluster");
    authenticationTopology.setName("test-cluster");
    final Service service = new Service();
    service.setRole("RANGER");
    service.addUrl("http://localhost:50070/");
    federationTopology.addService(service);
    authenticationTopology.addService(service);
    /* Add federation provider to first topology */
    final Provider provider = new Provider();
    provider.setRole("federation");
    provider.setName("SSOCookieProvider");
    provider.setEnabled(true);
    Param param = new Param();
    param.setName("sso.authentication.provider.url");
    param.setValue("https://www.local.com:8443/gateway/knoxsso/api/v1/websso");
    provider.addParam(param);
    federationTopology.addProvider(provider);
    /* Add authentication provider to second topology */
    final Provider provider2 = new Provider();
    provider2.setRole("authentication");
    provider2.setName("ShiroProvider");
    provider2.setEnabled(true);
    Param param2 = new Param();
    param2.setName("contextConfigLocation");
    param2.setValue("classpath:app-context-security.xml");
    provider2.addParam(param2);
    authenticationTopology.addProvider(provider2);
    final Provider asserter = new Provider();
    asserter.setRole("identity-assertion");
    asserter.setName("Default");
    asserter.setEnabled(true);
    federationTopology.addProvider(asserter);
    Provider authorizer = new Provider();
    authorizer.setRole("authorization");
    authorizer.setName("AclsAuthz");
    authorizer.setEnabled(true);
    federationTopology.addProvider(authorizer);
    authenticationTopology.addProvider(authorizer);
    final EnterpriseArchive war = DeploymentFactory.createDeployment(config, federationTopology);
    final EnterpriseArchive war2 = DeploymentFactory.createDeployment(config, federationTopology);
    final Document web = XmlUtils.readXml(war.get("%2F/WEB-INF/web.xml").getAsset().openStream());
    final Document web2 = XmlUtils.readXml(war2.get("%2F/WEB-INF/web.xml").getAsset().openStream());
    /* Make sure AnonymousAuthFilter is added to the chain */
    final Document gateway = XmlUtils.readXml(war.get("%2F/WEB-INF/gateway.xml").getAsset().openStream());
    assertThat(gateway, hasXPath("/gateway/resource[1]/pattern", equalTo("/ranger/service/public/**")));
    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.AnonymousAuthFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/role", equalTo("rewrite")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[2]/class", equalTo("org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[3]/role", equalTo("authorization")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[3]/class", equalTo("org.apache.knox.gateway.filter.AclsAuthorizationFilter")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/role", equalTo("dispatch")));
    assertThat(gateway, hasXPath("/gateway/resource[1]/filter[4]/class", equalTo("org.apache.knox.gateway.dispatch.GatewayDispatchFilter")));
    final Document gateway2 = XmlUtils.readXml(war.get("%2F/WEB-INF/gateway.xml").getAsset().openStream());
    assertThat(gateway2, hasXPath("/gateway/resource[1]/pattern", equalTo("/ranger/service/public/**")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[1]/role", equalTo("authentication")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[1]/class", equalTo("org.apache.knox.gateway.filter.AnonymousAuthFilter")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[2]/role", equalTo("rewrite")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[2]/class", equalTo("org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletFilter")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[3]/role", equalTo("authorization")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[3]/class", equalTo("org.apache.knox.gateway.filter.AclsAuthorizationFilter")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[4]/role", equalTo("dispatch")));
    assertThat(gateway2, hasXPath("/gateway/resource[1]/filter[4]/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 18 with Provider

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

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

the class ServiceDeploymentContributorBase method getProviderByRole.

protected Provider getProviderByRole(DeploymentContext context, String role) {
    Provider p = null;
    Collection<Provider> providers = context.getTopology().getProviders();
    for (Provider provider : providers) {
        if (role.equals(provider.getRole())) {
            p = provider;
            break;
        }
    }
    return p;
}
Also used : Provider(org.apache.knox.gateway.topology.Provider)

Example 20 with Provider

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

the class TopologyRulesModuleTest method testParseServiceParamsInAmbariFormat.

@Test
public void testParseServiceParamsInAmbariFormat() throws IOException, SAXException {
    Digester digester = loader.newDigester();
    String name = "org/apache/knox/gateway/topology/xml/service-param-topology-ambari-format.conf";
    URL url = ClassLoader.getSystemResource(name);
    assertThat("Failed to find URL for resource " + name, url, notNullValue());
    File file = new File(url.getFile());
    TopologyBuilder topologyBuilder = digester.parse(url);
    Topology topology = topologyBuilder.build();
    assertThat("Failed to parse resource " + name, topology, notNullValue());
    topology.setTimestamp(file.lastModified());
    assertThat(topology.getName(), is("test-topology-name"));
    assertThat(topology.getTimestamp(), is(file.lastModified()));
    assertThat(topology.getProviders().size(), is(1));
    Provider provider = topology.getProviders().iterator().next();
    assertThat(provider, notNullValue());
    assertThat(provider.getRole(), is("test-provider-role"));
    assertThat(provider.getName(), is("test-provider-name"));
    assertThat(provider.isEnabled(), is(true));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-1"), is("test-provider-param-value-1")));
    assertThat(provider.getParams(), hasEntry(is("test-provider-param-name-2"), is("test-provider-param-value-2")));
    assertThat(topology.getServices().size(), is(1));
    Service service = topology.getServices().iterator().next();
    assertThat(service, notNullValue());
    assertThat(service.getRole(), is("test-service-role"));
    assertThat(service.getUrls().size(), is(2));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host1:42/test-service-path"));
    assertThat(service.getUrls(), hasItem("test-service-scheme://test-service-host2:42/test-service-path"));
    assertThat(service.getName(), is("test-service-name"));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-1"), is("test-service-param-value-1")));
    assertThat(service.getParams(), hasEntry(is("test-service-param-name-2"), is("test-service-param-value-2")));
}
Also used : TopologyBuilder(org.apache.knox.gateway.topology.builder.TopologyBuilder) Digester(org.apache.commons.digester3.Digester) Service(org.apache.knox.gateway.topology.Service) Topology(org.apache.knox.gateway.topology.Topology) File(java.io.File) URL(java.net.URL) Provider(org.apache.knox.gateway.topology.Provider) 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