Skip to content

Migrate from IMDSv1 to IMDSv2 #78

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
merged 3 commits into from
Jun 6, 2022

Conversation

Himtanaya
Copy link
Contributor

Issue #, if available:
N/A

Description of changes:
IMDSv2 uses session authentication to retrieve EC2 instance metadata.
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@waifa
Copy link

waifa commented Jun 3, 2022

Looks like code build for your latest commit is failing?

@Himtanaya
Copy link
Contributor Author

Build for the last commit passed. Can you refresh maybe you are not seeing the latest commit or it's possible GitHub is not updating build status.

image

@waifa
Copy link

waifa commented Jun 3, 2022

Can we remove those two "Fix flake8 issues" commits? They don't seem to be adding anything substantial/relevant to this PR. If they are needed you can squash them into one commit to keep our commits clean.

@Himtanaya
Copy link
Contributor Author

Can we remove those two "Fix flake8 issues" commits? They don't seem to be adding anything substantial/relevant to this PR. If they are needed you can squash them into one commit to keep our commits clean.

I am going to squash and merge, so there will be only one commit.

Copy link

@evanuk evanuk left a comment

Choose a reason for hiding this comment

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

👍

@Himtanaya Himtanaya merged commit 61d9bfd into awslabs:master Jun 6, 2022
@Himtanaya Himtanaya deleted the migrate-to-imdsv2 branch June 6, 2022 17:55
@Himtanaya Himtanaya mentioned this pull request Jun 6, 2022
@hussam789
Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Impact
Possible issue
Check HTTP response status

The fetchString function should check the HTTP status code before returning the
response text. If the status code indicates an error, the function should raise
an exception to properly handle failure cases.

aws_embedded_metrics/environment/ec2_environment.py [48-52]

 async def fetchString(
     session: aiohttp.ClientSession, method: str, url: str, headers: Dict[str, str]
 ) -> str:
     async with session.request(method, url, timeout=2, headers=headers) as response:
+        response.raise_for_status()
         return await response.text()
  • Apply this suggestion
Suggestion importance[1-10]: 8

__

Why: This suggestion enhances error handling in the fetchString function by ensuring that HTTP error responses raise exceptions before processing the body, which improves robustness without altering the core functionality.

Medium
Validate HTTP response status

Similar to the fetchString function, fetchJSON should check the HTTP status code
before processing the response to ensure proper error handling.

aws_embedded_metrics/environment/ec2_environment.py [38-45]

 async def fetchJSON(
     session: aiohttp.ClientSession, method: str, url: str, headers: Dict[str, str],
 ) -> Dict[str, Any]:
     async with session.request(method, url, timeout=2, headers=headers) as response:
+        response.raise_for_status()
         # content_type=None prevents validation of the HTTP Content-Type header
         # The EC2 metadata endpoint uses text/plain instead of application/json
         # https://github.com/aio-libs/aiohttp/blob/7f777333a4ec0043ddf2e8d67146a626089773d9/aiohttp/web_request.py#L582-L585
         return cast(Dict[str, Any], await response.json(content_type=None))
  • Apply this suggestion
Suggestion importance[1-10]: 8

__

Why: By adding a status check using response.raise_for_status() in fetchJSON, this suggestion ensures that any HTTP errors are caught early, aligning error handling consistently with similar improvements and enhancing overall reliability.

Medium
  • More

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants