Skip to content

Commit

Permalink
auth: Treat IOExceptions as UNAVAILABLE
Browse files Browse the repository at this point in the history
Fixes #3267
  • Loading branch information
ejona86 authored Nov 17, 2017
1 parent 02817e2 commit b940af2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -96,7 +97,16 @@ public void run() {
// 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.
Expand All @@ -110,7 +120,9 @@ public void run() {
}
applier.apply(headers);
} catch (Throwable e) {
applier.fail(Status.UNAUTHENTICATED.withCause(e));
applier.fail(Status.UNAUTHENTICATED
.withDescription("Failed computing credential metadata")
.withCause(e));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,25 @@ public void invalidBase64() throws Exception {
}

@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 =
Expand Down

0 comments on commit b940af2

Please sign in to comment.