fix(docreader/auth): harden gRPC TLS/Token rollout from #1359

Follow-up to #1359. Addresses a set of correctness and security gaps in
the initial docreader auth implementation.

- docker-compose: inject GRPC_TLS_*/GRPC_TLS_SERVER_NAME/GRPC_AUTH_TOKEN
  into the WeKnora-app service. Without this the Go client never saw the
  knobs, so enabling token auth on the server broke every RPC.
- client: bind tokenAuth.RequireTransportSecurity() to TLSEnabled so a
  bearer token cannot be sent over an insecure channel once TLS is on.
- server: load_tls_credentials now raises TLSConfigError on misconfig
  (cert/key missing, file unreadable, mTLS without CA); main.py exits 1
  instead of silently downgrading to insecure.
- server: replace endswith("/Check"|"/Watch") health bypass with exact
  match against /grpc.health.v1.Health/{Check,Watch}.
- server: compare tokens with hmac.compare_digest, warn on tokens < 16B.
- server: AuthInterceptor now returns an abort handler matching the
  original RPC kind (unary/stream) and uses context.abort, so streaming
  RPCs surface UNAUTHENTICATED instead of INTERNAL.
- internal/infrastructure/docparser/grpc_parser.go: drop the duplicated
  TLS/tokenAuth block and reuse docreader/client.LoadAuthConfigFromEnv +
  BuildDialOptions. Single source of truth for client-side auth.
- Add GRPC_TLS_SERVER_NAME (client SNI override) and
  GRPC_MTLS_REQUIRE_CLIENT_CERT (server explicit mTLS toggle); document
  the differing CA semantics between client and server in .env*.example.
- Reject half-configured client mTLS (cert XOR key) loudly.
- Fix missing trailing newline in .env.lite.example.

Verified locally: go build ./... and go vet ./... clean; auth.py
fail-fast / token paths smoke-tested.
This commit is contained in:
wizardchen
2026-05-16 21:39:10 +08:00
committed by lyingbug
parent 5a02e22f52
commit a3411899cf
8 changed files with 208 additions and 126 deletions
+9 -4
View File
@@ -439,12 +439,17 @@ DOCREADER_TRANSPORT=grpc
# gRPC TLS 配置(可选)
# GRPC_TLS_ENABLED=false
# GRPC_TLS_CERT=/path/to/server.crt
# GRPC_TLS_KEY=/path/to/server.key
# GRPC_TLS_CA=/path/to/ca.crt
# GRPC_TLS_CERT=/path/to/server.crt # server: 必填;client: mTLS 时填
# GRPC_TLS_KEY=/path/to/server.key # 同上
# GRPC_TLS_CA=/path/to/ca.crt # client: 校验 server 证书用;server: mTLS 时签发客户端证书的 CA
# GRPC_TLS_SERVER_NAME= # 可选;当 DOCREADER_ADDR 用 IP/与证书 CN 不一致时设为证书的 hostname
# GRPC_MTLS_REQUIRE_CLIENT_CERT=false # server 端:true 时强制要求客户端证书(mTLS)
# gRPC 认证 Token(可选,客户端和服务端需配置相同的值)
# GRPC_AUTH_TOKEN=your-secret-token
# 注意:启用 GRPC_AUTH_TOKEN 时强烈建议同时启用 TLS,否则 token 会以明文随
# 每次 RPC 在网络上传输。当 GRPC_TLS_ENABLED=true 时客户端会拒绝在不安全
# 的连接上发送 token。
# GRPC_AUTH_TOKEN=your-secret-token-at-least-16-bytes
# Docreader 中 DOCX 解析的最大页数,默认 0(不限制)
# 设为正整数(如 500)可限制超大 Word 文档的解析开销;超过页数的内容将不会继续解析