envoy_ext_authz

    Envoy v3 external authorization server SDK for MoonBit

    Download zip
    Version
    0.1.0-rc.1
    License
    Apache-2.0
    Last updated
    yesterday
    Downloads
    11

    #envoy_ext_authz

    在 MoonBit 中实现授权策略,由 Envoy 在转发请求前执行。这个 SDK 实现原生 v3 gRPC 方法 /envoy.service.auth.v3.Authorization/Check。示例让 Alice 读取 Acme 的订单,让 Bob 读取另一个租户的订单,并把经过验证的身份写入上游请求。Envoy 协议

    #接入服务

    moon add python123-ops/envoy_ext_authz@0.1.0-rc.1

    在 Native 服务的 moon.pkg 中导入两个包。业务代码可以直接调用根包的 Service::check 处理原始 protobuf 消息,或通过 transport 启动 gRPC 服务。

    supported_targets = "native"

    import {
    "python123-ops/envoy_ext_authz" @authz,
    "python123-ops/envoy_ext_authz/transport",
    }

    下面的函数接收项目已有的凭据验证和权限检查函数。身份取自验证结果,上游使用 x-authenticated-user 读取该身份。

    async fn run_gateway_policy(
    authenticate : (String) -> String?,
    permitted : (String, String, String) -> Bool,
    ) -> Unit {
    let service = @authz.Service::new(
    req => {
    guard req.headers.get("authorization") is Some(credential) else { return Deny(401) }
    guard authenticate(credential) is Some(user) else { return Deny(401) }
    guard permitted(user, req.http_method, req.path) else { return Deny(403) }
    Allow([("x-authenticated-user", user)])
    },
    allowed_headers=["authorization"],
    identity_headers=["x-authenticated-user"],
    )
    @transport.serve(service, host="127.0.0.1", port=19001)
    }

    策略是同步、短时的函数,适合读取本地公钥、内存中的关系表或定期更新的快照。RPC 支持并发和截止时间,运行中的同步计算由回调自行控制耗时。调用方需要管理监听器和优雅停机时,可通过 @transport.new_server(service) 获取已设置单消息上限的 GrpcServer。公共接口 · 传输接口

    #运行订单示例

    moon update moon test --target native moon build --target native --release examples/server python3 verification/run.py

    最后一条命令在 Linux 启动真实 Envoy、授权服务和测试上游,依次运行简单路由与租户关系策略,检查每个请求的 HTTP 状态、上游收包数和身份值。需要 Python 3、PyYAML,以及 PATH 上的 Envoy 1.40.2,也可以用 --envoy /path/to/envoy 指定。脚本使用本机端口 19000–19002,结束时关闭其启动的进程。单独启动策略服务可运行 moon run examples/server --target native -- tenant,将 tenant 换成 simple 即可切换策略。示例中的公开测试凭据仅用于本机演示。网关配置 · MoonPolicyKit 授权示例

    策略结果网关行为
    Allow(headers)将本次给出的身份头覆盖写入上游。对声明过但本次未给出的身份头,要求 Envoy 删除客户端值。
    Deny(401)、Deny(403)将鉴权拒绝返回客户端,上游收不到请求。
    Unavailable、无效策略输出、服务超时或停机走 gRPC 或网关错误路径。随附配置在这些情况下拒绝转发并返回 503。

    #部署与兼容性

    • 已验证的组合为 MoonBit 1.0.0-rc1+3c0520448、moonrpc 0.22.3、MoonPolicyKit 0.2.0 和 Envoy 1.40.2 的 HTTP ext_authz 过滤器。Native 单元测试覆盖 Windows 和 Linux,网关联调在 Linux 运行。Istio 和 Envoy Gateway 提供同协议接入点,本版本的实测对象是独立 Envoy。Istio 对接文档 · Envoy Gateway 对接文档
    • 策略读取 HTTP 方法、Envoy 提供的原始请求目标(可能含查询串)和显式选定的小写请求头。请求体、动态元数据、网络层过滤器和路由重写不在本版接口范围内。路由策略与业务服务应采用一致的路径解释规则。
    • 使用 encode_raw_headers: true 保留独立的原始头。重复的策略输入头和重复的鉴权头会被拒绝。旧版字符串映射已合并同名值,SDK 会拒绝含逗号的 Authorization 值;合法 Digest 凭据请使用原始头模式。
    • 身份输出仅接受声明过的 x-auth-* 或 x-authenticated-* 小写名称。业务上游只信任这些头,并限制直接访问上游的网络路径。随附配置拒绝带下划线的请求头,以适配会对头名作归一化的后端。
    • 默认监听使用本机明文 HTTP/2,适用于可信的本地 Envoy。跨主机部署需保护该跳的传输与访问权限。64 KiB 限额作用于单条消息和 SDK 解码输入,moonrpc 对压缩输入和多条消息的缓冲不提供整条连接的内存硬上限。

    #性能复现

    python3 verification/run.py --samples 2000 --concurrency 8 --output verification/linux-results.json

    脚本在相同 Envoy 配置、上游和请求下比较启用及移除鉴权过滤器的结果。每组预热后,经 8 个客户端并发发送 2,000 个请求。结果文件包含端到端 P95/P99、吞吐、错误数、逐请求耗时及二进制摘要。这是本机回环链路测量,含 Envoy 和 Python 测试上游耗时。实测结果 · Linux CI

    本项目采用 Apache-2.0 许可证。LICENSE

    CheckError

    pub(all) suberror CheckError {
    InvalidRequest
    InvalidDecision
    PolicyUnavailable
    RequestTooLarge
    } derive(
    Debug
    )

    ConfigurationError

    pub suberror ConfigurationError {
    InvalidHeader(String)
    } derive(
    Debug
    )

    Decision

    pub(all) enum Decision {
    Allow(Array[(String, String)])
    Deny(Int)
    Unavailable
    }

    Allow supplies authoritative identity headers. Deny accepts 401 or 403. Unavailable signals an operational failure to the gateway.

    Request

    pub(all) struct Request {
    http_method : String
    path : String
    headers : Map[String, String]
    }

    HTTP metadata delivered by Envoy. Header names are lowercase. Only entries explicitly selected by the service owner are exposed to the policy.

    Service

    pub struct Service {
    // private fields
    }

    Service::check

    fn Service::check(self : Service, data : Bytes) -> Bytes raise CheckError

    Evaluate the serialized Envoy v3 CheckRequest. The response bytes are a CheckResponse only for completed decisions. Transport failures stay errors.

    Service::new

    fn Service::new(policy : (Request) -> Decision, allowed_headers~ : Array[String], identity_headers~ : Array[String]) -> Service raise ConfigurationError

    Build a reusable service. Declare only the incoming headers the policy reads and the outgoing x-auth-* or x-authenticated-* headers it owns.