Files
helmfile/pkg/agent/doctor/redact_test.go
T
yxxhero 9b943adc9e feat: add helmfile doctor command for AI-assisted diff analysis (#2660)
* feat: add `helmfile doctor` command for AI-assisted diff analysis

`helmfile doctor` runs `helmfile diff` and asks an OpenAI-compatible LLM to
summarize the changes and flag risks (data loss, security exposure, breaking
changes, downtime, performance, best-practice issues).

Key design decisions:
- When no LLM is configured, doctor is equivalent to `helmfile diff` with
  one exception: --show-secrets is always forced off (secrets never reach
  stdout, even without an LLM).
- Secrets are ALWAYS redacted via two layers: (1) ShowSecrets() forced to
  false so helm-diff emits <REDACTED> placeholders; (2) a defense-in-depth
  text redactor strips residual secret-looking content (Secret YAML blocks,
  sensitive key/value lines, base64 blobs, JWT tokens) before LLM transmission.
- LLM configuration precedence: env (HELMFILE_LLM_*) < helmfile.yaml (llm:)
  < CLI flags (--llm-*).
- Supports any OpenAI-compatible backend (OpenAI, Azure, One-API, LiteLLM,
  Ollama, etc.) with automatic response_format fallback for backends that
  don't support JSON mode.
- Prompt injection defense: release names and environment values are
  JSON-encoded before insertion into the LLM prompt.
- Exit codes: 0 (success/low-risk), 2 (high-risk gate, bypass with --force),
  1 (other errors). Helm-diff's 'detected changes' exit-2 is swallowed.

New packages:
- pkg/agent/llm: OpenAI-compatible client with JSON response parsing, mock
  client for testing, prompt builder with injection defense.
- pkg/agent/doctor: secret redactor (state machine + regex), report renderer
  (markdown + JSON), config resolver (env < yaml < flag merge).

Testing: 70+ unit tests covering redaction patterns, prompt injection,
response_format fallback, JSON parsing, yaml roundtrip, concurrency safety,
panic recovery, and error propagation. go test -race passes.

Documentation: full doctor section in docs/cli.md, llm: block reference in
docs/configuration.md, updated skills/helmfile for AI agents.

Signed-off-by: yxxhero <aiopsclub@163.com>

* docs: fix doctor equivalence wording per PR review

Per review feedback (PR #2660): the docs claimed doctor is 'equivalent to
helmfile diff — same flags, same output, same exit codes' in the unconfigured
path, but this over-promises because:

  1. doctor --output is the report format (not helm-diff's output format)
  2. helm-diff's --output is exposed as --diff-output in doctor
  3. --show-secrets is silently ignored

Updated all three locations (cli.md, cmd/doctor.go Long + godoc, pkg/app/doctor.go
godoc) to say 'falls back to helmfile diff with --show-secrets forced off' and
explicitly note the --output / --diff-output flag difference.

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
2026-06-22 16:52:35 +08:00

208 lines
6.8 KiB
Go

package doctor
import (
"strings"
"testing"
)
func TestSecretRedactor_LeavesNonSecretDiffAlone(t *testing.T) {
in := ` default, my-release, Deployment (apps) has changed:
- spec.replicas: 1
+ spec.replicas: 3
`
out, n := NewSecretRedactor().Redact(in)
if out != in {
t.Errorf("expected no change; got:\n%s", out)
}
if n != 0 {
t.Errorf("replacement count = %d, want 0", n)
}
}
func TestSecretRedactor_PreservesHelmDiffPlaceholder(t *testing.T) {
// helm-diff with ShowSecrets=false already emits "<REDACTED>"; we must
// not double-redact it (no count inflation, output stable).
in := ` default, my-release, Secret (v1) has changed:
- data.password: <REDACTED>
+ data.password: <REDACTED>
`
out, n := NewSecretRedactor().Redact(in)
if out != in {
t.Errorf("expected output unchanged; got:\n%s", out)
}
if n != 0 {
t.Errorf("replacement count = %d, want 0 (already redacted)", n)
}
}
func TestSecretRedactor_CatchesLeakedSensitiveKeyValue(t *testing.T) {
// User accidentally passed --show-secrets; the wrapper forces false but
// if helm-diff still leaks (bug/hook), the text redactor must catch it.
in := ` default, my-release, Secret (v1) has changed:
- data.password: SGVsbG8=
+ data.password: bmV3cGFzcw==
`
out, n := NewSecretRedactor().Redact(in)
if strings.Contains(out, "SGVsbG8=") || strings.Contains(out, "bmV3cGFzcw==") {
t.Errorf("secret value leaked into output:\n%s", out)
}
if n == 0 {
t.Errorf("expected replacements, got 0")
}
// Both lines should be redacted.
if c := strings.Count(out, RedactedPlaceholder); c < 2 {
t.Errorf("expected >=2 placeholders, got %d in:\n%s", c, out)
}
}
func TestSecretRedactor_CatchesFullSecretBlock(t *testing.T) {
// Full Secret YAML as emitted by `helm template` style diffs.
in := `+ # Source: mychart/templates/secret.yaml
+ apiVersion: v1
+ kind: Secret
+ metadata:
+ name: my-secret
+ data:
+ password: cGFzc3dvcmQxMjM=
+ tls.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0t
+ stringData:
+ config.yaml: |
+ database_url: postgres://user:supersecret@db:5432
`
out, n := NewSecretRedactor().Redact(in)
for _, leaked := range []string{"cGFzc3dvcmQxMjM=", "LS0tLS1CRUdJTi", "supersecret"} {
if strings.Contains(out, leaked) {
t.Errorf("leaked %q into output:\n%s", leaked, out)
}
}
if n == 0 {
t.Errorf("expected replacements, got 0")
}
}
func TestSecretRedactor_CatchesFreeFormLongBase64(t *testing.T) {
// Catch a base64-looking token that is not inside a Secret resource and
// not under a sensitive key (e.g. logged by a hook into a ConfigMap).
in := ` + annotations:
+ custom.io/signed-token: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signaturepartgoesheremorethan40charsXX`
out, n := NewSecretRedactor().Redact(in)
if strings.Contains(out, "eyJhbGciOi") {
t.Errorf("JWT-shaped token leaked:\n%s", out)
}
if n == 0 {
t.Errorf("expected replacements, got 0")
}
}
func TestSecretRedactor_DoesNotFlagShortValues(t *testing.T) {
// 10-char strings, resource versions, short labels — must not be flagged.
in := ` - metadata.resourceVersion: 12345
+ metadata.resourceVersion: 67890
- spec.template.metadata.labels.app: short
+ spec.template.metadata.labels.app: other
`
out, n := NewSecretRedactor().Redact(in)
if out != in {
t.Errorf("short values should not be flagged; got:\n%s", out)
}
if n != 0 {
t.Errorf("replacement count = %d, want 0", n)
}
}
func TestSecretRedactor_StripsANSIBeforeComparing(t *testing.T) {
// Color-coded helm diff output where secret was already redacted.
// helm-diff may emit something like "\x1b[31m<REDACTED>\x1b[0m" — we must
// recognize it as already redacted and not double-count.
in := " - data.password: \x1b[31m<REDACTED>\x1b[0m\n + data.password: \x1b[32m<REDACTED>\x1b[0m\n"
out, n := NewSecretRedactor().Redact(in)
if n != 0 {
t.Errorf("expected 0 replacements on colorized already-redacted; got %d", n)
}
if out != in {
t.Errorf("expected input preserved; got:\n%s", out)
}
}
func TestSecretRedactor_HonorsCustomPlaceholder(t *testing.T) {
r := SecretRedactor{Placeholder: "***"}
in := " - data.password: SGVsbG8=\n"
out, n := r.Redact(in)
if !strings.Contains(out, "***") {
t.Errorf("custom placeholder missing in:\n%s", out)
}
if strings.Contains(out, "SGVsbG8=") {
t.Errorf("original value leaked in:\n%s", out)
}
if n == 0 {
t.Errorf("expected replacements, got 0")
}
}
func TestSecretRedactor_HandlesSensitiveKeyVariations(t *testing.T) {
// Different naming conventions all need to be caught.
cases := []string{
" - data.api_key: abc==\n",
" - data.api-key: abc==\n",
" - data.apiKey: abc==\n",
" - data.API_KEY: abc==\n",
" - data.client_secret: xyz==\n",
" - values.refresh-token: r==\n",
" - config.bearer: b==\n",
}
r := NewSecretRedactor()
for _, in := range cases {
out, _ := r.Redact(in)
if strings.Contains(out, "abc==") || strings.Contains(out, "xyz==") || strings.Contains(out, "r==") || strings.Contains(out, "b==") {
t.Errorf("sensitive key not redacted for input %q; got %q", in, out)
}
}
}
// TestSecretRedactor_RealisticMixedDiff is a regression guard against the
// state-machine rewrite of redactSecretBlocks. The earlier regex-based
// implementation leaked "supersecret" inside a Secret resource block when the
// diff also contained unrelated Secret/ConfigMap sections. We reproduce that
// exact input here so the bug cannot come back.
func TestSecretRedactor_RealisticMixedDiff(t *testing.T) {
in := ` default, my-release, Secret (v1) has changed:
- data.password: SGVsbG8=
+ data.password: cGFzc3dvcmQxMjM=
default, api-config, ConfigMap (v1) has changed:
- data.api_key: abcdef==
+ data.api_key: bmV3a2V5
+ data.apiKey: verylongbase64stringthatislongerthan40charsXXXXXXXXXXXXXXX
- data.client_secret: short
+ data.client_secret: NEW
+ annotations:
+ cluster.x-k8s.io/signed-token: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjMifQ.signaturepart
+ # Source: mychart/templates/secret.yaml
+ apiVersion: v1
+ kind: Secret
+ metadata:
+ name: db-secret
+ stringData:
+ url: postgres://user:supersecret@db:5432
`
out, n := NewSecretRedactor().Redact(in)
// The known-bug sentinel must be gone.
if strings.Contains(out, "supersecret") {
t.Errorf("supersecret leaked into output:\n%s", out)
}
// Every other sensitive marker must also be gone.
for _, leak := range []string{"SGVsbG8=", "cGFzc3dvcmQxMjM=", "bmV3a2V5", "eyJhbGc"} {
if strings.Contains(out, leak) {
t.Errorf("secret value %q leaked into output:\n%s", leak, out)
}
}
if n == 0 {
t.Errorf("expected replacements, got 0")
}
// Sanity: the structure marker `kind: Secret` survives so the LLM still
// knows a Secret resource was involved.
if !strings.Contains(out, "kind: Secret") {
t.Errorf("expected `kind: Secret` to survive redaction; got:\n%s", out)
}
}