自定义Istio Mixer Adapter示例教程(附源码)

栏目: 后端 · 发布时间: 5年前

内容简介:作者:陈洪波快速开始:研究Istio下构建简洁的微服务架构,对Istio的研究也更深入,自定义Mixer Adapter必不可少,以下结合使用场景做一个自定义适配器的实践分享。

作者:陈洪波

快速开始: https://micro-mesh/examples/adapter/auth 源码传送门。

研究Istio下构建简洁的微服务架构,对Istio的研究也更深入,自定义Mixer Adapter必不可少,以下结合使用场景做一个自定义适配器的实践分享。

背景

自定义Istio Mixer Adapter示例教程(附源码)

结合 github.com/hb-go/micro… 的实践场景,需要在 ingressgatewayAPI service 间加入认证&鉴权(JWT&RBAC),自然考虑Istio提供的安全方案,但使用JWT做认证鉴权在后端是无状态的,这样在使用场景上有一定限制,如:

  • 密码修改、终端连接限制等场景下无法踢除

  • 访问控制策略无法实时生效

默认方案只是在一些场景下不合适,根据具体需求考虑。

基于这样的场景可以自定义Adapter来实现,目标:

  • Token-JWT

    • 服务端验证token有效性

    • 应对密码修改、终端数量限制等场景

  • ACL- Casbin

    • 服务端获取用户角色,做API访问控制

    • 用户角色及接口授权策略实时生效

以下示例对token验证、访问控制不做具体设计,重点介绍如何自定义一个 auth-adapter

自定义Adapter介绍

配置关系及执行流程如图:

自定义Istio Mixer Adapter示例教程(附源码)

  • 属性:使用 istioattributesistio/mixer/testdata/config/attributes.yaml

  • 属性与适配器输入映射模板:使用 istioauthorization 模板, istio/mixer/template/authorization/template.yaml ,通过 template.proto 查看协议内容

  • 适配器, micro-mesh/examples/adapter/auth/config/auth-adapter.yaml

    • go generate ./... 自动生成

  • 适配器服务启动配置, micro-mesh/examples/adapter/auth/config/config.proto

  • 适配器服务实例, micro-mesh/examples/adapter/auth/operatorconfig/cluster-service.yaml

  • 适配器配置, micro-mesh/examples/adapter/auth/operatorconfig/operator-cfg.yaml

目录结构

bin                         执行文件 cmd                            └ main.go                 适配器入口 config                      配置协议   ├ adapter.auth.config.pb.html                 #go generate ./... 自动生成   ├ auth-adapter.yaml       适配器描述文件       #go generate ./... 自动生成   ├ config.pb.go                                #go generate ./... 自动生成   ├ config.proto            适配器服务启动配置   └ config.proto_descriptor                     #go generate ./... 自动生成 operatorconfig              k8s配置   ├ attributes.yaml         属性                  #copy istio/mixer/testdata/config/attributes.yaml   ├ cluster-service.yaml    适配器服务实例   ├ operator-cfg.yaml       适配器配置   └ template.yaml           属性与适配器输入模板    #copy istio/mixer/template/authorization/template.yaml testdata                    测试配置   ├ attributes.yaml         属性                  #copy istio/mixer/testdata/config/attributes.yaml   ├ auth-adapter.yaml       适配器描述文件         #copy config/auth-adapter.yaml   ├ operator-cfg.yaml       适配器配置   └ template.yaml           属性与适配器输入模板    #copy istio/mixer/template/authorization/template.yaml auth.go                     适配器服务实现 Dockerfile                  Docker镜像复制代码

有3处与适配器实现相关:

  • 适配器服务启动配置 config/config.proto

  • 适配器服务实现 auth.go

  • 适配器入口 cmd/main.go

接下来使用 micro-mesh/examples/adapter/auth 源码按步骤操作,实现本地及 K8S 环境的测试部署。

步骤

开发环境

  • OSX

  • Go 1.11.1

  • protoc libprotoc 3.6.1

  • Istio 1.0.6

1.Istio源码

mkdir -p $GOPATH/src/istio.io/ cd $GOPATH/src/istio.io/ git clone https://github.com/istio/istio.git复制代码

2.micro-mesh源码

git clone https://github.com/hb-go/micro-mesh.git复制代码

3.Mixer开发工具

# build mixer server & client  cd istio make mixs make mixc复制代码

$GOPATH/out/darwin_amd64/release/ 生成 mixsmixc

4.构建Auth adapter项目

# copy auth adapter example cp {micro-mesh path}/examples/adapter/auth mixer/adapter/auth ​ cd mixer/adapter/auth复制代码

Optional

可以删除 config 目录除 config.proto 外的其他文件,看执行go generate后的结果

go generate ./... go build ./...复制代码

go generate 根据 config/config.proto 以及 auth.go 的注释自动生成 config 目录下的其他文件:

  • adapter.auth.config.pb.html

  • auth-adapter.yaml

  • config.pb.go

  • config.proto_descriptor

根据 auth.go 的以下注释, mixer_codegen.sh 使用 authorization 模板生成 nameauth-adapter 的适配器。

// nolint:lll // Generates the auth adapter's resource yaml. It contains the adapter's configuration, name, supported template // names (metric in this case), and whether it is session or no-session based. //go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -a mixer/adapter/auth/config/config.proto -x "-s=false -n auth-adapter -t authorization"复制代码

5.本地测试

本地测试使用testdata下的配置,其中 operator-cfg.yaml 有几处与正式部署不同:

  • handleraddress 使用本地服务 "[::]:44225"

  • 为了方便测试 instanceparams 参数以及 rulemath 条件做了简化

# 启动适配器服务 go run cmd/main.go 44225 ​ # 使用testdata下配置启动mixer server $GOPATH/out/darwin_amd64/release/mixs server \ --configStoreURL=fs://$GOPATH/src/istio.io/istio/mixer/adapter/auth/testdata \ --log_output_level=attributes:debug ​ # 测试Adapter是否生效 $GOPATH/out/darwin_amd64/release/mixc check -s request.host="localhost" --stringmap_attributes "request.headers=x-custom-token:efg" # Check RPC completed successfully. Check status was PERMISSION_DENIED (mm-example-auth.handler.istio-system:Unauthorized...) ​ $GOPATH/out/darwin_amd64/release/mixc check -s request.host="localhost" --stringmap_attributes "request.headers=x-custom-token:abc" # Check RPC completed successfully. Check status was OK复制代码

NOTE:出现预期结果不一致可能是由于mixer cache导致 Valid use count: 10000, valid duration: 9.726875254s ,请参考Istio Mixer Cache系列文章了解。

6.打包镜像

# build执行文件
CGO_ENABLED=0 GOOS=linux \
    go build -a -installsuffix cgo -v -o bin/auth ./cmd/
    
# docker镜像
docker build -t hbchen/micro-mesh-example-adapter-auth:v0.0.1 .
docker push hbchen/micro-mesh-example-adapter-auth:v0.0.1
复制代码

7.Istio环境部署

部署环境

  • GKE 1.11.7-gke.4

  • Istio 1.0.0

# 属性、模板
# attributes.yaml -> istio/mixer/testdata/config/attributes.yaml 
# template.yaml -> istio/mixer/template/authorization/template.yaml
kubectl apply -f examples/adapter/auth/testdata/attributes.yaml -f examples/adapter/auth/testdata/template.yaml

# 适配器
kubectl apply -f examples/adapter/auth/config/auth-adapter.yaml
复制代码

这里是以 micro-mesh 示例为基础的配置,如果使用 bookinfo 或者自己的服务需要做相应的修改

operator-cfg.yaml 与本地测试配置不同:

  • handleraddress 使用集群服务 "mm-example-auth-adapter-service:44225"

  • instanceparams 根据 authorization 模板及 auth-adapter 服务的需求配置

  • rulematch 条件使用 destination.service == "mm-example-api.default.svc.cluster.local" ,仅对 mm-example-api 服务生效

# 适配器服务实例部署
kubectl apply -f examples/adapter/auth/operatorconfig/cluser-service.yaml

# 适配器配置
kubectl apply -f examples/adapter/auth/operatorconfig/operator-cfg.yaml
复制代码

8.Istio环境部署测试

如果没有开Gateway的JWT验证可以忽略 Authorization ,其实做了自定义Auth后是多余的:joy:

TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.1/security/tools/jwt/samples/demo.jwt -s)

curl -H "Authorization: Bearer $TOKEN" -H "x-custom-token: efg" -X GET http://35.193.180.18/v1/example/call/Hobo
curl -H "Authorization: Bearer $TOKEN" -H "x-custom-token: abc" -X GET http://35.193.180.18/v1/example/call/Hobo
复制代码

参考

自定义Istio Mixer Adapter示例教程(附源码)


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

小米生态链战地笔记

小米生态链战地笔记

小米生态链谷仓学院 / 中信出版集团 / 2017-5 / 56.00

2013年下半年,小米开始做一件事,就是打造一个生态链布局IoT(物联网);2016年年底,小米生态链上已经拥有了77家企业,生态链企业整体销售额突破100亿元。这3年,是小米生态链快速奔跑的3年,也是小米在商场中不断厮杀着成长的3年。 3年,77家生态链企业,16家年销售额破亿,4家独角兽公司,边实战,边积累经验。 小米生态链是一个基于企业生态的智能硬件孵化器。过去的3年中,在毫无先......一起来看看 《小米生态链战地笔记》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具