JWT 令牌
此任務將說明如何設定 Istio 授權策略,以根據 JSON Web Token (JWT) 強制執行存取。Istio 授權策略同時支援字串類型和字串列表類型的 JWT 聲明。
開始之前
在開始此任務之前,請執行下列操作
完成 Istio 使用者端身份驗證任務。
閱讀 Istio 授權概念。
使用 Istio 安裝指南 安裝 Istio。
部署兩個工作負載:
httpbin
和curl
。將它們部署在同一個命名空間中,例如foo
。兩個工作負載都各自在前面運行一個 Envoy 代理。使用以下命令部署範例命名空間和工作負載$ 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 和列表類型聲明的請求
以下命令會在
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
驗證具有無效 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
驗證沒有 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
以下命令會在
foo
命名空間中為httpbin
工作負載建立require-jwt
授權政策。此政策要求所有對httpbin
工作負載的請求都必須具有有效的 JWT,並且requestPrincipal
設定為testing@secure.istio.io/testing@secure.istio.io
。Istio 會將 JWT 權杖的iss
和sub
以/
分隔符號組合起來,建構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
取得將
iss
和sub
金鑰設定為相同值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"}
驗證具有有效 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 的請求是否被拒絕
$ 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
以下命令會更新
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
取得將
groups
宣告設定為字串列表的 JWT:group1
和group2
$ 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"}
驗證具有在
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
驗證具有沒有
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