Skip to content

sign-kms · 架构

组件分层

关键文件

组件文件行号
Server 入口cmd/kms/main.go76-126
Wire DIcmd/kms/wire.go24-32
gRPC 注册internal/server/grpc.go23-56
HTTP 注册internal/server/http.go45-98
KmsServiceinternal/service/kms_service.go20-80
KmsManageServiceinternal/service/kms_manage_service.go26-100
KmsBizinternal/biz/kms_biz.go42-373
KmsManageBizinternal/biz/kms_manage_biz.go45-582
ProviderRegistrypkg/pkcs11/registry.go14-102
SessionPoolpkg/pkcs11/session_pool.go18-95
Auth Claimspkg/auth/claims.go11-49
gRPC Auth Middlewarepkg/auth/grpc_auth.go30-80
HTTP Auth Middlewarepkg/auth/http_auth.go27-56

启动序列

来源:cmd/kms/main.go:76-126

wireApp 的 providers(cmd/kms/wire.go:24-32):

  • server.ProviderSet
  • biz.ProviderSet
  • service.ProviderSet
  • data.ProviderSet
  • pkcs11.NewProviderRegistry
  • newApp

gRPC 中间件链

来源:internal/server/grpc.go:26-28

recovery → signet (tracing/metrics) → GrpcAuthMiddleware → audit

服务注册:RegisterKmsServiceServer(srv, kmsSvr)grpc.go:54)。

mTLS:当 Tls.Enable=trueCaFile 非空时启用客户端证书验证(grpc.go:44-51, 82)。

HTTP 中间件链

来源:internal/server/http.go:49-56

recovery → signet (tracing/metrics) → logging
FilterFunc: HttpAuthMiddleware()

注册:

  • RegisterKmsManageServiceHTTPServer(srv, svc)http.go:70
  • 静态文件:嵌入式 web/dist 或文件系统,SPA fallback(http.go:22-43, 76-95
  • 可观测路由:obs.RegisterRoutes(srv) 注册 /healthz/readyzhttp.go:73

ProviderRegistry

来源:pkg/pkcs11/registry.go

行号
ProviderInstance struct(IDNameTypeLibPathEnabledReadyPoolSizectxpoolsmu14-26
GetContext()29
GetPools()36
GetPoolSize()(取配置 pool_sizeDefaultPoolSize=543-47
ProviderRegistry struct(muproviders maporder []string60-64
NewProviderRegistry(conf.HSM)68
至少 1 个 provider 可用才能继续89-92
register 单个 provider 注册102

SessionPool

来源:pkg/pkcs11/session_pool.go

行号 / 值
SessionPool struct(tokenLabelslotIDuserPinp11pool chan、sizemuclosedstopOncestopCh、OTEL 指标字段)24-41
PoolStatus struct(TokenLabelSlotIDReachableAvailableSessionsTotalSessions44-50
NewSessionPool —— 大小受限、预创建、启动 healthCheckLoop54-95
DefaultPoolSize5(line 18)
MaxPoolSize30(line 19)
HealthCheckInterval30 秒(line 20)
其它方法(Borrow / Return / Discard / WarmupHandleCache实现位置已部分定位,细节 [未核]

JWT 与权限

来源:pkg/auth/claims.go:11-49

go
type KMSClaims struct {
    jwt.RegisteredClaims
    PartitionID string
    Role        string
    Keys        []string
}

角色映射(pkg/auth/claims.go:42-46RoleAllows():49):

Role允许的 operation
readonlyget_public_keyverify
readwritesignverifyget_public_key
adminsignverifyget_public_keygen_keypairsign_certsign_crl

Auth 中间件细节

来源:pkg/auth/grpc_auth.gopkg/auth/http_auth.gopkg/auth/config.go

gRPC(grpc_auth.go:48-80

  • 工厂签名:GrpcAuthMiddleware(store):48),store 可为 nil
  • 流程:解析 Bearer → 验证 JWT 签名 / 过期 → 查 TokenStore.GetByJTI → 若 Disabled 返回 Forbidden;否则用 DB 中的 Keys 覆盖 JWT claims.Keys:62-74)→ 注入到 context。

HTTP(http_auth.go:27-56

  • 仅检查 /api/*,白名单:/api/v1/signinpkg/auth/config.go:11NoAuthPaths)。
  • 从 cookie 读 token → ParseTokenjwt.NewContext

来源:pkg/auth/config.go

默认 / 行号
Cookie 名KMS_AUTH_COOKIEaccess_token(line 15)
SecureCookie由服务器设置(line 19)
authSecretVal懒加载 KMS_AUTH_SECRET(line 22-44;未设置时 panic:44
CookiePath/(line 52)
DefaultExpiry24 小时(管理后台 cookie)(line 55)
DefaultTokenExpiry365 天(KMS access token)(line 58)
MaxTokenExpiry730 天 hard cap(line 60)

DB schema 一览

来源:internal/data/ent/schema/

Entity文件关键字段
Namespacenamespace.go:18-51id (Unique, Immutable)、name (Unique)、token_label (Unique)、user_pin (Sensitive)、so_pin (Sensitive)、provider_iddescription、时间戳
Keypairkeypair.go:30-75id (Unique, Immutable, SKI hex)、tag (MaxLen 128)、algorithmcurvepublic_keypublic_key_pemkey_typestatusnamespace_idmetadata、时间戳;索引 tag+namespace_id
Useruser.go:15-25username (Unique)、password_hashis_active、时间戳
AccessTokenaccess_token.go:25-57id (Unique, Immutable, JTI)、subjectrolekeys (JSON array; 第 37-39 行注明 DB 为权威源)、status enum (active/disabled)、namespace_id FK、expires_at、时间戳

Keypair.id 设计(keypair.go:18-23

id = hex(sha256(0x04||X||Y)),其中 X||Y 是 EC P-256 公钥原始点;同时作为 HSM CKA_LABEL / CKA_ID,不接受用户提供,由服务器计算。

性能数字

仅来自 README.md:119-129 的明文:

出处
单次签名~100us、内存 328 B/opREADME.md:119-129
SoftHSM 天花板~11,000 tps(pool=5 可达)
Luna S790 标称22,000 tps(ECC P-256)

这些为 README 自述数字,未独立基准。