Skip to content

fix: redact secret values from kdn CLI error logs - #1949

Merged
serbangeorge-m merged 2 commits into
openkaiden:mainfrom
serbangeorge-m:fix/secret-value-exposure-in-logs
May 20, 2026
Merged

serbangeorge-m merged 2 commits into
openkaiden:mainfrom
serbangeorge-m:fix/secret-value-exposure-in-logs

Conversation

@serbangeorge-m

Copy link
Copy Markdown
Contributor

Summary

  • Redacts --value (API key/secret) from console.error output when kdn secret create fails, replacing it with ***
  • Handles the "secret already exists" case silently in KdnCli.createSecret — returns { name } instead of logging an error and forcing callers to catch-and-ignore
  • Simplifies AgentWorkspaceManager.ensureModelSecret by removing its now-unnecessary try/catch for duplicate secrets

Test plan

  • New tests: returns secret name silently when secret already exists, does not log error when secret already exists, rethrows non-"already exists" errors, redacts secret value from error logs on non-"already exists" failure
  • All 72 kdn-cli.spec.ts tests pass
  • All 89 agent-workspace-manager.spec.ts tests pass
  • All 16 secret-manager.spec.ts tests pass
  • Typecheck passes

Closes #1944

Made with Cursor

serbangeorge-m and others added 2 commits May 20, 2026 17:05
When `kdn secret create` fails (e.g. duplicate entry), the error
handler in execCLI logged the full command args including `--value`
with the plaintext API key. Add redactSensitiveArgs() to replace
sensitive flag values with `***` before logging.

Closes openkaiden#1944

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: serbangeorge-m <serbangeorge.m@gmail.com>
Move the "already exists" handling from ensureModelSecret into
KdnCli.createSecret so the error is caught before execCLI logs it.
When a secret already exists, createSecret now returns { name }
silently instead of logging a console.error and forcing callers
to catch-and-ignore.

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: serbangeorge-m <serbangeorge.m@gmail.com>
@serbangeorge-m
serbangeorge-m requested a review from a team as a code owner May 20, 2026 15:43
@serbangeorge-m
serbangeorge-m requested review from bmahabirbu and fbricon and removed request for a team May 20, 2026 15:43
@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR addresses bug #1944 by shifting "already exists" error handling from AgentWorkspaceManager to KdnCli.createSecret(), and adding sensitive argument redaction to error logs. KdnCli now masks --value arguments with *** in error output and gracefully returns success when the secret already exists instead of throwing.

Changes

KdnCli Secret Redaction and Duplicate Handling

Layer / File(s) Summary
KdnCli sensitive argument redaction
packages/main/src/plugin/kdn-cli/kdn-cli.ts
Adds redactSensitiveArgs() helper that scans arguments and replaces values after --value with ***. Updates execCLI error logging to use this helper, preventing secret values from appearing in console output.
KdnCli.createSecret direct execution and "already exists" handling
packages/main/src/plugin/kdn-cli/kdn-cli.ts
Changes createSecret to call exec.exec directly with --output json, bypassing the execCLI wrapper. When error detail contains "already exists", returns { name: options.name } silently; otherwise logs the redacted command before throwing.
KdnCli.createSecret test coverage
packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
Tests updated exec.exec call signatures, multiple optional flag combinations, silent success when secret already exists (no error log), rethrow of non-"already exists" errors, and validation that error logs contain --value *** rather than the plaintext secret.

AgentWorkspaceManager Secret Creation Simplification

Layer / File(s) Summary
AgentWorkspaceManager.ensureModelSecret error handling
packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
Removes try/catch wrapper around secretManager.create() call. Error handling is now delegated to KdnCli.createSecret(), which silently succeeds when the secret already exists.
AgentWorkspaceManager.ensureModelSecret test update
packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
Updates test to verify normal success path when secret already exists, reflecting the assumption that KdnCli now handles this case.

Sequence Diagram

sequenceDiagram
  participant AM as AgentWorkspaceManager
  participant KdnCli
  participant exec as exec.exec
  AM->>KdnCli: createSecret(name, value, ...)
  activate KdnCli
  KdnCli->>exec: exec secret create --output json
  alt Secret created successfully
    exec-->>KdnCli: { name: "..." }
    KdnCli-->>AM: { name: "..." }
  else Secret already exists
    exec-->>KdnCli: error with "already exists"
    KdnCli-->>AM: { name: options.name }
  else Other error
    exec-->>KdnCli: error
    KdnCli->>KdnCli: redactSensitiveArgs()
    KdnCli-->>AM: throw (after logging redacted command)
  end
  deactivate KdnCli
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • openkaiden/kaiden#1513: Both PRs add sensitive CLI argument redaction to kdn-cli.ts error logs to prevent secret exposure.
  • openkaiden/kaiden#1762: Both PRs modify secret-creation paths in AgentWorkspaceManager and KdnCli.createSecret with related error-handling changes for workspace setup.
  • openkaiden/kaiden#1877: Both PRs alter ensureModelSecret in agent-workspace-manager.ts around secret creation error handling behavior.

Suggested reviewers

  • fbricon
  • bmahabirbu
  • jeffmaury
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main objective: redacting secret values from kdn CLI error logs.
Description check ✅ Passed The description directly relates to the changeset, detailing the redaction of sensitive values, handling of duplicate secrets, and simplification of error handling.
Linked Issues check ✅ Passed All coding requirements from issue #1944 are met: secret values are redacted from logs, duplicate secrets are handled gracefully, and error handling is simplified.
Out of Scope Changes check ✅ Passed All changes directly support the stated objectives: redacting sensitive values, handling duplicate secrets, and simplifying error handling. No unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/main/src/plugin/kdn-cli/kdn-cli.ts (1)

337-375: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Use case-insensitive matching for the "already exists" error message.

The test suite (lines 942 and 954 of kdn-cli.spec.ts) confirms the error format as 'secret "my-secret": secret already exists', but the substring check on line 368 is case-sensitive. If the kdn CLI outputs this message with different casing (e.g., "Already exists" or "ALREADY EXISTS"), the graceful handling will fail and throw an error instead. Update the check to detail.toLowerCase().includes('already exists') to be more resilient.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/main/src/plugin/kdn-cli/kdn-cli.ts` around lines 337 - 375, In
createSecret, the check for the CLI "already exists" message is case-sensitive
and can miss variants; update the conditional that examines the extracted error
detail (from extractCliError) to perform a case-insensitive match (e.g. use
detail.toLowerCase().includes('already exists')) so that the method still
returns { name: options.name } when the CLI reports an existing secret
regardless of casing; keep the existing redaction and logging behavior for other
errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@packages/main/src/plugin/kdn-cli/kdn-cli.ts`:
- Around line 337-375: In createSecret, the check for the CLI "already exists"
message is case-sensitive and can miss variants; update the conditional that
examines the extracted error detail (from extractCliError) to perform a
case-insensitive match (e.g. use detail.toLowerCase().includes('already
exists')) so that the method still returns { name: options.name } when the CLI
reports an existing secret regardless of casing; keep the existing redaction and
logging behavior for other errors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 85ef880a-805d-4587-9adc-cef89130ce10

📥 Commits

Reviewing files that changed from the base of the PR and between 93693b4 and 789f5f0.

📒 Files selected for processing (4)
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: unit tests / windows-2025
  • GitHub Check: Linux
  • GitHub Check: smoke-e2e-tests (dev) / ubuntu-24.04 (ollama)
  • GitHub Check: smoke-e2e-tests (prod) / ubuntu-24.04 (ollama)
  • GitHub Check: unit tests / macos-15
  • GitHub Check: unit tests / ubuntu-24.04
  • GitHub Check: linter, formatters
  • GitHub Check: macOS
  • GitHub Check: Windows
  • GitHub Check: typecheck
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use /@/ path aliases instead of relative paths for imports outside the current directory's module group; use relative imports only for sibling modules within the same directory

Files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
packages/main/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

packages/main/src/**/*.{ts,tsx}: Use ipcHandle() to expose handlers in the main process with naming convention <registry-name>:<action> (e.g., container-provider-registry:listContainers)
Use apiSender.send() to send events from main process to renderer for real-time updates
Long-running operations should use TaskManager.createTask() with title and action configuration

Files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
packages/{main,renderer,preload}/src/**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Container operations must include engineId parameter to identify the container engine

Files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
**/*.spec.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.spec.{ts,tsx,js,jsx}: Use test() instead of it() for test cases in Vitest unit tests
Use vi.mock(import('...')) for auto-mocking modules in unit tests; avoid manual mock factories when possible
Use vi.resetAllMocks() in beforeEach hooks instead of vi.clearAllMocks() for resetting mocks between tests
When an auto-mocked function or class method needs a real implementation, use vi.mocked(...) with the prototype pattern for class methods: vi.mocked(MyClass.prototype.myMethod).mockImplementation(...)

Files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
🧠 Learnings (2)
📚 Learning: 2026-03-09T08:47:09.657Z
Learnt from: benoitf
Repo: kortex-hub/kortex PR: 1077
File: packages/main/src/plugin/skill/skill-manager.ts:80-109
Timestamp: 2026-03-09T08:47:09.657Z
Learning: In the kortex-hub/kortex repository, IPC handlers (via ipcHandle()) may be registered directly inside feature manager/service classes (e.g., SkillManager in packages/main/src/plugin/skill/skill-manager.ts) rather than exclusively in packages/main/src/plugin/index.ts. Treat this as an accepted design pattern for files under the plugin directory. Reviewers should not require centralization in index.ts; allow IPC registration proximity to the feature that owns the handler. When reviewing code, accept direct ipcHandle() registrations inside feature managers and ensure the pattern is consistently applied across similar feature-manager modules.

Applied to files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
📚 Learning: 2026-05-12T17:14:02.153Z
Learnt from: MarsKubeX
Repo: openkaiden/kaiden PR: 1850
File: packages/renderer/src/lib/agent-workspaces/AgentWorkspaceList.svelte:66-70
Timestamp: 2026-05-12T17:14:02.153Z
Learning: When reviewing code that uses `AgentWorkspaceSummaryUI.runtime`, treat it as a required, non-null `string` per the `openkaiden/kdn-api` 0.12.0 schema. Therefore, code like `a.runtime.localeCompare(b.runtime)` is safe and should not trigger warnings about possible `undefined`/`null` values or suggestions to use nullish coalescing/optional chaining for `runtime` (unless the current local types still mark `runtime` as optional, indicating a schema/version mismatch).

Applied to files:

  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts
  • packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.ts
  • packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts
🪛 OpenGrep (1.21.0)
packages/main/src/plugin/kdn-cli/kdn-cli.ts

[ERROR] 364-364: Dynamic command passed to child_process.exec/execSync. Use child_process.execFile or spawn with an argument array instead.

(coderabbit.command-injection.exec-js)

🔇 Additional comments (6)
packages/main/src/plugin/kdn-cli/kdn-cli.ts (2)

307-321: LGTM!


331-332: LGTM!

packages/main/src/plugin/kdn-cli/kdn-cli.spec.ts (2)

871-881: LGTM!

Also applies to: 890-890, 898-898, 906-906, 925-925


939-985: LGTM!

packages/main/src/plugin/agent-workspace/agent-workspace-manager.ts (1)

152-152: LGTM!

packages/main/src/plugin/agent-workspace/agent-workspace-manager.spec.ts (1)

643-655: LGTM!

Also applies to: 657-667

@codecov

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@fbricon fbricon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably make redactSensitiveArgs more generic and pass the sensitive flags as args, and have a redactSecretArgs function wrap it for ['--value'], but that's unnecessary for now as it's only used for secrets, so maybe overkill for now. We can always refactor it if/when needed.

@serbangeorge-m
serbangeorge-m merged commit 7f51c20 into openkaiden:main May 20, 2026
15 checks passed
@serbangeorge-m
serbangeorge-m deleted the fix/secret-value-exposure-in-logs branch May 21, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'kdn secret create' fails on duplicate entry and exposes unmasked secret value in console logs

2 participants