JWT 令牌

此任务向您展示如何设置 Istio 授权策略以根据 JSON Web 令牌 (JWT) 强制执行访问。 Istio 授权策略支持字符串类型和字符串列表类型的 JWT 声明。

开始之前

在开始此任务之前,请执行以下操作

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

  • 阅读 Istio 授权概念

  • 使用 Istio 安装指南 安装 Istio。

  • 部署两个工作负载:httpbincurl。 将这些工作负载部署在一个命名空间中,例如 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/curl/curl.yaml@) -n foo
    
  • 使用以下命令验证 curl 是否成功与 httpbin 通信

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

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

  1. 以下命令为 foo 命名空间中的 httpbin 工作负载创建 jwt-example 请求身份验证策略。 此策略针对 httpbin 工作负载接受由 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.24/security/tools/jwt/samples/jwks.json"
    EOF
    
  2. 验证具有无效 JWT 的请求是否被拒绝

    $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -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=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n"
    200
    
  4. 以下命令为 foo 命名空间中的 httpbin 工作负载创建 require-jwt 授权策略。 该策略要求所有对 httpbin 工作负载的请求都具有有效的 JWT,并且 requestPrincipal 设置为 testing@secure.istio.io/testing@secure.istio.io。 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. 获取一个JWT,其中isssub键的值相同,为testing@secure.istio.io。这将导致Istio生成属性requestPrincipal,其值为testing@secure.istio.io/testing@secure.istio.io

    $ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.24/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=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -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=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -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. 获取一个JWT,它将groups声明设置为一个字符串列表:group1group2

    $ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.24/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. 验证一个包含groups声明中包含group1的JWT的请求是否被允许

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

    $ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -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
这些信息对您有帮助吗?
您是否有任何改进建议?

感谢您的反馈!