From 03a3b9f6a94771b46f32623bff16ae25e79c3a70 Mon Sep 17 00:00:00 2001 From: xiewenjun <88080888@163.com> Date: Mon, 12 Oct 2026 12:20:53 +0800 Subject: [PATCH] feat: deliver deployment verification keys and instance trust config Co-Authored-By: Craft Agent --- backend/internal/httpapi/business.go | 1 + .../httpapi/integration_assets/protocol.md | 6 ++ backend/internal/httpapi/integration_kit.go | 2 +- backend/internal/httpapi/integration_trust.go | 32 ++++++++++ .../httpapi/integration_trust_test.go | 39 ++++++++++++ docs/client-integration-center.md | 8 +++ frontend/src/admin-api.ts | 4 ++ frontend/src/views/IntegrationPanel.test.ts | 4 +- frontend/src/views/IntegrationPanel.vue | 12 +++- frontend/src/views/Overview.vue | 3 +- frontend/src/views/TrustDeliveryPanel.test.ts | 30 +++++++++ frontend/src/views/TrustDeliveryPanel.vue | 62 +++++++++++++++++++ 12 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 backend/internal/httpapi/integration_trust.go create mode 100644 backend/internal/httpapi/integration_trust_test.go create mode 100644 frontend/src/views/TrustDeliveryPanel.test.ts create mode 100644 frontend/src/views/TrustDeliveryPanel.vue diff --git a/backend/internal/httpapi/business.go b/backend/internal/httpapi/business.go index 5f8592a..b49c5f5 100644 --- a/backend/internal/httpapi/business.go +++ b/backend/internal/httpapi/business.go @@ -72,6 +72,7 @@ func registerBusinessRoutes(r *gin.Engine, a *BusinessAPI) { secured.GET("/settings", a.getSettings) secured.PUT("/settings", a.updateSettings) secured.POST("/settings/password", a.changePassword) + secured.GET("/integration-trust", a.integrationTrust) secured.GET("/integration-kit", a.integrationKit) secured.GET("/integration-kit/download", a.downloadIntegrationKit) secured.GET("/integration-kit/archive", a.downloadIntegrationArchive) diff --git a/backend/internal/httpapi/integration_assets/protocol.md b/backend/internal/httpapi/integration_assets/protocol.md index 2d329c2..0e6c316 100644 --- a/backend/internal/httpapi/integration_assets/protocol.md +++ b/backend/internal/httpapi/integration_assets/protocol.md @@ -91,3 +91,9 @@ SPA 应通过同源 BFF 调用,不得在浏览器长期保存实例 secret。 ## 交付验收 覆盖正常验签、篡改字段/签名、未知/撤销密钥、v1/额外字段、实例不匹配、过期和超过 14 天、时钟回拨、非 active、幂等重试、429、掉线与原子存储失败。测试向量只使用随机测试密钥,绝不能导入生产信任配置。 + +## 管理员交付的信任配置(yhy-license-trust v1) + +从对应环境管理后台“授权 → 实例”下载信任 JSON,通过受控渠道交付。字段:format=yhy-license-trust、version=1、api_base_url(HTTPS 根地址,本机允许 HTTP 回环)、protocol_versions=[license-v2,lease-v2]、signature_algorithm=ECDSA-P256-SHA256、current_key_id、keys=[{key_id,public_key_pem,revoked,spki_sha256}]、instance_id、customer_id、product_id。spki_sha256 是公钥 SPKI DER 的 SHA-256 小写十六进制摘要。文件只含未撤销公钥,不含 secret;激活凭据文件单独导入。 + +客户端实现严格格式/大小校验,验证公钥为 P-256 SPKI PEM、摘要匹配、key_id 不重复且 current_key_id 存在、revoked=false,核对绑定与授权中心地址并提示操作者确认。只有受控交付才可新增信任根,不可通过发现接口自动新增。验证 license-v2、lease-v2 签名与实例/客户/产品绑定、有效期和状态;导入不是激活。开发先通过 ZIP 测试向量,再连接开发中心;生产重新交付生产信任配置和凭据。已交付公钥仍需受控撤销/更新策略,导出文件不能证明未来未撤销。 diff --git a/backend/internal/httpapi/integration_kit.go b/backend/internal/httpapi/integration_kit.go index bfea926..dfab338 100644 --- a/backend/internal/httpapi/integration_kit.go +++ b/backend/internal/httpapi/integration_kit.go @@ -118,7 +118,7 @@ func integrationPrompt(language, mode string) string { 默认授权平台根地址:%s。将它作为 SDK 的默认值,同时允许部署通过 LICENSE_API_BASE_URL 覆盖;配置为根地址,不要附加 /api/v1,不要在业务代码中散落硬编码 URL。 -首次设置由管理员在“授权 → 实例 → 创建实例并获取激活凭据”交付 instance_id、license_key、secret。支持管理台导出的 .license.json 文件导入,严格遵循 protocol.md“授权文件导入”的格式、大小限制、地址确认/允许列表和受保护存储要求;导入后仍需在线激活与验签,不能作为离线授权证书。UI 字段统一为“实例 ID”“授权码”“激活密钥”。实例 ID 不是数据库数字 ID;授权码是后台授权密钥;secret 持续用于心跳和下载;配置加密留存后可重复查看,旧实例无法从哈希恢复。公钥通过受控渠道预置,管理员密码与私钥绝不能要求最终用户输入。已激活后从受保护存储恢复,不要每次启动重新填写。当前每条授权只绑定一个实例,旧实例丢失 secret 无法恢复。 +首次设置由管理员在“授权 → 实例 → 创建实例并获取激活凭据”交付 instance_id、license_key、secret。支持管理台导出的 .license.json 文件导入,严格遵循 protocol.md“授权文件导入”的格式、大小限制、地址确认/允许列表和受保护存储要求;导入后仍需在线激活与验签,不能作为离线授权证书。UI 字段统一为“实例 ID”“授权码”“激活密钥”。实例 ID 不是数据库数字 ID;授权码是后台授权密钥;secret 持续用于心跳和下载;配置加密留存后可重复查看,旧实例无法从哈希恢复。公钥及绑定信息从对应管理后台实例页导出 yhy-license-trust v1 JSON(详见 protocol.md),客户端实现严格导入并通过受控渠道预置,管理员密码与私钥绝不能要求最终用户输入。已激活后从受保护存储恢复,不要每次启动重新填写。当前每条授权只绑定一个实例,旧实例丢失 secret 无法恢复。 实现激活 POST /api/v1/activation/instances/{instance_id},提交 license_key、secret 和 Idempotency-Key;实现心跳 POST /api/v1/instances/{instance_id}/heartbeat,提交 secret、RFC3339 UTC client_time 和 Idempotency-Key。 diff --git a/backend/internal/httpapi/integration_trust.go b/backend/internal/httpapi/integration_trust.go new file mode 100644 index 0000000..1bb115e --- /dev/null +++ b/backend/internal/httpapi/integration_trust.go @@ -0,0 +1,32 @@ +package httpapi + +import ( + "crypto/sha256" + "crypto/x509" + "encoding/hex" + "encoding/pem" + "github.com/gin-gonic/gin" +) + +func (a *BusinessAPI) integrationTrust(c *gin.Context) { + c.Header("Cache-Control", "no-store") + if a.keyring == nil { + apiError(c, 503, "keyring_unavailable", "当前部署未配置 v2 公钥环,请配置 LICENSE_KEYRING_FILE 及匹配签名私钥") + return + } + keys := make([]gin.H, 0, len(a.keyring.Keys)) + for _, key := range a.keyring.Keys { + block, _ := pem.Decode([]byte(key.PublicKeyPEM)) + if block == nil { + apiError(c, 503, "keyring_unavailable", "公钥格式无效") + return + } + if _, err := x509.ParsePKIXPublicKey(block.Bytes); err != nil { + apiError(c, 503, "keyring_unavailable", "公钥格式无效") + return + } + digest := sha256.Sum256(block.Bytes) + keys = append(keys, gin.H{"key_id": key.KeyID, "public_key_pem": key.PublicKeyPEM, "revoked": key.Revoked, "spki_sha256": hex.EncodeToString(digest[:])}) + } + c.JSON(200, gin.H{"data": gin.H{"current_key_id": a.keyring.Current, "keys": keys}}) +} diff --git a/backend/internal/httpapi/integration_trust_test.go b/backend/internal/httpapi/integration_trust_test.go new file mode 100644 index 0000000..400e4d7 --- /dev/null +++ b/backend/internal/httpapi/integration_trust_test.go @@ -0,0 +1,39 @@ +package httpapi + +import ( + "database/sql" + "github.com/gin-gonic/gin" + "net/http/httptest" + "strings" + "testing" +) + +func TestIntegrationTrust(t *testing.T) { + const public = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAESr8/VFFwt70o7fwa8HiRNyxWtZEu\nUqzCBfYRG72DazEgeOaSVAdlTv49OujtggXSJzFLOluMENJzaRmp9+iJ0Q==\n-----END PUBLIC KEY-----\n" + for _, configured := range []bool{false, true} { + a := &BusinessAPI{} + if configured { + a.keyring = &Keyring{Current: "prod-test", Keys: []VerificationKey{{KeyID: "prod-test", PublicKeyPEM: public}, {KeyID: "old", PublicKeyPEM: public, Revoked: true}}} + } + w := httptest.NewRecorder() + c, _ := gin.CreateTestContext(w) + a.integrationTrust(c) + if !configured { + if w.Code != 503 { + t.Fatal(w.Code) + } + continue + } + body := w.Body.String() + if w.Code != 200 || !strings.Contains(body, "22056bfacc8fe8a240dd2125b629e60ba9f4a5ea0041a1efe034f376a258d562") || !strings.Contains(body, `"revoked":true`) || strings.Contains(body, "PRIVATE KEY") || w.Header().Get("Cache-Control") != "no-store" { + t.Fatal("invalid trust response") + } + } + r := gin.New() + RegisterBusiness(r, &sql.DB{}) + w := httptest.NewRecorder() + r.ServeHTTP(w, httptest.NewRequest("GET", "/api/v1/integration-trust", nil)) + if w.Code != 401 { + t.Fatal("unauthenticated trust access") + } +} diff --git a/docs/client-integration-center.md b/docs/client-integration-center.md index 8a74fd4..ef577e5 100644 --- a/docs/client-integration-center.md +++ b/docs/client-integration-center.md @@ -44,3 +44,11 @@ SDK 与工程提示词默认使用 `https://license.yihuiyong.cn` 作为授权 客户端需按 protocol.md 的“授权文件导入”实现导入入口。接入中心生成的工程提示已包含这一要求,但目前不提供各语言已实现的导入 SDK。客户端应限制文件大小、校验字段及版本、核对受控授权中心允许列表并让用户确认,随后安全保存凭据、在线激活、验签证书和心跳租约。导入不能清空既有时间高水位或静默切换实例。文件本身未签名,不包含可信公钥,不构成离线运行凭证。 升级方案随 JSON、Markdown 和 ZIP 导出,ZIP 新增 upgrade.md(共六个文件)。实例创建响应设置 Cache-Control: no-store;前端不将 secret 写入日志或浏览器持久化存储。 + +## 开发到生产快速交付 + +接入中心展示当前部署公钥环、当前 key_id、撤销标记和 SHA-256(SPKI DER)指纹,支持复制/下载未撤销 PEM。需要管理员会话,公钥接口为 GET /api/v1/integration-trust;未配置 v2 keyring 返回 503。 + +授权实例页可导出 yhy-license-trust v1 JSON:api_base_url、protocol_versions、signature_algorithm、current_key_id、未撤销 keys(含 PEM/指纹)、instance_id、customer_id、product_id。不含 secret;激活凭据另行导出。地址必须由管理员确认,HTTPS 根地址或本机 HTTP 回环地址;本机 API 使用 18081,而非前端 15173。 + +客户端需实现此新格式的严格导入与受保护存储。导入公钥须经过受控渠道确认,不能把发现接口当作信任根;验证签名、key_id、实例/客户/产品绑定、时间及状态。生产切换必须从生产后台重新交付地址、信任和凭据。导出不是证书,不会激活客户端,也不替代后续撤销策略。各语言 SDK 尚未在本仓库实现。 diff --git a/frontend/src/admin-api.ts b/frontend/src/admin-api.ts index 4657eeb..fc7ea49 100644 --- a/frontend/src/admin-api.ts +++ b/frontend/src/admin-api.ts @@ -48,3 +48,7 @@ export function validatePassword(current:string, next:string, confirmation:strin if (next !== confirmation) return '两次输入的新密码不一致' return '' } + +export type TrustKey = {key_id:string;public_key_pem:string;revoked:boolean;spki_sha256:string} +export type IntegrationTrust = {current_key_id:string;keys:TrustKey[]} +export const getIntegrationTrust = () => api.get<{data:IntegrationTrust}>('/integration-trust') diff --git a/frontend/src/views/IntegrationPanel.test.ts b/frontend/src/views/IntegrationPanel.test.ts index afa36fe..901c97b 100644 --- a/frontend/src/views/IntegrationPanel.test.ts +++ b/frontend/src/views/IntegrationPanel.test.ts @@ -1,11 +1,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount } from '@vue/test-utils' import ElementPlus from 'element-plus' -const calls = vi.hoisted(() => ({getIntegrationKit:vi.fn()})) +const calls = vi.hoisted(() => ({getIntegrationKit:vi.fn(),getIntegrationTrust:vi.fn()})) vi.mock('../admin-api', async importOriginal => ({...await importOriginal(),...calls})) import IntegrationPanel from './IntegrationPanel.vue' const kit = {protocol_version:'license-v2 / lease-v2',platform_base_url:'https://license.yihuiyong.cn',language:'python',language_label:'Python 3.11+',mode:'service',mode_label:'常驻服务',endpoints:[{method:'POST',path:'/heartbeat',purpose:'刷新租约'}],security_rules:['不得泄露 secret'],prompt:'实现 LicenseClient',protocol:'DER signature',payload_example:{version:'lease-v2'}} -beforeEach(() => {vi.resetAllMocks();calls.getIntegrationKit.mockResolvedValue({data:{data:kit}})}) +beforeEach(() => {vi.resetAllMocks();calls.getIntegrationTrust.mockResolvedValue({data:{data:{current_key_id:"test",keys:[]}}});calls.getIntegrationKit.mockResolvedValue({data:{data:kit}})}) describe('IntegrationPanel',()=>{ it('loads generated materials and exposes both download formats',async()=>{ const wrapper=mount(IntegrationPanel,{global:{plugins:[ElementPlus]}});await flushPromises() diff --git a/frontend/src/views/IntegrationPanel.vue b/frontend/src/views/IntegrationPanel.vue index e3a6446..29e5d73 100644 --- a/frontend/src/views/IntegrationPanel.vue +++ b/frontend/src/views/IntegrationPanel.vue @@ -1,4 +1,5 @@ +