首页 > 解决方案 > Istio request.auth.claims[cognito:groups] 不工作

问题描述

apiVersion: "security.istio.io/v1beta1"
kind: "RequestAuthentication"
metadata:
  name: "jwt-example"
  namespace: istio-system
spec:
  selector:
    matchLabels:
      istio: ingressgateway
  jwtRules:
    - issuer: "https://cognito-idp.ap-southeast-1.amazonaws.com/ap-southeast-xxxxx"
      jwksUri: "https://cognito-idp.ap-southeast-1.amazonaws.com/ap-southeast-xxxxx/.well-known/jwks.json"

spec:
  selector:
    matchLabels:
      istio: ingressgateway
  action: ALLOW
  rules:
    - from:
        - source:
            notRequestPrincipals: ["*"]
      to:
        - operation:
            paths: ["/api/v1/*"]
      when:
        - key: request.auth.claims[cognito:groups]
          values: ["testing"]

/api/v1/*如果 JWT 令牌基于testingAWS Cognito 组,我尝试使用上面的代码片段来允许访问。不幸的是,它现在正在显示RBAC: access denied。请让我知道我该如何解决?

标签: amazon-cognitoistio

解决方案


问题

使用您当前的 AuthorizationPolicy,您可以使用正确的令牌阻止每个请求403 RBAC: access denied

我最近对 ​​AuthorizationPolicy 做了一些测试,值得花点时间了解它是如何工作的。


解决方案

正如我们在评论中讨论的那样,有两种方法可以实际完成这项工作。

action:ALLOW和_requestPrincipals

spec:
  selector:
    matchLabels:
      istio: ingressgateway
  action: ALLOW
  rules:
    - from:
        - source:
            requestPrincipals: ["*"]

action:DENYNotRequestPrincipals

spec:
  selector:
    matchLabels:
      istio: ingressgateway
  action: DENY
  rules:
    - from:
        - source:
            notRequestPrincipals: ["*"]

例子

在行动书中有一个来自istio的例子。

拒绝没有 JWT 令牌的请求

让我们创建一个授权策略,在没有 JWT 令牌的情况下拒绝针对 API 网关的请求:

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: app-gw-requires-jwt
 namespace: istio-system
spec:
 selector:
   matchLabels:
     app: istio-ingressgateway
 action: DENY
 rules:
 - from:
   - source:
       notRequestPrincipals: ["*"]
   to:
   - operation:
       hosts: ["apiserver.istioinaction.io"]

此策略使用属性 notRequestPrincipals 和“*”值,这意味着源匹配所有缺少请求主体属性的请求。Request Principal 属性从请求身份验证过滤器从令牌中提取并存储在过滤器元数据中的两个声明中获取其值。这两个声明是发行人和主题,格式为 iss/sub。


推荐阅读