Skip to content

Commit abd8129

Browse files
committed
fix: Simplify call to directly retrieve the default service account from MDS
1 parent a65c22d commit abd8129

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.api.client.http.HttpStatusCodes;
4242
import com.google.api.client.json.JsonObjectParser;
4343
import com.google.api.client.util.GenericData;
44+
import com.google.api.core.ObsoleteApi;
4445
import com.google.auth.CredentialTypeForMetrics;
4546
import com.google.auth.Credentials;
4647
import com.google.auth.Retryable;
@@ -69,7 +70,6 @@
6970
import java.util.Collections;
7071
import java.util.Date;
7172
import java.util.List;
72-
import java.util.Map;
7373
import java.util.Objects;
7474
import java.util.logging.Level;
7575
import java.util.logging.Logger;
@@ -627,11 +627,22 @@ public static String getUniverseDomainUrl() {
627627
+ "/computeMetadata/v1/universe/universe-domain";
628628
}
629629

630+
/**
631+
* This method is marked as Obsolete. Prefer to use {@link #getDefaultServiceAccount()} to
632+
* retrieve the default service account.
633+
*/
634+
@ObsoleteApi("Prefer getDefaultServiceAccountUrl() to retrieve the default service account")
630635
public static String getServiceAccountsUrl() {
631636
return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT)
632637
+ "/computeMetadata/v1/instance/service-accounts/?recursive=true";
633638
}
634639

640+
/** Url to retrieve the default service account entry from the Metadata Server. */
641+
public static String getDefaultServiceAccountUrl() {
642+
return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT)
643+
+ "/computeMetadata/v1/instance/service-accounts/default/email";
644+
}
645+
635646
public static String getIdentityDocumentUrl() {
636647
return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT)
637648
+ "/computeMetadata/v1/instance/service-accounts/default/identity";
@@ -733,7 +744,7 @@ public byte[] sign(byte[] toSign) {
733744

734745
private String getDefaultServiceAccount() throws IOException {
735746
HttpResponse response =
736-
getMetadataResponse(getServiceAccountsUrl(), RequestType.UNTRACKED, false);
747+
getMetadataResponse(getDefaultServiceAccountUrl(), RequestType.UNTRACKED, false);
737748
int statusCode = response.getStatusCode();
738749
if (statusCode == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
739750
throw new IOException(
@@ -756,12 +767,7 @@ private String getDefaultServiceAccount() throws IOException {
756767
// Mock transports will have success code with empty content by default.
757768
throw new IOException(METADATA_RESPONSE_EMPTY_CONTENT_ERROR_MESSAGE);
758769
}
759-
GenericData responseData = response.parseAs(GenericData.class);
760-
LoggingUtils.logResponsePayload(
761-
responseData, LOGGER_PROVIDER, "Received default service account payload");
762-
Map<String, Object> defaultAccount =
763-
OAuth2Utils.validateMap(responseData, "default", PARSE_ERROR_ACCOUNT);
764-
return OAuth2Utils.validateString(defaultAccount, "email", PARSE_ERROR_ACCOUNT);
770+
return response.parseAsString();
765771
}
766772

767773
public static class Builder extends GoogleCredentials.Builder {

oauth2_http/javatests/com/google/auth/oauth2/ComputeEngineCredentialsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ public void getAccount_missing_throws() {
590590
new MockMetadataServerTransport() {
591591
@Override
592592
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
593-
if (isGetServiceAccountsUrl(url)) {
593+
if (isGetDefaultServiceAccountsUrl(url)) {
594594
return new MockLowLevelHttpRequest(url) {
595595
@Override
596596
public LowLevelHttpResponse execute() throws IOException {
@@ -626,7 +626,7 @@ public void getAccount_emptyContent_throws() {
626626
new MockMetadataServerTransport() {
627627
@Override
628628
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
629-
if (isGetServiceAccountsUrl(url)) {
629+
if (isGetDefaultServiceAccountsUrl(url)) {
630630
return new MockLowLevelHttpRequest(url) {
631631
@Override
632632
public LowLevelHttpResponse execute() throws IOException {

oauth2_http/javatests/com/google/auth/oauth2/MockMetadataServerTransport.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public LowLevelHttpRequest buildRequest(String method, String url) throws IOExce
129129
if (url.startsWith(ComputeEngineCredentials.getTokenServerEncodedUrl())) {
130130
this.request = getMockRequestForTokenEndpoint(url);
131131
return this.request;
132-
} else if (isGetServiceAccountsUrl(url)) {
133-
this.request = getMockRequestForServiceAccount(url);
132+
} else if (isGetDefaultServiceAccountsUrl(url)) {
133+
this.request = getMockRequestForDefaultServiceAccount(url);
134134
return this.request;
135135
} else if (isSignRequestUrl(url)) {
136136
this.request = getMockRequestForSign(url);
@@ -176,22 +176,13 @@ public LowLevelHttpResponse execute() throws IOException {
176176
};
177177
}
178178

179-
private MockLowLevelHttpRequest getMockRequestForServiceAccount(String url) {
179+
private MockLowLevelHttpRequest getMockRequestForDefaultServiceAccount(String url) {
180180
return new MockLowLevelHttpRequest(url) {
181181
@Override
182-
public LowLevelHttpResponse execute() throws IOException {
183-
// Create the JSON response
184-
GenericJson serviceAccountsContents = new GenericJson();
185-
serviceAccountsContents.setFactory(OAuth2Utils.JSON_FACTORY);
186-
GenericJson defaultAccount = new GenericJson();
187-
defaultAccount.put("email", serviceAccountEmail);
188-
serviceAccountsContents.put("default", defaultAccount);
189-
190-
String serviceAccounts = serviceAccountsContents.toPrettyString();
191-
182+
public LowLevelHttpResponse execute() {
192183
return new MockLowLevelHttpResponse()
193184
.setContentType(Json.MEDIA_TYPE)
194-
.setContent(serviceAccounts);
185+
.setContent(serviceAccountEmail);
195186
}
196187
};
197188
}
@@ -341,8 +332,8 @@ public LowLevelHttpResponse execute() throws IOException {
341332
};
342333
}
343334

344-
protected boolean isGetServiceAccountsUrl(String url) {
345-
return url.equals(ComputeEngineCredentials.getServiceAccountsUrl());
335+
protected boolean isGetDefaultServiceAccountsUrl(String url) {
336+
return url.equals(ComputeEngineCredentials.getDefaultServiceAccountUrl());
346337
}
347338

348339
protected boolean isSignRequestUrl(String url) {

0 commit comments

Comments
 (0)