-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Refactor remote cluster interceptor and tests #135747
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
Changes from 4 commits
e2d1125
ac52102
d03c4e8
6c9c7b2
8582ae5
478bf6e
443db48
f60851e
38053c0
46f0119
755b480
0f519bb
e0e01cb
cda64b2
f1b255d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
import static org.elasticsearch.xpack.core.security.authc.CrossClusterAccessSubjectInfo.CROSS_CLUSTER_ACCESS_SUBJECT_INFO_HEADER_KEY; | ||
import static org.elasticsearch.xpack.security.authc.CrossClusterAccessHeaders.CROSS_CLUSTER_ACCESS_CREDENTIALS_HEADER_KEY; | ||
|
||
final class CrossClusterAccessServerTransportFilter extends ServerTransportFilter { | ||
final class CrossClusterAccessServerTransportFilter extends DefaultServerTransportFilter implements ServerTransportFilter { | ||
|
||
|
||
private static final Logger logger = LogManager.getLogger(CrossClusterAccessServerTransportFilter.class); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
package org.elasticsearch.xpack.security.transport; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.TransportVersion; | ||
import org.elasticsearch.action.ActionListener; | ||
import org.elasticsearch.action.IndicesRequest; | ||
import org.elasticsearch.action.admin.indices.close.TransportCloseIndexAction; | ||
import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction; | ||
import org.elasticsearch.action.admin.indices.open.OpenIndexAction; | ||
import org.elasticsearch.action.support.DestructiveOperations; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.transport.TaskTransportChannel; | ||
import org.elasticsearch.transport.TcpChannel; | ||
import org.elasticsearch.transport.TcpTransportChannel; | ||
import org.elasticsearch.transport.TransportChannel; | ||
import org.elasticsearch.transport.TransportRequest; | ||
import org.elasticsearch.transport.TransportService; | ||
import org.elasticsearch.transport.netty4.Netty4TcpChannel; | ||
import org.elasticsearch.xpack.core.security.SecurityContext; | ||
import org.elasticsearch.xpack.core.security.authc.Authentication; | ||
import org.elasticsearch.xpack.core.security.user.SystemUser; | ||
import org.elasticsearch.xpack.security.action.SecurityActionMapper; | ||
import org.elasticsearch.xpack.security.authc.AuthenticationService; | ||
import org.elasticsearch.xpack.security.authz.AuthorizationService; | ||
|
||
/** | ||
* The server transport filter that should be used in nodes as it ensures that an incoming | ||
* request is properly authenticated and authorized | ||
*/ | ||
public class DefaultServerTransportFilter implements ServerTransportFilter { | ||
|
||
private static final Logger logger = LogManager.getLogger(DefaultServerTransportFilter.class); | ||
|
||
private final AuthenticationService authcService; | ||
private final AuthorizationService authzService; | ||
private final ThreadContext threadContext; | ||
private final boolean extractClientCert; | ||
private final DestructiveOperations destructiveOperations; | ||
private final SecurityContext securityContext; | ||
|
||
public DefaultServerTransportFilter( | ||
AuthenticationService authcService, | ||
AuthorizationService authzService, | ||
ThreadContext threadContext, | ||
boolean extractClientCert, | ||
DestructiveOperations destructiveOperations, | ||
SecurityContext securityContext | ||
) { | ||
this.authcService = authcService; | ||
this.authzService = authzService; | ||
this.threadContext = threadContext; | ||
this.extractClientCert = extractClientCert; | ||
this.destructiveOperations = destructiveOperations; | ||
this.securityContext = securityContext; | ||
} | ||
|
||
@Override | ||
public void inbound(String action, TransportRequest request, TransportChannel transportChannel, ActionListener<Void> listener) { | ||
if (TransportCloseIndexAction.NAME.equals(action) | ||
|| OpenIndexAction.NAME.equals(action) | ||
|| TransportDeleteIndexAction.TYPE.name().equals(action)) { | ||
IndicesRequest indicesRequest = (IndicesRequest) request; | ||
try { | ||
destructiveOperations.failDestructive(indicesRequest.indices()); | ||
} catch (IllegalArgumentException e) { | ||
listener.onFailure(e); | ||
return; | ||
} | ||
} | ||
/* | ||
here we don't have a fallback user, as all incoming request are | ||
expected to have a user attached (either in headers or in context) | ||
We can make this assumption because in nodes we make sure all outgoing | ||
requests from all the nodes are attached with a user (either a serialize | ||
user an authentication token | ||
*/ | ||
String securityAction = SecurityActionMapper.action(action, request); | ||
|
||
TransportChannel unwrappedChannel = transportChannel; | ||
if (unwrappedChannel instanceof TaskTransportChannel) { | ||
unwrappedChannel = ((TaskTransportChannel) unwrappedChannel).getChannel(); | ||
} | ||
|
||
if (extractClientCert && (unwrappedChannel instanceof TcpTransportChannel)) { | ||
TcpChannel tcpChannel = ((TcpTransportChannel) unwrappedChannel).getChannel(); | ||
if (tcpChannel instanceof Netty4TcpChannel) { | ||
if (tcpChannel.isOpen()) { | ||
SSLEngineUtils.extractClientCertificates(logger, threadContext, tcpChannel); | ||
} | ||
} | ||
} | ||
|
||
TransportVersion version = transportChannel.getVersion(); | ||
authenticate(securityAction, request, listener.delegateFailureAndWrap((l, authentication) -> { | ||
if (authentication != null) { | ||
if (securityAction.equals(TransportService.HANDSHAKE_ACTION_NAME) | ||
&& SystemUser.is(authentication.getEffectiveSubject().getUser()) == false) { | ||
securityContext.executeAsSystemUser(version, original -> { | ||
final Authentication replaced = securityContext.getAuthentication(); | ||
authzService.authorize(replaced, securityAction, request, l); | ||
}); | ||
} else { | ||
authzService.authorize(authentication, securityAction, request, l); | ||
} | ||
} else { | ||
l.onFailure(new IllegalStateException("no authentication present but auth is allowed")); | ||
} | ||
})); | ||
} | ||
|
||
protected void authenticate( | ||
final String securityAction, | ||
final TransportRequest request, | ||
final ActionListener<Authentication> authenticationListener | ||
) { | ||
authcService.authenticate(securityAction, request, true, authenticationListener); | ||
} | ||
|
||
protected final ThreadContext getThreadContext() { | ||
return threadContext; | ||
} | ||
|
||
// Package private for testing | ||
boolean isExtractClientCert() { | ||
return extractClientCert; | ||
} | ||
|
||
} |
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.
Returning
Optional<ServerTransportFilter>
is more of a preference here. We could as well returnServerTransportFilter
and annotate the method as@Nullable