Failing to get EC2 instance metadata from the IMDSv2 API when running on EC2 instances
まだ誰も着手していません。
評価
調査の方向性
codeguru_profiler_agent/agent_metadata/aws_ec2_instance.py のトークン検索から始め、続いて codeguru_profiler_agent/agent_metadata/fleet_info.py と、issue で言及されている対応するテストを調べます。IMDSv2 のリクエスト動作を再現し、文書化されたレスポンスに一致するようにテストモックを更新します。EC2 メタデータが HTTP 405 エラーなしで IMDSv2 を通じて取得されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
The amazon-codeguru-profiler-python-agent fails to get EC2 instance metadata from the IMDSv2 api when running on EC2 instances due to using a GET request to fetch the token when a PUT request is required. I don't really know what the impact is because as the logs note: Unable to get Ec2 instance metadata, this is normal when running in a different environment (e.g. Fargate), profiler will still work. So the profiler seems to still work on EC2 instances but just doesn't have the EC2 instance metadata.
Simplified program to reproduce issue:
import logging
import time
from codeguru_profiler_agent import Profiler
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
Profiler(profiling_group_name='MyProfilingGroup').start()
time.sleep(30)
Relevant logs from running the program on an EC2 instance with IMDSv2 with python 3.7 and codeguru-profiler-agent 1.2.4:
...
DEBUG:codeguru_profiler_agent.agent_metadata.fleet_info:Making a request to http://169.254.169.254/latest/api/token with headers set for these keys: dict_keys(['X-aws-ec2-metadata-token-ttl-seconds'])
INFO:codeguru_profiler_agent.agent_metadata.aws_ec2_instance:Unable to get Ec2 instance metadata, this is normal when running in a different environment (e.g. Fargate), profiler will still work
DEBUG:codeguru_profiler_agent.agent_metadata.aws_ec2_instance:Caught exception:
Traceback (most recent call last):
File "/opt/env/lib/python3.7/site-packages/codeguru_profiler_agent/agent_metadata/aws_ec2_instance.py", line 68, in look_up_metadata
token = cls.__look_up_ec2_api_token()
File "/opt/env/lib/python3.7/site-packages/codeguru_profiler_agent/agent_metadata/aws_ec2_instance.py", line 62, in __look_up_ec2_api_token
headers={EC2_METADATA_TOKEN_TTL_HEADER_KEY: EC2_METADATA_TOKEN_TTL_HEADER_VALUE}) \
File "/opt/env/lib/python3.7/site-packages/codeguru_profiler_agent/agent_metadata/fleet_info.py", line 26, in http_get
return request.urlopen(req, timeout=METADATA_URI_TIMEOUT_SECONDS) # nosec
File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "/usr/local/lib/python3.7/urllib/request.py", line 531, in open
response = meth(req, response)
File "/usr/local/lib/python3.7/urllib/request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/local/lib/python3.7/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
result = func(*args)
File "/usr/local/lib/python3.7/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 405: Not Allowed
...
Looks like the issue was introduced in this PR here: https://github.com/aws/amazon-codeguru-profiler-python-agent/pull/24 and the test didn't catch it because the mock doesn't doesn't match the IMDSv2 API responses.
For the instance metadata docs see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html#instance-metadata-v2-how-it-works and https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html where it notes only a PUT request is allowed for fetching the token while the agent uses a GET request: https://github.com/aws/amazon-codeguru-profiler-python-agent/blob/main/codeguru_profiler_agent/agent_metadata/aws_ec2_instance.py#L61 which is why the request fails and returns HTTP Error 405: Not Allowed.
So the fix looks to be change the agent to use a PUT request to get the token and fix the corresponding test.
- 主要言語
- Python
- スター
- 16
- フォーク
- 12
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
aws/amazon-codeguru-profiler-python-agent のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
aws/amazon-codeguru-profiler-python-agent#62 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 45/100
aws/amazon-codeguru-profiler-python-agent#51 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
aws/amazon-codeguru-profiler-python-agent の issue をすべて見る
似ている issue
-
upstream update
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
conan-io/conan-center-index#31098 ·
メンテナーはふだん 2 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
john-kurkowski/tldextract#382 ·
-
comp/tools duplicate P2 sweeper:risk-compatibility tool/mcp type/bug
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
NousResearch/hermes-agent#132042 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
deepset-ai/haystack#13092 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 85/100
feder-cr/invisible_playwright_mcp#1408 ·
メンテナーはふだん 1 日以内に返信