kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 9945f5df57d4cb4dca34759d6d9af4b484de063a4f97ee62f3d754a55b1bb3df |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-09-20 14:36 -07:00 |
| Committer | GitHub <noreply@github.com> · 2016-09-20 14:36 -07:00 |
| Parents | 205c5dd93f7f |
5 files changed
modified changelog/README.md +1 −0
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | - [bug] JAVA-1272: BuiltStatement not able to print its query string if it contains mapped UDTs. |
| 15 | 15 | - [bug] JAVA-1292: 'Adjusted frame length' error breaks driver's ability to read data. |
| 16 | 16 | - [improvement] JAVA-1293: Make DecoderForStreamIdSize.MAX_FRAME_LENGTH configurable. |
| 17 | +- [improvement] JAVA-1053: Add a metric for authentication errors | |
| 17 | 18 | |
| 18 | 19 | |
| 19 | 20 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +15 −1
| @@ -249,7 +249,13 @@ | ||
| 249 | 249 | throw new TransportException(address, String.format("Error initializing connection: %s", error.message)); |
| 250 | 250 | case AUTHENTICATE: |
| 251 | 251 | Responses.Authenticate authenticate = (Responses.Authenticate) response; |
| 252 | - Authenticator authenticator = factory.authProvider.newAuthenticator(address, authenticate.authenticator); | |
| 252 | + Authenticator authenticator; | |
| 253 | + try { | |
| 254 | + authenticator = factory.authProvider.newAuthenticator(address, authenticate.authenticator); | |
| 255 | + } catch (AuthenticationException e) { | |
| 256 | + incrementAuthErrorMetric(); | |
| 257 | + throw e; | |
| 258 | + } | |
| 253 | 259 | switch (protocolVersion) { |
| 254 | 260 | case V1: |
| 255 | 261 | if (authenticator instanceof ProtocolV1Authenticator) |
| @@ -320,6 +326,7 @@ | ||
| 320 | 326 | case READY: |
| 321 | 327 | return checkClusterName(protocolVersion, executor); |
| 322 | 328 | case ERROR: |
| 329 | + incrementAuthErrorMetric(); | |
| 323 | 330 | throw new AuthenticationException(address, ((Responses.Error) authResponse).message); |
| 324 | 331 | default: |
| 325 | 332 | throw new TransportException(address, String.format("Unexpected %s response message from server to a CREDENTIALS message", authResponse.type)); |
| @@ -375,6 +382,7 @@ | ||
| 375 | 382 | if (message.startsWith("java.lang.ArrayIndexOutOfBoundsException: 15")) |
| 376 | 383 | message = String.format("Cannot use authenticator %s with protocol version 1, " |
| 377 | 384 | + "only plain text authentication is supported with this protocol version", authenticator); |
| 385 | + incrementAuthErrorMetric(); | |
| 378 | 386 | throw new AuthenticationException(address, message); |
| 379 | 387 | default: |
| 380 | 388 | throw new TransportException(address, String.format("Unexpected %s response message from server to authentication message", authResponse.type)); |
| @@ -383,6 +391,12 @@ | ||
| 383 | 391 | }; |
| 384 | 392 | } |
| 385 | 393 | |
| 394 | + private void incrementAuthErrorMetric() { | |
| 395 | + if (factory.manager.configuration.getMetricsOptions().isEnabled()) { | |
| 396 | + factory.manager.metrics.getErrorMetrics().getAuthenticationErrors().inc(); | |
| 397 | + } | |
| 398 | + } | |
| 399 | + | |
| 386 | 400 | private UnsupportedProtocolVersionException unsupportedProtocolVersionException(ProtocolVersion triedVersion, ProtocolVersion serverProtocolVersion) { |
| 387 | 401 | logger.debug("Got unsupported protocol version error from {} for version {} server supports version {}", address, triedVersion, serverProtocolVersion); |
| 388 | 402 | return new UnsupportedProtocolVersionException(address, triedVersion, serverProtocolVersion); |
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +10 −0
| @@ -268,6 +268,7 @@ | ||
| 268 | 268 | public class Errors { |
| 269 | 269 | |
| 270 | 270 | private final Counter connectionErrors = registry.counter("connection-errors"); |
| 271 | + private final Counter authenticationErrors = registry.counter("authentication-errors"); | |
| 271 | 272 | |
| 272 | 273 | private final Counter writeTimeouts = registry.counter("write-timeouts"); |
| 273 | 274 | private final Counter readTimeouts = registry.counter("read-timeouts"); |
| @@ -311,6 +312,15 @@ | ||
| 311 | 312 | return connectionErrors; |
| 312 | 313 | } |
| 313 | 314 | |
| 315 | + /** | |
| 316 | + * Returns the number of authentication errors while connecting to Cassandra nodes. | |
| 317 | + * | |
| 318 | + * @return the number of errors. | |
| 319 | + */ | |
| 320 | + public Counter getAuthenticationErrors() { | |
| 321 | + return authenticationErrors; | |
| 322 | + } | |
| 323 | + | |
| 314 | 324 | /** |
| 315 | 325 | * Returns the number of write requests that returned a timeout (independently |
| 316 | 326 | * of the final decision taken by the {@link com.datastax.driver.core.policies.RetryPolicy}). |
modified driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java +12 −2
| @@ -24,6 +24,7 @@ | ||
| 24 | 24 | |
| 25 | 25 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 26 | 26 | import static com.datastax.driver.core.TestUtils.findHost; |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | 28 | import static org.mockito.Mockito.*; |
| 28 | 29 | |
| 29 | 30 | /** |
| @@ -55,6 +56,7 @@ | ||
| 55 | 56 | .build(); |
| 56 | 57 | cluster.connect(); |
| 57 | 58 | verify(authProvider, atLeastOnce()).newAuthenticator(findHost(cluster, 1).getSocketAddress(), "org.apache.cassandra.auth.PasswordAuthenticator"); |
| 59 | + assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(0); | |
| 58 | 60 | } |
| 59 | 61 | |
| 60 | 62 | @Test(groups = "short", expectedExceptions = AuthenticationException.class) |
| @@ -64,7 +66,11 @@ | ||
| 64 | 66 | .withPort(ccm().getBinaryPort()) |
| 65 | 67 | .withCredentials("bogus", "bogus") |
| 66 | 68 | .build()); |
| 67 | - cluster.connect(); | |
| 69 | + try { | |
| 70 | + cluster.connect(); | |
| 71 | + } finally { | |
| 72 | + assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(1); | |
| 73 | + } | |
| 68 | 74 | } |
| 69 | 75 | |
| 70 | 76 | @Test(groups = "short", expectedExceptions = AuthenticationException.class) |
| @@ -73,7 +79,11 @@ | ||
| 73 | 79 | .addContactPoints(getContactPoints()) |
| 74 | 80 | .withPort(ccm().getBinaryPort()) |
| 75 | 81 | .build()); |
| 76 | - cluster.connect(); | |
| 82 | + try { | |
| 83 | + cluster.connect(); | |
| 84 | + } finally { | |
| 85 | + assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()).isEqualTo(1); | |
| 86 | + } | |
| 77 | 87 | } |
| 78 | 88 | |
| 79 | 89 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ReconnectionTest.java +2 −0
| @@ -115,6 +115,8 @@ | ||
| 115 | 115 | TimeUnit.SECONDS.sleep(5); |
| 116 | 116 | } while (iterations < maxIterations && authProvider.count.get() <= initialCount); |
| 117 | 117 | assertThat(iterations).isLessThan(maxIterations); |
| 118 | + assertThat(cluster.getMetrics().getErrorMetrics().getAuthenticationErrors().getCount()) | |
| 119 | + .isEqualTo(authProvider.count.get() - initialCount); | |
| 118 | 120 | |
| 119 | 121 | // Fix the credentials |
| 120 | 122 | authProvider.setPassword("cassandra"); |