Skip to content

Commit 6efda0b

Browse files
authored
fix: Simplify call to directly retrieve the default service account from MDS (#1844)
* fix: Simplify call to directly retrieve the default service account from MDS * chore: Make getDefaultServiceAccountUrl package-private
1 parent a92dff8 commit 6efda0b

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
import java.util.Collections;
7070
import java.util.Date;
7171
import java.util.List;
72-
import java.util.Map;
7372
import java.util.Objects;
7473
import java.util.logging.Level;
7574
import java.util.logging.Logger;
@@ -632,6 +631,12 @@ public static String getServiceAccountsUrl() {
632631
+ "/computeMetadata/v1/instance/service-accounts/?recursive=true";
633632
}
634633

634+
/** Url to retrieve the default service account entry from the Metadata Server. */
635+
static String getDefaultServiceAccountUrl() {
636+
return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT)
637+
+ "/computeMetadata/v1/instance/service-accounts/default/email";
638+
}
639+
635640
public static String getIdentityDocumentUrl() {
636641
return getMetadataServerUrl(DefaultCredentialsProvider.DEFAULT)
637642
+ "/computeMetadata/v1/instance/service-accounts/default/identity";
@@ -733,7 +738,7 @@ public byte[] sign(byte[] toSign) {
733738

734739
private String getDefaultServiceAccount() throws IOException {
735740
HttpResponse response =
736-
getMetadataResponse(getServiceAccountsUrl(), RequestType.UNTRACKED, false);
741+
getMetadataResponse(getDefaultServiceAccountUrl(), RequestType.UNTRACKED, false);
737742
int statusCode = response.getStatusCode();
738743
if (statusCode == HttpStatusCodes.STATUS_CODE_NOT_FOUND) {
739744
throw new IOException(
@@ -756,12 +761,7 @@ private String getDefaultServiceAccount() throws IOException {
756761
// Mock transports will have success code with empty content by default.
757762
throw new IOException(METADATA_RESPONSE_EMPTY_CONTENT_ERROR_MESSAGE);
758763
}
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);
764+
return response.parseAsString();
765765
}
766766

767767
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)