JWT 令牌

此任務將說明如何設定 Istio 授權策略,以根據 JSON Web Token (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. 取得將 isssub 金鑰設定為相同值 testing@secure.istio.io 的 JWT。這會導致 Istio 產生值為 testing@secure.istio.io/testing@secure.istio.io 的屬性 requestPrincipal

    $ 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. 取得將 groups 宣告設定為字串列表的 JWT: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
這項資訊是否有用?
您有任何改進建議嗎?

感謝您的意見回饋!