Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit a4794b2

Browse files
Alexey BakhtinYuri Nesterenko
authored andcommitted
8062947: Fix exception message to correctly represent LDAP connection failure
Backport-of: 61864c2
1 parent 0f169b0 commit a4794b2

File tree

3 files changed

+205
-51
lines changed

3 files changed

+205
-51
lines changed

src/java.naming/share/classes/com/sun/jndi/ldap/Connection.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -426,22 +426,34 @@ BerDecoder readReply(LdapRequest ldr) throws IOException, NamingException {
426426
}
427427
}
428428

429+
NamingException namingException = null;
429430
try {
430431
// if no timeout is set so we wait infinitely until
431-
// a response is received
432+
// a response is received OR until the connection is closed or cancelled
432433
// http://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-ldap.html#PROP
433434
rber = ldr.getReplyBer(readTimeout);
434435
} catch (InterruptedException ex) {
435436
throw new InterruptedNamingException(
436437
"Interrupted during LDAP operation");
438+
} catch (CommunicationException ce) {
439+
// Re-throw
440+
throw ce;
441+
} catch (NamingException ne) {
442+
// Connection is timed out OR closed/cancelled
443+
namingException = ne;
444+
rber = null;
437445
}
438446

439447
if (rber == null) {
440448
abandonRequest(ldr, null);
441-
throw new NamingException(
442-
"LDAP response read timed out, timeout used:"
443-
+ readTimeout + "ms." );
444-
449+
}
450+
// namingException can be not null in the following cases:
451+
// a) The response is timed-out
452+
// b) LDAP request connection has been closed or cancelled
453+
// The exception message is initialized in LdapRequest::getReplyBer
454+
if (namingException != null) {
455+
// Re-throw NamingException after all cleanups are done
456+
throw namingException;
445457
}
446458
return rber;
447459
}
@@ -889,15 +901,6 @@ public void run() {
889901
inbuf = Arrays.copyOf(inbuf, offset + left.length);
890902
System.arraycopy(left, 0, inbuf, offset, left.length);
891903
offset += left.length;
892-
/*
893-
if (dump > 0) {
894-
System.err.println("seqlen: " + seqlen);
895-
System.err.println("bufsize: " + offset);
896-
System.err.println("bytesleft: " + bytesleft);
897-
System.err.println("bytesread: " + bytesread);
898-
}
899-
*/
900-
901904

902905
try {
903906
retBer = new BerDecoder(inbuf, 0, offset);
@@ -1005,36 +1008,4 @@ private static byte[] readFully(InputStream is, int length)
10051008
}
10061009
return buf;
10071010
}
1008-
1009-
// This code must be uncommented to run the LdapAbandonTest.
1010-
/*public void sendSearchReqs(String dn, int numReqs) {
1011-
int i;
1012-
String attrs[] = null;
1013-
for(i = 1; i <= numReqs; i++) {
1014-
BerEncoder ber = new BerEncoder(2048);
1015-
1016-
try {
1017-
ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
1018-
ber.encodeInt(i);
1019-
ber.beginSeq(LdapClient.LDAP_REQ_SEARCH);
1020-
ber.encodeString(dn == null ? "" : dn);
1021-
ber.encodeInt(0, LdapClient.LBER_ENUMERATED);
1022-
ber.encodeInt(3, LdapClient.LBER_ENUMERATED);
1023-
ber.encodeInt(0);
1024-
ber.encodeInt(0);
1025-
ber.encodeBoolean(true);
1026-
LdapClient.encodeFilter(ber, "");
1027-
ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
1028-
ber.encodeStringArray(attrs);
1029-
ber.endSeq();
1030-
ber.endSeq();
1031-
ber.endSeq();
1032-
writeRequest(ber, i);
1033-
//System.err.println("wrote request " + i);
1034-
} catch (Exception ex) {
1035-
//System.err.println("ldap.search: Caught " + ex + " building req");
1036-
}
1037-
1038-
}
1039-
} */
10401011
}

src/java.naming/share/classes/com/sun/jndi/ldap/LdapRequest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -29,11 +29,14 @@
2929
import java.util.concurrent.BlockingQueue;
3030
import java.util.concurrent.LinkedBlockingQueue;
3131
import javax.naming.CommunicationException;
32+
import javax.naming.NamingException;
3233
import java.util.concurrent.TimeUnit;
3334

3435
final class LdapRequest {
3536

3637
private final static BerDecoder EOF = new BerDecoder(new byte[]{}, -1, 0);
38+
private final static String CLOSE_MSG = "LDAP connection has been closed";
39+
private final static String TIMEOUT_MSG_FMT = "LDAP response read timed out, timeout used: %d ms.";
3740

3841
LdapRequest next; // Set/read in synchronized Connection methods
3942
final int msgId; // read-only
@@ -95,14 +98,22 @@ synchronized boolean addReplyBer(BerDecoder ber) {
9598
return pauseAfterReceipt;
9699
}
97100

98-
BerDecoder getReplyBer(long millis) throws CommunicationException,
101+
/**
102+
* Read reply BER
103+
* @param millis timeout, infinite if the value is negative
104+
* @return BerDecoder if reply was read successfully
105+
* @throws CommunicationException request has been canceled and request does not need to be abandoned
106+
* @throws NamingException request has been closed or timed out. Request does need to be abandoned
107+
* @throws InterruptedException LDAP operation has been interrupted
108+
*/
109+
BerDecoder getReplyBer(long millis) throws NamingException,
99110
InterruptedException {
100111
if (cancelled) {
101112
throw new CommunicationException("Request: " + msgId +
102113
" cancelled");
103114
}
104115
if (isClosed()) {
105-
return null;
116+
throw new NamingException(CLOSE_MSG);
106117
}
107118

108119
BerDecoder result = millis > 0 ?
@@ -113,7 +124,15 @@ BerDecoder getReplyBer(long millis) throws CommunicationException,
113124
" cancelled");
114125
}
115126

116-
return result == EOF ? null : result;
127+
// poll from 'replies' blocking queue ended-up with timeout
128+
if (result == null) {
129+
throw new NamingException(String.format(TIMEOUT_MSG_FMT, millis));
130+
}
131+
// Unexpected EOF can be caused by connection closure or cancellation
132+
if (result == EOF) {
133+
throw new NamingException(CLOSE_MSG);
134+
}
135+
return result;
117136
}
118137

119138
boolean hasSearchCompleted() {
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8062947
27+
* @summary Test that NamingException message text matches the failure reason
28+
* @library /test/lib lib
29+
* @run testng NamingExceptionMessageTest
30+
*/
31+
32+
import javax.naming.Context;
33+
import javax.naming.NamingException;
34+
import javax.naming.directory.InitialDirContext;
35+
import java.io.IOException;
36+
import java.io.OutputStream;
37+
import java.net.InetAddress;
38+
import java.net.InetSocketAddress;
39+
import java.net.ServerSocket;
40+
import java.net.Socket;
41+
import java.util.Hashtable;
42+
import java.util.concurrent.CountDownLatch;
43+
import java.util.concurrent.TimeUnit;
44+
45+
import org.testng.annotations.Test;
46+
import org.testng.Assert;
47+
import jdk.test.lib.net.URIBuilder;
48+
49+
public class NamingExceptionMessageTest {
50+
51+
@Test
52+
public void timeoutMessageTest() throws Exception {
53+
try (var ldapServer = TestLdapServer.newInstance(false)) {
54+
ldapServer.start();
55+
ldapServer.awaitStartup();
56+
var env = ldapServer.getInitialLdapCtxEnvironment(TIMEOUT_VALUE);
57+
var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
58+
System.out.println("Got naming exception:" + namingException);
59+
Assert.assertEquals(namingException.getMessage(), EXPECTED_TIMEOUT_MESSAGE);
60+
}
61+
}
62+
63+
@Test
64+
public void connectionClosureMessageTest() throws Exception {
65+
try (var ldapServer = TestLdapServer.newInstance(true)) {
66+
ldapServer.start();
67+
ldapServer.awaitStartup();
68+
var env = ldapServer.getInitialLdapCtxEnvironment(0);
69+
var namingException = Assert.expectThrows(NamingException.class, () -> new InitialDirContext(env));
70+
System.out.println("Got naming exception:" + namingException);
71+
Assert.assertEquals(namingException.getMessage(), EXPECTED_CLOSURE_MESSAGE);
72+
}
73+
}
74+
75+
// Test LDAP server
76+
private static class TestLdapServer extends BaseLdapServer {
77+
78+
private final boolean closeConnections;
79+
private final CountDownLatch startupLatch = new CountDownLatch(1);
80+
81+
public Hashtable<Object, Object> getInitialLdapCtxEnvironment(int readTimeoutValue) {
82+
// Create environment for initial LDAP context
83+
Hashtable<Object, Object> env = new Hashtable<>();
84+
85+
// Activate LDAPv3
86+
env.put("java.naming.ldap.version", "3");
87+
88+
// De-activate the ManageDsaIT control
89+
env.put(Context.REFERRAL, "follow");
90+
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
91+
env.put(Context.PROVIDER_URL, getUrlString());
92+
env.put(Context.SECURITY_AUTHENTICATION, "simple");
93+
env.put(Context.SECURITY_PRINCIPAL, "name");
94+
env.put(Context.SECURITY_CREDENTIALS, "pwd");
95+
96+
if (readTimeoutValue > 0) {
97+
env.put("com.sun.jndi.ldap.read.timeout", String.valueOf(readTimeoutValue));
98+
env.put("com.sun.jndi.ldap.connect.timeout", String.valueOf(readTimeoutValue));
99+
}
100+
101+
return env;
102+
}
103+
104+
private String getUrlString() {
105+
String url = URIBuilder.newBuilder()
106+
.scheme("ldap")
107+
.loopback()
108+
.port(getPort())
109+
.buildUnchecked()
110+
.toString();
111+
return url;
112+
}
113+
114+
public static TestLdapServer newInstance(boolean closeConnections) throws IOException {
115+
ServerSocket srvSock = new ServerSocket();
116+
srvSock.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
117+
return new TestLdapServer(srvSock, closeConnections);
118+
}
119+
120+
void awaitStartup() throws InterruptedException {
121+
startupLatch.await();
122+
}
123+
124+
private TestLdapServer(ServerSocket serverSocket, boolean closeConnections) {
125+
super(serverSocket);
126+
this.closeConnections = closeConnections;
127+
128+
}
129+
130+
@Override
131+
protected void beforeAcceptingConnections() {
132+
startupLatch.countDown();
133+
}
134+
135+
@Override
136+
protected void handleRequest(Socket socket,
137+
LdapMessage msg,
138+
OutputStream out)
139+
throws IOException {
140+
switch (msg.getOperation()) {
141+
case BIND_REQUEST:
142+
if (closeConnections) {
143+
closeSilently(socket);
144+
} else {
145+
try {
146+
TimeUnit.DAYS.sleep(Integer.MAX_VALUE);
147+
} catch (InterruptedException e) {
148+
Thread.currentThread().interrupt();
149+
}
150+
}
151+
default:
152+
break;
153+
}
154+
}
155+
}
156+
157+
// Expected message for case when connection is closed on server side
158+
private static final String EXPECTED_CLOSURE_MESSAGE = "LDAP connection has been closed";
159+
// read and connect timeouts value
160+
private static final int TIMEOUT_VALUE = 129;
161+
// Expected message text when connection is timed-out
162+
private static final String EXPECTED_TIMEOUT_MESSAGE = String.format(
163+
"LDAP response read timed out, timeout used: %d ms.", TIMEOUT_VALUE);
164+
}

0 commit comments

Comments
 (0)