阿里云服务网格ASM之扩展能力(2):在ASM中支持自定义外部授权

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 前面的系列文档中介绍了如何创建服务网格ASM实例,并介绍了如何将一个应用示例部署到 ASM 实例中,本文在此基础上介绍如何在ASM中支持自定义外部授权。

本系列文章讲讲述阿里云服务网格ASM的一些扩展能力:

欢迎扫码入群进一步交流:
image

背景信息

服务网格中服务间存在着调用请求,这些请求的授权决定可以由运行在网格外部的gRPC服务处理。外部授权过滤器调用授权服务以检查传入请求是否被授权。如果在过滤器中将该请求视为未授权,则该请求将被403(禁止)响应拒绝。

建议将这些授权过滤器配置为过滤器链中的第一个过滤器,以便在其余过滤器处理请求之前对请求进行授权。

关于Envoy的外部授权的其他内容,可以参见External Authorization

gRPC外部服务需要相应的接口,实现该Check()方法。具体来说,external_auth.proto 定义了请求响应上下文:

// A generic interface for performing authorization check on incoming
// requests to a networked service.
service Authorization {
  // Performs authorization check based on the attributes associated with the
  // incoming request, and returns status `OK` or not `OK`.
  rpc Check(v2.CheckRequest) returns (v2.CheckResponse);
}

实现外部授权服务

基于上述gRPC服务接口定义,实现示例外部授权服务如下,在Check()方法中判断Bearer Token值是否以asm-开头。事实上,只要符合该接口定义,可以添加更为复杂的处理逻辑进行检查。

package main

import (
    "context"
    "log"
    "net"
    "strings"

    "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
    auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v2"
    envoy_type "github.com/envoyproxy/go-control-plane/envoy/type"
    "github.com/gogo/googleapis/google/rpc"
    "google.golang.org/grpc"
)

// empty struct because this isn't a fancy example
type AuthorizationServer struct{}

// inject a header that can be used for future rate limiting
func (a *AuthorizationServer) Check(ctx context.Context, req *auth.CheckRequest) (*auth.CheckResponse, error) {
    authHeader, ok := req.Attributes.Request.Http.Headers["authorization"]
    var splitToken []string
    if ok {
        splitToken = strings.Split(authHeader, "Bearer ")
    }
    if len(splitToken) == 2 {
        token := splitToken[1]
        // Normally this is where you'd go check with the system that knows if it's a valid token.

        if strings.HasPrefix(token, "asm-") {
            return &auth.CheckResponse{
                Status: &rpc.Status{
                    Code: int32(rpc.OK),
                },
                HttpResponse: &auth.CheckResponse_OkResponse{
                    OkResponse: &auth.OkHttpResponse{
                        Headers: []*core.HeaderValueOption{
                            {
                                Header: &core.HeaderValue{
                                    Key:   "x-custom-header-from-authz",
                                    Value: "some value",
                                },
                            },
                        },
                    },
                },
            }, nil
        }
    }
    return &auth.CheckResponse{
        Status: &rpc.Status{
            Code: int32(rpc.UNAUTHENTICATED),
        },
        HttpResponse: &auth.CheckResponse_DeniedResponse{
            DeniedResponse: &auth.DeniedHttpResponse{
                Status: &envoy_type.HttpStatus{
                    Code: envoy_type.StatusCode_Unauthorized,
                },
                Body: "Need an Authorization Header with a character bearer token using asm- as prefix!",
            },
        },
    }, nil
}

func main() {
    // create a TCP listener on port 4000
    lis, err := net.Listen("tcp", ":4000")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    log.Printf("listening on %s", lis.Addr())

    grpcServer := grpc.NewServer()
    authServer := &AuthorizationServer{}
    auth.RegisterAuthorizationServer(grpcServer, authServer)

    if err := grpcServer.Serve(lis); err != nil {
        log.Fatalf("Failed to start server: %v", err)
    }

}

可以直接使用镜像: registry.cn-beijing.aliyuncs.com/istio-samples/ext-authz-grpc-service:latest

或者可以基于以下Dockerfile构建镜像,具体代码参见istio_ext_authz_filter_sample

启动外部授权服务器

  • Github项目库中下载示例部署YAML 文件。 或者复制以下YAML定义:
apiVersion: v1
kind: Service
metadata:
  name: extauth-grpc-service
spec:
  ports:
  - port: 4000
    targetPort: 4000
    protocol: TCP
    name: grpc
  selector:
    app: extauth-grpc-service
  type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: extauth-grpc-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: extauth-grpc-service
    spec:
      containers:
      - name: extauth
        image: registry.cn-beijing.aliyuncs.com/istio-samples/ext-authz-grpc-service:latest
        ports:
          - containerPort: 4000
  • 通过 kubectl 连接到 ASM 实例中新添加的 ACK 集群,执行如下命令:
kubectl apply -n istio-system -f extauth-sample-grpc-service.yaml
  • 将看到以下输出显示已成功部署:
service/extauth-grpc-service created
deployment.extensions/extauth-grpc-service created

等待部署的外部授权pod启动之后,接下来开始部署示例应用。

部署示例应用

  • 该示例部署使用了命名空间default,并且已启动Sidecar自动注入。
  • Github项目库中下载示例部署示例httpbin服务的YAML 文件。
  • 通过 kubectl 连接到 ASM 实例中新添加的 ACK 集群,执行如下命令:
kubectl apply -f httpbin.yaml
  • 然后部署用于测试的客户端示例应用sleep。从 Github项目库中下载示例部署示例sleep服务的YAML 文件。
  • 通过 kubectl 连接到 ASM 实例中新添加的 ACK 集群,执行如下命令:
kubectl apply -f sleep.yaml

定义EnvoyFilter

  • 在控制平面区域,选择EnvoyFilter页签,然后单击新建。
  • 在新建页面中,选择相应的命名空间。本例中选择的命名空间为 default。
  • 在文本框中,定义EnvoyFilter,可以复制粘贴EnvoyFilter定义到编辑框中。可参考如下 YAML 定义:
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  # This needs adjusted to be the app name
  name: extauth-sample
spec:
  workloadSelector:
    labels:
      # This needs adjusted to be the app name
      app: httpbin
 
  # Patch the envoy configuration
  configPatches:
 
  # Adds the ext_authz HTTP filter for the ext_authz API
  - applyTo: HTTP_FILTER
    match:
      context: SIDECAR_INBOUND
      listener:
        name: virtualInbound
        filterChain:
          filter:
            name: "envoy.http_connection_manager"
    patch:
      operation: INSERT_BEFORE
      value:
        # Configure the envoy.ext_authz here:
        name: envoy.ext_authz
        config:
          grpc_service:
            # NOTE: *SHOULD* use envoy_grpc as ext_authz can use dynamic clusters and has connection pooling
            google_grpc:
              target_uri: extauth-grpc-service.istio-system:4000
              stat_prefix: ext_authz
            timeout: 0.2s
          failure_mode_allow: false
          with_request_body:
            max_request_bytes: 8192
            allow_partial_message: true
  • 单击确定,将会看到EnvoyFilter已成功创建。
    image.png

验证外部授权

  • 登录到Sleep Pod容器中执行如下命令:
export SLEEP_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})
kubectl exec -it $SLEEP_POD -c sleep -- sh -c 'curl  http://httpbin:8000/headers'

返回如下结果:

Need an Authorization Header with a character bearer token using asm- as prefix!


可以看到示例应用程序的请求没有通过外部授权的许可,原因是请求头中并没有满足Bearer Token值以asm-开头。

  • 在请求中添加以asm-开头的Bearer Token请求头,再次执行如下命令:
export SLEEP_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})
kubectl exec -it $SLEEP_POD -c sleep -- sh -c 'curl -H "Authorization: Bearer asm-token1" http://httpbin:8000/headers'

返回如下结果:

{
  "headers": {
    "Accept": "*/*",
    "Authorization": "Bearer asm-token1",
    "Content-Length": "0",
    "Host": "httpbin:8000",
    "User-Agent": "curl/7.64.0",
    "X-B3-Parentspanid": "dab85d9201369071",
    "X-B3-Sampled": "1",
    "X-B3-Spanid": "c29b18886e98a95f",
    "X-B3-Traceid": "66875d955ac13dfcdab85d9201369071",
    "X-Custom-Header-From-Authz": "some value"
  }
}


可以看到示例应用程序的请求通过外部授权的许可。

相关文章
|
2月前
|
搜索推荐 机器人 开发工具
5 天学会阿里云 RPA:自定义组件
机器人流程自动化(RPA)技术在各个行业都取得了显著的成就,阿里云 RPA 作为其中的佼佼者,为用户提供了一种高效、灵活的自动化解决方案。除了内置的组件和功能,阿里云 RPA 还支持自定义组件的开发,使得用户能够根据自己的需求和业务流程进行个性化的定制。
|
3月前
|
JSON 安全 Serverless
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数【1月更文挑战第23天】【1月更文挑战第112篇】
222 7
|
2月前
服务网格中授权策略对性能的影响大吗?
在使用服务网格时,面对一些大规模集群,大家可能会担心:网格中授权策略内容过于庞大时,对网格代理的性能影响大吗?本文中我们就会给大家简单测试一下:不同情况下,授权策略中配置上万条IP时,对请求影响有多大? 结论:大多数情况下,对请求影响十分微小。
191 1
|
3月前
|
弹性计算 监控 NoSQL
扩展至千万级用户:阿里云平台的初学者指南
【1月更文挑战第7天】这几天支原体感染了,每天半夜都咳醒,实在撑不住了才断更几天。
|
3月前
|
消息中间件 SQL Java
阿里云Flink-自定义kafka sink partitioner实践及相关踩坑记录
阿里云Flink-自定义kafka sink partitioner实践及相关踩坑记录
|
3月前
|
人工智能
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景
阿里云的向量检索服务是一种高效、易用、可扩展的向量检索工具,适用于多种应用场景,如语义检索、知识库搭建、AI多模态搜索等
265 0
|
4月前
阿里云RAM角色和自定义角色
阿里云RAM角色和自定义角色
72 1
|
6月前
|
负载均衡 网络协议 测试技术
使用eBPF加速阿里云服务网格ASM
服务网格下的Sidecar 代理业务服务的收发请求,并提供业务层面的流量控制(路由)、负载均衡等功能,会引入一定的Latency 延迟。 通过eBPF 技术(部署sidecar 加速组件)将同节点下两个进程间的TCP 报文进行socket 短路可以提升一定的性能,HTTP 场景下QPS 可提升15% 左右, 有效地降低业务请求的Latency 。
899 0
使用eBPF加速阿里云服务网格ASM
|
7月前
|
存储 弹性计算 Linux
阿里云服务器快速购买、自定义购买和通过活动选购三种方式图文教程及区别解析
阿里云服务器可以通过快速购买、自定义购买和活动选购三种方式去购买,这是一般用户最常见的购买方式,每种购买方式都有自己的适合场景,也有很多需要注意的地方,下面是这些购买方式的具体图文教程及注意事项,以供参考。
233 0
阿里云服务器快速购买、自定义购买和通过活动选购三种方式图文教程及区别解析
|
8月前
|
运维 Kubernetes Cloud Native
服务网格实施周期缩短 50%,丽迅物流基于阿里云 ACK 和 ASM 的云原生应用管理实践
通过本文介绍丽迅物流关于基于阿里云服务网格 ASM 如何加速企业业务云原生化进程的实践经验。