Skip to content

Commit 10835cb

Browse files
authored
ZOOKEEPER-2858: Disable reverse DNS lookup for SASL java client
Author: anmolnar Closes #2331 from anmolnar/ZOOKEEPER-2858
1 parent fb43500 commit 10835cb

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

zookeeper-docs/src/main/resources/markdown/zookeeperProgrammers.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,11 @@ and [SASL authentication for ZooKeeper](https://cwiki.apache.org/confluence/disp
13151315
the fully qualified domain name belonging to the address. You can disable this 'canonicalization'
13161316
by setting: zookeeper.sasl.client.canonicalize.hostname=false
13171317
1318+
* *zookeeper.sasl.client.allowReverseDnsLookup* :
1319+
**New in 3.9.5:**
1320+
Controls whether reverse DNS lookup is enabled when constructing the server principal for the SASL client.
1321+
Default: false
1322+
13181323
* *zookeeper.server.realm* :
13191324
Realm part of the server principal. By default it is the client principal realm.
13201325

zookeeper-server/src/main/java/org/apache/zookeeper/SaslServerPrincipal.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class SaslServerPrincipal {
3838
* @return the name of the principal.
3939
*/
4040
static String getServerPrincipal(InetSocketAddress addr, ZKClientConfig clientConfig) {
41-
return getServerPrincipal(new WrapperInetSocketAddress(addr), clientConfig);
41+
return getServerPrincipal(new WrapperInetSocketAddress(addr, clientConfig), clientConfig);
4242
}
4343

4444
/**
@@ -96,13 +96,20 @@ static String getServerPrincipal(WrapperInetSocketAddress addr, ZKClientConfig c
9696
static class WrapperInetSocketAddress {
9797

9898
private final InetSocketAddress addr;
99+
private final ZKClientConfig clientConfig;
99100

100-
WrapperInetSocketAddress(InetSocketAddress addr) {
101+
WrapperInetSocketAddress(InetSocketAddress addr, ZKClientConfig clientConfig) {
101102
this.addr = addr;
103+
this.clientConfig = clientConfig;
102104
}
103105

104106
public String getHostName() {
105-
return addr.getHostName();
107+
if (clientConfig.getBoolean(ZKClientConfig.ZK_SASL_CLIENT_ALLOW_REVERSE_DNS,
108+
ZKClientConfig.ZK_SASL_CLIENT_ALLOW_REVERSE_DNS_DEFAULT)) {
109+
return addr.getHostName();
110+
} else {
111+
return addr.getHostString();
112+
}
106113
}
107114

108115
public WrapperInetAddress getAddress() {

zookeeper-server/src/main/java/org/apache/zookeeper/client/ZKClientConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class ZKClientConfig extends ZKConfig {
6060
* Feature is disabled by default.
6161
*/
6262
public static final long ZOOKEEPER_REQUEST_TIMEOUT_DEFAULT = 0;
63+
public static final String ZK_SASL_CLIENT_ALLOW_REVERSE_DNS = "zookeeper.sasl.client.allowReverseDnsLookup";
64+
public static final boolean ZK_SASL_CLIENT_ALLOW_REVERSE_DNS_DEFAULT = false;
6365

6466
public ZKClientConfig() {
6567
super();
@@ -120,6 +122,7 @@ protected void handleBackwardCompatibility() {
120122
setProperty(DISABLE_AUTO_WATCH_RESET, System.getProperty(DISABLE_AUTO_WATCH_RESET));
121123
setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, System.getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET));
122124
setProperty(SECURE_CLIENT, System.getProperty(SECURE_CLIENT));
125+
setProperty(ZK_SASL_CLIENT_ALLOW_REVERSE_DNS, System.getProperty(ZK_SASL_CLIENT_ALLOW_REVERSE_DNS));
123126
}
124127

125128
/**

zookeeper-server/src/test/java/org/apache/zookeeper/ClientCanonicalizeTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.zookeeper;
2020

2121
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.mockito.Mockito.doReturn;
23+
import static org.mockito.Mockito.doThrow;
2224
import static org.mockito.Mockito.mock;
2325
import static org.mockito.Mockito.when;
2426
import java.io.IOException;
@@ -85,4 +87,39 @@ public void testGetServerPrincipalReturnConfiguredPrincipalName() {
8587
assertEquals(configuredPrincipal, serverPrincipal);
8688
}
8789

90+
@Test
91+
public void testAllowReverseDnsLookupDisabled() {
92+
// Arrange
93+
ZKClientConfig config = new ZKClientConfig();
94+
config.setProperty(ZKClientConfig.ZK_SASL_CLIENT_ALLOW_REVERSE_DNS, "false");
95+
InetSocketAddress addr = mock(InetSocketAddress.class);
96+
SaslServerPrincipal.WrapperInetSocketAddress ia = new SaslServerPrincipal.WrapperInetSocketAddress(addr, config);
97+
doReturn("this-is-the-right-hostname").when(addr).getHostString();
98+
doThrow(new UnsupportedOperationException("getHostName() should not be called when reverse DNS is disabled"))
99+
.when(addr).getHostName();
100+
101+
// Act
102+
String hostname = ia.getHostName();
103+
104+
// Assert
105+
assertEquals("this-is-the-right-hostname", hostname);
106+
}
107+
108+
@Test
109+
public void testAllowReverseDnsLookupEnabled() {
110+
// Arrange
111+
ZKClientConfig config = new ZKClientConfig();
112+
config.setProperty(ZKClientConfig.ZK_SASL_CLIENT_ALLOW_REVERSE_DNS, "true");
113+
InetSocketAddress addr = mock(InetSocketAddress.class);
114+
SaslServerPrincipal.WrapperInetSocketAddress ia = new SaslServerPrincipal.WrapperInetSocketAddress(addr, config);
115+
doReturn("this-is-the-right-hostname").when(addr).getHostName();
116+
doThrow(new UnsupportedOperationException("getHostString() should not be called when reverse DNS is enabled"))
117+
.when(addr).getHostString();
118+
119+
// Act
120+
String hostname = ia.getHostName();
121+
122+
// Assert
123+
assertEquals("this-is-the-right-hostname", hostname);
124+
}
88125
}

0 commit comments

Comments
 (0)