Elffuss BraidHub

kikocis/java-driver public

Merge pull request #424 from datastax/java719

Allow PlainTextAuthProvider to change its credentials at runtime (JAVA-719)
Changeset46d73a040f8d02599bc1b8777799534b6448134d5420f3a4336555484bcb5f22
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 19:06 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 19:06 +02:00
Parents5ee8679aaa3f, 270c83606bd0

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -28,6 +28,7 @@
2828 - [improvement] Randomize contact points list to prevent hotspots (JAVA-618)
2929 - [improvement] Surface the coordinator used on query failure (JAVA-720)
3030 - [bug] Handle contact points removed during init (JAVA-792)
31+- [improvement] Allow PlainTextAuthProvider to change its credentials at runtime (JAVA-719)
3132
3233 Merged from 2.0.10_fixes branch:
3334
modified driver-core/src/main/java/com/datastax/driver/core/PlainTextAuthProvider.java +25 −4
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core;
1717
18-import javax.security.sasl.SaslException;
1918 import java.net.InetSocketAddress;
2019 import java.util.Map;
2120
@@ -28,13 +27,13 @@
2827 * This provider allows to programmatically define authentication
2928 * information that will then apply to all hosts. The
3029 * PlainTextAuthenticator instances it returns support SASL
31- * authentication using the PLAIN mechanism for version 2 of the
30+ * authentication using the PLAIN mechanism for version 2 (or above) of the
3231 * CQL native protocol.
3332 */
3433 public class PlainTextAuthProvider implements AuthProvider {
3534
36- private final String username;
37- private final String password;
35+ private volatile String username;
36+ private volatile String password;
3837
3938 /**
4039 * Creates a new simple authentication information provider with the
@@ -47,6 +46,28 @@
4746 this.password = password;
4847 }
4948
49+ /**
50+ * Changes the user name.
51+ *
52+ * The new credentials will be used for all connections initiated after this method was called.
53+ *
54+ * @param username the new name.
55+ */
56+ public void setUsername(String username) {
57+ this.username = username;
58+ }
59+
60+ /**
61+ * Changes the password.
62+ *
63+ * The new credentials will be used for all connections initiated after this method was called.
64+ *
65+ * @param password the new password.
66+ */
67+ public void setPassword(String password) {
68+ this.password = password;
69+ }
70+
5071 /**
5172 * Uses the supplied credentials and the SASL PLAIN mechanism to login
5273 * to the server.
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolV1Authenticator.java +10 −2
@@ -17,8 +17,16 @@
1717
1818 import java.util.Map;
1919
20-// Not an interface because we don't want to expose it. We only support password authentication
21-// for the protocol V1 similarly to what the driver 1.x branch do.
20+/**
21+ * Parent class for {@link Authenticator} implementations that support native protocol v1 authentication.
22+ *
23+ * Protocol v1 uses simple, credentials-based authentication (as opposed to SASL for later protocol versions).
24+ * In order to support protocol v1, an authenticator must extend this class.
25+ *
26+ * We use an abstract class instead of an interface because we don't want to expose {@link #getCredentials()}.
27+ *
28+ * @see <a href="https://github.com/apache/cassandra/blob/trunk/doc/native_protocol_v1.spec">Native protocol v1 specification</a>
29+ */
2230 abstract class ProtocolV1Authenticator {
2331 abstract Map<String, String> getCredentials();
2432 }
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +10 −51
@@ -19,7 +19,6 @@
1919 import java.net.InetSocketAddress;
2020 import java.util.Collections;
2121 import java.util.Iterator;
22-import java.util.Map;
2322 import java.util.concurrent.ConcurrentHashMap;
2423 import java.util.concurrent.ConcurrentMap;
2524 import java.util.concurrent.TimeUnit;
@@ -27,8 +26,6 @@
2726
2827 import static java.util.concurrent.TimeUnit.SECONDS;
2928
30-import com.google.common.base.Charsets;
31-import com.google.common.collect.ImmutableMap;
3229 import com.google.common.util.concurrent.ListenableFuture;
3330 import org.testng.annotations.Test;
3431
@@ -96,7 +93,7 @@
9693 .build();
9794 ccm.start(1, "-Dcassandra.superuser_setup_delay_ms=0");
9895
99- DynamicPlainTextAuthProvider authProvider = new DynamicPlainTextAuthProvider("cassandra", "cassandra");
96+ CountingAuthProvider authProvider = new CountingAuthProvider("cassandra", "cassandra");
10097 int reconnectionDelayMs = 1000;
10198 CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMs));
10299
@@ -113,7 +110,7 @@
113110 // Stop the server, set wrong credentials and restart
114111 ccm.stop(1);
115112 ccm.waitForDown(1);
116- authProvider.password = "wrongPassword";
113+ authProvider.setPassword("wrongPassword");
117114 ccm.start(1);
118115 ccm.waitForUp(1);
119116
@@ -128,7 +125,7 @@
128125 assertThat(iterations).isLessThan(maxIterations);
129126
130127 // Fix the credentials
131- authProvider.password = "cassandra";
128+ authProvider.setPassword("cassandra");
132129
133130 // The driver should eventually reconnect to the node
134131 assertThat(cluster).host(1).comesUpWithin(120, SECONDS);
@@ -299,57 +296,19 @@
299296 ccm.remove();
300297 }
301298 }
302-
303- /**
304- * An authentication provider that allows changing the credentials at runtime, and tracks how many times they have been requested.
305- */
306- static class DynamicPlainTextAuthProvider implements AuthProvider {
307- volatile String username;
308- volatile String password;
309299
300+ /** Extends the plain text auth provider to track how many times the credentials have been requested */
301+ static class CountingAuthProvider extends PlainTextAuthProvider {
310302 final AtomicInteger count = new AtomicInteger();
311303
312- public DynamicPlainTextAuthProvider(String username, String password) {
313- this.username = username;
314- this.password = password;
304+ CountingAuthProvider(String username, String password) {
305+ super(username, password);
315306 }
316307
308+ @Override
317309 public Authenticator newAuthenticator(InetSocketAddress host) {
318- return new PlainTextAuthenticator();
319- }
320-
321- private class PlainTextAuthenticator extends ProtocolV1Authenticator implements Authenticator {
322-
323- @Override
324- public byte[] initialResponse() {
325- count.incrementAndGet();
326-
327- byte[] usernameBytes = username.getBytes(Charsets.UTF_8);
328- byte[] passwordBytes = password.getBytes(Charsets.UTF_8);
329- byte[] initialToken = new byte[usernameBytes.length + passwordBytes.length + 2];
330- initialToken[0] = 0;
331- System.arraycopy(usernameBytes, 0, initialToken, 1, usernameBytes.length);
332- initialToken[usernameBytes.length + 1] = 0;
333- System.arraycopy(passwordBytes, 0, initialToken, usernameBytes.length + 2, passwordBytes.length);
334- return initialToken;
335- }
336-
337- @Override
338- public byte[] evaluateChallenge(byte[] challenge) {
339- return null;
340- }
341-
342- @Override
343- public void onAuthenticationSuccess(byte[] token) {
344- // no-op, the server should send nothing anyway
345- }
346-
347- @Override
348- Map<String, String> getCredentials() {
349- count.incrementAndGet();
350- return ImmutableMap.of("username", username,
351- "password", password);
352- }
310+ count.incrementAndGet();
311+ return super.newAuthenticator(host);
353312 }
354313 }
355314