Setting up Basic Access Control

这个task展示如何使用k8s labels控制访问服务。

Before you begin

  • 在k8s集群上安装Istio
  • 部署 Bookinfo
  • 初始化应用版本,路由来自 reviews 服务v2版本的测试用户 “jason”的请求直接以及v3版本的其他用户请求。
istioctl create -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml
istioctl create -f samples/bookinfo/kube/route-rule-reviews-v3.yaml

注意:如果你在之前的tasks中有冲突的rules,使用 istioctl replace 来替代istioctl create

注意: 如果你使用了非 default的其他命名空间,使用 istioctl -n namespace ... 指定命名空间。

Access control using denials

使用Istio,你可以基于Mixer中可用的任何属性来控制对服务的访问。这种简单的访问控制形式基于使用Mixer选择器有条件的拒绝请求。
想一下 Bookinfo 示例, ratings 服务由 reviews 服务的多个版本进行访问。我们将切断reviews 服务的 v3 版本的访问。

1.在你的浏览器访问Bookinfo productpage (http://$GATEWAY_URL/productpage)。
如果你以用户 “jason”登陆,你将会在每条书评看到黑星评级,说明 ratings 服务被 reviews 服务的 “v2” 版本调用。
如果你以其他用户登陆(或登出),你将在每条书评看到红星评级,说明 ratings 服务被reviews 服务的“v3” 版本调用。

2.明确禁止访问 reviewsv3 版本。
运行以下命令以设置拒绝规则以及handler和实例。

istioctl create -f samples/bookinfo/kube/mixer-rule-deny-label.yaml

你将会看到输出类似:

Created config denier/default/denyreviewsv3handler at revision 2882105
Created config checknothing/default/denyreviewsv3request at revision 2882106
Created config rule/default/denyreviewsv3 at revision 2882107

注意下面在denyreviewsv3 规则中的内容:

match: destination.labels["app"] == "ratings" && source.labels["app"]=="reviews" && source.labels["version"] == "v3"

它匹配来自带有 v3 标签的reviews 服务到 ratings 服务的请求。
这个规则使用 denier 适配器来拒绝来自reviews 服务的 v3 版本的请求。这个适配器总是用一个前置状态码和信息来拒绝请求。这个状态码和信息在denier 适配器配置中被指定。

3.在你的浏览器中刷新 productpage
如果你登出或者以非 “jason” 用户登陆,你将不再看到红色星评,因为 reviews:v3 服务已经被拒绝访问ratings 服务。相反,如果你使用 “jason” (the reviews:v2 user)用户登陆,你将继续看到黑色星评。

Access control using whitelists

Istio也支持基于属性的白名单和黑名单。下面白名单的配置和上节 denier 配置相同。规则有效拒绝来自 reviews 服务的 v3 版本的请求。

1.移除你在上节添加的denier 配置。

istioctl delete -f samples/bookinfo/kube/mixer-rule-deny-label.yaml

2.确认当你没有登陆时访问Bookinfo productpage (http://$GATEWAY_URL/productpage)会看到红星。当你执行完下面的步骤,除非你使用“jason”登陆,否则你将不再能看到星级。

3.创建 list 适配器的配置,列出 v1, v2 版本。保存下面YAML段落到 whitelist-handler.yaml

apiVersion: config.istio.io/v1alpha2
kind: listchecker
metadata:
  name: whitelist
spec:
  # providerUrl: ordinarily black and white lists are maintained
  # externally and fetched asynchronously using the providerUrl.
  overrides: ["v1", "v2"]  # overrides provide a static list
  blacklist: false

然后运行

istioctl create -f whitelist-handler.yaml

4.创建 listentry 模板实例来提取version标签。将下面的YAML片段保存为 appversion-instance.yaml

apiVersion: config.istio.io/v1alpha2
kind: listentry
metadata:
  name: appversion
spec:
  value: source.labels["version"]

然后运行

istioctl create -f appversion-instance.yaml

5.使用 whitelist 检查ratings 服务。将如下YAML片段保存为 checkversion-rule.yaml

apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
  name: checkversion
spec:
  match: destination.labels["app"] == "ratings"
  actions:
  - handler: whitelist.listchecker
    instances:
    - appversion.listentry

然后运行

istioctl create -f checkversion-rule.yaml

6.确认当你不登录访问the Bookinfo productpage (http://$GATEWAY_URL/productpage) 时看不到星级。确认当你以“jason” 登陆后看到黑星。

Cleanup

  • 移除mixer配置
istioctl delete -f checkversion-rule.yaml
istioctl delete -f appversion-instance.yaml
istioctl delete -f whitelist-handler.yaml
  • 移除应用路由规则
istioctl delete -f samples/bookinfo/kube/route-rule-reviews-test-v2.yaml
istioctl delete -f samples/bookinfo/kube/route-rule-reviews-v3.yaml
  • 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。

猜你喜欢

转载自blog.csdn.net/ybt_c_index/article/details/80359855