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

相关推荐
珍珠是蚌的眼泪11 小时前
微服务_入门2
网关·微服务·gateway·远程调用·feign
铁板鱿鱼14013 小时前
统一网关--gateway(仅供自己参考)
gateway
bug菌¹13 小时前
滚雪球学SpringCloud[4.2讲]: Zuul:Netflix API Gateway详解
spring·spring cloud·gateway
&星辰入梦来&18 小时前
Nginx从入门到入土(三): 静态资源管理与代理服务
运维·nginx·负载均衡
hanniuniu131 天前
详细解读,F5服务器负载均衡的技术优势
运维·服务器·负载均衡
m0_635502201 天前
Spring Cloud Gateway组件
网关·微服务·负载均衡·过滤器
炸裂狸花猫2 天前
Kubernetes从零到精通(12-Ingress、Gateway API)
容器·kubernetes·gateway
云来喜3 天前
关于Spring Cloud Gateway中 Filters的理解
java·spring boot·gateway
小小小小关同学3 天前
【Gateway】网关服务快速上手
java·开发语言·gateway
小小小小关同学4 天前
【Gateway】Gateway Filter Factories
网络·gateway