JWT 令牌

本教程向您展示如何通过设置 Istio 授权策略来实现基于 JSON Web Token(JWT)的强制访问控制。 Istio 授权策略同时支持字符串类型和列表类型的 JWT 声明。

开始之前

在开始这个任务之前,请先完成以下操作:

  • 完成 Istio 最终用户身份验证任务

  • 阅读 Istio 授权概念

  • 参照 Istio 安装指南安装 Istio。

  • 部署两个工作负载(workload):httpbinsleep。将它们部署在同一个命名空间中,

  • 例如 foo。每个工作负载都在前面运行一个 Envoy 代理。您可以使用以下命令来部署它们:

    ZipZip
    $ kubectl create ns foo
    $ kubectl apply -f <(istioctl kube-inject -f @samples/httpbin/httpbin.yaml@) -n foo
    $ kubectl apply -f <(istioctl kube-inject -f @samples/sleep/sleep.yaml@) -n foo
    
  • 使用下面的命令验证 sleep 能够正常访问 httpbin 服务:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl http://httpbin.foo:8000/ip -sS -o /dev/null -w "%{http_code}\n"
    200
    

允许包含有效 JWT 和 列表类型声明的请求

  1. 以下命令为 foo 命名空间下的 httpbin 工作负载创建一个名为 jwt-example 的身份验证策略。这个策略使得 httpbin 工作负载接收 Issuer 为 testing@secure.istio.io 的 JWT 令牌:

    $ kubectl apply -f - <<EOF
    apiVersion: security.istio.io/v1
    kind: RequestAuthentication
    metadata:
      name: "jwt-example"
      namespace: foo
    spec:
      selector:
        matchLabels:
          app: httpbin
      jwtRules:
      - issuer: "testing@secure.istio.io"
        jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/jwks.json"
    EOF
    
  2. 验证使用无效 JWT 的请求被拒绝:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer invalidToken" -w "%{http_code}\n"
    401
    
  3. 验证没有 JWT 令牌的请求被允许,因为以上策略不包含授权策略:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n"
    200
    
  4. 以下命令为 foo 命名空间下的 httpbin 工作负载创建一个名为 require-jwt 的授权策略。这个策略要求所有发往 httpbin 服务的请求都要包含一个将 requestPrincipal 设置为 testing@secure.istio.io/testing@secure.istio.io 的有效 JWT。Istio 使用 / 连接 JWT 令牌的 isssub 以组成 requestPrincipal 字段。

    $ kubectl apply -f - <<EOF
    apiVersion: security.istio.io/v1
    kind: AuthorizationPolicy
    metadata:
      name: require-jwt
      namespace: foo
    spec:
      selector:
        matchLabels:
          app: httpbin
      action: ALLOW
      rules:
      - from:
        - source:
           requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"]
    EOF
    
  5. 获取 isssub 都为 testing@secure.istio.io 的 JWT。这会让 Istio 生成的 requestPrincipal 属性值为 testing@secure.istio.io/testing@secure.istio.io

    $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
    {"exp":4685989700,"foo":"bar","iat":1532389700,"iss":"testing@secure.istio.io","sub":"testing@secure.istio.io"}
    
  6. 验证使用有效 JWT 的请求被允许:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n"
    200
    
  7. 验证没有 JWT 的请求被拒绝:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n"
    403
    
  8. 以下命令更新 require-jwt 授权策略,使其同时要求 JWT 包含一个名为 groups 值为 group1 的声明:

    $ kubectl apply -f - <<EOF
    apiVersion: security.istio.io/v1
    kind: AuthorizationPolicy
    metadata:
      name: require-jwt
      namespace: foo
    spec:
      selector:
        matchLabels:
          app: httpbin
      action: ALLOW
      rules:
      - from:
        - source:
           requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"]
        when:
        - key: request.auth.claims[groups]
          values: ["group1"]
    EOF
    
  9. 获取 groups 声明列表为 group1group2 的 JWT:

    $ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.21/security/tools/jwt/samples/groups-scope.jwt -s) && echo "$TOKEN_GROUP" | cut -d '.' -f2 - | base64 --decode -
    {"exp":3537391104,"groups":["group1","group2"],"iat":1537391104,"iss":"testing@secure.istio.io","scope":["scope1","scope2"],"sub":"testing@secure.istio.io"}
    
  10. 验证包含 JWT 且 JWT 中包含名为 groups 值为 group1 声明的请求被允许:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN_GROUP" -w "%{http_code}\n"
    200
    
  11. 验证包含 JWT,但 JWT 不包含 groups 声明的请求被拒绝:

    $ kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c sleep -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n"
    403
    

清理

删除 foo 命名空间:

$ kubectl delete namespace foo
这些信息有用吗?
您是否有更多建议和改进意见?

感谢您的反馈!