Skip to content

Commit

Permalink
Merge pull request #159 from beobal/DSP-2945
Browse files Browse the repository at this point in the history
Make java-driver 2.0 compatible with DSE 3.x authentication
  • Loading branch information
Sylvain Lebresne committed Mar 4, 2014
2 parents 606943e + 9423131 commit e32c7c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
22 changes: 16 additions & 6 deletions driver-core/src/main/java/com/datastax/driver/core/Connection.java
Expand Up @@ -137,7 +137,13 @@ private void initializeTransport(int version) throws ConnectionException, Interr
case AUTHENTICATE:
Authenticator authenticator = factory.authProvider.newAuthenticator(address);
if (version == 1)
authenticateV1(authenticator);
{
if (authenticator instanceof ProtocolV1Authenticator)
authenticateV1(authenticator);
else
// DSE 3.x always uses SASL authentication backported from protocol v2
authenticateV2(authenticator);
}
else
authenticateV2(authenticator);
break;
Expand All @@ -161,10 +167,6 @@ private UnsupportedProtocolVersionException unsupportedProtocolVersionException(
}

private void authenticateV1(Authenticator authenticator) throws ConnectionException, BusyConnectionException, ExecutionException, InterruptedException {
if (!(authenticator instanceof ProtocolV1Authenticator))
throw new AuthenticationException(address, String.format("Cannot use authenticator %s with protocol version 1, "
+ "only plain text authentication is supported with this protocol version", authenticator));

Requests.Credentials creds = new Requests.Credentials(((ProtocolV1Authenticator)authenticator).getCredentials());
Message.Response authResponse = write(creds).get();
switch (authResponse.type) {
Expand Down Expand Up @@ -207,7 +209,15 @@ private void waitForAuthCompletion(Message.Response authResponse, Authenticator
}
break;
case ERROR:
throw new AuthenticationException(address, ((Responses.Error)authResponse).message);
// This is not very nice, but we're trying to identify if we
// attempted v2 auth against a server which only supports v1
// The AIOOBE indicates that the server didn't recognise the
// initial AuthResponse message
String message = ((Responses.Error)authResponse).message;
if (message.startsWith("java.lang.ArrayIndexOutOfBoundsException: 15"))
message = String.format("Cannot use authenticator %s with protocol version 1, "
+ "only plain text authentication is supported with this protocol version", authenticator);
throw new AuthenticationException(address, message);
default:
throw new TransportException(address, String.format("Unexpected %s response message from server to authentication message", authResponse.type));
}
Expand Down
Expand Up @@ -69,7 +69,7 @@ public enum Type {
EXECUTE (10, Requests.Execute.coderV1, Requests.Execute.coderV2),
REGISTER (11, Requests.Register.coder, Requests.Register.coder),
BATCH (13, null, Requests.Batch.coder),
AUTH_RESPONSE (15, null, Requests.AuthResponse.coder);
AUTH_RESPONSE (15, Requests.AuthResponse.coder, Requests.AuthResponse.coder);

public final int opcode;
private final Coder<?> coderV1;
Expand Down Expand Up @@ -113,8 +113,8 @@ public enum Type {
SUPPORTED (6, Responses.Supported.decoder, Responses.Supported.decoder),
RESULT (8, Responses.Result.decoderV1, Responses.Result.decoderV2),
EVENT (12, Responses.Event.decoder, Responses.Event.decoder),
AUTH_CHALLENGE (14, Responses.AuthChallenge.decoderV1, Responses.AuthChallenge.decoderV2),
AUTH_SUCCESS (16, Responses.AuthSuccess.decoderV1, Responses.AuthSuccess.decoderV2);
AUTH_CHALLENGE (14, Responses.AuthChallenge.decoder, Responses.AuthChallenge.decoder),
AUTH_SUCCESS (16, Responses.AuthSuccess.decoder, Responses.AuthSuccess.decoder);

public final int opcode;
private final Decoder<?> decoderV1;
Expand Down
18 changes: 2 additions & 16 deletions driver-core/src/main/java/com/datastax/driver/core/Responses.java
Expand Up @@ -523,14 +523,7 @@ public String toString() {

public static class AuthChallenge extends Message.Response {

public static final Message.Decoder<AuthChallenge> decoderV1 = new Message.Decoder<AuthChallenge>() {
public AuthChallenge decode(ChannelBuffer body) {
// AUTH_CHALLENGE is protocol v2 only.
throw new DriverInternalError("Got AUTH_CHALLENGE in protocol V1, this shouldn't happen since AUTH_CHALLENGE is a protocol V2 only message");
}
};

public static final Message.Decoder<AuthChallenge> decoderV2 = new Message.Decoder<AuthChallenge>() {
public static final Message.Decoder<AuthChallenge> decoder = new Message.Decoder<AuthChallenge>() {
public AuthChallenge decode(ChannelBuffer body) {
ByteBuffer b = CBUtil.readValue(body);
if (b == null)
Expand All @@ -552,14 +545,7 @@ private AuthChallenge(byte[] token) {

public static class AuthSuccess extends Message.Response {

public static final Message.Decoder<AuthSuccess> decoderV1 = new Message.Decoder<AuthSuccess>() {
public AuthSuccess decode(ChannelBuffer body) {
// AUTH_SUCCESS is protocol v2 only.
throw new DriverInternalError("Got AUTH_SUCCESS in protocol V1, this shouldn't happen since AUTH_SUCCESS is a protocol V2 only message");
}
};

public static final Message.Decoder<AuthSuccess> decoderV2 = new Message.Decoder<AuthSuccess>() {
public static final Message.Decoder<AuthSuccess> decoder = new Message.Decoder<AuthSuccess>() {
public AuthSuccess decode(ChannelBuffer body) {
ByteBuffer b = CBUtil.readValue(body);
if (b == null)
Expand Down

0 comments on commit e32c7c4

Please sign in to comment.