-
Notifications
You must be signed in to change notification settings - Fork 580
fix(pd): resolve hostname entries in IpAuthHandler allowlist #2962
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
base: master
Are you sure you want to change the base?
Changes from all commits
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,7 +18,10 @@ | |
| package org.apache.hugegraph.pd.raft.auth; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.net.InetAddress; | ||
| import java.net.UnknownHostException; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| import io.netty.channel.ChannelDuplexHandler; | ||
|
|
@@ -30,11 +33,14 @@ | |
| @ChannelHandler.Sharable | ||
| public class IpAuthHandler extends ChannelDuplexHandler { | ||
|
|
||
| // Retained for potential refresh of resolvedIps on membership changes | ||
| private final Set<String> allowedIps; | ||
| private volatile Set<String> resolvedIps; | ||
| private static volatile IpAuthHandler instance; | ||
|
|
||
| private IpAuthHandler(Set<String> allowedIps) { | ||
| this.allowedIps = Collections.unmodifiableSet(allowedIps); | ||
| this.resolvedIps = resolveAll(allowedIps); | ||
| } | ||
|
|
||
| public static IpAuthHandler getInstance(Set<String> allowedIps) { | ||
|
|
@@ -65,7 +71,24 @@ private static String getClientIp(ChannelHandlerContext ctx) { | |
| } | ||
|
|
||
| private boolean isIpAllowed(String ip) { | ||
| return allowedIps.isEmpty() || allowedIps.contains(ip); | ||
| Set<String> resolved = this.resolvedIps; | ||
| return resolved.isEmpty() || resolved.contains(ip); | ||
| } | ||
|
|
||
| private static Set<String> resolveAll(Set<String> entries) { | ||
|
Member
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. |
||
| Set<String> result = new HashSet<>(entries); | ||
|
|
||
| for (String entry : entries) { | ||
| try { | ||
| for (InetAddress addr : InetAddress.getAllByName(entry)) { | ||
| result.add(addr.getHostAddress()); | ||
| } | ||
| } catch (UnknownHostException e) { | ||
| log.warn("Could not resolve allowlist entry '{}': {}", entry, e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| return Collections.unmodifiableSet(result); | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
resolvedIpsis only built once in the constructor, butIpAuthHandleris still a JVM-wide singleton.RaftEngine#changePeerList()can replace the peer set after startup, and hostname-based peers can also resolve to a different IP later. In both cases this handler keeps the original resolved addresses forever, so a valid PD peer can be blocked until the whole process restarts. Please add a refresh path for membership/DNS changes, or defer hostname resolution to validation time with a bounded cache/TTL.