|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2012, Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. See the copyright.txt file in the |
| 5 | + * distribution for a full listing of individual contributors. |
| 6 | + * |
| 7 | + * This is free software; you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU Lesser General Public License as |
| 9 | + * published by the Free Software Foundation; either version 2.1 of |
| 10 | + * the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This software is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this software; if not, write to the Free |
| 19 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 21 | + */ |
| 22 | +package org.jboss.as.test.integration.security.jacc; |
| 23 | + |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.UnsupportedEncodingException; |
| 28 | +import java.net.MalformedURLException; |
| 29 | +import java.net.URISyntaxException; |
| 30 | +import java.net.URL; |
| 31 | + |
| 32 | +import org.apache.http.client.ClientProtocolException; |
| 33 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 34 | +import org.jboss.arquillian.container.test.api.RunAsClient; |
| 35 | +import org.jboss.arquillian.junit.Arquillian; |
| 36 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 37 | +import org.jboss.as.arquillian.api.ServerSetup; |
| 38 | +import org.jboss.as.arquillian.api.ServerSetupTask; |
| 39 | +import org.jboss.as.security.Constants; |
| 40 | +import org.jboss.as.test.integration.security.common.AbstractSecurityDomainsServerSetupTask; |
| 41 | +import org.jboss.as.test.integration.security.common.Utils; |
| 42 | +import org.jboss.as.test.integration.security.common.config.SecurityDomain; |
| 43 | +import org.jboss.as.test.integration.security.common.config.SecurityModule; |
| 44 | +import org.jboss.as.test.integration.security.jacc.propagation.BridgeBean; |
| 45 | +import org.jboss.as.test.integration.security.jacc.propagation.Manage; |
| 46 | +import org.jboss.as.test.integration.security.jacc.propagation.PropagationTestServlet; |
| 47 | +import org.jboss.as.test.integration.security.jacc.propagation.TargetBean; |
| 48 | +import org.jboss.as.test.integration.security.loginmodules.UsersRolesLoginModuleTestCase; |
| 49 | +import org.jboss.logging.Logger; |
| 50 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 51 | +import org.jboss.shrinkwrap.api.asset.StringAsset; |
| 52 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 53 | +import org.junit.Ignore; |
| 54 | +import org.junit.Test; |
| 55 | +import org.junit.runner.RunWith; |
| 56 | + |
| 57 | +/** |
| 58 | + * Tests, which checks run-as identity handling in EJB JACC authorization module. |
| 59 | + * |
| 60 | + * @author Josef Cacek |
| 61 | + */ |
| 62 | +@RunWith(Arquillian.class) |
| 63 | +@ServerSetup({ JACCAuthzPropagationTestCase.SecurityDomainsSetup.class }) |
| 64 | +@RunAsClient |
| 65 | +public class JACCAuthzPropagationTestCase { |
| 66 | + |
| 67 | + private static final Logger LOGGER = Logger.getLogger(JACCAuthzPropagationTestCase.class); |
| 68 | + |
| 69 | + private static final String TEST_NAME = Manage.TEST_NAME; |
| 70 | + |
| 71 | + // Public methods -------------------------------------------------------- |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates {@link WebArchive} deployment. |
| 75 | + */ |
| 76 | + @Deployment(name = "war") |
| 77 | + public static WebArchive warDeployment() { |
| 78 | + LOGGER.info("Start WAR deployment"); |
| 79 | + final WebArchive war = ShrinkWrap.create(WebArchive.class, TEST_NAME + ".war"); |
| 80 | + war.addClasses(PropagationTestServlet.class, Manage.class, BridgeBean.class, TargetBean.class); |
| 81 | + final StringAsset usersRolesAsset = new StringAsset(Utils.createUsersFromRoles(Manage.ROLES_ALL)); |
| 82 | + war.addAsResource(usersRolesAsset, "users.properties"); |
| 83 | + war.addAsResource(usersRolesAsset, "roles.properties"); |
| 84 | + |
| 85 | + war.addAsWebInfResource(UsersRolesLoginModuleTestCase.class.getPackage(), "web-basic-authn.xml", "web.xml"); |
| 86 | + war.addAsWebInfResource(Utils.getJBossWebXmlAsset(TEST_NAME), "jboss-web.xml"); |
| 87 | + war.addAsWebInfResource(Utils.getJBossEjb3XmlAsset(TEST_NAME), "jboss-ejb3.xml"); |
| 88 | + LOGGER.info(war.toString(true)); |
| 89 | + return war; |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests direct permissions (RolesAllowed). |
| 95 | + * |
| 96 | + * @param webAppURL |
| 97 | + * @throws Exception |
| 98 | + */ |
| 99 | + @Test |
| 100 | + public void testTarget(@ArquillianResource URL webAppURL) throws Exception { |
| 101 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_TARGET, PropagationTestServlet.METHOD_NAME_ADMIN, Manage.ROLE_ADMIN); |
| 102 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_TARGET, PropagationTestServlet.METHOD_NAME_MANAGE, Manage.ROLE_MANAGER); |
| 103 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_TARGET, PropagationTestServlet.METHOD_NAME_WORK, Manage.ROLE_ADMIN); |
| 104 | + |
| 105 | + assertAccessDenied(webAppURL, Manage.BEAN_NAME_TARGET, PropagationTestServlet.METHOD_NAME_ADMIN, Manage.ROLE_MANAGER); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Tests run-as permissions. |
| 110 | + * |
| 111 | + * @param webAppURL |
| 112 | + * @throws Exception |
| 113 | + */ |
| 114 | + @Test |
| 115 | + @Ignore("JBPAPP6-1686") |
| 116 | + public void testBridge(@ArquillianResource URL webAppURL) throws Exception { |
| 117 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_MANAGE, Manage.ROLE_ADMIN); |
| 118 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_MANAGE, Manage.ROLE_MANAGER); |
| 119 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_MANAGE, Manage.ROLE_USER); |
| 120 | + assertAccessAllowed(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_WORK, Manage.ROLE_USER); |
| 121 | + |
| 122 | + assertAccessDenied(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_ADMIN, Manage.ROLE_ADMIN); |
| 123 | + assertAccessDenied(webAppURL, Manage.BEAN_NAME_BRIDGE, PropagationTestServlet.METHOD_NAME_ADMIN, Manage.ROLE_MANAGER); |
| 124 | + } |
| 125 | + |
| 126 | + // Private methods ------------------------------------------------------- |
| 127 | + |
| 128 | + /** |
| 129 | + * Asserts the access to the given method in the given bean is allowed for given role. |
| 130 | + * |
| 131 | + * @param webAppURL |
| 132 | + * @param beanName |
| 133 | + * @param methodName |
| 134 | + * @param roleName |
| 135 | + * @throws ClientProtocolException |
| 136 | + * @throws IOException |
| 137 | + * @throws URISyntaxException |
| 138 | + */ |
| 139 | + private void assertAccessAllowed(URL webAppURL, String beanName, String methodName, String roleName) |
| 140 | + throws ClientProtocolException, IOException, URISyntaxException { |
| 141 | + final URL testUrl = getTestURL(webAppURL, beanName, methodName); |
| 142 | + assertEquals("Access of role " + roleName + " to " + methodName + " method in " + beanName + " should be allowed.", |
| 143 | + Manage.RESULT, Utils.makeCallWithBasicAuthn(testUrl, roleName, roleName, 200)); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Asserts the access to the given method in the given bean is denied for given role. |
| 148 | + * |
| 149 | + * @param webAppURL |
| 150 | + * @param beanName |
| 151 | + * @param methodName |
| 152 | + * @param roleName |
| 153 | + * @throws ClientProtocolException |
| 154 | + * @throws IOException |
| 155 | + * @throws URISyntaxException |
| 156 | + */ |
| 157 | + private void assertAccessDenied(URL webAppURL, String beanName, String methodName, String roleName) |
| 158 | + throws ClientProtocolException, IOException, URISyntaxException { |
| 159 | + final URL testUrl = getTestURL(webAppURL, beanName, methodName); |
| 160 | + assertEquals("Access of role " + roleName + " to " + methodName + " method in " + beanName + " should be denied.", |
| 161 | + PropagationTestServlet.RESULT_EJB_ACCESS_EXCEPTION, |
| 162 | + Utils.makeCallWithBasicAuthn(testUrl, roleName, roleName, 200)); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Creates URL of the test application with the given values of request parameters. |
| 167 | + * |
| 168 | + * @param webAppURL |
| 169 | + * @param beanName |
| 170 | + * @param method |
| 171 | + * @return |
| 172 | + * @throws MalformedURLException |
| 173 | + * @throws UnsupportedEncodingException |
| 174 | + */ |
| 175 | + private URL getTestURL(URL webAppURL, String beanName, String method) throws MalformedURLException, |
| 176 | + UnsupportedEncodingException { |
| 177 | + return new URL(webAppURL.toExternalForm() + PropagationTestServlet.SERVLET_PATH.substring(1) + "?" // |
| 178 | + + PropagationTestServlet.PARAM_BEAN_NAME + "=" + beanName + "&" // |
| 179 | + + PropagationTestServlet.PARAM_METHOD_NAME + "=" + method); |
| 180 | + } |
| 181 | + |
| 182 | + // Embedded classes ------------------------------------------------------ |
| 183 | + |
| 184 | + /** |
| 185 | + * A {@link ServerSetupTask} instance which creates security domains for this test case. |
| 186 | + * |
| 187 | + * @author Josef Cacek |
| 188 | + */ |
| 189 | + static class SecurityDomainsSetup extends AbstractSecurityDomainsServerSetupTask { |
| 190 | + /** |
| 191 | + * @see org.jboss.as.test.integration.security.common.AbstractSecurityDomainsServerSetupTask#getSecurityDomains() |
| 192 | + */ |
| 193 | + @Override |
| 194 | + protected SecurityDomain[] getSecurityDomains() { |
| 195 | + return new SecurityDomain[] { new SecurityDomain.Builder().name(TEST_NAME) |
| 196 | + .loginModules(new SecurityModule.Builder().name("UsersRoles").flag(Constants.REQUIRED).build()) // |
| 197 | + .authorizationModules(new SecurityModule.Builder().name("JACC").flag(Constants.REQUIRED).build()) // |
| 198 | + .cacheType("default") // |
| 199 | + .build() }; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments