Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: fix builder invocation for converting Google service account to Jwt access credential #6106

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -296,8 +296,8 @@ public JwtHelper(Class<?> rawServiceAccountClass, ClassLoader loader)
methodPairs.add(new MethodPair(getter, setter));
}
{
Method getter = serviceAccountClass.getMethod("getPrivateKey");
Method setter = builderClass.getMethod("setPrivateKey", getter.getReturnType());
Method getter = serviceAccountClass.getMethod("getPrivateKeyId");
Method setter = builderClass.getMethod("setPrivateKeyId", getter.getReturnType());
methodPairs.add(new MethodPair(getter, setter));
}
}
Expand Down
Expand Up @@ -34,13 +34,15 @@
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.OAuth2Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import io.grpc.Attributes;
import io.grpc.CallCredentials;
import io.grpc.CallCredentials.MetadataApplier;
import io.grpc.CallCredentials.RequestInfo;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.SecurityLevel;
Expand Down Expand Up @@ -388,6 +390,38 @@ public void oauthClassesNotInClassPath() throws Exception {
Iterables.toArray(authorization, String.class));
}

@Test
public void jwtAccessCredentialsInRequestMetadata() throws Exception {
KeyPair pair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
RequestInfo requestInfo = new RequestInfoImpl("example.com:123");

ServiceAccountJwtAccessCredentials jwtCreds =
ServiceAccountJwtAccessCredentials.newBuilder()
.setClientId("test-client")
.setClientEmail("test-email@example.com")
.setPrivateKey(pair.getPrivate())
.setPrivateKeyId("test-private-key-id")
.build();
List<String> expectedAuthMetadata = jwtCreds
.getRequestMetadata(new URI("https://example.com:123/a.service")).get("Authorization");

ServiceAccountCredentials credentials =
ServiceAccountCredentials.newBuilder()
.setClientId("test-client")
.setClientEmail("test-email@example.com")
.setPrivateKey(pair.getPrivate())
.setPrivateKeyId("test-private-key-id")
.build();
GoogleAuthLibraryCallCredentials callCredentials =
new GoogleAuthLibraryCallCredentials(credentials);
callCredentials.applyRequestMetadata(requestInfo, executor, applier);

verify(applier).apply(headersCaptor.capture());
Metadata headers = headersCaptor.getValue();
assertArrayEquals(Iterables.toArray(expectedAuthMetadata, String.class),
Iterables.toArray(headers.getAll(AUTHORIZATION), String.class));
}

private int runPendingRunnables() {
ArrayList<Runnable> savedPendingRunnables = pendingRunnables;
pendingRunnables = new ArrayList<>();
Expand Down