TLS客户端认证
场景说明
- 服务端使用TLS客户端认证对客户端进行认证。
配置步骤
- Step 1. 生成根证书
openssl genrsa -out root.key 2048openssl req -new -x509 -days 365 -key root.key -out root.crt
- Step 2. 创建客户端证书签名申请
openssl genrsa -out client.key 2048openssl req -new -out client.csr -key client.key
- Step 3. 生成客户端证书
echo "extendedKeyUsage = clientAuth" > openssl.cnfopenssl x509 -req -in client.csr -out client.crt -signkey client.key -CA root.crt -CAkey root.key -days 365 -extfile openssl.cnf
Step 4. 配置4层负载均衡服务
客户端认证针对VIP启用,配置4层负载均衡服务是为了获取VIP。
示例中,使用HAproxy作为4层负载均衡服务,通过PROXY协议将VIP传递给BFE,HAproxy与BFE同机部署。
安装HAproxy,下载www.haproxy.org。Ubuntu系统可通过apt install haproxy安装。
配置HAproxy,配置文件(haproxy.cfg)示例:
globaldefaultsmode tcpbalance leastconntimeout client 3000mstimeout server 3000mstimeout connect 3000msfrontend fr_server_httpbind 0.0.0.0:7080default_backend bk_server_httpbackend bk_server_httpserver srv1 0.0.0.0:8080 maxconn 2048 send-proxyfrontend fr_server_httpsbind 0.0.0.0:7443default_backend bk_server_httpsbackend bk_server_httpsserver srv1 0.0.0.0:8443 maxconn 2048 send-proxy
启动HAproxy
haproxy -f haproxy.cfg
- Step 5. 配置BFE客户端证书文件存储路径(conf/bfe.conf),将root.crt复制到tls_conf/client_ca目录 注:根证书文件后缀名必须为.crt
[Server]...Layer4LoadBalancer = "PROXY"...[HttpsBasic]...ClientCABaseDir = tls_conf/client_ca...
修改 conf/tls_conf_rule.data,将ClientAuth置为true,ClientCAName填写根证书文件名。
{"Version": "12","DefaultNextProtos": ["http/1.1"],"Config": {"example_product": {"VipConf": ["127.0.0.1"],"SniConf": null,"CertName": "example.org","NextProtos": ["h2;rate=0;isw=65535;mcs=200;level=0","http/1.1"],"Grade": "C","ClientAuth": true,"ClientCAName": "root"}}}
启动BFE
./bfe -c ../conf
- Step 6. 验证配置
openssl s_client -connect 127.0.0.1:7443 -cert client.crt -key client.key -state -quiet