-
Notifications
You must be signed in to change notification settings - Fork 53
ELYWEB-267 - Add test for Security roles lost following failover #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pedro-hos
wants to merge
1
commit into
wildfly-security:4.x
Choose a base branch
from
pedro-hos:ELYWEB-267
base: 4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
.../src/test/java/org/wildfly/elytron/web/undertow/server/servlet/CustomCallbackHandler.java
This file contains hidden or 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,58 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.wildfly.elytron.web.undertow.server.servlet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.Principal; | ||
|
|
||
| import javax.security.auth.callback.Callback; | ||
| import javax.security.auth.callback.CallbackHandler; | ||
| import javax.security.auth.callback.NameCallback; | ||
| import javax.security.auth.callback.PasswordCallback; | ||
| import javax.security.auth.callback.UnsupportedCallbackException; | ||
|
|
||
| import org.wildfly.security.evidence.PasswordGuessEvidence; | ||
|
|
||
| /** | ||
| * @author <a href="mailto:pesilva@redhat.com">Pedro Hos</a> | ||
| * | ||
| */ | ||
| public class CustomCallbackHandler implements CallbackHandler { | ||
|
|
||
| private Principal principal; | ||
| private char[] evidence; | ||
|
|
||
| public CustomCallbackHandler() {} | ||
|
|
||
| public void setSecurityInfo(final Principal principal, final Object evidence) { | ||
| this.principal = principal; | ||
| if(evidence instanceof PasswordGuessEvidence) { | ||
| this.evidence = ((PasswordGuessEvidence) evidence).getGuess(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { | ||
|
|
||
| if (callbacks == null) { | ||
| throw new IllegalArgumentException("The callbacks argument cannot be null"); | ||
| } | ||
|
|
||
| for (Callback callback : callbacks) { | ||
| if (callback instanceof NameCallback && principal != null) { | ||
| NameCallback nameCallback = (NameCallback) callback; | ||
| nameCallback.setName(this.principal.getName()); | ||
| } else if (callback instanceof PasswordCallback) { | ||
| PasswordCallback passwordCallback = (PasswordCallback) callback; | ||
| passwordCallback.setPassword((this.evidence)); | ||
| } else { | ||
| throw new UnsupportedCallbackException(callback, "Unsupported callback"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
136 changes: 136 additions & 0 deletions
136
...elytron/web/undertow/server/servlet/JAASFormServletAuthenticationWithClusterTestTest.java
This file contains hidden or 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,136 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.wildfly.elytron.web.undertow.server.servlet; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.Map; | ||
|
|
||
| import org.infinispan.Cache; | ||
| import org.infinispan.commons.configuration.ClassAllowList; | ||
| import org.infinispan.commons.marshall.JavaSerializationMarshaller; | ||
| import org.infinispan.configuration.cache.CacheMode; | ||
| import org.infinispan.configuration.cache.ConfigurationBuilder; | ||
| import org.infinispan.configuration.global.GlobalConfigurationBuilder; | ||
| import org.infinispan.manager.DefaultCacheManager; | ||
| import org.infinispan.manager.EmbeddedCacheManager; | ||
| import org.infinispan.remoting.transport.jgroups.JGroupsTransport; | ||
| import org.jgroups.util.UUID; | ||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.Rule; | ||
| import org.wildfly.elytron.web.undertow.common.UndertowServer; | ||
| import org.wildfly.security.auth.permission.LoginPermission; | ||
| import org.wildfly.security.auth.realm.JaasSecurityRealm; | ||
| import org.wildfly.security.auth.server.SecurityDomain; | ||
| import org.wildfly.security.auth.server.SecurityRealm; | ||
| import org.wildfly.security.http.HttpServerAuthenticationMechanismFactory; | ||
| import org.wildfly.security.http.util.sso.DefaultSingleSignOnManager; | ||
| import org.wildfly.security.http.util.sso.DefaultSingleSignOnSessionFactory; | ||
| import org.wildfly.security.http.util.sso.DefaultSingleSignOnSessionIdentifierFactory; | ||
| import org.wildfly.security.http.util.sso.SingleSignOnEntry; | ||
| import org.wildfly.security.http.util.sso.SingleSignOnManager; | ||
| import org.wildfly.security.http.util.sso.SingleSignOnServerMechanismFactory; | ||
| import org.wildfly.security.http.util.sso.SingleSignOnSessionFactory; | ||
|
|
||
| /** | ||
| * @author <a href="mailto:pesilva@redhat.com">Pedro Hos</a> | ||
| * | ||
| */ | ||
| public class JAASFormServletAuthenticationWithClusterTestTest extends FormAuthenticationSSOBase { | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() { | ||
| System.setProperty("java.security.auth.login.config", JAASFormServletAuthenticationWithClusterTestTest.class.getResource("jaas-login.config").toString()); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void afterClass() { | ||
| System.clearProperty("java.security.auth.login.config"); | ||
| } | ||
|
|
||
| @Rule | ||
| public final UndertowServer serverA = createUndertowServer(7776); | ||
|
|
||
| @Rule | ||
| public final UndertowServer serverB = createUndertowServer(7777); | ||
|
|
||
| public JAASFormServletAuthenticationWithClusterTestTest() throws Exception { | ||
| } | ||
|
|
||
| @Override | ||
| protected URI createUriAppA(String alternativePath) throws URISyntaxException { | ||
| return serverA.createUri(alternativePath); | ||
| } | ||
|
|
||
| @Override | ||
| protected URI createUriAppB(String alternativePath) throws URISyntaxException { | ||
| return serverB.createUri(alternativePath); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getContextRootAppA() { | ||
| return serverA.getContextRoot(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getContextRootAppB() { | ||
| return serverB.getContextRoot(); | ||
| } | ||
|
|
||
| @Override | ||
| protected HttpServerAuthenticationMechanismFactory getHttpServerAuthenticationMechanismFactory(Map<String, ?> properties) { | ||
| HttpServerAuthenticationMechanismFactory delegate = super.getHttpServerAuthenticationMechanismFactory(properties); | ||
|
|
||
| String cacheManagerName = UUID.randomUUID().toString(); | ||
| ClassAllowList allowList = new ClassAllowList(); | ||
| allowList.addRegexps(".*"); | ||
|
|
||
| EmbeddedCacheManager cacheManager = new DefaultCacheManager( | ||
| GlobalConfigurationBuilder.defaultClusteredBuilder() | ||
| .globalJmxStatistics().cacheManagerName(cacheManagerName).defaultCacheName("Default") | ||
| .transport().nodeName(cacheManagerName).addProperty(JGroupsTransport.CONFIGURATION_FILE, "fast.xml") | ||
| .serialization().marshaller(new JavaSerializationMarshaller(allowList)) | ||
| .build(), | ||
| new ConfigurationBuilder() | ||
| .clustering() | ||
| .cacheMode(CacheMode.REPL_SYNC) | ||
| .build() | ||
| ); | ||
|
|
||
| Cache<String, SingleSignOnEntry> cache = cacheManager.getCache(); | ||
| SingleSignOnManager manager = new DefaultSingleSignOnManager(cache, new DefaultSingleSignOnSessionIdentifierFactory(), (id, entry) -> cache.put(id, entry)); | ||
| SingleSignOnServerMechanismFactory.SingleSignOnConfiguration signOnConfiguration = | ||
| new SingleSignOnServerMechanismFactory.SingleSignOnConfiguration("JSESSIONSSOID", null, | ||
| "/", false, false); | ||
|
|
||
| if (keyPairSupplier == null) { | ||
| keyPairSupplier = new KeyPairSupplier(); | ||
| } | ||
| SingleSignOnSessionFactory singleSignOnSessionFactory = new DefaultSingleSignOnSessionFactory(manager, keyPairSupplier.get()); | ||
|
|
||
| return new SingleSignOnServerMechanismFactory(delegate, singleSignOnSessionFactory, signOnConfiguration); | ||
| } | ||
|
|
||
| @Override | ||
| protected UndertowServer createUndertowServer(int port) throws Exception { | ||
| return createUndertowServerBuilder(port).setSecurityRoles("Admin") | ||
| .setRoleAllowed("Admin") | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| protected SecurityDomain doCreateSecurityDomain() throws Exception { | ||
| SecurityRealm realm = new JaasSecurityRealm("Entry1", null, null, new CustomCallbackHandler()); | ||
| //SecurityRealm realm = new JaasSecurityRealm("Entry1"); | ||
| SecurityDomain securityDomain = SecurityDomain.builder().setDefaultRealmName("default") | ||
| .addRealm("default", realm).build() | ||
| .setPermissionMapper(((permissionMappable, roles) -> LoginPermission.getInstance())) | ||
| .build(); | ||
| return securityDomain; | ||
| } | ||
|
|
||
| } | ||
114 changes: 114 additions & 0 deletions
114
...ervlet/src/test/java/org/wildfly/elytron/web/undertow/server/servlet/TestLoginModule.java
This file contains hidden or 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,114 @@ | ||
| /* | ||
| * Copyright The WildFly Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.wildfly.elytron.web.undertow.server.servlet; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.Principal; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import javax.security.auth.Subject; | ||
| import javax.security.auth.callback.Callback; | ||
| import javax.security.auth.callback.CallbackHandler; | ||
| import javax.security.auth.callback.NameCallback; | ||
| import javax.security.auth.callback.PasswordCallback; | ||
| import javax.security.auth.callback.UnsupportedCallbackException; | ||
| import javax.security.auth.login.LoginException; | ||
| import javax.security.auth.spi.LoginModule; | ||
|
|
||
| import org.wildfly.security.auth.principal.NamePrincipal; | ||
|
|
||
| /** | ||
| * @author <a href="mailto:pesilva@redhat.com">Pedro Hos</a> | ||
| * | ||
| */ | ||
| public class TestLoginModule implements LoginModule { | ||
|
|
||
| private final Map<String, char[]> usersMap = new HashMap<String, char[]>(); | ||
| private Principal principal; | ||
| private Subject subject; | ||
| private CallbackHandler handler; | ||
|
|
||
| @Override | ||
| public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, | ||
| Map<String, ?> options) { | ||
| this.subject = subject; | ||
| this.handler = callbackHandler; | ||
| this.usersMap.put("ladybird", "Coleoptera".toCharArray()); | ||
| this.usersMap.put("dung", "Coleopterida".toCharArray()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean login() throws LoginException { | ||
|
|
||
| // obtain the incoming username and password from the callback handler | ||
| NameCallback nameCallback = new NameCallback("Username"); | ||
| PasswordCallback passwordCallback = new PasswordCallback("Password", false); | ||
| Callback[] callbacks = new Callback[] { nameCallback, passwordCallback }; | ||
|
|
||
| try { | ||
| this.handler.handle(callbacks); | ||
| } catch (UnsupportedCallbackException | IOException e) { | ||
| throw new LoginException("Error handling callback: " + e.getMessage()); | ||
| } | ||
|
|
||
| final String username = nameCallback.getName(); | ||
| this.setPrincipal(new NamePrincipal(username)); | ||
|
|
||
| final char[] password = passwordCallback.getPassword(); | ||
|
|
||
| char[] storedPassword = this.usersMap.get(username); | ||
| if (!Arrays.equals(storedPassword, password)) { | ||
| throw new LoginException("Invalid password"); | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean commit() throws LoginException { | ||
| if (this.principal.getName().equals("ladybird") || this.principal.getName().equals("dung")) { | ||
| this.subject.getPrincipals().add(new Roles("Admin")); | ||
| this.subject.getPrincipals().add(new Roles("User")); | ||
| this.subject.getPrincipals().add(new Roles("Guest")); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean abort() throws LoginException { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean logout() throws LoginException { | ||
| this.subject.getPrincipals().clear(); | ||
| return true; | ||
| } | ||
|
|
||
| public Principal getPrincipal() { | ||
| return principal; | ||
| } | ||
|
|
||
| public void setPrincipal(Principal principal) { | ||
| this.principal = principal; | ||
| } | ||
|
|
||
| public static class Roles implements Principal { | ||
|
|
||
| private String role; | ||
|
|
||
| Roles(String role) { | ||
| this.role = role; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return this.role; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
3 changes: 3 additions & 0 deletions
3
...vlet/src/test/resources/org/wildfly/elytron/web/undertow/server/servlet/jaas-login.config
This file contains hidden or 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,3 @@ | ||
| Entry1 { | ||
| org.wildfly.elytron.web.undertow.server.servlet.TestLoginModule required; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@darranl The test isn't work properly yet when running under cluster mode. I suspect that I need to change something here, on cache configuration. The fix works on https://issues.redhat.com/browse/WFLY-18650 reproducer, but under the undertow test here, the second node can't see the roles, and can't login properly. Do you have any light on how to configure the HttpServerAuthenticationMechanismFactory to make it work? Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedro-hos Please try to run your test on top of this PR wildfly-security/wildfly-elytron#2294 , thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Skyllarr great, all tests are green w/ your change. Thank You