Skip to content

Commit c166c13

Browse files
fredddchingor13
authored andcommitted
Add metadata-flavor header to metadata server ping for compute engine (#283)
1 parent 0a5443a commit c166c13

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public class ComputeEngineCredentials extends GoogleCredentials implements Servi
8686
static final int MAX_COMPUTE_PING_TRIES = 3;
8787
static final int COMPUTE_PING_CONNECTION_TIMEOUT_MS = 500;
8888

89+
private static final String METADATA_FLAVOR = "Metadata-Flavor";
90+
private static final String GOOGLE = "Google";
91+
8992
private static final String PARSE_ERROR_PREFIX = "Error parsing token refresh response. ";
9093
private static final String PARSE_ERROR_ACCOUNT = "Error parsing service account response. ";
9194
private static final String PARSE_ERROR_SIGNATURE = "Error parsing signature response. ";
@@ -157,7 +160,7 @@ private HttpResponse getMetadataResponse(String url) throws IOException {
157160
HttpRequest request = transportFactory.create().createRequestFactory().buildGetRequest(genericUrl);
158161
JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY);
159162
request.setParser(parser);
160-
request.getHeaders().set("Metadata-Flavor", "Google");
163+
request.getHeaders().set(METADATA_FLAVOR, GOOGLE);
161164
request.setThrowExceptionOnExecuteError(false);
162165
HttpResponse response;
163166
try {
@@ -183,12 +186,14 @@ static boolean runningOnComputeEngine(
183186
HttpRequest request =
184187
transportFactory.create().createRequestFactory().buildGetRequest(tokenUrl);
185188
request.setConnectTimeout(COMPUTE_PING_CONNECTION_TIMEOUT_MS);
189+
request.getHeaders().set(METADATA_FLAVOR, GOOGLE);
190+
186191
HttpResponse response = request.execute();
187192
try {
188193
// Internet providers can return a generic response to all requests, so it is necessary
189194
// to check that metadata header is present also.
190195
HttpHeaders headers = response.getHeaders();
191-
return OAuth2Utils.headersContainValue(headers, "Metadata-Flavor", "Google");
196+
return OAuth2Utils.headersContainValue(headers, METADATA_FLAVOR, GOOGLE);
192197
} finally {
193198
response.disconnect();
194199
}

0 commit comments

Comments
 (0)