首页 > 其他分享 >Gateway API 实践之(九)FSM Gateway 的双向 TLS

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

时间:2024-02-06 12:32:59浏览次数:29  
标签:TLS -- ca FSM pem cert key com Gateway

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

FSM Gateway 流量管理策略系列:

故障注入

黑白名单访问控制

限速

重试

会话保持

健康检查

负载均衡算法

TLS 上游

双向 TLS


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

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

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

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

前置条件

  • Kubernetes 集群
  • kubectl 工具

环境准备

安装 FSM Gateway

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

下载 FSM CLI。

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,默认情况是不启用的。

fsm install \
    --set=fsm.fsmGateway.enabled=true

创建网关 TLS 证书

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 时,只会用到服务端证书和密钥。

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 网关和路由。

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。

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 认证,会验证客户端证书。

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 证书,为客户端颁发证书。

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 证书以外,还要指定客户端的证书和密钥,就能够通过网关的验证成功访问了。

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"
  }
}

标签:TLS,--,ca,FSM,pem,cert,key,com,Gateway
From: https://blog.51cto.com/u_16455808/9619722

相关文章

  • Golang Grpc-Gateway生成-buf版
    官网有个工具buf可以自动生成https://github.com/bufbuild/buf/releases按照自己的平台下载对应的文件,并把可执行文件加入到环境变量下proto同级目录下新增buf.gen.yaml或者执行bufmodinit,buf默认会扫描所有文件夹的*.proto,所以我在同级目录下创建version:v1plugins:-......
  • Golang Grpc-Gateway生成-基础版
    时间久了不用就会忘记指令,这里做个笔记创建一个文件//+buildtoolspackagetoolsimport(_"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"_"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"_"google.gol......
  • 面试官:SpringCloudGateway过滤器类型有哪些?
    在SpringCloudGateway中,过滤器是在请求到达目标服务之前或之后,执行某些特定操作的一种机制。例如,它可以实现对传入的请求进行验证、修改、日志记录、身份验证、流量控制等各种功能。在SpringCloudGateway中,过滤器总共分为以下两大类:局部过滤器:只作用于某一个路由(route)。全......
  • FastGateway 一个可以用于代替Nginx的网关
    在我本人研究Yarp的时候经常用于公司项目的业务网关代理,这时候就个大佬问我是否可以实现动态加载HTTPS证书?那时候我说不太可能实现,然而在某一天我看到微软使用Yarp代替了Nginx吞吐量提升了百分之八十!这个时候我就萌生了自己使用yarp造一个Gateway的项目,应为我本身也经常使用ngi......
  • 面试官:SpringCloudGateway过滤器类型有哪些?
    在SpringCloudGateway中,过滤器是在请求到达目标服务之前或之后,执行某些特定操作的一种机制。例如,它可以实现对传入的请求进行验证、修改、日志记录、身份验证、流量控制等各种功能。在SpringCloudGateway中,过滤器总共分为以下两大类:局部过滤器:只作用于某一个路由(route......
  • Gateway
    全局跨域配置:globalcors:cors-configurations:'[/**]':allowedOrigins:"*"allowedMethods:-GET-POST-DELETE-PUT-OPTION这部分配置定义了全局的跨域资源共享(CORS)设置。它允许所有来源(allowed......
  • DBeaver连接SqlServer报“The server selected protocol version TLS10 is not accept
    1、......
  • 重写SpringCloudGateway路由查找算法,性能提升100倍!
    如果你也在做SpringCloudGateway网关开发,希望这篇文章能给你带来一些启发背景先说背景,某油项目,通过SpringCloudGateway配置了1.6万个路由规则,实际接口调用过程中,会偶现部分接口从发起请求到业务应用处理间隔了大概5秒的时间,经排查后发现是SpringCloudGateway底层在查找对应的R......
  • Gateway API 实践之(四)FSM Gateway 的重试功能
    网关的重试功能是一种重要的网络通信机制,旨在提高系统服务调用的可靠性和容错性。这个功能允许网关在初次请求失败时自动重新发送请求,从而减少临时性问题(如网络波动、服务瞬时过载等)对最终用户体验的影响。它的工作原理是,当网关向下游服务发送请求时,如果遇到特定类型的失败(如连接错......
  • STM32CubeMX教程23 FSMC - IS62WV51216(SRAM)驱动
    1、准备材料开发板(正点原子stm32f407探索者开发板V2.4)STM32CubeMX软件(Version6.10.0)野火DAP仿真器keilµVision5IDE(MDK-Arm)ST-LINK/V2驱动XCOMV2.6串口助手2、实验目标使用STM32CubeMX软件配置STM32F407开发板的FSMC实现以轮询或DMA的方式读写IS62WV51216(SRAM)芯片3、......