forked from wildfly-extras/creaper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add commands for custom-role-decoder
- Loading branch information
Hynek Svabek
committed
Sep 23, 2016
1 parent
ca64b44
commit d9315f6
Showing
3 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...rc/main/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...est/java/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> configuration) { | ||
if (configuration.containsKey("throwException")) { | ||
throw new IllegalStateException("Only test purpose. This exception was thrown on demand."); | ||
} | ||
} | ||
|
||
|
||
} |
197 changes: 197 additions & 0 deletions
197
...va/org/wildfly/extras/creaper/commands/elytron/mapper/AddCustomRoleDecoderOnlineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} |