Skip to content

Commit e32c7c4

Browse files
author
Sylvain Lebresne
committed
Merge pull request apache#159 from beobal/DSP-2945
Make java-driver 2.0 compatible with DSE 3.x authentication
2 parents 606943e + 9423131 commit e32c7c4

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

driver-core/src/main/java/com/datastax/driver/core/Connection.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ private void initializeTransport(int version) throws ConnectionException, Interr
137137
case AUTHENTICATE:
138138
Authenticator authenticator = factory.authProvider.newAuthenticator(address);
139139
if (version == 1)
140-
authenticateV1(authenticator);
140+
{
141+
if (authenticator instanceof ProtocolV1Authenticator)
142+
authenticateV1(authenticator);
143+
else
144+
// DSE 3.x always uses SASL authentication backported from protocol v2
145+
authenticateV2(authenticator);
146+
}
141147
else
142148
authenticateV2(authenticator);
143149
break;
@@ -161,10 +167,6 @@ private UnsupportedProtocolVersionException unsupportedProtocolVersionException(
161167
}
162168

163169
private void authenticateV1(Authenticator authenticator) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
164-
if (!(authenticator instanceof ProtocolV1Authenticator))
165-
throw new AuthenticationException(address, String.format("Cannot use authenticator %s with protocol version 1, "
166-
+ "only plain text authentication is supported with this protocol version", authenticator));
167-
168170
Requests.Credentials creds = new Requests.Credentials(((ProtocolV1Authenticator)authenticator).getCredentials());
169171
Message.Response authResponse = write(creds).get();
170172
switch (authResponse.type) {
@@ -207,7 +209,15 @@ private void waitForAuthCompletion(Message.Response authResponse, Authenticator
207209
}
208210
break;
209211
case ERROR:
210-
throw new AuthenticationException(address, ((Responses.Error)authResponse).message);
212+
// This is not very nice, but we're trying to identify if we
213+
// attempted v2 auth against a server which only supports v1
214+
// The AIOOBE indicates that the server didn't recognise the
215+
// initial AuthResponse message
216+
String message = ((Responses.Error)authResponse).message;
217+
if (message.startsWith("java.lang.ArrayIndexOutOfBoundsException: 15"))
218+
message = String.format("Cannot use authenticator %s with protocol version 1, "
219+
+ "only plain text authentication is supported with this protocol version", authenticator);
220+
throw new AuthenticationException(address, message);
211221
default:
212222
throw new TransportException(address, String.format("Unexpected %s response message from server to authentication message", authResponse.type));
213223
}

driver-core/src/main/java/com/datastax/driver/core/Message.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public enum Type {
6969
EXECUTE (10, Requests.Execute.coderV1, Requests.Execute.coderV2),
7070
REGISTER (11, Requests.Register.coder, Requests.Register.coder),
7171
BATCH (13, null, Requests.Batch.coder),
72-
AUTH_RESPONSE (15, null, Requests.AuthResponse.coder);
72+
AUTH_RESPONSE (15, Requests.AuthResponse.coder, Requests.AuthResponse.coder);
7373

7474
public final int opcode;
7575
private final Coder<?> coderV1;
@@ -113,8 +113,8 @@ public enum Type {
113113
SUPPORTED (6, Responses.Supported.decoder, Responses.Supported.decoder),
114114
RESULT (8, Responses.Result.decoderV1, Responses.Result.decoderV2),
115115
EVENT (12, Responses.Event.decoder, Responses.Event.decoder),
116-
AUTH_CHALLENGE (14, Responses.AuthChallenge.decoderV1, Responses.AuthChallenge.decoderV2),
117-
AUTH_SUCCESS (16, Responses.AuthSuccess.decoderV1, Responses.AuthSuccess.decoderV2);
116+
AUTH_CHALLENGE (14, Responses.AuthChallenge.decoder, Responses.AuthChallenge.decoder),
117+
AUTH_SUCCESS (16, Responses.AuthSuccess.decoder, Responses.AuthSuccess.decoder);
118118

119119
public final int opcode;
120120
private final Decoder<?> decoderV1;

driver-core/src/main/java/com/datastax/driver/core/Responses.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,7 @@ public String toString() {
523523

524524
public static class AuthChallenge extends Message.Response {
525525

526-
public static final Message.Decoder<AuthChallenge> decoderV1 = new Message.Decoder<AuthChallenge>() {
527-
public AuthChallenge decode(ChannelBuffer body) {
528-
// AUTH_CHALLENGE is protocol v2 only.
529-
throw new DriverInternalError("Got AUTH_CHALLENGE in protocol V1, this shouldn't happen since AUTH_CHALLENGE is a protocol V2 only message");
530-
}
531-
};
532-
533-
public static final Message.Decoder<AuthChallenge> decoderV2 = new Message.Decoder<AuthChallenge>() {
526+
public static final Message.Decoder<AuthChallenge> decoder = new Message.Decoder<AuthChallenge>() {
534527
public AuthChallenge decode(ChannelBuffer body) {
535528
ByteBuffer b = CBUtil.readValue(body);
536529
if (b == null)
@@ -552,14 +545,7 @@ private AuthChallenge(byte[] token) {
552545

553546
public static class AuthSuccess extends Message.Response {
554547

555-
public static final Message.Decoder<AuthSuccess> decoderV1 = new Message.Decoder<AuthSuccess>() {
556-
public AuthSuccess decode(ChannelBuffer body) {
557-
// AUTH_SUCCESS is protocol v2 only.
558-
throw new DriverInternalError("Got AUTH_SUCCESS in protocol V1, this shouldn't happen since AUTH_SUCCESS is a protocol V2 only message");
559-
}
560-
};
561-
562-
public static final Message.Decoder<AuthSuccess> decoderV2 = new Message.Decoder<AuthSuccess>() {
548+
public static final Message.Decoder<AuthSuccess> decoder = new Message.Decoder<AuthSuccess>() {
563549
public AuthSuccess decode(ChannelBuffer body) {
564550
ByteBuffer b = CBUtil.readValue(body);
565551
if (b == null)

0 commit comments

Comments
 (0)