OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

在Service Mesh中,兩個微服務之間雙向傳輸安全是由Sidecar完成的。如下圖,運行微服務的容器和Sidecar容器之間是網絡互信的,因此是通過通過一般網絡連接的,而Service A和Service B之間的網絡安全傳輸是通過在與它們相對應的Sidecar之間的Mutual TLS實現的。本文演示如何為訪問Preference微服務配置雙向傳輸安全Mutual TLS。

OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

在開始配置前需要根據《OpenShift 4 之Istio-Tutorial (2) 部署三個微服務》準備微服務環境即可。

1. 先在istio-tutorial項目中部署一個pod,以後會使用其上的curl功能。

<code>$ oc apply -f curl/Deployment.yaml -n istio-tutorial/<code>

2. 在第一個窗口執行以下腳本,分別進入curl和customer的pod中,然後訪問perference微服務的Service的訪問地址-http://preference:8080 。

<code>$ ./scripts/run_mTLS.shExecuting curl in curl pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100    61  100    61    0     0   4692      0 --:--:-- --:--:-- --:--:--     0preference => recommendation v1 from '67976848-4l4s7': 4602----------------------------------------------------------------------------Executing curl in customer pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0preference => recommendation v1 from '67976848-4l4s7': 4603100    61  100    61    0     0  10166      0 --:--:-- --:--:-- --:--:-- 10166----------------------------------------------------------------------------/<code>

3. 在Kiali控制檯中可以看到有2個源向preference發送請求。

OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

4. 在第二個窗口執行命令創建Policy,為preference指定Mutual TLS。

文件istiofiles/authentication-enable-tls.yml為preference目標配置了一個Policy,並設置了mtls mode為STRICT,即preference接收的必須的是TLS請求。

<code>apiVersion: "authentication.istio.io/v1alpha1"kind: "Policy"metadata:  name: "preference-mutualtls"spec:  targets:  - name: preference  peers:  - mtls:      mode: STRICT/<code>

執行命令創建policy。

<code>$ oc apply -f istiofiles/authentication-enable-tls.ymlpolicy.authentication.istio.io/preference-mutualtls created$ oc get policyNAME                   AGEpreference-mutualtls   21s/<code>

5. 此時第一個窗口的輸出已經發生變化,從curl和customer的pod發出請求都由於被preference拒絕而失敗了。

<code>Executing curl in curl pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (56) Recv failure: Connection reset by peercommand terminated with exit code 56----------------------------------------------------------------------------Executing curl in customer pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100    95  100    95    0     0  47500      0 --:--:-- --:--:-- --:--:-- 47500upstream connect error or disconnect/reset before headers. reset reason: connection termination----------------------------------------------------------------------------/<code>

6. 在Kiali控制檯中可以看到100%紅色的失敗狀態。

OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

7. 第二個窗口文件查看istiofiles/destination-rule-tls.yml文件,它創建一個DestinationRule對象,並在其中指定通過TLS的ISTIO_MUTUAL方式(在同一istio範圍的客戶端會使用istio提供的證書)訪問preference。

<code>apiVersion: "networking.istio.io/v1alpha3"kind: "DestinationRule"metadata:  name: "preference-destination-rule"spec:  host: "preference"  trafficPolicy:    tls:      mode: ISTIO_MUTUAL/<code>

執行命令創建DestinationRule對象:

<code>$ oc apply -f istiofiles/destination-rule-tls.yml/<code>

8. 在第一個窗口繼續查看輸出,確認現在已經可以從customer向preference正常訪問,不過從curl pod發的請求還不能訪問preference,這是由於curl pod不是以Service Mesh運行的,即它是單獨運行的pod,其中沒有Sidecar容器來自動完成mTLS過程。

<code>Executing curl in curl pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (56) Recv failure: Connection reset by peercommand terminated with exit code 56----------------------------------------------------------------------------Executing curl in customer pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0preference => recommendation v1 from '67976848-4l4s7': 5858100    61  100    61    0     0  12200      0 --:--:-- --:--:-- --:--:-- 12200----------------------------------------------------------------------------/<code>

9. 在Kiali控制檯中也可以看到從customer已經可以正常訪問preference了。在Kiali的Graph界面中可選中Display的Security選項,此時可以看到從customer到preference的訪問連線上有個一“鎖”的圖標,這代表是通過mTLS訪問的。

OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

10. 為了能讓preference也能接收到來自curl pod發過來的請求(由於這個pod不是以istio體系運行的,所以只能發送非證書加密的plaintext明文),需要用istiofiles/authentication-enable-tls-permissive.yml文件更新preference的Policy為PERMISSIVE模式,讓其即可接收mTLS請求,還可接收plaintext明文請求。

文件istiofiles/authentication-enable-tls-permissive.yml:

<code>apiVersion: "authentication.istio.io/v1alpha1"kind: "Policy"metadata:  name: "preference-mutualtls"spec:  targets:  - name: preference  peers:  - mtls:      mode: PERMISSIVE/<code>

執行命令更新Policy對象:

<code>$ oc apply -f istiofiles/authentication-enable-tls-permissive.yml/<code>

11. 在第一個窗口繼續查看輸出,確認現在已經可以從curl和customer向preference正常訪問了。

<code>Executing curl in customer pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0preference => recommendation v1 from '67976848-4l4s7': 6937100    61  100    61    0     0   8714      0 --:--:-- --:--:-- --:--:--  8714----------------------------------------------------------------------------Executing curl in curl pod  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0preference => recommendation v1 from '67976848-4l4s7': 6938100    61  100    61    0     0   4692      0 --:--:-- --:--:-- --:--:--  4692----------------------------------------------------------------------------/<code>

12. 在Kiali控制檯中也可以看到從curl和customer已經可以正常訪問preference了。其中一個使用過“有鎖”的mTLS訪問,另一個是“無鎖”的PlainText明文訪問的。

OpenShift 4 之Istio-Tutorial (8) 在服務之間配置雙向安全傳輸

13. 執行命令,恢復環境。

<code>$ oc delete -f istiofiles/authentication-enable-tls.yml$ oc delete -f istiofiles/destination-rule-tls.yml/<code>


分享到:


相關文章: