diff --git a/commands/src/main/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoder.java b/commands/src/main/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoder.java new file mode 100644 index 00000000..3e9d67de --- /dev/null +++ b/commands/src/main/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoder.java @@ -0,0 +1,50 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.wildfly.extras.creaper.commands.elytron.mapper; + +import org.wildfly.extras.creaper.commands.elytron.AbstractAddCustom; + +public final class AddCustomRoleDecoder extends AbstractAddCustom { + + protected AddCustomRoleDecoder(Builder builder) { + super(builder); + } + + @Override + protected String getCustomTypeName() { + return "custom-role-decoder"; + } + + public static final class Builder extends AbstractAddCustom.Builder { + public Builder(String name) { + super(name); + } + + public AddCustomRoleDecoder build() { + if (className == null || className.isEmpty()) { + throw new IllegalArgumentException("className must not be null or empty string"); + } + return new AddCustomRoleDecoder(this); + } + } +} diff --git a/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderImpl.java b/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderImpl.java new file mode 100644 index 00000000..9d7da5d6 --- /dev/null +++ b/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderImpl.java @@ -0,0 +1,47 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2016, Red Hat, Inc., and individual contributors + * as indicated by the @author tags. See the copyright.txt file in the + * distribution for a full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ + +package org.wildfly.extras.creaper.commands.elytron.mapper; + +import java.util.Map; + +import org.wildfly.extension.elytron.Configurable; +import org.wildfly.security.authz.AuthorizationIdentity; +import org.wildfly.security.authz.RoleDecoder; +import org.wildfly.security.authz.Roles; + +public class AddCustomRoleDecoderImpl implements RoleDecoder, Configurable { + + @Override + public Roles decodeRoles(AuthorizationIdentity authorizationIdentity) { + return Roles.of("anyRole"); + } + + @Override + public void initialize(Map configuration) { + if (configuration.containsKey("throwException")) { + throw new IllegalStateException("Only test purpose. This exception was thrown on demand."); + } + } + + +} diff --git a/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderOnlineTest.java b/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderOnlineTest.java new file mode 100644 index 00000000..01c9c24e --- /dev/null +++ b/testsuite/standalone/src/test/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderOnlineTest.java @@ -0,0 +1,197 @@ +package org.wildfly.extras.creaper.commands.elytron.mapper; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +import org.jboss.arquillian.junit.Arquillian; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.wildfly.extras.creaper.commands.elytron.AbstractElytronOnlineTest; +import org.wildfly.extras.creaper.commands.modules.AddModule; +import org.wildfly.extras.creaper.commands.modules.RemoveModule; +import org.wildfly.extras.creaper.core.CommandFailedException; +import org.wildfly.extras.creaper.core.online.OnlineManagementClient; +import org.wildfly.extras.creaper.core.online.operations.Address; + + +@RunWith(Arquillian.class) +public class AddCustomRoleDecoderOnlineTest extends AbstractElytronOnlineTest { + + private static final String TEST_ADD_CUSTOM_ROLE_DECODER_NAME = "CreaperTestAddCustomRoleDecoder"; + private static final Address TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS = SUBSYSTEM_ADDRESS.and("custom-role-decoder", + TEST_ADD_CUSTOM_ROLE_DECODER_NAME); + private static final String TEST_ADD_CUSTOM_ROLE_DECODER_NAME2 = "CreaperTestAddCustomRoleDecoder2"; + private static final Address TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS2 = SUBSYSTEM_ADDRESS.and("custom-role-decoder", + TEST_ADD_CUSTOM_ROLE_DECODER_NAME2); + private static final String CUSTOM_ROLE_DECODER_MODULE_NAME = "org.jboss.customroledecoderimpl"; + + @BeforeClass + public static void setUp() throws IOException, CommandFailedException, InterruptedException, TimeoutException { + try (OnlineManagementClient client = createManagementClient()) { + File testJar1 = createJar("testJar", AddCustomRoleDecoderImpl.class); + AddModule addModule = new AddModule.Builder(CUSTOM_ROLE_DECODER_MODULE_NAME) + .resource(testJar1) + .resourceDelimiter(":") + .dependency("org.wildfly.security.elytron") + .dependency("org.wildfly.extension.elytron") + .build(); + client.apply(addModule); + } + } + + @AfterClass + public static void afterClass() throws IOException, CommandFailedException, InterruptedException, TimeoutException { + try (OnlineManagementClient client = createManagementClient()) { + RemoveModule removeModule = new RemoveModule(CUSTOM_ROLE_DECODER_MODULE_NAME); + client.apply(removeModule); + } + } + + @After + public void cleanup() throws Exception { + ops.removeIfExists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS); + ops.removeIfExists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS2); + administration.reloadIfRequired(); + } + + @Test + public void addCustomRoleDecoder() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module(CUSTOM_ROLE_DECODER_MODULE_NAME) + .addConfiguration("param", "parameterValue") + .build(); + + client.apply(addAddCustomRoleDecoder); + + assertTrue("Add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS)); + } + + @Test + public void addCustomRoleDecoders() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module(CUSTOM_ROLE_DECODER_MODULE_NAME) + .build(); + + AddCustomRoleDecoder addAddCustomRoleDecoder2 = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME2) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module(CUSTOM_ROLE_DECODER_MODULE_NAME) + .build(); + + client.apply(addAddCustomRoleDecoder); + client.apply(addAddCustomRoleDecoder2); + + assertTrue("Add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS)); + assertTrue("Second add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS2)); + + checkAttribute(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS, "class-name", + AddCustomRoleDecoderImpl.class.getName()); + checkAttribute(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS2, "class-name", + AddCustomRoleDecoderImpl.class.getName()); + + administration.reload(); + + checkAttribute(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS, "class-name", + AddCustomRoleDecoderImpl.class.getName()); + checkAttribute(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS2, "class-name", + AddCustomRoleDecoderImpl.class.getName()); + } + + @Test(expected = CommandFailedException.class) + public void addDuplicateCustomRoleDecoderNotAllowed() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module(CUSTOM_ROLE_DECODER_MODULE_NAME) + .build(); + + client.apply(addAddCustomRoleDecoder); + assertTrue("Add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS)); + client.apply(addAddCustomRoleDecoder); + fail("Add custom role decoder " + TEST_ADD_CUSTOM_ROLE_DECODER_NAME + + " already exists in configuration, exception should be thrown"); + } + + @Test(expected = IllegalArgumentException.class) + public void addCustomRoleDecoder_nullName() throws Exception { + new AddCustomRoleDecoder.Builder(null); + fail("Creating command with null name should throw exception"); + } + + @Test(expected = IllegalArgumentException.class) + public void addAddCustomRoleDecoder_emptyName() throws Exception { + new AddCustomRoleDecoder.Builder(""); + fail("Creating command with empty name should throw exception"); + } + + @Test(expected = CommandFailedException.class) + public void addCustomRoleDecoder_noModule() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .build(); + + client.apply(addAddCustomRoleDecoder); + + assertTrue("Add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS)); + } + + @Test(expected = IllegalArgumentException.class) + public void addCustomRoleDecoder_noClassName() throws Exception { + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME).build(); + fail("Creating command with no custom should throw exception"); + } + + @Test(expected = IllegalArgumentException.class) + public void addCustomRoleDecoder_emptyClassName() throws Exception { + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className("") + .build(); + + fail("Creating command with empty classname should throw exception"); + } + + @Test(expected = CommandFailedException.class) + public void addCustomRoleDecoder_wrongModule() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module("wrongModule") + .build(); + + client.apply(addAddCustomRoleDecoder); + + assertTrue("Add custom role decoder should be created", + ops.exists(TEST_ADD_CUSTOM_ROLE_DECODER_ADDRESS)); + } + + @Test(expected = CommandFailedException.class) + public void addCustomRoleDecoder_configurationWithException() throws Exception { + AddCustomRoleDecoder addAddCustomRoleDecoder = + new AddCustomRoleDecoder.Builder(TEST_ADD_CUSTOM_ROLE_DECODER_NAME) + .className(AddCustomRoleDecoderImpl.class.getName()) + .module(CUSTOM_ROLE_DECODER_MODULE_NAME) + .addConfiguration("throwException", "parameterValue") + .build(); + + client.apply(addAddCustomRoleDecoder); + + fail("Creating command with test configuration should throw exception"); + } +}