kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 46d73a040f8d02599bc1b8777799534b6448134d5420f3a4336555484bcb5f22 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 19:06 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-05 19:06 +02:00 |
| Parents | 5ee8679aaa3f, 270c83606bd0 |
4 files changed
modified changelog/README.md +1 −0
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | - [improvement] Randomize contact points list to prevent hotspots (JAVA-618) |
| 29 | 29 | - [improvement] Surface the coordinator used on query failure (JAVA-720) |
| 30 | 30 | - [bug] Handle contact points removed during init (JAVA-792) |
| 31 | +- [improvement] Allow PlainTextAuthProvider to change its credentials at runtime (JAVA-719) | |
| 31 | 32 | |
| 32 | 33 | Merged from 2.0.10_fixes branch: |
| 33 | 34 |
modified driver-core/src/main/java/com/datastax/driver/core/PlainTextAuthProvider.java +25 −4
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import javax.security.sasl.SaslException; | |
| 19 | 18 | import java.net.InetSocketAddress; |
| 20 | 19 | import java.util.Map; |
| 21 | 20 | |
| @@ -28,13 +27,13 @@ | ||
| 28 | 27 | * This provider allows to programmatically define authentication |
| 29 | 28 | * information that will then apply to all hosts. The |
| 30 | 29 | * 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 | |
| 32 | 31 | * CQL native protocol. |
| 33 | 32 | */ |
| 34 | 33 | public class PlainTextAuthProvider implements AuthProvider { |
| 35 | 34 | |
| 36 | - private final String username; | |
| 37 | - private final String password; | |
| 35 | + private volatile String username; | |
| 36 | + private volatile String password; | |
| 38 | 37 | |
| 39 | 38 | /** |
| 40 | 39 | * Creates a new simple authentication information provider with the |
| @@ -47,6 +46,28 @@ | ||
| 47 | 46 | this.password = password; |
| 48 | 47 | } |
| 49 | 48 | |
| 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 | + | |
| 50 | 71 | /** |
| 51 | 72 | * Uses the supplied credentials and the SASL PLAIN mechanism to login |
| 52 | 73 | * to the server. |
modified driver-core/src/main/java/com/datastax/driver/core/ProtocolV1Authenticator.java +10 −2
| @@ -17,8 +17,16 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.util.Map; |
| 19 | 19 | |
| 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 | + */ | |
| 22 | 30 | abstract class ProtocolV1Authenticator { |
| 23 | 31 | abstract Map<String, String> getCredentials(); |
| 24 | 32 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +10 −51
| @@ -19,7 +19,6 @@ | ||
| 19 | 19 | import java.net.InetSocketAddress; |
| 20 | 20 | import java.util.Collections; |
| 21 | 21 | import java.util.Iterator; |
| 22 | -import java.util.Map; | |
| 23 | 22 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | 23 | import java.util.concurrent.ConcurrentMap; |
| 25 | 24 | import java.util.concurrent.TimeUnit; |
| @@ -27,8 +26,6 @@ | ||
| 27 | 26 | |
| 28 | 27 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 29 | 28 | |
| 30 | -import com.google.common.base.Charsets; | |
| 31 | -import com.google.common.collect.ImmutableMap; | |
| 32 | 29 | import com.google.common.util.concurrent.ListenableFuture; |
| 33 | 30 | import org.testng.annotations.Test; |
| 34 | 31 | |
| @@ -96,7 +93,7 @@ | ||
| 96 | 93 | .build(); |
| 97 | 94 | ccm.start(1, "-Dcassandra.superuser_setup_delay_ms=0"); |
| 98 | 95 | |
| 99 | - DynamicPlainTextAuthProvider authProvider = new DynamicPlainTextAuthProvider("cassandra", "cassandra"); | |
| 96 | + CountingAuthProvider authProvider = new CountingAuthProvider("cassandra", "cassandra"); | |
| 100 | 97 | int reconnectionDelayMs = 1000; |
| 101 | 98 | CountingReconnectionPolicy reconnectionPolicy = new CountingReconnectionPolicy(new ConstantReconnectionPolicy(reconnectionDelayMs)); |
| 102 | 99 | |
| @@ -113,7 +110,7 @@ | ||
| 113 | 110 | // Stop the server, set wrong credentials and restart |
| 114 | 111 | ccm.stop(1); |
| 115 | 112 | ccm.waitForDown(1); |
| 116 | - authProvider.password = "wrongPassword"; | |
| 113 | + authProvider.setPassword("wrongPassword"); | |
| 117 | 114 | ccm.start(1); |
| 118 | 115 | ccm.waitForUp(1); |
| 119 | 116 | |
| @@ -128,7 +125,7 @@ | ||
| 128 | 125 | assertThat(iterations).isLessThan(maxIterations); |
| 129 | 126 | |
| 130 | 127 | // Fix the credentials |
| 131 | - authProvider.password = "cassandra"; | |
| 128 | + authProvider.setPassword("cassandra"); | |
| 132 | 129 | |
| 133 | 130 | // The driver should eventually reconnect to the node |
| 134 | 131 | assertThat(cluster).host(1).comesUpWithin(120, SECONDS); |
| @@ -299,57 +296,19 @@ | ||
| 299 | 296 | ccm.remove(); |
| 300 | 297 | } |
| 301 | 298 | } |
| 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; | |
| 309 | 299 | |
| 300 | + /** Extends the plain text auth provider to track how many times the credentials have been requested */ | |
| 301 | + static class CountingAuthProvider extends PlainTextAuthProvider { | |
| 310 | 302 | final AtomicInteger count = new AtomicInteger(); |
| 311 | 303 | |
| 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); | |
| 315 | 306 | } |
| 316 | 307 | |
| 308 | + @Override | |
| 317 | 309 | 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); | |
| 353 | 312 | } |
| 354 | 313 | } |
| 355 | 314 |