Uh oh!
There was an error while loading. Please reload this page.
Add validation for project-id returned by metadata server - #2868
Conversation
the Metadata-Flavor field in the header.
yihanzhen
commented
Feb 6, 2018
This fixes #2440 |
wsh
left a comment
There was a problem hiding this comment.
Please double check the null safety of the changes to ServiceOptions!
| } | ||
| @Test | ||
| public void testResponseHeaderContainsMetaDataFlavor() throws Exception { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath())); | ||
| } | ||
| @Test |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| @InternalApi("Visible for testing") | ||
| static boolean headerContainsMetadataFlavor(HttpResponse response) { | ||
| return response.getHeaders() | ||
| .getFirstHeaderStringValue("Metadata-Flavor").equals("Google"); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| <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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| <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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| @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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
yihanzhen
commented
Feb 7, 2018
| 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.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| headers.put("Metadata-Flavor", "Google"); | ||
| httpResponse = createHttpResponseWithHeader(headers); | ||
| assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isTrue(); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
yihanzhen
commented
Feb 7, 2018
Another look? @wsh |
| } | ||
| @Test | ||
| public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
| 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
pongad
left a comment
There was a problem hiding this comment.
IT failures OK, they usually don't run on PRs anyway.
🤖 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).
…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).
Check whether the
HttpResponsefrom metadata server contains a fieldMetadata-Flavorin 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
ServiceOptionswill continue to look for project-id elsewhere.