Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
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
Expand Down
Loading