gpt4 book ai didi

amazon-web-services - ECS 容器在调用 ssm API 端点时挂起

转载 作者:行者123 更新时间:2023-12-02 18:59:02 27 4
gpt4 key购买 nike

调用 ssm.ap-southeast-2.amazonaws.com 时似乎 ECS API 挂起.下面是它挂起的调试结果

2020-06-11 22:47:10,831 - MainThread - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (2): ssm.ap-southeast-2.amazonaws.com:443

这在 EC2 实例上运行良好。仅在 ECS 任务容器内不起作用,并且连接超时。

这背后的原因可能是什么?

最佳答案

Works fine on EC2 instance



嗯...我认为您的容器是 IMDSv2 的受害者。请允许我解释一下。

实例元数据是有关您的实例的数据,可用于配置或管理正在运行的实例。实例元数据分为多个类别,例如主机名、事件和安全组。您可以通过调用以下 URL 来查询实例元数据:
http://169.254.169.254/latest/meta-data/

2019 年 11 月 19 日,实例元数据服务 v2 发布。 EC2 实例元数据服务版本 2 (IMDSv2) 引入的功能之一是“防止开放的第 3 层防火墙和 NAT” 1它在包含 secret token 的低级 IP 数据包上设置 1 的 TTL(或跃点限制 2),因此数据包只能通过一台主机。 TTL 为 1 意味着实例无法将数据包转发到在 ECS 容器实例上运行的 Docker 容器,因为这将被视为另一跳。

来自 1 :

With IMDSv2, setting the TTL value to “1” means that requests from the EC2 instance itself will work because they’re returned to the caller (on the instance) before the subtraction occurs. But if the EC2 instance has been misconfigured as an open router, layer 3 firewall, VPN, tunnel, or NAT device, the response containing the token will have its TTL reduced to zero before leaving the instance, and the packet containing the response will be discarded on its way out of the instance, preventing transport to the attacker. The information simply won’t make it further than the EC2 instance itself, which means that an attacker won’t get the response back with the token, and with it the ability to access instance metadata, even if they’ve been successful at getting past all other defenses.



此更改的结果是,在 Bridge 或 AWSVPC 模式下的 ECS 实例上运行的 Docker 容器无法再查询元数据端点。以下请求将超时:
$ curl -X PUT -H "x-aws-ec2-metadata-token-ttl-seconds: 120" "http://169.254.169.254/latest/api/token"

如果使用 AWS CLI,它具有 IMDSv1 的回退机制,但经过长时间延迟(5 秒)后,它变得相当不可用。

发件人: https://github.com/aws/aws-sdk-js/issues/3024#issuecomment-589135606 :

From v2.575.0, the SDK is configured to default to the IMDSv2 workflow and, by default, will try three times (with a timeout of one second between attempts) to obtain the required token. If all three attempts fail, the SDK will then fall back to the IDMSv1 workflow.



选项 1(谨慎使用)

可以使用“修改实例元数据选项” 3 AWS CLI 调用容器实例,通过指定 --http-put-response-hop-limit 标志的值将 TTL 更改为更高的值。

以下 AWS CLI 命令在 EC2 实例上运行时将该值修改为“2”:
$ aws ec2 modify-instance-metadata-options --instance-id $(curl 169.254.169.254/latest/meta-data/instance-id) --http-put-response-hop-limit 2 --http-endpoint enabled

...之后,针对 token 端点的 curl 命令从 Docker 容器中成功。

可以从 Autoscaling 生命周期 Hook 调用 Lambda 函数,以使用 ModifyInstanceMetadataOptions api 调用在任何启动实例上配置值“2”。另一种选择是将此命令放在 EC2 实例的 UserData 中,以便每个实例都可以使用更新的跃点限制“自我配置”自己。请注意,在这种情况下,实例配置文件应具有与“ec2:ModifyInstanceMetadataOptions”权限相关联的策略,此调用才能成功。

选项 2(推荐)

对于 ECS,从容器访问实例凭证不是最佳实践,而是建议设置任务角色并使用 AWS_CONTAINER_CREDENTIALS_RELATIVE_URI 环境变量从 ECS 代理检索容器特定凭证,例如使用“curl 169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"命令,最新版本的 AWS CLI 默认使用此命令。

您可以在此处阅读有关任务角色凭据的更多信息 4 .任务元数据的类似端点也可用 5 .

更多讨论可以在这里找到:
  • https://github.com/aws/aws-sdk-ruby/issues/2177
  • https://github.com/aws/containers-roadmap/issues/670
  • https://github.com/aws/aws-sdk-js/issues/3024
  • 关于amazon-web-services - ECS 容器在调用 ssm API 端点时挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62324816/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com