diff options
author | Eric Anderson <ejona@google.com> | 2017-11-17 08:29:35 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-17 08:29:35 -0800 |
commit | b940af2daeeb6c0a0f6743f5359e0b999a0bedc1 (patch) | |
tree | 36aa54eb2916a5852073b2220709a4f583bf7d97 /auth | |
parent | 02817e2a9dfe0731579bdca09e02bec6848b40fb (diff) | |
download | grpc-grpc-java-b940af2daeeb6c0a0f6743f5359e0b999a0bedc1.tar.gz |
auth: Treat IOExceptions as UNAVAILABLE
Fixes #3267
Diffstat (limited to 'auth')
-rw-r--r-- | auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java | 16 | ||||
-rw-r--r-- | auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java | 21 |
2 files changed, 33 insertions, 4 deletions
diff --git a/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java b/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java index 0f4b3e964..d8035d3ac 100644 --- a/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java +++ b/auth/src/main/java/io/grpc/auth/GoogleAuthLibraryCallCredentials.java @@ -27,6 +27,7 @@ import io.grpc.Metadata; import io.grpc.MethodDescriptor; import io.grpc.Status; import io.grpc.StatusException; +import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -96,7 +97,16 @@ final class GoogleAuthLibraryCallCredentials implements CallCredentials { // https://github.com/google/google-auth-library-java/issues/3 is resolved. // // Some implementations may return null here. - Map<String, List<String>> metadata = creds.getRequestMetadata(uri); + Map<String, List<String>> metadata; + try { + metadata = creds.getRequestMetadata(uri); + } catch (IOException e) { + // Since it's an I/O failure, let the call be retried with UNAVAILABLE. + applier.fail(Status.UNAVAILABLE + .withDescription("Credentials failed to obtain metadata") + .withCause(e)); + return; + } // Re-use the headers if getRequestMetadata() returns the same map. It may return a // different map based on the provided URI, i.e., for JWT. However, today it does not // cache JWT and so we won't bother tring to save its return value based on the URI. @@ -110,7 +120,9 @@ final class GoogleAuthLibraryCallCredentials implements CallCredentials { } applier.apply(headers); } catch (Throwable e) { - applier.fail(Status.UNAUTHENTICATED.withCause(e)); + applier.fail(Status.UNAUTHENTICATED + .withDescription("Failed computing credential metadata") + .withCause(e)); } } }); diff --git a/auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java b/auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java index 9522b6d28..1bf595b1e 100644 --- a/auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java +++ b/auth/src/test/java/io/grpc/auth/GoogleAuthLibraryCallCredentialsTest.java @@ -171,8 +171,25 @@ public class GoogleAuthLibraryCallCredentialsTest { } @Test - public void credentialsThrows() throws Exception { - IOException exception = new IOException("Broken"); + public void credentialsThrowsIoException() throws Exception { + Exception exception = new IOException("Broken"); + when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception); + + GoogleAuthLibraryCallCredentials callCredentials = + new GoogleAuthLibraryCallCredentials(credentials); + callCredentials.applyRequestMetadata(method, attrs, executor, applier); + assertEquals(1, runPendingRunnables()); + + verify(credentials).getRequestMetadata(eq(expectedUri)); + verify(applier).fail(statusCaptor.capture()); + Status status = statusCaptor.getValue(); + assertEquals(Status.Code.UNAVAILABLE, status.getCode()); + assertEquals(exception, status.getCause()); + } + + @Test + public void credentialsThrowsRuntimeException() throws Exception { + Exception exception = new RuntimeException("Broken"); when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception); GoogleAuthLibraryCallCredentials callCredentials = |