zh: content_zh/docs/tasks/security/authz-http (#3403)

update
This commit is contained in:
flydragon 2019-02-27 20:38:31 +08:00 committed by istio-bot
parent ef69559874
commit 9872599621
1 changed files with 5 additions and 180 deletions

View File

@ -1,7 +1,7 @@
---
title: HTTP 服务的访问控制
description: 展示为 HTTP 服务设置基于角色的访问控制方法。
weight: 40
weight: 10
keywords: [security,access-control,rbac,authorization]
---
@ -12,8 +12,11 @@ Istio 采用基于角色的访问控制方式,本文内容涵盖了为 HTTP
任务中的活动做出如下假设:
* 理解[访问控制](/zh/docs/concepts/security/#授权和鉴权)概念。
* 按照[快速开始](/zh/docs/setup/kubernetes/quick-start/)的步骤,在 Kubernetes 上安装了 Istio 并**启用认证功能**,本教程依赖双向 TLS 功能,在[安装步骤](/zh/docs/setup/kubernetes/quick-start/#安装步骤)中介绍了启用双向 TLS 的方法。
* 部署 [Bookinfo](/zh/docs/examples/bookinfo/) 示例应用。
* 这一任务中,借助 Service Account 在网格中提供加密的访问控制能力。为了给不同的微服务赋予不同的访问权限,就需要创建一些 Service Account 用来运行 Bookinfo 中的微服务。
运行命令完成两个目标:
@ -56,184 +59,6 @@ Istio 采用基于角色的访问控制方式,本文内容涵盖了为 HTTP
多次刷新该页面,可能会看到页面中显示了 “Book Reviews” 的不同版本,红星、黑星和无星三个版本会轮换展示。
## 宽松模式PERMISSIVE Mode
宽松模式是 Istio 1.1 中的一个实验功能。因此将来可能会发生变更。如果不想尝试这一功能,可以直接跳到[启用 Istio 访问控制](#enable-Istio-access-control)一节。
本节展示了在两种场景中启用宽松模式的方法:
* 在环境中没有访问控制的情况下,测试启用访问控制是否安全。
* 环境中已开启访问控制,测试添加新的策略是否安全。
### 测试全局开启访问控制是否安全
这一节内容演示的是利用宽松模式来对启用全局访问控制的可行性进行验证的方法。
首先确定一下是否完成了[开始之前](#开始之前)所述操作。
1. 将全局访问控制方式设置为宽松模式:
运行下列命令:
{{< text bash >}}
$ kubectl apply -f - <<EOF
apiVersion: "rbac.istio.io/v1alpha1"
kind: ClusterRbacConfig
metadata:
name: default
spec:
mode: 'ON_WITH_INCLUSION'
inclusion:
namespaces: ["default"]
enforcement_mode: PERMISSIVE
EOF
{{< /text >}}
浏览 Bookinfo 的 `productpage``http://$GATEWAY_URL/productpage`),会看到一切功能都和[开始之前](#开始之前)步骤中一样正常运行。
1. 利用 YAML 文件设置宽松模式的指标收集。
运行如下命令:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/platform/kube/rbac/rbac-permissive-telemetry.yaml@
logentry.config.istio.io/rbacsamplelog created
stdio.config.istio.io/rbacsamplehandler created
rule.config.istio.io/rabcsamplestdio created
{{< /text >}}
1. 向示例应用发送流量。
使用浏览器浏览 `http://$GATEWAY_URL/productpage` 或者执行下面的命令:
{{< text bash >}}
$ curl http://$GATEWAY_URL/productpage
{{< /text >}}
1. 查看日志,并检查 `permissiveResponseCode`
Kubernetes 环境中,可以用下列操作搜索 `istio-telemetry` 的 Pod 日志:
{{< text bash json >}}
$ kubectl -n istio-system logs -l istio-mixer-type=telemetry -c mixer | grep \"instance\":\"rbacsamplelog.logentry.istio-system\"
{"level":"warn","time":"2018-08-30T21:53:42.059444Z","instance":"rbacsamplelog.logentry.istio-system","destination":"ratings","latency":"9.158879ms","permissiveResponseCode":"403","permissiveResponsePolicyID":"","responseCode":200,"responseSize":48,"source":"reviews","user":"cluster.local/ns/default/sa/bookinfo-reviews"}
{"level":"warn","time":"2018-08-30T21:53:41.037824Z","instance":"rbacsamplelog.logentry.istio-system","destination":"reviews","latency":"1.091670916s","permissiveResponseCode":"403","permissiveResponsePolicyID":"","responseCode":200,"responseSize":379,"source":"productpage","user":"cluster.local/ns/default/sa/bookinfo-productpage"}
{"level":"warn","time":"2018-08-30T21:53:41.019851Z","instance":"rbacsamplelog.logentry.istio-system","destination":"productpage","latency":"1.112521495s","permissiveResponseCode":"403","permissiveResponsePolicyID":"","responseCode":200,"responseSize":5723,"source":"istio-ingressgateway","user":"cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account"}
{{< /text >}}
上面的遥测日志中,`responseCode` 显示为 200就是用户目前所见`permissiveResponseCode` 的 403表示如果将全局访问控制模式从 `PERMISSIVE` 转向 `ENFORCE` 模式之后会发生的结果,这说明全局访问控制配置在实施之后会产生我们想要的结果。
1. 在我们向生产环境推出新的访问控制策略之前,首先使用宽松模式。
**注意**,全局访问控制配置设置为宽松模式的情况下,所有的策略都缺省为宽松模式。
运行下列命令:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/platform/kube/rbac/productpage-policy.yaml@
{{< /text >}}
1. 再次向示例应用发送流量。
使用浏览器浏览 `http://$GATEWAY_URL/productpage` 或者执行下面的命令:
{{< text bash >}}
$ curl http://$GATEWAY_URL/productpage
{{< /text >}}
1. 再一次查看日志,并检查 `permissiveResponseCode`
Kubernetes 环境中,可以用下列操作搜索 `istio-telemetry` 的 Pod 日志:
{{< text bash json >}}
$ kubectl -n istio-system logs -l istio-mixer-type=telemetry -c mixer | grep \"instance\":\"rbacsamplelog.logentry.istio-system\"
{"level":"warn","time":"2018-08-30T21:55:53.590430Z","instance":"rbacsamplelog.logentry.istio-system","destination":"ratings","latency":"4.415633ms","permissiveResponseCode":"403","permissiveResponsePolicyID":"","responseCode":200,"responseSize":48,"source":"reviews","user":"cluster.local/ns/default/sa/bookinfo-reviews"}
{"level":"warn","time":"2018-08-30T21:55:53.565914Z","instance":"rbacsamplelog.logentry.istio-system","destination":"reviews","latency":"32.97524ms","permissiveResponseCode":"403","permissiveResponsePolicyID":"","responseCode":200,"responseSize":379,"source":"productpage","user":"cluster.local/ns/default/sa/bookinfo-productpage"}
{"level":"warn","time":"2018-08-30T21:55:53.544441Z","instance":"rbacsamplelog.logentry.istio-system","destination":"productpage","latency":"57.800056ms","permissiveResponseCode":"200","permissiveResponsePolicyID":"productpage-viewer","responseCode":200,"responseSize":5723,"source":"istio-ingressgateway","user":"cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account"}
{{< /text >}}
上面的遥测日志中,`responseCode` 显示为 200就是用户目前所见`permissiveResponseCode` 在 productpage 服务中是 200ratings 和 reviews 服务中是 403这代表了访问控制策略从 `PERMISSIVE` 模式改为 `ENFORCED` 模式时会发生的后果;这一结果是符合[第一步](#第一步-开放到-productpage-服务的访问)中所述内容的。
1. 删除宽松模式的相关 yaml 文件:
{{< text bash >}}
$ kubectl delete -f @samples/bookinfo/platform/kube/rbac/productpage-policy.yaml@
$ kubectl delete -f @samples/bookinfo/platform/kube/rbac/rbac-config-on-permissive.yaml@
$ kubectl delete -f @samples/bookinfo/platform/kube/rbac/rbac-permissive-telemetry.yaml@
{{< /text >}}
1. 现在我们已经完成了对开启访问控制后果的验证,可以按照[启用 Istio 访问控制](#enable-Istio-access-control)的步骤来继续操作了。
### 测试添加新的策略是否安全
这一节的内容会在在已经启用访问控制的网格中完成,展示如何使用宽松访问控制模式验证新的访问控制策略。
开始之前,请确认已经完成了[第一步](#第一步-开放到-productpage-服务的访问)中所述操作。
1. 应用新策略之前,可以先将模式设置为宽松,方便进行测试:
运行下列命令:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/platform/kube/rbac/details-reviews-policy-permissive.yaml@
{{< /text >}}
这一策略和[开放到 details 和 reviews 服务的访问](#第二步-开放到-details-和-reviews-服务的访问)步骤中的策略基本是一致的,只不过在 ServiceRoleBinding 中设置了 `PERMISSIVE` 模式。
{{< text yaml >}}
apiVersion: "rbac.istio.io/v1alpha1"
kind: ServiceRoleBinding
metadata:
name: bind-details-reviews
namespace: default
spec:
subjects:
- user: "cluster.local/ns/default/sa/bookinfo-productpage"
roleRef:
kind: ServiceRole
name: "details-reviews-viewer"
mode: PERMISSIVE
{{< /text >}}
用浏览器打开 `productpage``http://$GATEWAY_URL/productpage`),会看到错误信息:`Error fetching product details` 和 `Error fetching product reviews`,因为设置了 `PERMISSIVE`,这些错误是符合预期的。
1. 应用 YAML 文件,用于收集宽松模式下的监控指标。
运行下列命令:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/platform/kube/rbac/rbac-permissive-telemetry.yaml@
{{< /text >}}
1. 向应用发送流量。
使用浏览器浏览 `http://$GATEWAY_URL/productpage` 或者执行下面的命令:
{{< text bash >}}
$ curl http://$GATEWAY_URL/productpage
{{< /text >}}
1. 查看日志,检查 `permissiveResponseCode`
在 Kubernetes 环境中,查看 `istio-telemetry` pod 的日志:
{{< text bash json >}}
$ kubectl -n istio-system logs -l istio-mixer-type=telemetry -c mixer | grep \"instance\":\"rbacsamplelog.logentry.istio-system\"
{"level":"warn","time":"2018-08-30T22:59:42.707093Z","instance":"rbacsamplelog.logentry.istio-system","destination":"details","latency":"423.381µs","permissiveResponseCode":"200","permissiveResponsePolicyID":"details-reviews-viewer","responseCode":403,"responseSize":19,"source":"productpage","user":"cluster.local/ns/default/sa/bookinfo-productpage"}
{"level":"warn","time":"2018-08-30T22:59:42.763423Z","instance":"rbacsamplelog.logentry.istio-system","destination":"reviews","latency":"237.333µs","permissiveResponseCode":"200","permissiveResponsePolicyID":"details-reviews-viewer","responseCode":403,"responseSize":19,"source":"productpage","user":"cluster.local/ns/default/sa/bookinfo-productpage"}
{{< /text >}}
上面的遥测日志可以看到ratings 和 reviews 服务的 `responseCode` 是 403也就是页面看到的错误原因。而 ratings 和 reviews 的 `permissiveResponseCode` 是 200这代表用户将策略模式从 `PERMISSIVE` 切换为 `ENFORCED` 之后会看到的结果,也就是说新策略推出到生产环境是会按照预想条件工作的。
1. 移除宽松模式相关的 YAML 文件:
{{< text bash >}}
$ kubectl delete -f @samples/bookinfo/platform/kube/rbac/details-reviews-policy-permissive.yaml@
$ kubectl delete -f @samples/bookinfo/platform/kube/rbac/rbac-permissive-telemetry.yaml@
{{< /text >}}
1. 现在我们知道新策略会按设计目标工作,就可以安全的按照[步骤 2](#第二步-开放到-details-和-reviews-服务的访问) 的步骤来应用策略了。
## 启用 Istio 访问控制 {#enable-Istio-access-control}
运行下面的命令,在 `default` 命名空间中启用 Istio 访问控制:
@ -360,7 +185,7 @@ $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/productpage-policy.yaml@
apiVersion: "rbac.istio.io/v1alpha1"
kind: ServiceRoleBinding
metadata:
name: bind-productpager-viewer
name: bind-productpage-viewer
namespace: default
spec:
subjects: