JWTトークン
このタスクでは、JSON Web Token (JWT)に基づいてアクセスを強制するIstio認可ポリシーの設定方法を示します。Istio認可ポリシーは、文字列型と文字列リスト型のJWTクレームの両方をサポートします。
始める前に
このタスクを開始する前に、以下の手順を実行してください。
Istioエンドユーザー認証タスクを完了してください。
Istio認可の概念をお読みください。
Istioインストールガイドを使用してIstioをインストールしてください。
2つのワークロード(
httpbin
とcurl
)をデプロイします。これらのワークロードは、例えばfoo
などの1つの名前空間でデプロイします。両方のワークロードは、各々の前に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
ワークロードへのすべてのリクエストに、requestPrincipal
がtesting@secure.istio.io/testing@secure.istio.io
に設定された有効なJWTを要求します。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にgroup1
という値を含むgroups
という名前のクレームも要求するようにします。$ 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
クレームを文字列のリスト(group1
とgroup2
)に設定するJWTを取得します。$ 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