From b3898eb10193a218b0aa7507065e01ed40582d53 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 29 Apr 2026 19:43:35 +0800 Subject: [PATCH] fix(helm): preserve SYSTEM_AES_KEY/TENANT_AES_KEY across upgrades The Secret template defaulted both keys to randAlphaNum 32, which Helm re-rolls on every template render. As a result, any `helm upgrade` without explicit secrets.systemAesKey / secrets.tenantAesKey rotated the keys, breaking decryption of every previously encrypted field (tenants.api_key, model API keys, vector store credentials, web search provider keys, WeKnoraCloud.AppSecret) and surfacing "enc:v1:..." ciphertext in the UI. Use Helm's `lookup` to reuse the values stored in the existing Secret when one is already present, falling back to randAlphaNum only on first install. Also document the recovery caveat in values.yaml so operators understand the risk of relying on the auto-generated value. --- helm/templates/secrets.yaml | 26 +++++++++++++++++++++++--- helm/values.yaml | 15 +++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/helm/templates/secrets.yaml b/helm/templates/secrets.yaml index 531d729f..e09e9de5 100644 --- a/helm/templates/secrets.yaml +++ b/helm/templates/secrets.yaml @@ -11,10 +11,30 @@ IMPORTANT: For production deployments, use one of these approaches: 4. Provide an existing secret via secrets.existingSecret */}} {{- if not .Values.secrets.existingSecret }} +{{/* +Reuse the previously generated random keys on upgrade. Without this lookup, +randAlphaNum would re-roll on every `helm upgrade` and rotate SYSTEM_AES_KEY / +TENANT_AES_KEY, making any data encrypted with the old key unreadable +(e.g. tenants.api_key would surface as enc:v1:... in the UI). +*/}} +{{- $secretName := include "weknora.secretName" . }} +{{- $existing := lookup "v1" "Secret" .Release.Namespace $secretName }} +{{- $existingTenantKey := "" }} +{{- $existingSystemKey := "" }} +{{- if and $existing $existing.data }} + {{- if index $existing.data "TENANT_AES_KEY" }} + {{- $existingTenantKey = index $existing.data "TENANT_AES_KEY" | b64dec }} + {{- end }} + {{- if index $existing.data "SYSTEM_AES_KEY" }} + {{- $existingSystemKey = index $existing.data "SYSTEM_AES_KEY" | b64dec }} + {{- end }} +{{- end }} +{{- $tenantAesKey := .Values.secrets.tenantAesKey | default $existingTenantKey | default (randAlphaNum 32) }} +{{- $systemAesKey := .Values.secrets.systemAesKey | default $existingSystemKey | default (randAlphaNum 32) }} apiVersion: v1 kind: Secret metadata: - name: {{ include "weknora.secretName" . }} + name: {{ $secretName }} namespace: {{ .Release.Namespace }} labels: {{- include "weknora.labels" . | nindent 4 }} @@ -29,8 +49,8 @@ stringData: REDIS_PASSWORD: {{ required "secrets.redisPassword is required" .Values.secrets.redisPassword | quote }} # Application secrets JWT_SECRET: {{ required "secrets.jwtSecret is required" .Values.secrets.jwtSecret | quote }} - TENANT_AES_KEY: {{ .Values.secrets.tenantAesKey | default (randAlphaNum 32) | quote }} - SYSTEM_AES_KEY: {{ .Values.secrets.systemAesKey | default (randAlphaNum 32) | quote }} + TENANT_AES_KEY: {{ $tenantAesKey | quote }} + SYSTEM_AES_KEY: {{ $systemAesKey | quote }} {{- if .Values.neo4j.enabled }} # Neo4j credentials (for GraphRAG) NEO4J_USERNAME: {{ .Values.neo4j.username | quote }} diff --git a/helm/values.yaml b/helm/values.yaml index 70aea7b8..f0504a48 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -392,9 +392,20 @@ secrets: redisPassword: "" # -- JWT signing secret (REQUIRED: change in production) jwtSecret: "" - # -- Tenant AES encryption key + # -- Tenant AES encryption key (32 bytes). + # STRONGLY RECOMMENDED to set this explicitly in production. + # If left empty, a random 32-char value is generated on first install and + # reused on subsequent `helm upgrade` via Secret lookup. However, if the + # Secret is ever deleted/recreated (e.g. namespace recreated, GitOps prune, + # cluster rebuilt) a new random value will be generated and any data + # encrypted with the old key becomes UNRECOVERABLE. tenantAesKey: "" - # -- System AES-256 key for database API key encryption (32 bytes) + # -- System AES-256 key for database field encryption (must be exactly 32 bytes). + # Encrypts: tenants.api_key, model API keys, vector store credentials, + # web search provider keys, WeKnoraCloud.AppSecret. + # STRONGLY RECOMMENDED to set this explicitly in production. Same caveat as + # tenantAesKey: losing this key makes all encrypted fields unreadable + # (the UI will display ciphertext like "enc:v1:..." instead of plaintext). systemAesKey: "" # -- Use existing secret instead of creating one