Skip to content

Commit

Permalink
Ensure getUser() is the logical user, not API key creator for RCS 2.0 (
Browse files Browse the repository at this point in the history
…#107023)

This commit changes SecurityContext#getUser() to provide the original user that 
initiated the call when run across clusters for RCS 2.0. Before this change the getUser() 
would provide the RCS 2.0 API key creator as the current user.
  • Loading branch information
jakelandis authored Apr 3, 2024
1 parent 147f5a0 commit a37debd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import static org.elasticsearch.xpack.core.security.authc.Authentication.getAuthenticationFromCrossClusterAccessMetadata;
import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.AUTHENTICATION_KEY;
import static org.elasticsearch.xpack.core.security.authz.AuthorizationServiceField.AUTHORIZATION_INFO_KEY;

Expand Down Expand Up @@ -71,6 +72,11 @@ public User requireUser() {
@Nullable
public User getUser() {
Authentication authentication = getAuthentication();
if (authentication != null) {
if (authentication.isCrossClusterAccess()) {
authentication = getAuthenticationFromCrossClusterAccessMetadata(authentication);
}
}
return authentication == null ? null : authentication.getEffectiveSubject().getUser();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.xpack.core.security.authc.Authentication.RealmRef;
import org.elasticsearch.xpack.core.security.authc.AuthenticationField;
import org.elasticsearch.xpack.core.security.authc.AuthenticationTestHelper;
import org.elasticsearch.xpack.core.security.authc.CrossClusterAccessSubjectInfo;
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.AuthorizationInfo;
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.ParentActionAuthorization;
import org.elasticsearch.xpack.core.security.authz.AuthorizationServiceField;
Expand Down Expand Up @@ -78,6 +79,20 @@ public void testGetAuthenticationAndUser() throws IOException {
assertEquals(user, securityContext.getUser());
}

public void testGetUserForAPIKeyBasedCrossCluster() throws IOException {
final User user = new User("test");
final CrossClusterAccessSubjectInfo crossClusterAccessSubjectInfo = AuthenticationTestHelper.randomCrossClusterAccessSubjectInfo(
AuthenticationTestHelper.builder().user(user).realmRef(new RealmRef("ldap", "foo", "node1")).build(false)
);
final Authentication authentication = AuthenticationTestHelper.builder()
.crossClusterAccess(randomAlphaOfLengthBetween(10, 20), crossClusterAccessSubjectInfo)
.build(false);
User apiKeyUser = authentication.getEffectiveSubject().getUser();
authentication.writeToContext(threadContext);
assertEquals(user, securityContext.getUser());
assertNotEquals(apiKeyUser, securityContext.getUser());
}

public void testGetAuthenticationDoesNotSwallowIOException() {
threadContext.putHeader(AuthenticationField.AUTHENTICATION_KEY, ""); // an intentionally corrupt header
final SecurityContext securityContext = new SecurityContext(Settings.EMPTY, threadContext);
Expand Down

0 comments on commit a37debd

Please sign in to comment.