トラストドメインの移行
このタスクでは、認可ポリシーを変更せずに、あるトラストドメインから別のトラストドメインに移行する方法を示します。
Istio 1.4では、認可ポリシーの信頼ドメインの移行をサポートするアルファ機能を導入します。これは、Istioメッシュがその信頼ドメインを変更する必要がある場合、認可ポリシーを手動で変更する必要がないことを意味します。Istioでは、ワークロードが名前空間fooでサービスアカウントbarを使用して実行されており、システムの信頼ドメインがmy-tdの場合、そのワークロードのIDはspiffe://my-td/ns/foo/sa/barになります。インストール時に指定しない限り、Istioメッシュの信頼ドメインはデフォルトでcluster.localです。
始める前に
このタスクを開始する前に、次の手順を実行してください。
Istioの認可の概念をお読みください。
カスタム信頼ドメインと双方向TLSを有効にしてIstioをインストールします。
$ istioctl install --set profile=demo --set meshConfig.trustDomain=old-tdhttpbinサンプルを
default名前空間に、curlサンプルをdefaultおよびcurl-allow名前空間にデプロイします。$ kubectl label namespace default istio-injection=enabled $ kubectl apply -f @samples/httpbin/httpbin.yaml@ $ kubectl apply -f @samples/curl/curl.yaml@ $ kubectl create namespace curl-allow $ kubectl label namespace curl-allow istio-injection=enabled $ kubectl apply -f @samples/curl/curl.yaml@ -n curl-allow以下の認可ポリシーを適用して、
curl-allow名前空間のcurlからのリクエストを除く、httpbinへのすべてのリクエストを拒否します。$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: service-httpbin.default.svc.cluster.local namespace: default spec: rules: - from: - source: principals: - old-td/ns/curl-allow/sa/curl to: - operation: methods: - GET selector: matchLabels: app: httpbin --- EOF認可ポリシーがサイドカーに伝播されるまで、数十秒かかる場合があります。
httpbinへのリクエストが以下から拒否されることを確認します。default名前空間のcurl$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403curl-allow名前空間のcurlは許可されます。$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
トラストドメインエイリアスを使用しないトラストドメインの移行
新しい信頼ドメインを使用してIstioをインストールします。
$ istioctl install --set profile=demo --set meshConfig.trustDomain=new-td信頼ドメインの変更を反映するために、istiodを再デプロイします。
$ kubectl rollout restart deployment -n istio-system istiodIstioメッシュは、新しい信頼ドメイン
new-tdで実行されるようになりました。新しいIstioコントロールプレーンからの変更を反映するために、
httpbinおよびcurlアプリケーションを再デプロイします。$ kubectl delete pod --all$ kubectl delete pod --all -n curl-allowdefault名前空間とcurl-allow名前空間の両方のcurlからのhttpbinへのリクエストが拒否されることを確認します。$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403これは、
httpbinへのすべてのリクエストを拒否する認可ポリシーを指定したためです。ただし、old-td/ns/curl-allow/sa/curlID、つまりcurl-allow名前空間のcurlアプリケーションの古いIDからのリクエストは除きます。上記の新しい信頼ドメイン、つまりnew-tdに移行したとき、このcurlアプリケーションのIDはnew-td/ns/curl-allow/sa/curlになり、これはold-td/ns/curl-allow/sa/curlと同じではありません。したがって、curl-allow名前空間のcurlアプリケーションからhttpbinへのリクエストは、以前は許可されていましたが、現在は拒否されています。Istio 1.4より前は、これを機能させる唯一の方法は、認可ポリシーを手動で変更することでした。Istio 1.4では、以下に示すように、簡単な方法が導入されています。
トラストドメインエイリアスを使用したトラストドメインの移行
新しい信頼ドメインと信頼ドメインエイリアスを使用してIstioをインストールします。
$ cat <<EOF > ./td-installation.yaml apiVersion: install.istio.io/v1alpha1 kind: IstioOperator spec: meshConfig: trustDomain: new-td trustDomainAliases: - old-td EOF $ istioctl install --set profile=demo -f td-installation.yaml -y認可ポリシーを変更せずに、
httpbinへのリクエストが以下から拒否されることを確認します。default名前空間のcurl$ kubectl exec "$(kubectl get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 403curl-allow名前空間のcurlは許可されます。$ kubectl exec "$(kubectl -n curl-allow get pod -l app=curl -o jsonpath={.items..metadata.name})" -c curl -n curl-allow -- curl http://httpbin.default:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
ベストプラクティス
Istio 1.4以降、認可ポリシーを記述する際は、ポリシーの信頼ドメイン部分に値cluster.localを使用することを検討してください。たとえば、old-td/ns/curl-allow/sa/curlの代わりに、cluster.local/ns/curl-allow/sa/curlにする必要があります。この場合、cluster.localはIstioメッシュの信頼ドメイン(信頼ドメインはまだold-td)ではないことに注意してください。ただし、認可ポリシーでは、cluster.localは現在の信頼ドメイン、つまりold-td(および後のnew-td)とそのエイリアスを指すポインターです。認可ポリシーでcluster.localを使用することにより、新しい信頼ドメインに移行すると、Istioはこれを検出し、エイリアスを含めなくても新しい信頼ドメインを古い信頼ドメインとして扱います。
クリーンアップ
$ kubectl delete authorizationpolicy service-httpbin.default.svc.cluster.local
$ kubectl delete deploy httpbin; kubectl delete service httpbin; kubectl delete serviceaccount httpbin
$ kubectl delete deploy curl; kubectl delete service curl; kubectl delete serviceaccount curl
$ istioctl uninstall --purge -y
$ kubectl delete namespace curl-allow istio-system
$ rm ./td-installation.yaml