Gateway API 实践之(九)FSM Gateway 的双向 TLS

FSM Gateway 流量管理策略系列:


网关开启 mTLS(双向 TLS 验证)的功能是一种高级安全措施,它不仅要求服务器向客户端证明其身份,同样要求客户端提供证书以证实其身份。这种双向验证极大地增强了通信的安全性,确保只有持有有效证书的客户端能与服务器建立连接。mTLS 特别适用于对安全性要求极高的场景,如金融交易、企业内部网络或涉及敏感数据的应用。它提供了一种强化的身份验证机制,有效减少未经授权的访问,同时帮助组织遵守严格的数据保护法规。

通过实施 mTLS,网关不仅保护了数据的安全传输,也为客户端和服务器之间的交互提供了一个更加可靠和安全的环境。

这篇就带大家体验如何在 FSM Gateway 上开启 mTLS,让我们开始吧!

前置条件

  • Kubernetes 集群
  • kubectl 工具

环境准备

安装 FSM Gateway

FSM Gateway 的安装,可以参考 安装文档。这里选择 CLI 的方式安装。

下载 FSM CLI。

shell 复制代码
system=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m | sed -E 's/x86_/amd/' | sed -E 's/aarch/arm/')
release=v1.2.0
curl -L https://github.com/flomesh-io/fsm/releases/download/$release/fsm-$release-$system-$arch.tar.gz | tar -vxzf -
./$system-$arch/fsm version
sudo cp ./$system-$arch/fsm /usr/local/bin/fsm

在安装 FSM 时启用 FSM Gateway,默认情况是不启用的。

shell 复制代码
fsm install \
    --set=fsm.fsmGateway.enabled=true

创建网关 TLS 证书

shell 复制代码
openssl genrsa 2048 > ca-key.pem

openssl req -new -x509 -nodes -days 365000 \
   -key ca-key.pem \
   -out ca-cert.pem \
   -subj '/CN=flomesh.io'

openssl genrsa -out server-key.pem 2048
openssl req -new -key server-key.pem -out server.csr -subj '/CN=foo.example.com'
openssl x509 -req -in server.csr -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -days 365

使用 CA 证书、服务器证书和密钥创建 Secret server-cert。当网关仅启用 TLS 时,只会用到服务端证书和密钥。

shell 复制代码
kubectl create namespace httpbin
#TLS cert secret
kubectl create secret generic -n httpbin simple-gateway-cert \
  --from-file=tls.crt=./server-cert.pem \
  --from-file=tls.key=./server-key.pem \
  --from-file=ca.crt=ca-cert.pem

部署示例应用

部署 httpbin 服务,并为其创建 TLS 网关和路由。

shell 复制代码
kubectl apply -n httpbin -f https://raw.githubusercontent.com/flomesh-io/fsm-docs/main/manifests/gateway/tls-termination.yaml

使用上面创建的 CA 证书通过网关来访问 httpbin 服务,访问成功。

复制代码
curl --cacert ca-cert.pem https://foo.example.com/headers --connect-to foo.example.com:443:$GATEWAY_IP:8000
{
  "headers": {
    "Accept": "*/*",
    "Host": "foo.example.com",
    "User-Agent": "curl/8.1.2"
  }
}

网关 mTLS 验证

开启 mTLS

现在参考 GatewayTLSPolicy 文档,为网关开启 mTLS。

shell 复制代码
kubectl apply -n httpbin -f - <<EOF
apiVersion: gateway.flomesh.io/v1alpha1
kind: GatewayTLSPolicy
metadata:
  name: gateway-tls-policy-sample
spec:
  targetRef:
    group: gateway.networking.k8s.io
    kind: Gateway
    name: simple-fsm-gateway
    namespace: httpbin
  ports:
  - port: 8000
    config:
      mTLS: true
EOF

此时,假如我们仍使用原来的方式访问,访问会被拒绝。因为网关已经开始了双向 mTLS 认证,会验证客户端证书。

shell 复制代码
curl --cacert ca-cert.pem https://foo.example.com/headers --connect-to foo.example.com:443:$GATEWAY_IP:8000

curl: (52) Empty reply from server

颁发客户端证书

使用前面的 CA 证书,为客户端颁发证书。

shell 复制代码
openssl genrsa -out client-key.pem 2048
openssl req -new -key client-key.pem -out client.csr -subj '/CN=example.com'
openssl x509 -req -in client.csr -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem -days 365

现在发起请求时,除了指定 CA 证书以外,还要指定客户端的证书和密钥,就能够通过网关的验证成功访问了。

shell 复制代码
curl --cacert ca-cert.pem --cert client-cert.pem --key client-key.pem https://foo.example.com/headers --connect-to foo.example.com:443:$GATEWAY_IP:8000
{
  "headers": {
    "Accept": "*/*",
    "Host": "foo.example.com",
    "User-Agent": "curl/8.1.2"
  }
}

关于 Flomesh

Flomesh(易衡科技)成立于 2018 年,自主研发并开源了高性能可编程代理 Pipy(https://github.com/flomesh-io/pipy)。以 Pipy 为基础,Flomesh 研发了软件负载均衡、服务网格两款软件产品。为工信部认证的可信云产品、可信开源项目。

Flomesh 核心竞争力来自完全自研的核心组件 Pipy,该组件高性能、高可靠、低延迟、可编程、可扩展、低依赖,采用 C++ 开发,内置自研的 JS 引擎,支持适用 JS 脚本做扩展开发。支持包括 x86、arm、龙芯、海光等硬件 CPU 架构;支持 Linux、FreeBSD、OpenWrt 等多种核心的操作系统。

Flomesh 成立以来,以技术为根基、以客户为导向,产品被应用在头部股份制商业银行总行、大型保险公司、运营商总部以及研究院等众多客户和多个场景。

相关推荐
明夜之约13 小时前
Spring Cloud Gateway 深度解析:从路由原理到生产级网关实战
java·spring·spring cloud·gateway
Kyl2n2 天前
【阿里云负载均衡SLB产品家族包含四类负载均衡:ALB、NLB、CLB 和 GWLB的区别】
阿里云·云计算·负载均衡
tyler_download2 天前
深入深出openclaw:gateway 服务进程的启动逻辑
gateway
Upsy-Daisy2 天前
OpenClaw 源码解析(七):Gateway 控制平面与 WebSocket RPC 机制
websocket·平面·gateway
香菜烤面包2 天前
SGLang Model Gateway 核心功能解析
gateway
Yana.nice3 天前
负载均衡之——会话保持
运维·负载均衡
難釋懷3 天前
Nginx负载均衡策略
运维·nginx·负载均衡
悠然南风4 天前
Linux高可用与负载均衡技术栈指南
负载均衡
難釋懷4 天前
Nginx基于反向代理的负载均衡
nginx·github·负载均衡
源远流长jerry5 天前
LVS 与 Nginx 负载均衡:从原理到生产实战
运维·网络·网络协议·tcp/ip·nginx·负载均衡·lvs