Skip to content

Commit 211b8c2

Browse files
Flatten stacks in ServerTransportFilter via promise pattern
Follow up to previous stack-flattening in authz, keeping things a little simpler here.
1 parent 80deeb8 commit 211b8c2

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/CrossClusterAccessServerTransportFilter.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.apache.logging.log4j.Logger;
1212
import org.elasticsearch.action.ActionListener;
1313
import org.elasticsearch.action.support.DestructiveOperations;
14+
import org.elasticsearch.common.util.concurrent.ListenableFuture;
1415
import org.elasticsearch.common.util.concurrent.ThreadContext;
1516
import org.elasticsearch.license.LicenseUtils;
1617
import org.elasticsearch.license.XPackLicenseState;
@@ -71,27 +72,25 @@ final class CrossClusterAccessServerTransportFilter extends ServerTransportFilte
7172
}
7273

7374
@Override
74-
protected void authenticate(
75-
final String securityAction,
76-
final TransportRequest request,
77-
final ActionListener<Authentication> authenticationListener
78-
) {
75+
protected ListenableFuture<Authentication> authenticate(final String securityAction, final TransportRequest request) {
76+
final ListenableFuture<Authentication> listener = new ListenableFuture<>();
7977
if (false == Security.ADVANCED_REMOTE_CLUSTER_SECURITY_FEATURE.check(licenseState)) {
8078
onFailureWithDebugLog(
8179
securityAction,
8280
request,
83-
authenticationListener,
81+
listener,
8482
LicenseUtils.newComplianceException(Security.ADVANCED_REMOTE_CLUSTER_SECURITY_FEATURE.getName())
8583
);
8684
} else {
8785
try {
8886
validateHeaders();
8987
} catch (Exception ex) {
90-
onFailureWithDebugLog(securityAction, request, authenticationListener, ex);
91-
return;
88+
onFailureWithDebugLog(securityAction, request, listener, ex);
89+
return listener;
9290
}
93-
crossClusterAccessAuthcService.authenticate(securityAction, request, authenticationListener);
91+
crossClusterAccessAuthcService.authenticate(securityAction, request, listener);
9492
}
93+
return listener;
9594
}
9695

9796
private void validateHeaders() {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/transport/ServerTransportFilter.java

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.action.admin.indices.delete.TransportDeleteIndexAction;
1616
import org.elasticsearch.action.admin.indices.open.OpenIndexAction;
1717
import org.elasticsearch.action.support.DestructiveOperations;
18+
import org.elasticsearch.common.util.concurrent.ListenableFuture;
1819
import org.elasticsearch.common.util.concurrent.ThreadContext;
1920
import org.elasticsearch.transport.TaskTransportChannel;
2021
import org.elasticsearch.transport.TcpChannel;
@@ -102,29 +103,44 @@ requests from all the nodes are attached with a user (either a serialize
102103
}
103104

104105
TransportVersion version = transportChannel.getVersion();
105-
authenticate(securityAction, request, listener.delegateFailureAndWrap((l, authentication) -> {
106-
if (authentication != null) {
107-
if (securityAction.equals(TransportService.HANDSHAKE_ACTION_NAME)
108-
&& SystemUser.is(authentication.getEffectiveSubject().getUser()) == false) {
109-
securityContext.executeAsSystemUser(version, original -> {
110-
final Authentication replaced = securityContext.getAuthentication();
111-
authzService.authorize(replaced, securityAction, request, l);
112-
});
113-
} else {
114-
authzService.authorize(authentication, securityAction, request, l);
115-
}
106+
var authFuture = authenticate(securityAction, request);
107+
if (authFuture.isSuccess()) {
108+
handleAuthentication(request, listener, authFuture.result(), securityAction, version);
109+
} else {
110+
authFuture.addListener(
111+
listener.delegateFailureAndWrap(
112+
(l, authentication) -> handleAuthentication(request, l, authentication, securityAction, version)
113+
)
114+
);
115+
}
116+
}
117+
118+
private void handleAuthentication(
119+
TransportRequest request,
120+
ActionListener<Void> listener,
121+
Authentication authentication,
122+
String securityAction,
123+
TransportVersion version
124+
) {
125+
if (authentication != null) {
126+
if (securityAction.equals(TransportService.HANDSHAKE_ACTION_NAME)
127+
&& SystemUser.is(authentication.getEffectiveSubject().getUser()) == false) {
128+
securityContext.executeAsSystemUser(version, original -> {
129+
final Authentication replaced = securityContext.getAuthentication();
130+
authzService.authorize(replaced, securityAction, request, listener);
131+
});
116132
} else {
117-
l.onFailure(new IllegalStateException("no authentication present but auth is allowed"));
133+
authzService.authorize(authentication, securityAction, request, listener);
118134
}
119-
}));
135+
} else {
136+
listener.onFailure(new IllegalStateException("no authentication present but auth is allowed"));
137+
}
120138
}
121139

122-
protected void authenticate(
123-
final String securityAction,
124-
final TransportRequest request,
125-
final ActionListener<Authentication> authenticationListener
126-
) {
127-
authcService.authenticate(securityAction, request, true, authenticationListener);
140+
protected ListenableFuture<Authentication> authenticate(final String securityAction, final TransportRequest request) {
141+
final ListenableFuture<Authentication> listener = new ListenableFuture<>();
142+
authcService.authenticate(securityAction, request, true, listener);
143+
return listener;
128144
}
129145

130146
protected final ThreadContext getThreadContext() {

0 commit comments

Comments
 (0)