Repository: knox Updated Branches: refs/heads/master 335dbd989 -> 7446fbc43
KNOX-1219 - Eliminated duplicate useTwoWaySsl dispatch filter params Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/ed0ec11e Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/ed0ec11e Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/ed0ec11e Branch: refs/heads/master Commit: ed0ec11e23c828d395c01949e98a1b574a44208a Parents: a27c8eb Author: Phil Zampino <[email protected]> Authored: Tue Mar 20 14:47:39 2018 -0400 Committer: Phil Zampino <[email protected]> Committed: Tue Mar 20 14:47:39 2018 -0400 ---------------------------------------------------------------------- .../impl/FilterParamDescriptorImpl.java | 11 ++ ...viceDefinitionDeploymentContributorTest.java | 127 +++++++++++++++++++ 2 files changed, 138 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/ed0ec11e/gateway-server/src/main/java/org/apache/knox/gateway/descriptor/impl/FilterParamDescriptorImpl.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/descriptor/impl/FilterParamDescriptorImpl.java b/gateway-server/src/main/java/org/apache/knox/gateway/descriptor/impl/FilterParamDescriptorImpl.java index b84f946..76171d2 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/descriptor/impl/FilterParamDescriptorImpl.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/descriptor/impl/FilterParamDescriptorImpl.java @@ -47,6 +47,17 @@ public class FilterParamDescriptorImpl implements FilterParamDescriptor { @Override public FilterParamDescriptor name( String name ) { this.name = name; + + // If there is already a param identified by the new name, remove it, such that it is REPLACED with this new param + if (parent != null) { + for (FilterParamDescriptor param : parent.params()) { + if (param.name().equals(name) && (param != this)) { + parent.params().remove(param); + break; + } + } + } + return this; } http://git-wip-us.apache.org/repos/asf/knox/blob/ed0ec11e/gateway-server/src/test/java/org/apache/knox/gateway/deploy/impl/ServiceDefinitionDeploymentContributorTest.java ---------------------------------------------------------------------- diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/deploy/impl/ServiceDefinitionDeploymentContributorTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/deploy/impl/ServiceDefinitionDeploymentContributorTest.java index c8923bb..4045789 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/deploy/impl/ServiceDefinitionDeploymentContributorTest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/deploy/impl/ServiceDefinitionDeploymentContributorTest.java @@ -17,13 +17,34 @@ */ package org.apache.knox.gateway.deploy.impl; +import org.apache.knox.gateway.config.GatewayConfig; +import org.apache.knox.gateway.deploy.DeploymentContext; import org.apache.knox.gateway.deploy.ProviderDeploymentContributor; +import org.apache.knox.gateway.descriptor.FilterDescriptor; +import org.apache.knox.gateway.descriptor.FilterParamDescriptor; +import org.apache.knox.gateway.descriptor.ResourceDescriptor; +import org.apache.knox.gateway.descriptor.impl.GatewayDescriptorImpl; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor; +import org.apache.knox.gateway.service.definition.CustomDispatch; +import org.apache.knox.gateway.service.definition.Rewrite; +import org.apache.knox.gateway.service.definition.Route; +import org.apache.knox.gateway.service.definition.ServiceDefinition; +import org.apache.knox.gateway.topology.Provider; +import org.apache.knox.gateway.topology.Service; +import org.apache.knox.gateway.topology.Topology; +import org.easymock.EasyMock; import org.junit.Test; +import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.ServiceLoader; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; public class ServiceDefinitionDeploymentContributorTest { @@ -40,4 +61,110 @@ public class ServiceDefinitionDeploymentContributorTest { } } } + + /** + * Test that service param useTwoWaySsl in topologies overrides the corresponding custom dispatch property. + */ + @Test + public void testServiceAttributeUseTwoWaySSLParamOverride() throws Exception { + + final String TEST_SERVICE_ROLE = "Test"; + final String USE_TWO_WAY_SSL_PARAM = "useTwoWaySsl"; + + UrlRewriteRulesDescriptor clusterRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class); + EasyMock.replay(clusterRules); + + UrlRewriteRulesDescriptor svcRules = EasyMock.createNiceMock(UrlRewriteRulesDescriptor.class); + EasyMock.replay(svcRules); + + ServiceDefinition svcDef = EasyMock.createNiceMock(ServiceDefinition.class); + EasyMock.expect(svcDef.getRole()).andReturn(TEST_SERVICE_ROLE).anyTimes(); + List<Route> svcRoutes = new ArrayList<>(); + Route route = EasyMock.createNiceMock(Route.class); + List<Rewrite> filters = new ArrayList<>(); + EasyMock.expect(route.getRewrites()).andReturn(filters).anyTimes(); + svcRoutes.add(route); + EasyMock.replay(route); + EasyMock.expect(svcDef.getRoutes()).andReturn(svcRoutes).anyTimes(); + CustomDispatch cd = EasyMock.createNiceMock(CustomDispatch.class); + EasyMock.expect(cd.getClassName()).andReturn("TestDispatch").anyTimes(); + EasyMock.expect(cd.getHaClassName()).andReturn("TestHADispatch").anyTimes(); + EasyMock.expect(cd.getHaContributorName()).andReturn(null).anyTimes(); + + // Let useTwoWaySsl be FALSE by default + EasyMock.expect(cd.getUseTwoWaySsl()).andReturn(false).anyTimes(); + + EasyMock.replay(cd); + EasyMock.expect(svcDef.getDispatch()).andReturn(cd).anyTimes(); + EasyMock.replay(svcDef); + + ServiceDefinitionDeploymentContributor sddc = new ServiceDefinitionDeploymentContributor(svcDef, svcRules); + + DeploymentContext context = EasyMock.createNiceMock(DeploymentContext.class); + EasyMock.expect(context.getDescriptor("rewrite")).andReturn(clusterRules).anyTimes(); + GatewayConfig gc = EasyMock.createNiceMock(GatewayConfig.class); + EasyMock.expect(gc.isXForwardedEnabled()).andReturn(false).anyTimes(); + EasyMock.expect(gc.isCookieScopingToPathEnabled()).andReturn(false).anyTimes(); + EasyMock.replay(gc); + EasyMock.expect(context.getGatewayConfig()).andReturn(gc).anyTimes(); + + // Configure the HaProvider + Topology topology = EasyMock.createNiceMock(Topology.class); + List<Provider> providers = new ArrayList<>(); + Provider haProvider = EasyMock.createNiceMock(Provider.class); + EasyMock.expect(haProvider.getRole()).andReturn("ha").anyTimes(); + EasyMock.expect(haProvider.isEnabled()).andReturn(true).anyTimes(); + Map<String, String> providerParams = new HashMap<>(); + providerParams.put(TEST_SERVICE_ROLE, "whatever"); + EasyMock.expect(haProvider.getParams()).andReturn(providerParams).anyTimes(); + + EasyMock.replay(haProvider); + providers.add(haProvider); + EasyMock.expect(topology.getProviders()).andReturn(providers).anyTimes(); + EasyMock.replay(topology); + EasyMock.expect(context.getTopology()).andReturn(topology).anyTimes(); + + TestGatewayDescriptor gd = new TestGatewayDescriptor(); + EasyMock.expect(context.getGatewayDescriptor()).andReturn(gd).anyTimes(); + EasyMock.replay(context); + + // Configure the service with the useTwoWaySsl param to OVERRIDE the value in the service definition + Service service = EasyMock.createNiceMock(Service.class); + Map<String, String> svcParams = new HashMap<>(); + svcParams.put(USE_TWO_WAY_SSL_PARAM, "true"); + EasyMock.expect(service.getParams()).andReturn(svcParams).anyTimes(); + EasyMock.replay(service); + + sddc.contributeService(context, service); + + List<ResourceDescriptor> resources = gd.resources(); + assertEquals(1, gd.resources().size()); + ResourceDescriptor res = gd.resources().get(0); + assertNotNull(res); + List<FilterDescriptor> filterList = res.filters(); + assertEquals(1, filterList.size()); + FilterDescriptor f = filterList.get(0); + assertNotNull(f); + assertEquals("dispatch", f.role()); + List<FilterParamDescriptor> fParams = f.params(); + assertNotNull(fParams); + + // Collect the values of filter params named useTwoWaySsl + List<String> useTwoWaySslFilterParamValues = new ArrayList<>(); + for (FilterParamDescriptor param : fParams) { + if (param.name().equals(USE_TWO_WAY_SSL_PARAM)) { + useTwoWaySslFilterParamValues.add(param.value()); + } + } + + assertEquals("Expected only a single filter param named " + USE_TWO_WAY_SSL_PARAM, + 1, useTwoWaySslFilterParamValues.size()); + assertEquals("Expected the service param to override the service definition value for " + USE_TWO_WAY_SSL_PARAM, + "true", useTwoWaySslFilterParamValues.get(0)); + } + + + private static class TestGatewayDescriptor extends GatewayDescriptorImpl { + } + }
