Skip to content

Commit a988c03

Browse files
[8.x] Fix lingering license warning header in IP filter (#115510) (#115838)
* Fix lingering license warning header in IP filter (#115510) Fixes another place where we do not stash thread context that causes the license warning header to persist in the thread context across Netty worker threads. Resolves #114865 Relates to #107573
1 parent a1bfc99 commit a988c03

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

docs/changelog/115510.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 115510
2+
summary: Fix lingering license warning header in IP filter
3+
area: License
4+
type: bug
5+
issues:
6+
- 114865

muted-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,6 @@ tests:
316316
- class: org.elasticsearch.xpack.remotecluster.RemoteClusterSecurityWithApmTracingRestIT
317317
method: testTracingCrossCluster
318318
issue: https://github.com/elastic/elasticsearch/issues/112731
319-
- class: org.elasticsearch.license.LicensingTests
320-
issue: https://github.com/elastic/elasticsearch/issues/114865
321319
- class: org.elasticsearch.xpack.inference.TextEmbeddingCrudIT
322320
method: testPutE5Small_withPlatformSpecificVariant
323321
issue: https://github.com/elastic/elasticsearch/issues/113950

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/license/LicensingTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.xpack.security.LocalStateSecurity;
4242
import org.hamcrest.Matchers;
4343
import org.junit.After;
44+
import org.junit.Assert;
4445
import org.junit.Before;
4546

4647
import java.nio.file.Files;
@@ -241,6 +242,7 @@ public void testNoWarningHeaderWhenAuthenticationFailed() throws Exception {
241242
Header[] headers = null;
242243
try {
243244
getRestClient().performRequest(request);
245+
Assert.fail("expected response exception");
244246
} catch (ResponseException e) {
245247
headers = e.getResponse().getHeaders();
246248
List<String> afterWarningHeaders = getWarningHeaders(headers);

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.netty.channel.ChannelHandlerContext;
1111
import io.netty.handler.ipfilter.AbstractRemoteAddressFilter;
1212

13+
import org.elasticsearch.common.util.concurrent.ThreadContext;
1314
import org.elasticsearch.xpack.security.transport.filter.IPFilter;
1415

1516
import java.net.InetSocketAddress;
@@ -19,16 +20,21 @@ class IpFilterRemoteAddressFilter extends AbstractRemoteAddressFilter<InetSocket
1920

2021
private final IPFilter filter;
2122
private final String profile;
23+
private final ThreadContext threadContext;
2224

23-
IpFilterRemoteAddressFilter(final IPFilter filter, final String profile) {
25+
IpFilterRemoteAddressFilter(final IPFilter filter, final String profile, final ThreadContext threadContext) {
2426
this.filter = filter;
2527
this.profile = profile;
28+
this.threadContext = threadContext;
2629
}
2730

2831
@Override
2932
protected boolean accept(final ChannelHandlerContext ctx, final InetSocketAddress remoteAddress) throws Exception {
3033
// at this stage no auth has happened, so we do not have any principal anyway
31-
return filter.accept(profile, remoteAddress);
34+
// this prevents thread-context changes to propagate beyond the channel accept test, as netty worker threads are reused
35+
try (ThreadContext.StoredContext ignore = threadContext.newStoredContext()) {
36+
return filter.accept(profile, remoteAddress);
37+
}
3238
}
3339

3440
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected void initChannel(final Channel ch) throws Exception {
104104

105105
private void maybeAddIPFilter(final Channel ch, final String name) {
106106
if (authenticator != null) {
107-
ch.pipeline().addFirst("ipfilter", new IpFilterRemoteAddressFilter(authenticator, name));
107+
ch.pipeline().addFirst("ipfilter", new IpFilterRemoteAddressFilter(authenticator, name, getThreadPool().getThreadContext()));
108108
}
109109
}
110110

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/transport/netty4/IpFilterRemoteAddressFilterTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.common.transport.BoundTransportAddress;
1717
import org.elasticsearch.common.transport.TransportAddress;
18+
import org.elasticsearch.common.util.concurrent.ThreadContext;
1819
import org.elasticsearch.http.HttpServerTransport;
1920
import org.elasticsearch.license.MockLicenseState;
2021
import org.elasticsearch.license.TestUtils;
@@ -90,10 +91,11 @@ public void init() throws Exception {
9091
ipFilter.setBoundHttpTransportAddress(httpTransport.boundAddress());
9192
}
9293

94+
ThreadContext threadContext = new ThreadContext(Settings.EMPTY);
9395
if (isHttpEnabled) {
94-
handler = new IpFilterRemoteAddressFilter(ipFilter, IPFilter.HTTP_PROFILE_NAME);
96+
handler = new IpFilterRemoteAddressFilter(ipFilter, IPFilter.HTTP_PROFILE_NAME, threadContext);
9597
} else {
96-
handler = new IpFilterRemoteAddressFilter(ipFilter, "default");
98+
handler = new IpFilterRemoteAddressFilter(ipFilter, "default", threadContext);
9799
}
98100
}
99101

@@ -106,7 +108,11 @@ public void testThatFilteringWorksByIp() throws Exception {
106108
}
107109

108110
public void testFilteringWorksForRemoteClusterPort() throws Exception {
109-
handler = new IpFilterRemoteAddressFilter(ipFilter, RemoteClusterPortSettings.REMOTE_CLUSTER_PROFILE);
111+
handler = new IpFilterRemoteAddressFilter(
112+
ipFilter,
113+
RemoteClusterPortSettings.REMOTE_CLUSTER_PROFILE,
114+
new ThreadContext(Settings.EMPTY)
115+
);
110116
InetSocketAddress localhostAddr = new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 12345);
111117
assertThat(handler.accept(mock(ChannelHandlerContext.class), localhostAddr), is(true));
112118

0 commit comments

Comments
 (0)