From bc718471daacf0d5ddeed5203beb0347221dca95 Mon Sep 17 00:00:00 2001 From: zschwarz Date: Wed, 19 Oct 2016 11:55:54 +0200 Subject: [PATCH] KEYCLOAK-3747 Servlet filter dependencies --- .../DeploymentArchiveProcessor.java | 44 +++++++++---------- .../annotation/UseServletFilter.java | 1 + .../org/keycloak/testsuite/util/IOUtil.java | 23 ++++++++++ .../AbstractDemoFilterServletAdapterTest.java | 3 +- .../AbstractSAMLFilterServletAdapterTest.java | 3 +- .../jboss-deployment-structure.xml | 2 + 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/DeploymentArchiveProcessor.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/DeploymentArchiveProcessor.java index fe3bce8269..00deb0a53a 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/DeploymentArchiveProcessor.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/DeploymentArchiveProcessor.java @@ -42,15 +42,7 @@ import static org.keycloak.testsuite.arquillian.AppServerTestEnricher.hasAppServ import static org.keycloak.testsuite.arquillian.AppServerTestEnricher.isRelative; import static org.keycloak.testsuite.arquillian.AppServerTestEnricher.isTomcatAppServer; import static org.keycloak.testsuite.arquillian.AuthServerTestEnricher.getAuthServerContextRoot; -import static org.keycloak.testsuite.util.IOUtil.appendChildInDocument; -import static org.keycloak.testsuite.util.IOUtil.documentToString; -import static org.keycloak.testsuite.util.IOUtil.getElementTextContent; -import static org.keycloak.testsuite.util.IOUtil.loadJson; -import static org.keycloak.testsuite.util.IOUtil.loadXML; -import static org.keycloak.testsuite.util.IOUtil.modifyDocElementAttribute; -import static org.keycloak.testsuite.util.IOUtil.modifyDocElementValue; -import static org.keycloak.testsuite.util.IOUtil.removeElementsFromDoc; - +import static org.keycloak.testsuite.util.IOUtil.*; /** @@ -68,6 +60,7 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor { public static final String ADAPTER_CONFIG_PATH_TENANT2 = "/WEB-INF/classes/tenant2-keycloak.json"; public static final String ADAPTER_CONFIG_PATH_JS = "/keycloak.json"; public static final String SAML_ADAPTER_CONFIG_PATH = "/WEB-INF/keycloak-saml.xml"; + public static final String JBOSS_DEPLOYMENT_XML_PATH = "/WEB-INF/jboss-deployment-structure.xml"; @Override public void process(Archive archive, TestClass testClass) { @@ -122,9 +115,6 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor { // For running SAML tests it is necessary to have few dependencies on app-server side. // Few of them are not in adapter zip so we need to add them manually here - log.info("Adding SAMLFilter dependencies to " + archive.getName()); - ((WebArchive) archive).addAsLibraries(KeycloakDependenciesResolver.resolveDependencies("org.keycloak:keycloak-saml-servlet-filter-adapter:" + System.getProperty("project.version"))); - } else { // OIDC adapter config try { AdapterConfig adapterConfig = loadJson(archive.get(adapterConfigPath) @@ -148,17 +138,7 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor { } catch (IOException ex) { log.log(Level.FATAL, "Cannot serialize adapter config to JSON.", ex); } - - log.info("Adding OIDCFilter dependencies to " + archive.getName()); - ((WebArchive) archive).addAsLibraries(KeycloakDependenciesResolver.resolveDependencies("org.keycloak:keycloak-servlet-filter-adapter:" + System.getProperty("project.version"))); - } - - } else if (archive.getName().equals("customer-portal-subsystem.war")) { - - log.info("Adding OIDCFilter dependencies to " + archive.getName()); - ((WebArchive) archive).addAsLibraries(KeycloakDependenciesResolver.resolveDependencies("org.keycloak:keycloak-servlet-filter-adapter:" + System.getProperty("project.version"))); - } } @@ -176,6 +156,23 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor { return libs; } + public void addFilterDependencies(Archive archive, TestClass testClass) { + log.info("Adding filter dependencies to " + archive.getName()); + String dependency = testClass.getAnnotation(UseServletFilter.class).filterDependency(); + ((WebArchive) archive).addAsLibraries(KeycloakDependenciesResolver.resolveDependencies((dependency + ":" + System.getProperty("project.version")))); + + try { + Document jbossXmlDoc = loadXML(archive.get(JBOSS_DEPLOYMENT_XML_PATH).getAsset().openStream()); + removeNodeByAttributeValue(jbossXmlDoc, "dependencies", "module", "name", "org.keycloak.keycloak-saml-core"); + removeNodeByAttributeValue(jbossXmlDoc, "dependencies", "module", "name", "org.keycloak.keycloak-adapter-spi"); + archive.add(new StringAsset((documentToString(jbossXmlDoc))), JBOSS_DEPLOYMENT_XML_PATH); + } catch (TransformerException e) { + log.error("Can't transform document to String"); + throw new RuntimeException(e); + } + + } + protected void modifyWebXml(Archive archive, TestClass testClass) { try { Document webXmlDoc = loadXML( @@ -185,6 +182,9 @@ public class DeploymentArchiveProcessor implements ApplicationArchiveProcessor { } if (testClass.getJavaClass().isAnnotationPresent(UseServletFilter.class)) { + + addFilterDependencies(archive, testClass); + //We need to add filter declaration to web.xml log.info("Adding filter to " + testClass.getAnnotation(UseServletFilter.class).filterClass() + " with mapping " + testClass.getAnnotation(UseServletFilter.class).filterPattern() + " for " + archive.getName()); diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/annotation/UseServletFilter.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/annotation/UseServletFilter.java index a94737d486..d712cfea0c 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/annotation/UseServletFilter.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/arquillian/annotation/UseServletFilter.java @@ -17,6 +17,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; @Inherited public @interface UseServletFilter { + String filterDependency(); String filterName(); String filterClass(); String filterPattern() default "/*"; diff --git a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/IOUtil.java b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/IOUtil.java index 345e77eb8f..9148ce14bc 100644 --- a/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/IOUtil.java +++ b/testsuite/integration-arquillian/tests/base/src/main/java/org/keycloak/testsuite/util/IOUtil.java @@ -113,6 +113,29 @@ public class IOUtil { node.setTextContent(node.getTextContent().replace(regex, replacement)); } + public static void removeNodeByAttributeValue(Document doc, String parentTag, String tagName, String attributeName, String value){ + NodeList parentNodes = doc.getElementsByTagName(parentTag); + if (parentNodes.getLength() != 1) { + log.warn("Not able or ambiguous to find element: " + parentTag); + return; + } + + Element parentElement = (Element) parentNodes.item(0); + if (parentElement == null) { + log.warn("Not able to find element: " + parentTag); + return; + } + + NodeList nodes = doc.getElementsByTagName(tagName); + for (int i = 0; i < nodes.getLength(); i++){ + Node node = nodes.item(i).getAttributes().getNamedItem(attributeName); + if (node.getTextContent().equals(value)){ + parentElement.removeChild(nodes.item(i)); + return; + } + } + } + public static void modifyDocElementValue(Document doc, String tagName, String regex, String replacement) { NodeList nodes = doc.getElementsByTagName(tagName); if (nodes.getLength() != 1) { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractDemoFilterServletAdapterTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractDemoFilterServletAdapterTest.java index b53fdcc7bc..fed5ab7ff5 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractDemoFilterServletAdapterTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractDemoFilterServletAdapterTest.java @@ -8,7 +8,8 @@ import org.keycloak.testsuite.arquillian.annotation.UseServletFilter; * Created by zschwarz on 9/14/16. */ -@UseServletFilter(filterName = "oidc-filter", filterClass = "org.keycloak.adapters.servlet.KeycloakOIDCFilter") +@UseServletFilter(filterName = "oidc-filter", filterClass = "org.keycloak.adapters.servlet.KeycloakOIDCFilter", + filterDependency = "org.keycloak:keycloak-servlet-filter-adapter") public abstract class AbstractDemoFilterServletAdapterTest extends AbstractDemoServletsAdapterTest { diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractSAMLFilterServletAdapterTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractSAMLFilterServletAdapterTest.java index 268285a5b4..5adfb94ce0 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractSAMLFilterServletAdapterTest.java +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/adapter/servlet/AbstractSAMLFilterServletAdapterTest.java @@ -10,7 +10,8 @@ import org.keycloak.testsuite.arquillian.annotation.UseServletFilter; * @author mhajas */ -@UseServletFilter(filterName = "saml-filter", filterClass = "org.keycloak.adapters.saml.servlet.SamlFilter") +@UseServletFilter(filterName = "saml-filter", filterClass = "org.keycloak.adapters.saml.servlet.SamlFilter", + filterDependency = "org.keycloak:keycloak-saml-servlet-filter-adapter") public abstract class AbstractSAMLFilterServletAdapterTest extends AbstractSAMLServletsAdapterTest { @Before diff --git a/testsuite/integration-arquillian/tests/base/src/test/resources/adapter-test/jboss-deployment-structure.xml b/testsuite/integration-arquillian/tests/base/src/test/resources/adapter-test/jboss-deployment-structure.xml index 6b5322defc..9a09a89b71 100644 --- a/testsuite/integration-arquillian/tests/base/src/test/resources/adapter-test/jboss-deployment-structure.xml +++ b/testsuite/integration-arquillian/tests/base/src/test/resources/adapter-test/jboss-deployment-structure.xml @@ -27,6 +27,8 @@ + +