Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add validation for project-id returned by metadata server#2868

Merged
yihanzhen merged 9 commits into
googleapis:mastergoogleapis/google-cloud-java:masterfrom
yihanzhen:fix-project-id-from-metadata-serveryihanzhen/google-cloud-java:fix-project-id-from-metadata-serverCopy head branch name to clipboard
Feb 13, 2018
Merged

Add validation for project-id returned by metadata server#2868
yihanzhen merged 9 commits into
googleapis:mastergoogleapis/google-cloud-java:masterfrom
yihanzhen:fix-project-id-from-metadata-serveryihanzhen/google-cloud-java:fix-project-id-from-metadata-serverCopy head branch name to clipboard

Conversation

@yihanzhen

Copy link
Copy Markdown
Contributor

Check whether the HttpResponse from metadata server contains a field Metadata-Flavor in its header when requesting project-id.
If yes, the response will be parsed as the project-id;
If not, the response will be considered invalid and ServiceOptions will continue to look for project-id elsewhere.

@yihanzhen yihanzhen requested a review from pongad February 6, 2018 18:58
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Feb 6, 2018
@yihanzhen

Copy link
Copy Markdown
Contributor Author

This fixes #2440

@wsh wsh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check the null safety of the changes to ServiceOptions!

}

@Test
public void testResponseHeaderContainsMetaDataFlavor() throws Exception {

This comment was marked as spam.

assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath()));
}

@Test

This comment was marked as spam.

@InternalApi("Visible for testing")
static boolean headerContainsMetadataFlavor(HttpResponse response) {
return response.getHeaders()
.getFirstHeaderStringValue("Metadata-Flavor").equals("Google");

This comment was marked as spam.

HttpTransport mockHttpTransport = Mockito.mock(MockHttpTransport.class);
MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.addHeader("Metadata-Flavor", "Google");
Mockito.when(mockHttpTransport.buildRequest(Matchers.anyString(), Matchers.anyString()).execute()).thenReturn(mockResponse);

This comment was marked as spam.

This comment was marked as spam.

Comment thread google-cloud-core/pom.xml Outdated
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>

This comment was marked as spam.

This comment was marked as spam.

return response.getHeaders()
.getFirstHeaderStringValue("Metadata-Flavor").equals("Google");
String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor");
return metadataFlavorValue != null && metadataFlavorValue.equals("Google");

This comment was marked as spam.

HttpTransport mockHttpTransport = Mockito.mock(MockHttpTransport.class);
MockLowLevelHttpResponse mockResponse = new MockLowLevelHttpResponse();
mockResponse.addHeader("Metadata-Flavor", "Google");
Mockito.when(mockHttpTransport.buildRequest(Matchers.anyString(), Matchers.anyString()).execute()).thenReturn(mockResponse);

This comment was marked as spam.

Comment thread google-cloud-core/pom.xml Outdated
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>

This comment was marked as spam.

@InternalApi("Visible for testing")
static boolean headerContainsMetadataFlavor(HttpResponse response) {
String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor");
return metadataFlavorValue != null && metadataFlavorValue.equals("Google");

This comment was marked as spam.

Add a null check test in ServiceOptionsTest
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isTrue();
}

private HttpResponse createHttpResponseWithHeader(final Map<String, String> headers) throws Exception {

This comment was marked as spam.

This comment was marked as spam.

@yihanzhen

Copy link
Copy Markdown
Contributor Author

Can you take a look? @wsh @pongad Thanks!

static boolean headerContainsMetadataFlavor(HttpResponse response) {
String metadataFlavorValue = response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor");
return metadataFlavorValue != null && metadataFlavorValue.equals("Google");
return "Google".equals(response.getHeaders().getFirstHeaderStringValue("Metadata-Flavor"));

This comment was marked as spam.


headers.put("Metadata-Flavor", "Google");
httpResponse = createHttpResponseWithHeader(headers);
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isTrue();

This comment was marked as spam.

assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isTrue();
}

private HttpResponse createHttpResponseWithHeader(final Map<String, String> headers) throws Exception {

This comment was marked as spam.

@yihanzhen

Copy link
Copy Markdown
Contributor Author

Another look? @wsh

}

@Test
public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception {

This comment was marked as spam.

public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
for (Map.Entry<String, String> entry : headers.entrySet()) {
for (Map.Entry<String, String> entry : headers.entries()) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@pongad pongad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IT failures OK, they usually don't run on PRs anyway.

@yihanzhen yihanzhen merged commit f2a2387 into googleapis:master Feb 13, 2018
suztomo pushed a commit that referenced this pull request Mar 9, 2026
chingor13 pushed a commit that referenced this pull request Mar 24, 2026
🤖 I have created a release *beep* *boop*
---


## [6.60.1](https://togithub.com/googleapis/java-spanner/compare/v6.60.0...v6.60.1) (2024-02-23)


### Dependencies

* Update dependency com.google.cloud:google-cloud-monitoring to v3.37.0 ([#2920](https://togithub.com/googleapis/java-spanner/issues/2920)) ([c2115a6](https://togithub.com/googleapis/java-spanner/commit/c2115a698b00f2dce810b7a48ad7b722d085200c))
* Update dependency org.graalvm.buildtools:native-maven-plugin to v0.10.0 ([#2861](https://togithub.com/googleapis/java-spanner/issues/2861)) ([d32adc3](https://togithub.com/googleapis/java-spanner/commit/d32adc3587f388464f3f5c5e42ecd94b9b49bb2f))
* Update dependency org.graalvm.buildtools:native-maven-plugin to v0.10.1 ([#2919](https://togithub.com/googleapis/java-spanner/issues/2919)) ([cbd6629](https://togithub.com/googleapis/java-spanner/commit/cbd66293b45aac7b6d3c6951af2fd1b3880839e9))
* Update dependency org.json:json to v20240205 ([#2913](https://togithub.com/googleapis/java-spanner/issues/2913)) ([98fa243](https://togithub.com/googleapis/java-spanner/commit/98fa243025b624b397a4589cca6d94934496d011))
* Update dependency org.junit.vintage:junit-vintage-engine to v5.10.2 ([#2868](https://togithub.com/googleapis/java-spanner/issues/2868)) ([030d281](https://togithub.com/googleapis/java-spanner/commit/030d281a120cfc56a628e19dd4cb01f986f283ab))
* Update opentelemetry.version to v1.35.0 ([#2902](https://togithub.com/googleapis/java-spanner/issues/2902)) ([f2cdf12](https://togithub.com/googleapis/java-spanner/commit/f2cdf126be9cd3fc1741a9cf2d29041d34a372a9))

---
This PR was generated with [Release Please](https://togithub.com/googleapis/release-please). See [documentation](https://togithub.com/googleapis/release-please#release-please).
rahul2393 pushed a commit to akash329d/google-cloud-java that referenced this pull request May 21, 2026
…ogleapis#2868)

Change-Id: I1a4f684c2cb939fbf1e1ec4705e360bfaeeb4a46

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
- [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code!  That way we can discuss the change, evaluate designs, and agree on the general idea
- [ ] Ensure the tests and linter pass
- [ ] Code coverage does not decrease (if any source code was changed)
- [ ] Appropriate docs were updated (if necessary)
- [ ] Rollback plan is reviewed and LGTMed
- [ ] All new data plane features have a completed end to end testing plan

Fixes #<issue_number_goes_here> ☕️

If you write sample code, please follow the [samples format](
https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.