Skip to content

Commit

Permalink
[WFLY-13716] Integration tests for Elytron failover-realm
Browse files Browse the repository at this point in the history
  • Loading branch information
OndrejKotek committed Sep 14, 2020
1 parent 898c9ce commit 3bc17fc
Show file tree
Hide file tree
Showing 7 changed files with 739 additions and 4 deletions.

Large diffs are not rendered by default.

@@ -0,0 +1,24 @@
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<login-config>
<auth-method>BEARER_TOKEN</auth-method>
</login-config>

<security-constraint>
<web-resource-collection>
<web-resource-name>secured-area</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<role-name>*</role-name>
</security-role>
</web-app>
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Test realm</realm-name>
</login-config>

<security-constraint>
<web-resource-collection>
<web-resource-name>secured-area</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<role-name>*</role-name>
</security-role>
</web-app>
@@ -0,0 +1,126 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2020, Red Hat Middleware LLC, 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.test.security.common.elytron;

import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.PathElement;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.operations.common.Util;
import org.jboss.as.test.integration.management.util.CLIWrapper;
import org.jboss.as.test.integration.security.common.Utils;
import org.jboss.dmr.ModelNode;

/**
* A {@link ConfigurableElement} to define the failover-realm resource within the Elytron subsystem.
*
* @author Ondrej Kotek
*/
public class FailoverRealm implements SecurityRealm {

private final PathAddress address;
private final String name;
private final String delegateRealm;
private final String failoverRealm;
private final Boolean emitEvents;

FailoverRealm(final String name, final Builder builder) {
this.name = name;
this.address = PathAddress.pathAddress(PathElement.pathElement("subsystem", "elytron"), PathElement.pathElement("failover-realm", name));
this.delegateRealm = builder.delegateRealm;
this.failoverRealm = builder.failoverRealm;
this.emitEvents = builder.emitEvents;
}

@Override
public String getName() {
return name;
}

public ModelNode getAddOperation() {
ModelNode addOperation = Util.createAddOperation(address);
if (this.delegateRealm != null) {
addOperation.get("delegate-realm").set(this.delegateRealm);
}
if (this.failoverRealm != null) {
addOperation.get("failover-realm").set(this.failoverRealm);
}
if (this.emitEvents != null) {
addOperation.get("emit-events").set(this.emitEvents);
}

return addOperation;
}

public ModelNode getRemoveOperation() {
return Util.createRemoveOperation(address);
}

@Override
public void create(ModelControllerClient client, CLIWrapper cli) throws Exception {
Utils.applyUpdate(getAddOperation(), client);
}

@Override
public void remove(ModelControllerClient client, CLIWrapper cli) throws Exception {
Utils.applyUpdate(getRemoveOperation(), client);
}

public static Builder builder(final String name) {
return new Builder(name);
}

public static class Builder {

private final String name;
private String delegateRealm;
private String failoverRealm;
private Boolean emitEvents;

Builder(final String name) {
this.name = name;
}

public Builder withDelegateRealm(final String realm) {
this.delegateRealm = realm;

return this;
}

public Builder withFailoverRealm(final String realm) {
this.failoverRealm = realm;

return this;
}

public Builder withEmitEvents(final Boolean emitEvents) {
this.emitEvents = emitEvents;

return this;
}

public SecurityRealm build() {
return new FailoverRealm(name, this);
}

}

}
Expand Up @@ -74,6 +74,10 @@ public void remove(CLIWrapper cli) throws Exception {
FileUtils.deleteQuietly(tempFolder);
}

public Path getPath() {
return this.path;
}

/**
* Creates builder to build {@link FileSystemRealm}.
*
Expand Down
Expand Up @@ -38,11 +38,13 @@ public class PropertiesRealm extends AbstractUserAttributeValuesCapableElement i
private static final Logger LOGGER = Logger.getLogger(PropertiesRealm.class);

private final String groupsAttribute;
private final boolean plainText; // true by default
private File tempFolder;

private PropertiesRealm(Builder builder) {
super(builder);
this.groupsAttribute = builder.groupsAttribute;
this.plainText = builder.plainText;
}

@Override
Expand All @@ -61,8 +63,8 @@ public void create(CLIWrapper cli) throws Exception {
// groups-properties={path=/tmp/groups.properties}, groups-attribute="groups")
final String groupsAttrStr = groupsAttribute == null ? "" : String.format(", groups-attribute=\"%s\"", groupsAttribute);
cli.sendLine(String.format(
"/subsystem=elytron/properties-realm=%s:add(users-properties={path=\"%s\", plain-text=true}, groups-properties={path=\"%s\"}%s)",
name, asAbsolutePath(usersFile), asAbsolutePath(rolesFile), groupsAttrStr));
"/subsystem=elytron/properties-realm=%s:add(users-properties={path=\"%s\", plain-text=%b}, groups-properties={path=\"%s\"}%s)",
name, asAbsolutePath(usersFile), plainText, asAbsolutePath(rolesFile), groupsAttrStr));
}

@Override
Expand All @@ -85,8 +87,11 @@ private File writeProperties(Properties properties, String fileName) throws IOEx
File result = new File(tempFolder, fileName);
LOGGER.debugv("Creating property file {0}", result);
try (FileOutputStream fos = new FileOutputStream(result)) {
// comment $REALM_NAME is just a workaround for https://issues.jboss.org/browse/WFLY-7104
properties.store(fos, "$REALM_NAME=" + name + "$");
if (plainText) {
properties.store(fos, null);
} else {
properties.store(fos, "$REALM_NAME=" + name + "$");
}
}
return result;
}
Expand All @@ -96,6 +101,7 @@ private File writeProperties(Properties properties, String fileName) throws IOEx
*/
public static final class Builder extends AbstractUserAttributeValuesCapableElement.Builder<Builder> {
private String groupsAttribute;
private boolean plainText = true;

private Builder() {
}
Expand All @@ -105,6 +111,11 @@ public Builder withGroupsAttribute(String groupsAttribute) {
return this;
}

public Builder withPlainText(boolean plainText) {
this.plainText = plainText;
return this;
}

public PropertiesRealm build() {
return new PropertiesRealm(this);
}
Expand Down
Expand Up @@ -135,6 +135,10 @@ public static JwtBuilder jwtBuilder() {
return new JwtBuilder();
}

public static Oauth2IntrospectionBuilder oauth2IntrospectionBuilder() {
return new Oauth2IntrospectionBuilder();
}

public static final class Builder {

private final String name;
Expand Down

0 comments on commit 3bc17fc

Please sign in to comment.