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

fix: create and reuse self signed jwt creds for better performance #1154

Merged
merged 2 commits into from Feb 14, 2023
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 @@ -110,6 +110,8 @@ public class ServiceAccountCredentials extends GoogleCredentials

private transient HttpTransportFactory transportFactory;

private transient JwtCredentials selfSignedJwtCredentialsWithScope = null;

/**
* Internal constructor
*
Expand Down Expand Up @@ -704,6 +706,11 @@ public boolean getUseJwtAccessWithScope() {
return useJwtAccessWithScope;
}

@VisibleForTesting
JwtCredentials getSelfSignedJwtCredentialsWithScope() {
return selfSignedJwtCredentialsWithScope;
}

@Override
public String getAccount() {
return getClientEmail();
Expand Down Expand Up @@ -935,8 +942,11 @@ public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException
// Otherwise, use self signed JWT with uri as the audience.
JwtCredentials jwtCredentials;
if (!createScopedRequired() && useJwtAccessWithScope) {
// Create JWT credentials with the scopes.
jwtCredentials = createSelfSignedJwtCredentials(null);
// Create selfSignedJwtCredentialsWithScope when needed and reuse it for better performance.
if (selfSignedJwtCredentialsWithScope == null) {
selfSignedJwtCredentialsWithScope = createSelfSignedJwtCredentials(null);
}
jwtCredentials = selfSignedJwtCredentialsWithScope;
wangyutongg marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Create JWT credentials with the uri as audience.
jwtCredentials = createSelfSignedJwtCredentials(uri);
Expand Down
Expand Up @@ -1465,6 +1465,7 @@ public void getRequestMetadata_selfSignedJWT_withScopes() throws IOException {
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
assertNotNull(((ServiceAccountCredentials) credentials).getSelfSignedJwtCredentialsWithScope());
verifyJwtAccess(metadata, "dummy.scope");
}

Expand Down Expand Up @@ -1518,6 +1519,7 @@ public void getRequestMetadata_selfSignedJWT_withAudience() throws IOException {
.build();

Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
assertNull(((ServiceAccountCredentials) credentials).getSelfSignedJwtCredentialsWithScope());
verifyJwtAccess(metadata, null);
}

Expand Down