-
Notifications
You must be signed in to change notification settings - Fork 972
Saikred/feature/output handling #3293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
millems
merged 5 commits into
aws:feature/master/imds
from
srsaikumarreddy:saikred/feature/output_handling
Jul 15, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef8e285
Implemented Response Handling and Parsing Methods for output
78e8a3d
Added Javadoc info
9128793
Implemented PR comments
17534eb
Merged from feature/imds with changes of feature/endpoint_resolution
581a903
Changed dependency scope
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
core/imds/src/main/java/software/amazon/awssdk/imds/MetadataResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.imds; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.core.document.Document; | ||
| import software.amazon.awssdk.protocols.json.internal.unmarshall.document.DocumentUnmarshaller; | ||
| import software.amazon.awssdk.protocols.jsoncore.JsonNode; | ||
| import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser; | ||
| import software.amazon.awssdk.utils.Validate; | ||
|
|
||
| /** | ||
| * The class is used for Response Handling and Parsing the metadata fetched by the get call in the {@link Ec2Metadata} interface. | ||
| * The class provides convenience methods to the users to parse the metadata as a String, List and Document. | ||
| */ | ||
| @SdkPublicApi | ||
| public class MetadataResponse { | ||
|
|
||
| private static final Logger log = LoggerFactory.getLogger(MetadataResponse.class); | ||
|
|
||
| private static final JsonNodeParser JSON_NODE_PARSER = JsonNode.parserBuilder().removeErrorLocations(true).build(); | ||
|
|
||
| private final String body; | ||
|
|
||
| public MetadataResponse(String body) { | ||
| this.body = Validate.notNull(body, "Metadata is null"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Metadata Response body as a String. This method can be used for parsing the retrieved | ||
| * singular metadata from IMDS. | ||
| * | ||
| * @return String Representation of the Metadata Response Body. | ||
| * | ||
| * <p> | ||
| * Example: | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * Ec2Metadata ec2Metadata = Ec2Metadata.create(); | ||
| * MetadataResponse metadataResponse = client.get("/latest/meta-data/ami-id"); | ||
| * String response = metadataResponse.asString(); | ||
| * } | ||
| * </pre> | ||
| */ | ||
| public String asString() { | ||
| return body; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Parses the response String into a list of Strings split by delimiter ("\n"). This method can be used for parsing the | ||
| * list-type metadata from IMDS. | ||
| * | ||
| * @return List Representation of the Metadata Response Body. | ||
| * | ||
| * <p> | ||
| * Example: | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * Ec2Metadata ec2Metadata = Ec2Metadata.create(); | ||
| * MetadataResponse metadataResponse = client.get("/latest/meta-data/ancestor-ami-ids"); | ||
| * List<String>response = metadataResponse.asList(); | ||
| * } | ||
| * </pre> | ||
| */ | ||
| public List<String> asList() { | ||
| return Arrays.asList(body.split("\n")); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Parses the response String into {@link Document} type. This method can be used for | ||
| * parsing the metadata in a String Json Format. | ||
| * | ||
| * @return Document Representation of the Metadata Response Body. | ||
| * @throws IOException in case parsing does not happen correctly. | ||
| * | ||
| * <p> | ||
| * Example: | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * Ec2Metadata ec2Metadata = Ec2Metadata.create(); | ||
| * MetadataResponse metadataResponse = client.get("/latest/dynamic/instance-identity/document"); | ||
| * Document document = metadataResponse.asDocument(); | ||
| * } | ||
| * </pre> | ||
| */ | ||
|
|
||
| public Document asDocument() throws IOException { | ||
|
|
||
| JsonNode node = JSON_NODE_PARSER.parse(body); | ||
| return node.visit(new DocumentUnmarshaller()); | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,8 @@ | |
| import org.mockito.junit.MockitoJUnitRunner; | ||
| import software.amazon.awssdk.core.SdkSystemSetting; | ||
| import software.amazon.awssdk.core.exception.SdkServiceException; | ||
| import software.amazon.awssdk.http.ExecutableHttpRequest; | ||
| import software.amazon.awssdk.protocols.jsoncore.JsonNode; | ||
| import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser; | ||
|
|
||
| /** | ||
| * Unit Tests to test the Ec2Metadata Client functionality | ||
|
|
@@ -65,6 +66,8 @@ public class Ec2MetadataTest { | |
| @Rule | ||
| public WireMockRule mockMetadataEndpoint = new WireMockRule(); | ||
|
|
||
| private static final JsonNodeParser jsonParser = JsonNode.parser(); | ||
|
|
||
| @Before | ||
| public void methodSetup() { | ||
| System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), "http://localhost:" + mockMetadataEndpoint.port()); | ||
|
|
@@ -74,8 +77,9 @@ public void methodSetup() { | |
| @Test | ||
| public void when_dummy_string_is_returned(){ | ||
|
|
||
| when(ec2Metadata.get("/ami-id")).thenReturn("IMDS"); | ||
| assertThat(ec2Metadata.get("/ami-id")).isEqualTo("IMDS"); | ||
| MetadataResponse metadataResponse = new MetadataResponse("IMDS"); | ||
| when(ec2Metadata.get("/ami-id")).thenReturn(metadataResponse); | ||
| assertThat(ec2Metadata.get("/ami-id").asString()).isEqualTo("IMDS"); | ||
|
|
||
| } | ||
|
|
||
|
|
@@ -93,9 +97,9 @@ public void get_AmiId_onMetadataResource_200_Success() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}"))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isEqualTo("{}"); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse.asString()).isEqualTo("{}"); | ||
|
|
||
| WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); | ||
| WireMock.verify(getRequestedFor(urlPathEqualTo(AMI_ID_RESOURCE)).withHeader(TOKEN_HEADER, equalTo("some-token"))); | ||
|
|
@@ -111,7 +115,7 @@ public void get_AmiId_onMetadataResource_404Error_throws() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}").withStatus(404))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -121,9 +125,8 @@ public void get_AmiId_onMetadataResource_401Error_throws() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}").withStatus(401))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isNull(); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse).isNull(); | ||
|
Comment on lines
+128
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning null seems weird here. Any reason we wouldn't throw an exception? I'd think we'd never want get() to return null. |
||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -133,9 +136,9 @@ public void get_AmiId_onMetadataResource_IOException_throws() { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withFixedDelay(Integer.MAX_VALUE))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isNull(); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse).isNull(); | ||
|
|
||
| WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); | ||
| WireMock.verify(getRequestedFor(urlPathEqualTo(AMI_ID_RESOURCE)).withHeader(TOKEN_HEADER, equalTo("some-token"))); | ||
|
|
@@ -151,7 +154,7 @@ public void get_AmiId_onTokenResource_403Error_throws() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}"))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -161,9 +164,9 @@ public void get_AmiId_onTokenResource_401Error_throws() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}"))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isNull(); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse).isNull(); | ||
| WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); | ||
|
|
||
| } | ||
|
|
@@ -174,22 +177,20 @@ public void getAmiId_onTokenResource_IOError_throws() throws IOException { | |
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}"))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isNull(); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse).isNull(); | ||
|
|
||
| WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); | ||
| // WireMock.verify(getRequestedFor(urlPathEqualTo(AMI_ID_RESOURCE)).withHeader(TOKEN_HEADER, equalTo("some-token"))); | ||
| } | ||
| @Test | ||
| public void getAmiId_onTokenResource_200() throws IOException { | ||
| stubFor(put(urlPathEqualTo(TOKEN_RESOURCE_PATH)).willReturn(aResponse().withBody("some-token"))); | ||
| stubFor(get(urlPathEqualTo(AMI_ID_RESOURCE)).willReturn(aResponse().withBody("{}"))); | ||
|
|
||
| Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build(); | ||
| String data = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
|
|
||
| assertThat(data).isEqualTo("{}"); | ||
| MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id"); | ||
| assertThat(metadataResponse.asString()).isEqualTo("{}"); | ||
|
|
||
| WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600"))); | ||
| WireMock.verify(getRequestedFor(urlPathEqualTo(AMI_ID_RESOURCE)).withHeader(TOKEN_HEADER, equalTo("some-token"))); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should put something in the backlog to avoid needing this dependency at GA. I know it was needed for the DocumentMarshaller, but I think it's relatively small and we could copy it to avoid the dependency.