Skip to content
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

WFLY-18650 - Security roles lost following failover #2247

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,16 @@ public Principal getRealmIdentityPrincipal() {
return principal;
}

@Override
public Subject getSubject() {
return subject;
}

@Override
public void setSubject(Subject subject) {
this.subject = subject;
}

@Override
public SupportLevel getCredentialAcquireSupport(final Class<? extends Credential> credentialType, final String algorithmName, final AlgorithmParameterSpec parameterSpec) throws RealmUnavailableException {
return JaasSecurityRealm.this.getCredentialAcquireSupport(credentialType, algorithmName, parameterSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@

package org.wildfly.security.auth.callback;

import static org.wildfly.common.Assert.checkNotNullParam;

import java.security.Principal;
import java.util.Set;
import java.util.function.Function;

import javax.security.auth.Subject;

import org.wildfly.common.Assert;
import org.wildfly.security.auth.principal.NamePrincipal;
import org.wildfly.security.auth.server.RealmIdentity;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityIdentity;
import org.wildfly.security.cache.CachedIdentity;
import org.wildfly.security.cache.IdentityCache;

import java.security.Principal;
import java.util.function.Function;

import static org.wildfly.common.Assert.checkNotNullParam;

/**
* <p>A callback that is capable of perform authorization based on the identities managed by an {@link IdentityCache}.
*
Expand Down Expand Up @@ -139,6 +143,25 @@ public CachedIdentityAuthorizeCallback(Principal principal, Function<SecurityDom
this.localCache = localCache;
}

/**
* Set the Roles present on {@link CachedIdentity} into the {@link RealmIdentity#setSubject(Subject)} in order to get authenticate on all HA nodes;
* @param realmIdentity
*/
public void setSubject(RealmIdentity realmIdentity) {
checkNotNullParam("realmIdentity", realmIdentity);
Subject subject = realmIdentity.getSubject();
if (subject == null) {
CachedIdentity cachedIdentity = createDomainCache().get();
if (cachedIdentity != null) {
subject = new Subject();
Set<Principal> principals = subject.getPrincipals();
principals.add(realmIdentity.getRealmIdentityPrincipal());
cachedIdentity.getRoles().forEach(role -> principals.add(new Roles(role)));
realmIdentity.setSubject(subject);
}
}
}

/**
* Indicates if a cached identity was successfully authorized.
*
Expand Down Expand Up @@ -229,4 +252,19 @@ public boolean needsInformation() {
private IdentityCache createDomainCache() {
return this.identityCache.apply(securityDomain);
}

private static class Roles implements Principal {

private final String name;

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.security.spec.AlgorithmParameterSpec;
import java.util.function.Function;

import javax.security.auth.Subject;

import org.wildfly.common.Assert;
import org.wildfly.security.auth.SupportLevel;
import org.wildfly.security.auth.principal.AnonymousPrincipal;
Expand Down Expand Up @@ -55,6 +57,12 @@ public interface RealmIdentity {
*/
Principal getRealmIdentityPrincipal();

default Subject getSubject() {
return null;
}

default void setSubject(Subject subject) {}

/**
* @deprecated Transition method; remove before GA.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
import org.wildfly.security.auth.callback.EvidenceVerifyCallback;
import org.wildfly.security.auth.callback.ExclusiveNameCallback;
import org.wildfly.security.auth.callback.FastUnsupportedCallbackException;
import org.wildfly.security.auth.callback.PrincipalAuthorizeCallback;
import org.wildfly.security.auth.callback.MechanismInformationCallback;
import org.wildfly.security.auth.callback.IdentityCredentialCallback;
import org.wildfly.security.auth.callback.MechanismInformationCallback;
import org.wildfly.security.auth.callback.PeerPrincipalCallback;
import org.wildfly.security.auth.callback.PrincipalAuthorizeCallback;
import org.wildfly.security.auth.callback.RequestInformationCallback;
import org.wildfly.security.auth.callback.SSLCallback;
import org.wildfly.security.auth.callback.SecurityIdentityCallback;
Expand All @@ -74,6 +74,7 @@
import org.wildfly.security.auth.permission.RunAsPrincipalPermission;
import org.wildfly.security.auth.principal.AnonymousPrincipal;
import org.wildfly.security.auth.principal.NamePrincipal;
import org.wildfly.security.auth.server._private.ElytronMessages;
import org.wildfly.security.auth.server.event.RealmFailedAuthenticationEvent;
import org.wildfly.security.auth.server.event.RealmIdentityFailedAuthorizationEvent;
import org.wildfly.security.auth.server.event.RealmIdentitySuccessfulAuthorizationEvent;
Expand All @@ -98,7 +99,6 @@
import org.wildfly.security.password.spec.ClearPasswordSpec;
import org.wildfly.security.ssl.SSLConnection;
import org.wildfly.security.x500.X500;
import org.wildfly.security.auth.server._private.ElytronMessages;

/**
* Server-side authentication context. Instances of this class are used to perform all authentication and re-authorization
Expand Down Expand Up @@ -1133,6 +1133,7 @@ private void handleOne(final Callback[] callbacks, final int idx) throws IOExcep
}
if (principal != null) {
setAuthenticationPrincipal(principal);
authorizeCallback.setSubject(stateRef.get().getRealmIdentity());
if (authorize()) {
authorizedIdentity = getAuthorizedIdentity();
}
Expand Down
Loading