Skip to content

Commit

Permalink
auth: Add support for Retryable interface
Browse files Browse the repository at this point in the history
Retryable was added in google-auth-library 1.5.3 to make clear the
situations that deserve a retry of the RPC. Bump to that version and
swap away from the imprecise IOException heuristic.
go/auth-correct-retry

Fixes grpc#6808
  • Loading branch information
ejona86 committed Apr 22, 2022
1 parent 8e65700 commit 59b5f23
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

import com.google.auth.Credentials;
import com.google.auth.RequestMetadataCallback;
import com.google.auth.Retryable;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.BaseEncoding;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.SecurityLevel;
import io.grpc.Status;
import io.grpc.StatusException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
Expand Down Expand Up @@ -133,8 +133,8 @@ public void onSuccess(Map<String, List<String>> metadata) {

@Override
public void onFailure(Throwable e) {
if (e instanceof IOException) {
// Since it's an I/O failure, let the call be retried with UNAVAILABLE.
if (e instanceof Retryable && ((Retryable) e).isRetryable()) {
// Let the call be retried with UNAVAILABLE.
applier.fail(Status.UNAVAILABLE
.withDescription("Credentials failed to obtain metadata")
.withCause(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.google.auth.Credentials;
import com.google.auth.RequestMetadataCallback;
import com.google.auth.Retryable;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
Expand Down Expand Up @@ -191,8 +192,9 @@ public void invalidBase64() throws Exception {
}

@Test
public void credentialsFailsWithIoException() throws Exception {
Exception exception = new IOException("Broken");
public void credentialsFailsWithRetryableRetryableException() throws Exception {
boolean retryable = true;
Exception exception = new RetryableException(retryable);
when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception);

GoogleAuthLibraryCallCredentials callCredentials =
Expand All @@ -206,6 +208,23 @@ public void credentialsFailsWithIoException() throws Exception {
assertEquals(exception, status.getCause());
}

@Test
public void credentialsFailsWithUnretryableRetryableException() throws Exception {
boolean retryable = false;
Exception exception = new RetryableException(retryable);
when(credentials.getRequestMetadata(eq(expectedUri))).thenThrow(exception);

GoogleAuthLibraryCallCredentials callCredentials =
new GoogleAuthLibraryCallCredentials(credentials);
callCredentials.applyRequestMetadata(new RequestInfoImpl(), executor, applier);

verify(credentials).getRequestMetadata(eq(expectedUri));
verify(applier).fail(statusCaptor.capture());
Status status = statusCaptor.getValue();
assertEquals(Status.Code.UNAUTHENTICATED, status.getCode());
assertEquals(exception, status.getCause());
}

@Test
public void credentialsFailsWithRuntimeException() throws Exception {
Exception exception = new RuntimeException("Broken");
Expand Down Expand Up @@ -453,4 +472,21 @@ public Attributes getTransportAttrs() {
return Attributes.EMPTY;
}
}

private static class RetryableException extends IOException implements Retryable {
private final boolean retryable;

public RetryableException(boolean retryable) {
super("Broken");
this.retryable = retryable;
}

@Override public boolean isRetryable() {
return retryable;
}

@Override public int getRetryCount() {
return 0;
}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ subprojects {

nettyVersion = '4.1.72.Final'
guavaVersion = '31.0.1-android'
googleauthVersion = '1.4.0'
googleauthVersion = '1.5.3'
protobufVersion = '3.19.2'
protocVersion = protobufVersion
opencensusVersion = '0.28.0'
Expand Down
7 changes: 3 additions & 4 deletions gcp-observability/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ dependencies {
implementation project(':grpc-protobuf'),
project(':grpc-stub'),
project(':grpc-alts'),
libraries.google_auth_credentials,
libraries.google_auth_oauth2_http,
libraries.autovalue_annotation,
libraries.perfmark,
('com.google.guava:guava:31.0.1-jre'),
('com.google.errorprone:error_prone_annotations:2.11.0'),
('com.google.auth:google-auth-library-credentials:1.4.0'),
('org.checkerframework:checker-qual:3.20.0'),
('com.google.auto.value:auto-value-annotations:1.9'),
('com.google.http-client:google-http-client:1.41.0'),
('com.google.http-client:google-http-client-gson:1.41.0'),
('com.google.http-client:google-http-client:1.41.3'),
('com.google.http-client:google-http-client-gson:1.41.3'),
('com.google.api.grpc:proto-google-common-protos:2.7.1'),
("com.google.cloud:google-cloud-logging:${cloudLoggingVersion}")

Expand Down

0 comments on commit 59b5f23

Please sign in to comment.