-
Notifications
You must be signed in to change notification settings - Fork 3.7k
PIP-55: Refresh Authentication Credentials #6074
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 all commits
0c356d6
c198308
1dd0fb7
d07a926
e5e1fbb
d8e8854
e10b038
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 |
|---|---|---|
|
|
@@ -18,17 +18,27 @@ | |
| */ | ||
| package org.apache.pulsar.broker.service; | ||
|
|
||
| import static org.apache.bookkeeper.util.SafeRunnable.safeRun; | ||
|
|
||
| import java.net.SocketAddress; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.apache.pulsar.broker.PulsarService; | ||
| import org.apache.pulsar.broker.ServiceConfiguration; | ||
| import org.apache.pulsar.common.protocol.ByteBufPair; | ||
| import org.apache.pulsar.common.protocol.Commands; | ||
| import org.apache.pulsar.common.util.NettySslContextBuilder; | ||
|
|
||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
|
|
||
| import io.netty.channel.ChannelInitializer; | ||
| import io.netty.channel.socket.SocketChannel; | ||
| import io.netty.handler.codec.LengthFieldBasedFrameDecoder; | ||
| import io.netty.handler.flow.FlowControlHandler; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| @Slf4j | ||
| public class PulsarChannelInitializer extends ChannelInitializer<SocketChannel> { | ||
|
|
||
| public static final String TLS_HANDLER = "tls"; | ||
|
|
@@ -38,6 +48,14 @@ public class PulsarChannelInitializer extends ChannelInitializer<SocketChannel> | |
| private final NettySslContextBuilder sslCtxRefresher; | ||
| private final ServiceConfiguration brokerConf; | ||
|
|
||
| // This cache is used to maintain a list of active connections to iterate over them | ||
| // We keep weak references to have the cache to be auto cleaned up when the connections | ||
| // objects are GCed. | ||
| private final Cache<SocketAddress, ServerCnx> connections = Caffeine.newBuilder() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Why not Cache<ServerCnx, ServerCnx> and avoid possibly allocating the SocketAddress? We don't care about the keys in any case. Maybe add a comment that the you're using Cache to avoid having to clean stuff up.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The socket address is already kept in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added comment |
||
| .weakKeys() | ||
| .weakValues() | ||
| .build(); | ||
|
|
||
| /** | ||
| * @param pulsar | ||
| * An instance of {@link PulsarService} | ||
|
|
@@ -59,6 +77,10 @@ public PulsarChannelInitializer(PulsarService pulsar, boolean enableTLS) throws | |
| this.sslCtxRefresher = null; | ||
| } | ||
| this.brokerConf = pulsar.getConfiguration(); | ||
|
|
||
| pulsar.getExecutor().scheduleAtFixedRate(safeRun(this::refreshAuthenticationCredentials), | ||
| pulsar.getConfig().getAuthenticationRefreshCheckSeconds(), | ||
| pulsar.getConfig().getAuthenticationRefreshCheckSeconds(), TimeUnit.SECONDS); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -78,6 +100,19 @@ protected void initChannel(SocketChannel ch) throws Exception { | |
| // ServerCnx ends up reading higher number of messages and broker can not throttle the messages by disabling | ||
| // auto-read. | ||
| ch.pipeline().addLast("flowController", new FlowControlHandler()); | ||
| ch.pipeline().addLast("handler", new ServerCnx(pulsar)); | ||
| ServerCnx cnx = new ServerCnx(pulsar); | ||
| ch.pipeline().addLast("handler", cnx); | ||
|
|
||
| connections.put(ch.remoteAddress(), cnx); | ||
| } | ||
|
|
||
| private void refreshAuthenticationCredentials() { | ||
| connections.asMap().values().forEach(cnx -> { | ||
| try { | ||
| cnx.refreshAuthenticationCredentials(); | ||
| } catch (Throwable t) { | ||
| log.warn("[{}] Failed to refresh auth credentials", cnx.getRemoteAddress()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're using Cache to hold the connections, and the lifetime of the entry in connections is based on whether it's been GC'd, this will likely spam the logs. Maybe only log if after throwing, the cnx is still in connected state.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we get the ref to if (getState() != State.Connected || !isActive) {
// Connection is either still being established or already closed.
return;
}to avoid getting errors on a connections that's already gone |
||
| } | ||
| }); | ||
| } | ||
| } | ||
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.
we should disable it by default.
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.
It will "disabled by default" in the sense that the current credentials are not expiring as of now.