IoT 百萬設備架構選型 Part 2:安全與多租戶
English Abstract — Part 2 of 3. Device security: HTTPS/TLS for all communication, mTLS X.509 for device authentication, software-based certificate rotation (90-365d), JIT provisioning, anti-spoofing measures. Multi-tenancy: MQTT Topic ACL namespace isolation, PostgreSQL Row-Level Security, 4-role RBAC model, dual-layer command authorization.
系列文章: Part 1 核心架構 Part 2 安全與多租戶(本篇) Part 3 運維與可靠性
Device Identity
認證方式
| 方式 | 安全性 | 適用 | 說明 |
|---|---|---|---|
| mTLS (X.509) | 最高 | 預設 | CA chain 免存 per-device credential |
| PSK | 中 | 受限設備 | gateway 後方使用,rotation 較痛苦 |
| JWT | 高 | OAuth2 整合 | Stateless 驗證,需 refresh |
MAC 可偽造、serial 可猜測 — Device ID 必須搭配密碼學憑證:
- MQTT Client ID:
{tenant}:{type}:{serial} - X.509 CN 匹配 Client ID → mTLS 自動綁定
- DB PK:UUID v4
通訊安全
| 層 | 機制 | 說明 |
|---|---|---|
| 傳輸 | TLS 1.2+ (8883) | 加密 + 完整性 |
| 身份 | mTLS 雙向驗證 | Broker 驗 device,device 驗 broker |
| 應用 | Payload HMAC (optional) | 防中間人改寫 |
Certificate Rotation:
- 有效期 90-365 天,到期前 30 天自動 CSR 換發
- 雙 CA chain 確保 rotation 不斷線
- 到期未更新 → CRL 撤銷 → 強制斷線 + 告警
Provisioning
flowchart TD
R[Root CA] --> F[Intermediate CA]
F -->|Bootstrap| D[First Connect]
D -->|Verify| REG[Registry]
REG -->|Issue cert| D2[Online]
| 方式 | 安全 | 適用 |
|---|---|---|
| JIT | 高 | 一般 fleet(推薦) |
| Claim-based | 中 | 批量同型號 |
| API 預註冊 | 高 | 已知 device list |
防偽裝: One-time bootstrap token、Device fingerprint hash、Provisioning API rate limit、Allowlist/Denylist。
EMQX 認證鏈
- mTLS → cert CN 取 device identity(
peer_cert_as_clientid = cn) - JWT → RS256 簽名 + claims 驗證
- HTTP → 外部 auth service(legacy 設備)
EMQX 支援 CRL + OCSP Stapling — 設備 compromise 時即時撤銷。
Multi-Tenancy
Broker 隔離
| 模式 | 隔離 | 適用 | 說明 |
|---|---|---|---|
| 共享 EMQX + Topic ACL | 邏輯 | 95% 租戶 | 成本最低,ACL 管理 |
| Broker-per-tenant | 進程 | 法規要求 | 醫療/金融等合規場景 |
| 混合 | 視 tier | 推薦 | Standard 共享 + Enterprise 獨立 |
Topic 命名空間
{tenant}/d/{device}/telemetry # 遙測
{tenant}/d/{device}/cmd/request # 指令
{tenant}/d/{device}/cmd/response # 回應
{tenant}/d/{device}/config/desired # 期望組態
{tenant}/g/{group}/cmd/request # 群組廣播
Tenant ID 永遠第一層 → ACL 前綴比對。設備禁止 wildcard subscribe。
RBAC
| 權限 | Super Admin | Tenant Admin | Operator | Viewer |
|---|---|---|---|---|
| 管理 tenants | ✓ | |||
| 註冊/停用設備 | ✓ | ✓ | ||
| 發送任意指令 | ✓ | ✓ | ||
| 發送預核准指令 | ✓ | ✓ | ✓ | |
| 查看 Dashboard | ✓ | ✓ | ✓ | ✓ |
| OTA 部署 | ✓ | ✓ |
Command 雙層驗證
- API 端: User role + command 權限 + device status + rate limit
- Device 端: 驗簽名(防 injection)+ 驗 timestamp(防 replay)+ 驗 command_type
DB Tenant 隔離
| 策略 | 隔離 | 適用 | 說明 |
|---|---|---|---|
| Row-Level Security | 邏輯 | 預設 | 單 schema,policy 自動過濾 |
| Schema-per-tenant | 中 | 中等需求 | N 個 schema migration |
| DB-per-tenant | 最強 | Enterprise | 最高成本,完全隔離 |
TimescaleDB 按 (tenant_id, time) 分區 → 查詢自動 pruning,可按 tenant 設定不同 retention。
下一篇
- Part 1:核心架構 — EMQX + TimescaleDB + FastAPI + BFF、成本估算
- Part 3:運維、成本與可靠性 — Rate Limiting、Edge Resilience、DR、Observability、成本估算
Enjoy Reading This Article?
Here are some more articles you might like to read next: