GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][proxy] PIP-478: serialize and bound the proxy's broker-client a… · apache/pulsar@b761129

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-proxy/src
    • main/java/org/apache/pulsar/proxy/server
      • DirectProxyHandler.java
      • ProxyService.java
    • test/java/org/apache/pulsar/proxy/server
      • DirectProxyHandlerAuthTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java‎

Copy file name to clipboardExpand all lines: pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/DirectProxyHandler.java
+64-2Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public class DirectProxyHandler {
8181
private final String clientAuthMethod;
8282
public static final String TLS_HANDLER = "tls";
8383

84+
// PIP-478: hard cap on broker challenge rounds within a single binary authentication exchange, mirroring
85+
// ClientCnx.MAX_AUTH_CHALLENGE_ROUNDS (both in turn mirror HttpAuthenticationDriver.MAX_CHALLENGE_ROUNDS).
86+
// A broker that answers every CommandAuthResponse with another challenge would otherwise loop against the
87+
// proxy forever, and each round now also schedules credential work onto a blocking pool.
88+
static final int MAX_AUTH_CHALLENGE_ROUNDS = 10;
89+
8490
private final Authentication authentication;
8591
private final ProxyService service;
8692
private final Runnable onHandshakeCompleteAction;
@@ -263,6 +269,15 @@ public class ProxyBackendHandler extends PulsarDecoder {
263269
// PIP-478: the v5 exchange this backend connection authenticates through. Replaced on a broker-pushed
264270
// REFRESH, which starts a fresh exchange. Only ever touched on this channel's event loop.
265271
private AuthenticationExchange authExchange;
272+
// PIP-478: round state. AuthenticationExchange is single-round and non-thread-safe, and serializing
273+
// its rounds is the caller's obligation; this class is its second caller after ClientCnx. While the
274+
// frame decoder is still running (state Init) two challenge frames arriving in one read reach
275+
// handleAuthChallenge in the same event-loop turn, before either resolution has completed, and would
276+
// otherwise drive the same exchange concurrently. Both fields are touched only on this channel's
277+
// event loop (channelActive, handleAuthChallenge, and the continuations dispatched there), so they
278+
// need no synchronization.
279+
private boolean authRoundInProgress;
280+
private int authChallengeRounds;
266281

267282
public ProxyBackendHandler(ProxyConfiguration config, int protocolVersion, String remoteHostName,
268283
FeatureFlags featureFlags) {
@@ -286,6 +301,11 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
286301
// call it used to make is arbitrary plugin code — an OAuth2 token endpoint round trip, an Athenz
287302
// ZTS fetch, a GSSAPI exchange with the KDC — which stalled every connection multiplexed onto
288303
// that loop for its duration. The exchange's calls always off-load.
304+
//
305+
// The auth method name is read from the v4 plugin here and in handleAuthChallenge below, where
306+
// ClientCnx instead takes it from the exchange that produced the credential. Both are correct for
307+
// the proxy — it owns one started plugin, and the bridge's authMethodName() delegates straight to
308+
// it — and reading it from the plugin does not depend on a round having completed. Deliberate.
289309
authExchange = service.getProxyClientAuthenticationDriver().newAuthenticationExchange(remoteHostName);
290310
sendWhenResolved(authExchange.getAuthDataAsync(),
291311
authData -> Commands.newConnect(
@@ -303,15 +323,25 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception {
303323
* written there whether the credential was already in memory or needed I/O — command ordering on the
304324
* channel is therefore unchanged from the synchronous version. A failure closes the backend channel:
305325
* the proxy has no credential to send, and leaving the connection open would wait out the broker's
306-
* timeout instead of letting the client retry.
326+
* timeout instead of letting the client retry. That covers a failure to build the command too, not
327+
* just a failure to resolve the credential.
328+
*
329+
* <p>This is where an authentication round begins: the round is marked in progress so that a
330+
* challenge arriving before it completes is dropped rather than re-entering the exchange
331+
* concurrently.
307332
*
308333
* @param resolution the credential being resolved
309334
* @param commandBuilder builds the command to send from the resolved credential
310335
* @param what what is being authenticated, for logging
311336
*/
312337
private void sendWhenResolved(CompletableFuture<AuthData> resolution,
313338
Function<AuthData, ByteBuf> commandBuilder, String what) {
339+
authRoundInProgress = true;
340+
// The future returned by whenCompleteAsync is intentionally discarded: the continuation handles
341+
// every outcome itself, and the only way that future fails is ctx.executor() rejecting during
342+
// event-loop shutdown — at which point the channel is already going away.
314343
resolution.whenCompleteAsync((authData, throwable) -> {
344+
authRoundInProgress = false;
315345
if (throwable != null) {
316346
Throwable cause = FutureUtil.unwrapCompletionException(throwable);
317347
log.error().attr("channel", ctx.channel()).attr("stage", what).exception(cause)
@@ -320,7 +350,10 @@ private void sendWhenResolved(CompletableFuture<AuthData> resolution,
320350
return;
321351
}
322352
if (!ctx.channel().isActive()) {
323-
// The backend connection went away while the credential was resolving.
353+
// The backend connection went away while the credential was resolving. Logged so that
354+
// "backend connected but never sent CommandConnect" is diagnosable rather than silent.
355+
log.debug().attr("channel", ctx.channel()).attr("stage", what)
356+
.log("Backend channel closed while the proxy's broker-client credential resolved");
324357
return;
325358
}
326359
try {
@@ -395,8 +428,37 @@ protected void handleAuthChallenge(CommandAuthChallenge authChallenge) {
395428
// fresh exchange whose getAuthDataAsync() re-produces the current credential, rather than being
396429
// routed into the conversation it just terminated. Any other challenge is a round of the current
397430
// exchange, whose state slot carries conversation state across rounds. This mirrors ClientCnx.
431+
// The REFRESH branch is conformance with that rule rather than a path the broker can reach here:
432+
// this handler stops decoding once state == HandshakeCompleted, and the broker arms its refresh
433+
// task with an initial delay of authenticationRefreshCheckSeconds after connect completes, so by
434+
// the time a REFRESH is pushed it is proxied straight through to the client, which answers it.
435+
// The proxy's own credential refresh lives in ProxyConnection, not here.
398436
boolean refresh =
399437
Arrays.equals(AuthData.REFRESH_AUTH_DATA_BYTES, authChallenge.getChallenge().getAuthData());
438+
// PIP-478 (serialize-or-drop): a broker never pipelines challenges — it waits for each
439+
// CommandAuthResponse — so a challenge arriving while a round is still in flight is anomalous,
440+
// and servicing it would re-enter the same single-round, non-thread-safe exchange concurrently.
441+
// Dropping it makes rounds strictly serialized, which is why this class needs none of the
442+
// generation guarding ClientCnx carries: nothing here can supersede an in-flight round. ClientCnx
443+
// does need it, because it lets a REFRESH supersede one; the proxy can drop a REFRESH instead,
444+
// both because it cannot reach this handler and because the broker's refresh check is a
445+
// scheduleAtFixedRate task that would re-send it on the next tick.
446+
if (authRoundInProgress) {
447+
log.debug().attr("channel", ctx.channel())
448+
.log("Dropping a broker auth challenge received while an auth round is in progress");
449+
return;
450+
}
451+
// Bound the exchange. A REFRESH opens a fresh exchange, so it resets the counter; any other
452+
// challenge counts towards the cap.
453+
if (refresh) {
454+
authChallengeRounds = 0;
455+
} else if (++authChallengeRounds > MAX_AUTH_CHALLENGE_ROUNDS) {
456+
log.error().attr("channel", ctx.channel()).attr("maxChallengeRounds", MAX_AUTH_CHALLENGE_ROUNDS)
457+
.log("Binary authentication exceeded the maximum challenge rounds; closing the "
458+
+ "broker connection");
459+
ctx.close();
460+
return;
461+
}
400462
CompletableFuture<AuthData> resolution;
401463
try {
402464
if (refresh) {
Collapse file

‎pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyService.java‎

Copy file name to clipboardExpand all lines: pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/ProxyService.java
+18-6Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public class ProxyService implements Closeable {
100100
private final ProxyConfiguration proxyConfig;
101101
private final Authentication proxyClientAuthentication;
102102
// PIP-478: lazily built from proxyClientAuthentication; see getProxyClientAuthenticationDriver().
103-
private BinaryAuthenticationDriver proxyClientAuthenticationDriver;
103+
// Volatile because that getter reads it without holding the monitor.
104+
private volatile BinaryAuthenticationDriver proxyClientAuthenticationDriver;
104105
@Getter
105106
private final DnsAddressResolverGroup dnsAddressResolverGroup;
106107
@Getter
@@ -697,14 +698,25 @@ public Authentication getProxyClientAuthenticationPlugin() {
697698
* pool, which is the case {@code V5AuthContexts} documents for exactly this caller — the alternative,
698699
* running it inline, is the Netty event loop.
699700
*
701+
* <p>Read on every backend connection's {@code channelActive}, so the hit path is lock-free and only a
702+
* miss takes the monitor — as {@code ClientCnx.resolveAuthDriver} does, and for the same reason: this
703+
* monitor is the {@link ProxyService} one, shared with the metrics-servlet accessors, and a connection
704+
* being set up should not have to queue behind unrelated machinery.
705+
*
700706
* @return the shared binary authentication driver
701707
*/
702-
public synchronized BinaryAuthenticationDriver getProxyClientAuthenticationDriver() {
703-
if (proxyClientAuthenticationDriver == null) {
704-
proxyClientAuthenticationDriver = new V5BinaryAuthenticationDriver(
705-
V5AuthenticationLoader.forStartedV4Plugin(proxyClientAuthentication));
708+
public BinaryAuthenticationDriver getProxyClientAuthenticationDriver() {
709+
BinaryAuthenticationDriver resolved = proxyClientAuthenticationDriver;
710+
if (resolved != null) {
711+
return resolved;
712+
}
713+
synchronized (this) {
714+
if (proxyClientAuthenticationDriver == null) {
715+
proxyClientAuthenticationDriver = new V5BinaryAuthenticationDriver(
716+
V5AuthenticationLoader.forStartedV4Plugin(proxyClientAuthentication));
717+
}
718+
return proxyClientAuthenticationDriver;
706719
}
707-
return proxyClientAuthenticationDriver;
708720
}
709721

710722
public synchronized PrometheusMetricsServlet getMetricsServlet() {

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.