feat(claude-code): add api_key_helper for short-lived credentials; mark claude_api_key sensitive#865
Open
morganl-ant wants to merge 1 commit intocoder:mainfrom
Open
Conversation
…itive; stop writing primaryApiKey to ~/.claude.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The module currently treats the Anthropic API key as a static, long-lived secret:
claude_api_keyis not markedsensitive, so it can appear in plain text in Terraform plan output and CI logs (onlyclaude_code_oauth_tokenhad the flag).install.shwrites the key to~/.claude.jsonasprimaryApiKey. The CLI already reads the key from theANTHROPIC_API_KEY/CLAUDE_API_KEYenvironment variable that this module sets viacoder_env, so the disk write is redundant and leaves a credential at rest in a user-readable file.pre_install_scriptand still end up with a static value in TF state.Changes
api_key_helperinput ({ script, ttl_ms }). The script is written to~/.claude/coder-api-key-helper.sh(mode 0700) and registered via the Claude CodeapiKeyHelpersetting at/etc/claude-code/managed-settings.d/20-coder-apikeyhelper.json. Claude invokes the helper whenever it needs a key and caches the result forttl_ms(default 5 minutes), so the credential never lands in TF state, the agent environment, or~/.claude.json.coder_envforCLAUDE_CODE_API_KEY_HELPER_TTL_MSwhenapi_key_helperis set.claude_api_keynow hassensitive = trueand an updated description.install.shno longer writesprimaryApiKeyto~/.claude.json. Onboarding-bypass keys (hasCompletedOnboarding,hasTrustDialogAccepted, etc.) are kept so the CLI still starts non-interactively. The early-return guard is widened so onboarding bypass also runs whenapi_key_helperis the auth source.api_key_helperis mutually exclusive withclaude_api_key,claude_code_oauth_token, andenable_aibridge.This is install-time configuration only, so it composes cleanly with the direction in #861.
Validation
terraform fmtcleanterraform validatecleanterraform test: 23/23 pass (4 new:test_api_key_helper,test_api_key_helper_default_ttl,test_api_key_helper_validation_with_api_key,test_api_key_helper_validation_with_aibridge)bun test: 2 new tests pass (api-key-helper,claude-api-key-not-written-to-claude-json); existing suite untouchedshellcheck --severity=warning scripts/install.shcleanDisclosure
I work at Anthropic on the Claude Code team. This change is part of a small batch of upstream contributions based on what we have seen enterprise customers ask for; happy to adjust scope or split further if that helps review.