Skip to content

Commit

Permalink
Call logSucceededLogin in SecurityRestHandler instead of BackendRegis…
Browse files Browse the repository at this point in the history
…try in order to get request body.

This fixes the issue #4094

Signed-off-by: Timo Olkkonen <[email protected]>
  • Loading branch information
Timo Olkkonen authored and olkkoti committed Feb 8, 2025
1 parent e1c05c3 commit e4f6268
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ public boolean authenticate(final SecurityRequestChannel request) {
UserSubject subject = new UserSubjectImpl(threadPool, superuser);
threadContext.putPersistent(ConfigConstants.OPENDISTRO_SECURITY_AUTHENTICATED_USER, subject);
threadContext.putTransient(ConfigConstants.OPENDISTRO_SECURITY_USER, superuser);
auditLog.logSucceededLogin(sslPrincipal, true, null, request);
return true;
}

Expand Down Expand Up @@ -393,9 +392,12 @@ public boolean authenticate(final SecurityRequestChannel request) {
final User impersonatedUser = impersonate(request, authenticatedUser);
final User effectiveUser = impersonatedUser == null ? authenticatedUser : impersonatedUser;
threadPool.getThreadContext().putTransient(ConfigConstants.OPENDISTRO_SECURITY_USER, effectiveUser);
if (effectiveUser != authenticatedUser) {
threadPool.getThreadContext()
.putTransient(ConfigConstants.OPENDISTRO_SECURITY_IMPERSONATION_INITIATING_USER, authenticatedUser.getName());
}
UserSubject subject = new UserSubjectImpl(threadPool, effectiveUser);
threadPool.getThreadContext().putPersistent(ConfigConstants.OPENDISTRO_SECURITY_AUTHENTICATED_USER, subject);
auditLog.logSucceededLogin(effectiveUser.getName(), false, authenticatedUser.getName(), request);
} else {
if (isDebugEnabled) {
log.debug("User still not authenticated after checking {} auth domains", restAuthDomains.size());
Expand Down Expand Up @@ -426,7 +428,6 @@ public boolean authenticate(final SecurityRequestChannel request) {

threadPool.getThreadContext().putTransient(ConfigConstants.OPENDISTRO_SECURITY_USER, anonymousUser);
threadPool.getThreadContext().putPersistent(ConfigConstants.OPENDISTRO_SECURITY_AUTHENTICATED_USER, subject);
auditLog.logSucceededLogin(anonymousUser.getName(), false, null, request);
if (isDebugEnabled) {
log.debug("Anonymous User is authenticated");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@

import static org.opensearch.security.OpenSearchSecurityPlugin.LEGACY_OPENDISTRO_PREFIX;
import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX;
import static org.opensearch.security.support.ConfigConstants.OPENDISTRO_SECURITY_IMPERSONATION_INITIATING_USER;

public class SecurityRestFilter {

Expand Down Expand Up @@ -156,6 +157,7 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c

final SecurityRequestChannel requestChannel = SecurityRequestFactory.from(request, channel);

String intiatingUser = threadContext.getTransient(OPENDISTRO_SECURITY_IMPERSONATION_INITIATING_USER);
// Authenticate request
if (!NettyAttribute.popFrom(request, Netty4HttpRequestHeaderVerifier.IS_AUTHENTICATED).orElse(false)) {
// we aren't authenticated so we should skip this step
Expand All @@ -170,6 +172,7 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
final User user = threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (userIsSuperAdmin(user, adminDNs)) {
// Super admins are always authorized
auditLog.logSucceededLogin(user.getName(), true, intiatingUser, requestChannel);
delegate.handleRequest(request, channel, client);
return;
}
Expand All @@ -189,6 +192,7 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
}

// Caller was authorized, forward the request to the handler
auditLog.logSucceededLogin(user == null ? null : user.getName(), false, intiatingUser, requestChannel);
delegate.handleRequest(request, channel, client);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public class ConfigConstants {

public static final String OPENDISTRO_SECURITY_USER_INFO_THREAD_CONTEXT = OPENDISTRO_SECURITY_CONFIG_PREFIX + "user_info";

public static final String OPENDISTRO_SECURITY_IMPERSONATION_INITIATING_USER = OPENDISTRO_SECURITY_CONFIG_PREFIX
+ "impersonation_initiating_user";

public static final String OPENDISTRO_SECURITY_INJECTED_USER = "injected_user";
public static final String OPENDISTRO_SECURITY_INJECTED_USER_HEADER = "injected_user_header";

Expand Down

0 comments on commit e4f6268

Please sign in to comment.