Skip to content

Commit 2c8f4e9

Browse files
committed
Throwing exception upon AuthFailed and added new test case.
1 parent 8154536 commit 2c8f4e9

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

‎CHANGELOG.markdown‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
ZkClient 0.8 (???)
55
---------------
66
- #45: Support for conditional deletes
7+
- Adding ZkAuthFailedException
78

89

910
ZkClient 0.7 (Nov 2015)

‎src/main/java/org/I0Itec/zkclient/ZkClient.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.security.auth.login.Configuration;
3232

3333
import org.I0Itec.zkclient.ZkEventThread.ZkEvent;
34+
import org.I0Itec.zkclient.exception.ZkAuthFailedException;
3435
import org.I0Itec.zkclient.exception.ZkBadVersionException;
3536
import org.I0Itec.zkclient.exception.ZkException;
3637
import org.I0Itec.zkclient.exception.ZkInterruptedException;
@@ -940,6 +941,10 @@ public boolean waitForKeeperState(KeeperState keeperState, long time, TimeUnit t
940941
return false;
941942
}
942943
stillWaiting = getEventLock().getStateChangedCondition().awaitUntil(timeout);
944+
// Throw an exception in the case authorization fails
945+
if (_currentState == KeeperState.AuthFailed) {
946+
throw new ZkAuthFailedException("Authentication failure");
947+
}
943948
}
944949
LOG.debug("State is " + _currentState);
945950
return true;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2010 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.I0Itec.zkclient.exception;
17+
18+
public class ZkAuthFailedException extends ZkException {
19+
20+
private static final long serialVersionUID = 1L;
21+
22+
public ZkAuthFailedException() {
23+
super();
24+
}
25+
26+
public ZkAuthFailedException(String message, Throwable cause) {
27+
super(message, cause);
28+
}
29+
30+
public ZkAuthFailedException(String message) {
31+
super(message);
32+
}
33+
34+
public ZkAuthFailedException(Throwable cause) {
35+
super(cause);
36+
}
37+
}

‎src/test/java/org/I0Itec/zkclient/SaslAuthenticatedTest.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.security.auth.login.Configuration;
2929

30+
import org.I0Itec.zkclient.exception.ZkAuthFailedException;
3031
import org.I0Itec.zkclient.exception.ZkException;
3132
import org.I0Itec.zkclient.exception.ZkTimeoutException;
3233
import org.apache.log4j.Logger;
@@ -150,7 +151,7 @@ public void testAuthFailure_AllowFailedSasl() throws IOException {
150151
bootstrapWithAuthFailure();
151152
fail("Expected to fail!");
152153
} catch (ZkException e) {
153-
assertThat(e).isInstanceOf(ZkTimeoutException.class);
154+
assertThat(e).isInstanceOf(ZkAuthFailedException.class);
154155
} finally {
155156
System.clearProperty(ZK_ALLOW_FAILED_SASL);
156157
}
@@ -172,4 +173,20 @@ public void testAuthFailure_DisabledSasl() throws IOException {
172173
}
173174
}
174175

176+
@Test
177+
public void testUnauthenticatedClient() throws IOException {
178+
ZkClient unauthed = null;
179+
try {
180+
bootstrap();
181+
System.clearProperty(ZkClient.JAVA_LOGIN_CONFIG_PARAM);
182+
System.setProperty("zookeeper.sasl.client", "true");
183+
unauthed = new ZkClient("localhost:" + _port, 6000);
184+
unauthed.createPersistent("/test", new byte[0], Ids.OPEN_ACL_UNSAFE);
185+
} finally {
186+
if (unauthed != null) {
187+
unauthed.close();
188+
}
189+
}
190+
}
191+
175192
}

0 commit comments

Comments
 (0)