mirror of https://github.com/istio/istio.io.git
				
				
				
			zh: content/docs/tasks/security/role-based-access-control (#2631)
* zh: content/docs/tasks/security/role-based-access-control Keep consistent with the latest version Signed-off-by: ylf <1045438139@qq.com> * re-lint * re-lint * re-lint * zh: content/docs/tasks/security/role-based-access-control fix link * re-lint * zh: content/docs/tasks/security/role-based-access-control Fix legacy issues fix blank * re-lint * re-lint * re-lint * re-lint * re-lint * re-lint * zh: content/docs/tasks/security/role-based-access-control Fix md
This commit is contained in:
		
							parent
							
								
									caa3a8d1ac
								
							
						
					
					
						commit
						b39f584ca8
					
				|  | @ -7,7 +7,7 @@ keywords: [安全,访问控制,rbac,鉴权] | |||
| 
 | ||||
| 在服务网格中为服务进行授权控制(基于角色的访问控制)时,会涉及到本例中包含的一系列操作。在[授权](/zh/docs/concepts/security/#授权和鉴权)一节中讲述了更多这方面的内容,并且还有一个基本的 Istio 安全方面的教程。 | ||||
| 
 | ||||
| ## 开始之前 | ||||
| ## 准备任务 | ||||
| 
 | ||||
| 本文活动开始之前,我们有如下假设: | ||||
| 
 | ||||
|  | @ -28,7 +28,7 @@ keywords: [安全,访问控制,rbac,鉴权] | |||
|     $ kubectl apply -f <(istioctl kube-inject -f @samples/bookinfo/platform/kube/bookinfo-add-serviceaccount.yaml@) | ||||
|     {{< /text >}} | ||||
| 
 | ||||
| > 如果使用的命名空间不是 `default`,就应改用 `istioctl -n namespace ...` 来指定命名空间。 | ||||
| > 如果使用的命名空间不是 `default`,就应改用 `kubectl -n namespace ...` 来指定命名空间。 | ||||
| 
 | ||||
| * Istio 1.0 中的 RBAC 有较大更新。请确认在继续之前,已经清理了所有现存 RBAC 规则。 | ||||
| 
 | ||||
|  | @ -56,6 +56,195 @@ keywords: [安全,访问控制,rbac,鉴权] | |||
| 
 | ||||
|     如果刷新几次,会发现 `productpage` 在切换使用不同的 `reviews` 版本(红星、黑星、无)。 | ||||
| 
 | ||||
| ## 授权许可模式 | ||||
| 
 | ||||
| 本节介绍如何在以下两种情况下使用授权许可模式: | ||||
| 
 | ||||
|     * 在未经授权的环境中,测试是否可以安全地启用授权。 | ||||
|     * 在已启用授权的环境中,测试添加新授权策略是否安全。 | ||||
| 
 | ||||
| ### 测试启用全局授权是否安全 | ||||
| 
 | ||||
| 此任务说明如何使用授权许可模式来测试是否可以安全地启用全局授权。 | ||||
| 
 | ||||
| 在开始之前,请确保您已完成[准备任务](#准备任务)。 | ||||
| 
 | ||||
| 1.  将全局授权配置设置为许可模式。 | ||||
| 
 | ||||
|     运行以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ kubectl apply -f - <<EOF | ||||
|     apiVersion: "rbac.istio.io/v1alpha1" | ||||
|     kind: RbacConfig | ||||
|     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.  将流量发送到示例应用程序。 | ||||
| 
 | ||||
|     对于 Bookinfo 示例,请在 Web 浏览器中访问 `http://$GATEWAY_URL/productpage` 或发出以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ curl http://$GATEWAY_URL/productpage | ||||
|     {{< /text >}} | ||||
| 
 | ||||
|     将您的浏览器指向 Bookinfo `productpage`(`http://$GATEWAY_URL/productpage`),您应该看到一切正常。 | ||||
| 
 | ||||
| 1.  验证已创建日志流并检查 `permissiveResponseCode`。 | ||||
| 
 | ||||
|     在 Kubernetes 环境中,搜索日志以查找 istio-telemetry pods,如下所示: | ||||
| 
 | ||||
|     {{< 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。 | ||||
|     用户在将全局授权配置从 `PERMISSIVE` 模式切换到 `ENFORCED` 模式后将看到 `permissiveResponseCode` 是 403, | ||||
|     这表示全局授权配置在滚动到生产后将按预期工作。 | ||||
| 
 | ||||
| 1.  在生产中推出新的授权策略之前,请以许可模式应用它。 | ||||
|     `注意`,当全局授权配置处于许可模式时,默认情况下所有策略都将处于许可模式。 | ||||
| 
 | ||||
|     运行以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/productpage-policy.yaml@ | ||||
|     {{< /text >}} | ||||
| 
 | ||||
| 1.  再次向示例应用程序发送流量。 | ||||
| 
 | ||||
|     对于 Bookinfo 示例,请在 Web 浏览器中访问 `http://$GATEWAY_URL/productpage` 或发出以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ curl http://$GATEWAY_URL/productpage | ||||
|     {{< /text >}} | ||||
| 
 | ||||
|     将您的浏览器指向 Bookinfo `productpage`(`http://$GATEWAY_URL/productpage`),您应该看到一切正常。 | ||||
| 
 | ||||
| 1.  验证已创建日志流并检查 `permissiveResponseCode`。 | ||||
| 
 | ||||
|     在 Kubernetes 环境中,搜索日志以查找 istio-telemetry pods,如下所示: | ||||
| 
 | ||||
|     {{< 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。 | ||||
|     对于 `productpage` 页面服务,`permissiveResponseCode` 为 200,对于 `ratings` 和 `reviews` 服务为 403, | ||||
|     这是用户在将策略模式从 `PERMISSIVE` 模式切换到 `ENFORCED` 模式后将看到的内容; | ||||
|     结果与[步骤1](#第一步-允许到-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 授权](#启用-Istio-授权)下面打开授权是安全的。 | ||||
| 
 | ||||
| ### 在滚动到生产之前,测试新的授权策略按预期工作 | ||||
| 
 | ||||
| 此任务说明如何使用授权许可模式来测试新授权策略在已启用授权的环境中按预期工作。 | ||||
| 
 | ||||
| 在开始之前,请确保您已完成[步骤1](#第一步-允许到-productpage-服务的访问)。 | ||||
| 
 | ||||
| 1.  在应用新策略之前,通过将其模式设置为permissive来测试它: | ||||
| 
 | ||||
|     运行以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/details-reviews-policy-permissive.yaml@ | ||||
|     {{< /text >}} | ||||
| 
 | ||||
|     该策略与[允许访问详细信息和评论服务](#第二步-允许对-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 >}} | ||||
| 
 | ||||
|     将您的浏览器指向 Bookinfo `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.  将流量发送到示例应用程序。 | ||||
| 
 | ||||
|     对于 Bookinfo 示例,请在 Web 浏览器中访问 `http://$GATEWAY_URL/productpage` 或发出以下命令: | ||||
| 
 | ||||
|     {{< text bash >}} | ||||
|     $ curl http://$GATEWAY_URL/productpage | ||||
|     {{< /text >}} | ||||
| 
 | ||||
| 1.  验证日志并再次检查 `permissiveResponseCode`。 | ||||
| 
 | ||||
|     在 Kubernetes 环境中,搜索日志以查找 istio-telemetry pods,如下所示: | ||||
| 
 | ||||
|     {{< 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 授权 | ||||
| 
 | ||||
| 运行下面的命令,为 `default` 命名空间启用 Istio 授权: | ||||
|  | @ -64,8 +253,6 @@ keywords: [安全,访问控制,rbac,鉴权] | |||
| $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/rbac-config-ON.yaml@ | ||||
| {{< /text >}} | ||||
| 
 | ||||
| > 如果前面已经创建了冲突的规则,应该使用 `istioctl replace` 替代 `istioctl create`。 | ||||
| 
 | ||||
| 用浏览器再次打开 `productpage` (`http://$GATEWAY_URL/productpage`),这次会看到 `RBAC: access denied`。Istio 的鉴权行为是“缺省拒绝”的,也就是说必须要显式的进行授权,才能对服务进行访问。 | ||||
| 
 | ||||
| > 缓存和传播可能会造成一定的延迟。 | ||||
|  | @ -194,9 +381,9 @@ $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/productpage-policy.yaml@ | |||
| 
 | ||||
| > 缓存和传播可能会造成一定的延迟。 | ||||
| 
 | ||||
| ### 第二步,允许对 `details` 和 `reviews` 服务的访问。 | ||||
| ### 第二步,允许对 `details` 和 `reviews` 服务的访问 | ||||
| 
 | ||||
| 创建一条策略,让 `productpage` 服务能够读取 `details` 和 `reviews` 服务。注意在[开始之前](#开始之前)中,我们给 `productpage` 服务创建了一个命名为 `bookinfo-productpage` 的 Service account,它就是 `productpage` 服务的认证 ID。 | ||||
| 创建一条策略,让 `productpage` 服务能够读取 `details` 和 `reviews` 服务。注意在[准备任务](#准备任务)中,我们给 `productpage` 服务创建了一个命名为 `bookinfo-productpage` 的 Service account,它就是 `productpage` 服务的认证 ID。 | ||||
| 
 | ||||
| 运行下面的命令: | ||||
| 
 | ||||
|  | @ -242,7 +429,7 @@ $ kubectl apply -f @samples/bookinfo/platform/kube/rbac/details-reviews-policy.y | |||
| 
 | ||||
| ### 第三步,允许对 `ratings` 服务的访问 | ||||
| 
 | ||||
| 接下来新建一条策略,允许 `reviews` 服务对 `ratings` 发起读取访问。注意,我们在[开始之前](#开始之前)步骤里为 `reviews` 服务创建了 Service account `bookinfo-reviews`,这个账号就是 `reviews` 服务的认证凭据。 | ||||
| 接下来新建一条策略,允许 `reviews` 服务对 `ratings` 发起读取访问。注意,我们在[准备任务](#准备任务)步骤里为 `reviews` 服务创建了 Service account `bookinfo-reviews`,这个账号就是 `reviews` 服务的认证凭据。 | ||||
| 
 | ||||
| 下面的命令会创建一条允许 `reviews` 服务读取 `ratings` 服务的策略。 | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue