关于 cloudflare-zero-trust
cloudflare-zero-trust 是配置 Cloudflare Zero Trust 的技能,面向需要保护内部服务和远程访问的团队,涵盖隧道、身份策略、设备检查与 DNS 过滤。
主要功能
- 01通过 Tunnel 暴露内部服务
- 02按身份设置应用访问策略
- 03使用设备状态检查控制访问
- 04配置 Gateway DNS 过滤
- 05通过 WARP 路由流量
适用场景
- 01以身份策略替代内部应用 VPN
- 02安全开放 SSH 或 RDP 访问
- 03阻止 DNS 层的恶意域名
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bagelhole/ devops-security-agent-skills --skill cloudflare-zero-trust安装后在 Claude Code 中输入/cloudflare-zero-trust确认已加载。
详细介绍
Cloudflare Zero Trust
使用 Cloudflare 的 Zero Trust 平台(Access、Tunnel、Gateway 和 WARP),无需 VPN 即可安全访问内部服务。
适用场景
- 替代 VPN,访问内部 Web 应用、SSH 或 RDP。
- 对内部工具(仪表板、管理面板)实施基于身份的访问策略。
- 安全地向远程团队开放本地部署或专用网络中的服务。
- 过滤 DNS 流量,阻止恶意软件、网络钓鱼和影子 IT。
- 强制执行设备状态检查(受管设备、OS 版本、磁盘加密)。
前提条件
- Cloudflare 账户,并开通 Zero Trust 套餐(免费层最多支持 50 位用户)。
- 在 Cloudflare 上托管的域名(用于 Access 应用主机名)。
- 已配置身份提供商(Google Workspace、Okta、Azure AD/Entra ID、GitHub)。
- 在托管内部服务的服务器上安装
cloudflaredCLI。
# Install cloudflared
# macOS
brew install cloudflared
# Debian/Ubuntu
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt update && sudo apt install -y cloudflared
# Docker
docker pull cloudflare/cloudflared:latest
Cloudflare Tunnel 设置
Tunnel 会在你的基础设施与 Cloudflare 边缘之间建立加密的出站连接,无需开放入站端口。
创建和配置 Tunnel
# Authenticate with Cloudflare
cloudflared tunnel login
# Create a named tunnel
cloudflared tunnel create internal-apps
# This creates credentials at ~/.cloudflared/<TUNNEL_ID>.json
# List tunnels
cloudflared tunnel list
# Route DNS to the tunnel (creates a CNAME record)
cloudflared tunnel route dns internal-apps grafana.example.com
cloudflared tunnel route dns internal-apps wiki.example.com
cloudflared tunnel route dns internal-apps ssh.example.com
Tunnel 配置文件
# ~/.cloudflared/config.yml
tunnel: <TUNNEL_ID>
credentials-file: /home/deploy/.cloudflared/<TUNNEL_ID>.json
ingress:
# Grafana dashboard
- hostname: grafana.example.com
service: http://localhost:3000
# Internal wiki
- hostname: wiki.example.com
service: http://localhost:8080
originRequest:
noTLSVerify: true
# SSH access via browser
- hostname: ssh.example.com
service: ssh://localhost:22
# Private network access (CIDR routing)
- hostname: internal.example.com
service: http://10.0.0.0/24
# Catch-all — required as the last rule
- service: http_status:404
运行 Tunnel
# Run in foreground (for testing)
cloudflared tunnel run internal-apps
# Install as a systemd service
sudo cloudflared service install
sudo systemctl enable cloudflared
sudo systemctl start cloudflared
# Or run via Docker
docker run -d --name cloudflared \
--restart unless-stopped \
-v /home/deploy/.cloudflared:/etc/cloudflared \
cloudflare/cloudflared:latest \
tunnel run internal-apps
使用 Tunnel 的 Docker Compose
# docker-compose.yml
version: "3.8"
services:
cloudflared:
image: cloudflare/cloudflared:latest
restart: unless-stopped
command: tunnel run
environment:
- TUNNEL_TOKEN=${TUNNEL_TOKEN}
networks:
- internal
grafana:
image: grafana/grafana:latest
networks:
- internal
wiki:
image: requarks/wiki:2
networks:
- internal
networks:
internal:
driver: bridge
Access 策略
Access 策略用于控制谁可以访问 Cloudflare 后方的应用。
创建 Access 应用
# Via API — create a self-hosted application
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/apps" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Grafana",
"domain": "grafana.example.com",
"type": "self_hosted",
"session_duration": "12h",
"auto_redirect_to_identity": true,
"allowed_idps": ["<IDP_UUID>"]
}'
策略类型和示例
# Allow policy — members of the engineering group
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/apps/<APP_ID>/policies" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Engineering Team",
"decision": "allow",
"include": [
{ "group": { "id": "<GROUP_UUID>" } }
],
"require": [
{ "login_method": { "id": "<MFA_METHOD_UUID>" } }
]
}'
常见策略模式
| 模式 | 包含规则 | 要求规则 |
|---|---|---|
| 所有员工 | 电子邮件域 @company.com | - |
| 仅限工程团队 | Access 组“Engineering” | MFA |
| 承包商(限时) | 电子邮件列表 | 设备状态 |
| CI/CD 自动化 | 服务令牌 | - |
| 外部合作伙伴 | 指定电子邮件 | 国家/地区检查 |
用于自动化的服务令牌
# Create a service token for CI/CD
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/access/service_tokens" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "github-actions-deploy"}'
# Response includes Client ID and Client Secret
# Use in CI with headers:
# CF-Access-Client-Id: <CLIENT_ID>
# CF-Access-Client-Secret: <CLIENT_SECRET>
# Use service token in CI/CD
curl -H "CF-Access-Client-Id: $CF_CLIENT_ID" \
-H "CF-Access-Client-Secret: $CF_CLIENT_SECRET" \
https://grafana.example.com/api/health
设备状态检查
在授予访问权限前强制检查终端设备是否符合要求。
配置状态检查(控制面板)
- 前往 Settings > WARP Client > Device posture。
- 添加检查项:
- 磁盘加密:要求启用 FileVault(macOS)或 BitLocker(Windows)。
- OS 版本:最低要求为 macOS 14.0 或 Windows 11。
- 防火墙:确保已启用主机防火墙。
- Crowdstrike/SentinelOne:确认 EDR Agent 正在运行。
- 在 Access 策略的 Require 规则中引用状态检查。
Gateway DNS 过滤
在 DNS 层阻止恶意域名并强制执行可接受使用策略。
DNS 位置
# Configure DNS endpoints for offices or networks
# Dashboard: Gateway > DNS Locations > Add a location
# Assign the Gateway DNS IPs to your network's DNS resolver:
# IPv4: 172.64.36.1, 172.64.36.2
# IPv6: 2606:4700:4700::1111
# DoH: https://<UNIQUE_ID>.cloudflare-gateway.com/dns-query
DNS 策略
# Create a DNS policy to block malware and phishing
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
-H "Authorization: Bearer $CF_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Block Security Threats",
"enabled": true,
"action": "block",
"traffic": "any(dns.security_category[*] in {80 83 131 134 151 153})",
"filters": ["dns"]
}'
常见 DNS 策略规则
| 规则名称 | 流量表达式 | 操作 |
|---|---|---|
| 阻止恶意软件 | any(dns.security_category[*] in {80 83}) | 阻止 |
| 阻止网络钓鱼 | any(dns.security_category[*] in {131 134}) | 阻止 |
| 阻止社交媒体 | any(dns.content_category[*] in {75}) | 阻止 |
| 允许例外 | dns.fqdn == "allowed.example.com" | 允许 |
WARP 客户端部署
部署 Cloudflare WARP 客户端,通过 Gateway 路由流量。
# MDM deployment — macOS configuration profile
# Use Cloudflare's managed deployment:
# Dashboard: Settings > WARP Client > Device enrollment
# Manual enrollment
# 1. Install WARP client from https://1.1.1.1
# 2. Click gear icon > Account > Login with Cloudflare Zero Trust
# 3. Enter your team name (from Settings > General)
# Verify WARP is connected
curl https://connectivity.cloudflare.com/cdn-cgi/trace
# Look for: warp=on
WARP 分流隧道
# Configure split tunnels to exclude certain traffic from WARP
# Dashboard: Settings > WARP Client > Device settings > Split Tunnels
# Exclude mode (default): WARP handles everything except listed IPs
# Include mode: WARP only handles listed IPs/domains
# Common exclusions:
# - Local network: 192.168.0.0/16, 10.0.0.0/8
# - Video conferencing: zoom.us, *.teams.microsoft.com
# - Printer subnets
SSH 和基于浏览器的终端
# In cloudflared config.yml — expose SSH via browser rendering
ingress:
- hostname: ssh.example.com
service: ssh://localhost:22
# Users access ssh.example.com in their browser
# Cloudflare renders an in-browser terminal after Access authentication
# Or use cloudflared on the client side for native SSH
cloudflared access ssh --hostname ssh.example.com
# Add to SSH config for seamless access
# ~/.ssh/config
# Host ssh.example.com
# ProxyCommand /usr/local/bin/cloudflared access ssh --hostname %h
故障排查
| 症状 | 原因 | 修复方法 |
|---|---|---|
隧道在仪表板中显示 ERR | cloudflared 未运行或配置有误 | 检查 systemctl status cloudflared;验证配置 YAML |
| 身份正确但访问仍返回 403 | 策略顺序有误或缺少 require 规则 | 策略按从上到下的顺序评估;确保 Allow 位于 Block 上方 |
| WARP 显示“无法连接” | 团队名称错误或已禁用注册 | 在 Settings > General 中验证团队名称;检查注册权限 |
| 服务令牌身份验证失败 | 令牌已过期或请求头错误 | 重新生成令牌;同时使用 CF-Access-Client-Id 和 CF-Access-Client-Secret |
| DNS 过滤未生效 | 客户端未使用 Gateway DNS 解析器 | 确认 DNS 设置为 172.64.36.1;检查 WARP 是否已连接 |
| 隧道延迟激增 | 隧道运行在负载过高的主机上 | 监控 cloudflared 的资源使用情况;在专用基础设施上运行 |
| “没有健康的源站”错误 | 后端服务已停止 | 检查配置的 ingress 端口上的服务;查看 cloudflared 日志 |
相关技能
- cloudflare-workers - Access 策略背后的边缘计算
- dns-management - DNS 路由和记录管理
- reverse-proxy - 替代网关模式
- service-mesh - 服务间内部安全
评论
支持 Markdown