调用启用身份验证的 aws 应用程序负载均衡器时如何传递 cookie

How to pass cookies when calling authentication enabled aws application loadbalancer

我做了以下配置

  1. 已创建负载均衡器
  2. 已创建用户池
  3. 创建了具有机密和启用的授权代码授予和 openid 范围的客户端

使用上面创建的资源详细信息在负载均衡器中配置身份验证

  1. 创建了带有认知身份验证的侦听器规则
  2. 添加了所需的配置,包括范围 openid
  3. 为未经身份验证的请求配置deny

问题是当我使用从配置为应用程序的另一个应用程序客户端检索到的 ID 令牌或访问令牌(作为 cookie)调用负载均衡器 url 时,负载均衡器返回 401

当我尝试为未经身份验证的请求配置 authenticate 时。我能够成功调用负载均衡器 url 并获得成功响应(负载均衡器将重定向到认知登录,成功登录后,负载均衡器将设置 cookie)。

成功场景设置的cookie正在分片

cookie 条目类似于 cookie_name-0cookie_name-1(我不知道如何手动 reproduce/recreate)

以下 aws 文档的摘录证实了上述分片行为

The load balancer creates the authentication session cookie and sends it to the client so that the client's user agent can send the cookie to the load balancer when making requests. Because most browsers limit a cookie to 4K in size, the load balancer shards a cookie that is greater than 4K in size into multiple cookies. If the total size of the user claims and access token received from the IdP is greater than 11K bytes in size, the load balancer returns an HTTP 500 error to the client and increments the ELBAuthUserClaimsSizeExceeded metric.

  1. 我如何成功调用负载均衡器 url 并将未验证请求配置为 deny 我需要手动设置 cookie?
  2. 手动生成的id token/access token长度都在1k左右,为什么loadbalancer检索到的token超过4k?

参考 - https://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html#authentication-flow

https://aws.amazon.com/premiumsupport/knowledge-center/elb-configure-authentication-alb/

我认为您正在尝试做的是:您从 cognito 获取 JWT 令牌,并且您希望使用它们通过使用 cognito 身份验证检查的 ALB 对 Web 请求进行身份验证。也就是说,您正在尝试寻找某种方法来自己生成 AWSELBAuthSessionCookie cookie,或者调用 /oauth2/idpresponse 以便 ALB 设置这些 cookie。

简短回答:截至 2021 年 6 月,您不能

这是我从 AWS 支持部门得到的关于这个问题的答案,我试图让 python 客户端使用 Cognito JWT 令牌通过 ALB 调用:

As per the design, automated clients ( such as your python script ) will not be in position to utilize this ALB functionality. Further ALB will not accept any JWT tokens passed by clients in the request. This is to prevent any kind of replay attacks while communicating with load balancer. ALB will only initiate the authentication process if client request triggers authentication rule.

似乎设置这些 ALB cookie 的唯一方法是让网络浏览器打开授权页面。对不起。 Cognito 糟透了。开放标准我的 ____.