6.3. Service Mesh 1.x での JSON Web トークン認証の使用
Service Mesh 1.x と OpenShift Serverless を使用して、Knative サービスで JSON Web Token (JWT) 認証を使用できます。これを行うには、ServiceMeshMemberRoll
オブジェクトのメンバーであるアプリケーション namespace にポリシーを作成する必要があります。サービスのサイドカーインジェクションも有効にする必要があります。
6.3.1. Service Mesh 1.x および OpenShift Serverless の JSON Web トークン認証の設定
knative-serving
および knative-serving-ingress
などのシステム namespace の Pod へのサイドカー挿入の追加は、Kourier が有効化されている場合はサポートされません。
OpenShift Container Platform では、これらの namespace の Pod にサイドカーの挿入が必要な場合は、サービスメッシュと OpenShift Serverless のネイティブに統合に関する OpenShift Serverless のドキュメントを参照してください。
前提条件
- OpenShift Serverless Operator、Knative Serving、および Red Hat OpenShift Service Mesh をクラスターにインストールしました。
-
OpenShift CLI (
oc
) がインストールされている。 - OpenShift Container Platform でアプリケーションおよび他のワークロードを作成するために、プロジェクトを作成しているか、適切なロールおよびパーミッションを持つプロジェクトにアクセスできる。
手順
sidecar.istio.io/inject="true"
アノテーションをサービスに追加します。サービスの例
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: <service_name> spec: template: metadata: annotations: sidecar.istio.io/inject: "true" sidecar.istio.io/rewriteAppHTTPProbers: "true" ...
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: <service_name> spec: template: metadata: annotations: sidecar.istio.io/inject: "true"
1 sidecar.istio.io/rewriteAppHTTPProbers: "true"
2 ...
Copy to Clipboard Copied! Service
リソースを適用します。oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied! 有効な JSON Web Tokens (JWT) の要求のみを許可する
ServiceMeshMemberRoll
オブジェクトのメンバーであるサーバーレスアプリケーション namespace でポリシーを作成します。重要パスの
/metrics
および/healthz
は、knative-serving
namespace のシステム Pod からアクセスされるため、excludedPaths
に組み込まれる必要があります。apiVersion: authentication.istio.io/v1alpha1 kind: Policy metadata: name: default namespace: <namespace> spec: origins: - jwt: issuer: testing@secure.istio.io jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json" triggerRules: - excludedPaths: - prefix: /metrics - prefix: /healthz principalBinding: USE_ORIGIN
apiVersion: authentication.istio.io/v1alpha1 kind: Policy metadata: name: default namespace: <namespace> spec: origins: - jwt: issuer: testing@secure.istio.io jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json" triggerRules: - excludedPaths: - prefix: /metrics
1 - prefix: /healthz
2 principalBinding: USE_ORIGIN
Copy to Clipboard Copied! Policy
リソースを適用します。oc apply -f <filename>
$ oc apply -f <filename>
Copy to Clipboard Copied!
検証
curl
要求を使用して Knative サービス URL を取得しようとすると、これは拒否されます。curl http://hello-example-default.apps.mycluster.example.com/
$ curl http://hello-example-default.apps.mycluster.example.com/
Copy to Clipboard Copied! 出力例
Origin authentication failed.
Origin authentication failed.
Copy to Clipboard Copied! 有効な JWT で要求を確認します。
有効な JWT トークンを取得します。
TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -
Copy to Clipboard Copied! curl
要求ヘッダーで有効なトークンを使用してサービスにアクセスします。curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"
Copy to Clipboard Copied! これで要求が許可されます。
出力例
Hello OpenShift!
Hello OpenShift!
Copy to Clipboard Copied!