將 JWT 宣告複製到 HTTP 標頭
此任務會示範如何在透過 Istio 請求驗證原則成功完成 JWT 驗證後,將有效的 JWT 宣告複製到 HTTP 標頭。
開始之前
在開始此任務之前,請執行下列操作
熟悉 Istio 終端使用者驗證支援。
使用 Istio 安裝指南安裝 Istio。
在命名空間
foo
中部署httpbin
和curl
工作負載,並啟用 Sidecar 注入。使用這些命令部署範例命名空間和工作負載$ kubectl create ns foo $ kubectl label namespace foo istio-injection=enabled $ kubectl apply -f @samples/httpbin/httpbin.yaml@ -n foo $ kubectl apply -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 和列表類型宣告的請求
下列命令會為
foo
命名空間中的httpbin
工作負載建立jwt-example
請求驗證原則。此原則接受由testing@secure.istio.io
發出的 JWT,並將宣告foo
的值複製到 HTTP 標頭X-Jwt-Claim-Foo
$ 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" outputClaimToHeaders: - header: "x-jwt-claim-foo" claim: "foo" EOF
驗證具有無效 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
取得由
testing@secure.istio.io
發行的 JWT,其中包含一個鍵為foo
的聲明。$ 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"}
驗證具有有效 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
驗證請求是否包含帶有 JWT 聲明值的有效 HTTP 標頭
$ 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 -H "Authorization: Bearer $TOKEN" | jq '.headers["X-Jwt-Claim-Foo"][0]' "bar"
清除
移除 foo
名稱空間
$ kubectl delete namespace foo