九 Istio:istio安全之授權( 三 )

apiVersion: v1 kind: ServiceAccount metadata:name: web-frontend --- apiVersion: apps/v1 kind: Deployment metadata:name: web-frontendlabels:app: web-frontend spec:replicas: 1selector:matchLabels:app: web-frontendtemplate:metadata:labels:app: web-frontendversion: v1spec:serviceAccountName: web-frontendcontainers:- image: gcr.io/tetratelabs/web-frontend:1.0.0imagePullPolicy: Alwaysname: webports:- containerPort: 8080env:- name: CUSTOMER_SERVICE_URLvalue: 'http://customers.default.svc.cluster.local' --- kind: Service apiVersion: v1 metadata:name: web-frontendlabels:app: web-frontend spec:selector:app: web-frontendports:- port: 80name: httptargetPort: 8080 --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: web-frontend spec:hosts:- '*'gateways:- gatewayhttp:- route:- destination:host: web-frontend.default.svc.cluster.localport:number: 80將上述 YAML 保存為 web-frontend.yaml,并使用 kubectl apply -f web-frontend.yaml 創建資源 。
最后,我們將部署 customers v1 服務 。
apiVersion: v1 kind: ServiceAccount metadata:name: customers-v1 --- apiVersion: apps/v1 kind: Deployment metadata:name: customers-v1labels:app: customersversion: v1 spec:replicas: 1selector:matchLabels:app: customersversion: v1template:metadata:labels:app: customersversion: v1spec:serviceAccountName: customers-v1containers:- image: gcr.io/tetratelabs/customers:1.0.0imagePullPolicy: Alwaysname: svcports:- containerPort: 3000 --- kind: Service apiVersion: v1 metadata:name: customerslabels:app: customers spec:selector:app: customersports:- port: 80name: httptargetPort: 3000 --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata:name: customers spec:hosts:- 'customers.default.svc.cluster.local'http:- route:- destination:host: customers.default.svc.cluster.localport:number: 80將上述內容保存為 customers-v1.yaml,并使用 kubectl apply -f customers-v1.yaml 創建部署和服務 。如果我們打開 GATEWAY_URL,應該會顯示帶有 customers v1 服務數據的 web 前端頁面 。
讓我們先創建一個授權策略 , 拒絕 default 命名空間的所有請求 。
apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata:name: deny-allnamespace: default spec:{}將上述內容保存為 deny-all.yaml,并使用 kubectl apply -f deny-all.yaml 創建該策略 。
如果我們嘗試訪問 GATEWAY_URL , 我們將得到以下響應 。
RBAC: access denied同樣,如果我們試圖在集群內運行一個 Pod,并從 default 命名空間內向 Web 前端或 customers 服務提出請求 , 我們會得到同樣的錯誤 。
讓我們試試吧 。
$ kubectl run curl --image=radial/busyboxplus:curl -i --tty If you don't see a command prompt, try pressing enter. [ root@curl:/ ]$ curl customers RBAC: access denied [ root@curl:/ ]$ curl web-frontend RBAC: access denied [ root@curl:/ ]$在這兩種情況下,我們都得到了拒絕訪問的錯誤 。
我們要做的第一件事是使用 ALLOW 動作,允許從入口網關向 web-frontend 應用程序發送請求 。在規則中,我們指定了入口網關運行的源命名空間(istio-system)和入口網關的服務賬戶名稱 。
apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata:name: allow-ingress-frontendnamespace: default spec:selector:matchLabels:app: web-frontendaction: ALLOWrules:- from:- source:namespaces: ["istio-system"]- source:principals: ["cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account"]將上述內容保存為 allow-ingress-frontend.yaml , 并使用 kubectl apply -f allow-ingress-frontend.yaml 創建策略 。
如果我們嘗試從我們的主機向GATEWAY_URL 發出請求,這次我們會得到一個不同的錯誤 。
$ curl http://$GATEWAY_URL "Request failed with status code 403"

請注意,策略需要幾秒鐘才能分發到所有代理,所以你可能仍然會看到 RBAC:access denied 的消息,時間為幾秒鐘 。
這個錯誤來自 customers 服務——記得我們允許調用 Web 前端 。然而 , web-frontend仍然不能調用 customers 服務 。
如果我們回到我們在集群內運行的 curl Pod,嘗試請求 http://web-frontend,我們會得到一個 RBAC 錯誤 。DENY 策略是有效的,我們只允許從入口網關進行調用 。
當我們部署 Web 前端時,我們也為 Pod 創建了一個服務賬戶(否則 , 命名空間中的所有 Pod 都被分配了默認的服務賬戶) ?,F在我們可以使用該服務賬戶來指定 customers 服務調用的來源 。

推薦閱讀