blob: ae7df9ac7cfece0e4c1ab52021aed1b15d6cd542 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.yahoo.vespa.zookeeper;
import org.apache.zookeeper.server.NIOServerCnxn;
import org.apache.zookeeper.server.NIOServerCnxnFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
/**
* This class is created by zookeeper by reflection, see the ZooKeeperServer constructor.
*
* @author bratseth
*/
@SuppressWarnings("unused")
public class RestrictedServerCnxnFactory extends NIOServerCnxnFactory {
private static final Logger log = Logger.getLogger(RestrictedServerCnxnFactory.class.getName());
private final Set<String> zooKeeperServerHostnames;
public RestrictedServerCnxnFactory() throws IOException {
super();
zooKeeperServerHostnames = toHostnameSet(System.getProperty(ZooKeeperServer.ZOOKEEPER_VESPA_SERVERS_PROPERTY));
}
private Set<String> toHostnameSet(String commaSeparatedString) {
if (commaSeparatedString == null || commaSeparatedString.isEmpty())
throw new IllegalArgumentException("We have not received the list of ZooKeeper servers in this system");
Set<String> hostnames = new HashSet<>();
for (String hostname : commaSeparatedString.split(","))
hostnames.add(hostname.trim());
return hostnames;
}
@Override
protected NIOServerCnxn createConnection(SocketChannel socket, SelectionKey selection) throws IOException {
String remoteHost = ((InetSocketAddress)socket.getRemoteAddress()).getHostName();
if ( ! remoteHost.equals("localhost") && ! zooKeeperServerHostnames.contains(remoteHost)) {
String errorMessage = "Rejecting connection to ZooKeeper from " + remoteHost +
": This cluster only allow connection among its own hosts. " +
"Hosts in this cluster: " + zooKeeperServerHostnames;
log.warning(errorMessage);
throw new IllegalArgumentException(errorMessage);
}
return super.createConnection(socket, selection);
}
}
|