-
Notifications
You must be signed in to change notification settings - Fork 26
fix: Resolve Cognito authentication issues for CLI credential process #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Resolve Cognito authentication issues for CLI credential process #72
Conversation
This commit fixes three critical authentication issues: 1. Cognito User Pool client secret requirement - Added GenerateSecret: false to cognito-user-pool-setup.yaml - CLI apps are public clients and use PKCE instead of client secrets - Previous behavior: Token exchange failed with "invalid_client_secret" 2. Path expansion in Claude Code settings - Changed ~ to $HOME in install.sh and install.bat scripts - Claude Code doesn't expand ~ for otelHeadersHelper setting - Added __CREDENTIAL_PROCESS_PATH__ placeholder for consistency - Both helpers now use absolute paths after installation 3. Credential process recursive loop with Direct STS - Added environment variable clearing in get_aws_credentials_direct() - Prevents infinite recursion when AWS_PROFILE is set - Matches existing pattern in get_aws_credentials_cognito() - Previous behavior: Credential process called itself infinitely All three issues prevented successful authentication with Cognito User Pool and Direct STS federation. Authentication now works correctly for CLI and Claude Code integration. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Testing Summary: Environment Variable Clearing FixUnderstanding Your Use CaseWe interpreted your concern as this workflow:
Your concern: Does the env clearing in How We TestedTest 1: Verify the recursion bug exists (without fix) # Branch: fix/cognito-authentication-issues-no-env-clear
rm -f ~/.aws/credentials
AWS_PROFILE=ClaudeCode aws sts get-caller-identity --debugResult: Hung for 43 seconds, then failed with nested error: The error shows "custom-process" twice - proving the recursion. Test 2: Verify the fix works # Branch: fix/cognito-authentication-issues (with env clearing)
rm -f ~/.aws/credentials
AWS_PROFILE=ClaudeCode aws sts get-caller-identityResult: Browser opened, authenticated, returned: {
"UserId": "AROAXTX6EUZAUCUEJCHZ6:claude-code-...",
"Account": "523445249601",
"Arn": "arn:aws:sts::523445249601:assumed-role/BedrockCognitoFederatedRole/..."
}Test 3: Verify exported credentials survive credential-process call export AWS_ACCESS_KEY_ID="AKIAXTX6EUZA4NG4GXME"
export AWS_SECRET_ACCESS_KEY="kIJ1io..."
echo "Before: AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
~/claude-code-with-bedrock/credential-process --profile ClaudeCode > /dev/null
echo "After: AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID"
aws sts get-caller-identityResult: Exported credentials unchanged. Still returns Test 4: Full Claude Code integration (with OTEL enabled) export AWS_ACCESS_KEY_ID="AKIAXTX6EUZA4NG4GXME"
export AWS_SECRET_ACCESS_KEY="kIJ1io..."
export AWS_PROFILE=ClaudeCode
claudeInside Claude Code: Result: Claude Code authenticated to Bedrock via Cognito, but CLI commands used the exported Why The Change Is SafeProcess Isolation: When a subprocess modifies environment variables, those changes only exist in the subprocess's memory. The parent shell is never affected. This is fundamental Unix process isolation. Additionally, the fix restores the env vars in a Risk of NOT Having This ChangeWithout the env clearing, the recursion bug occurs:
This breaks fresh authentication entirely when Summary
The fix is required for basic functionality and does not affect your workflow. |
|
Cherry picked in the parts that hadn't already been fixed. Everything should be live now. Thanks! |
This PR fixes three critical authentication issues that prevented successful CLI authentication when using Cognito User Pools with Direct STS federation.
Problems Fixed
Symptom: Token exchange failed with error:
Error: Token exchange failed: {"error":"invalid_client","error_description":"invalid_client_secret"}
Root Cause: CloudFormation template created Cognito client with a client secret by default. CLI applications are public clients and must use PKCE (Proof Key for Code
Exchange) instead of client secrets.
Fix: Added GenerateSecret: false to cognito-user-pool-setup.yaml UserPoolClient resource.
Symptom: Claude Code failed to start with error:
error: otelHeadersHelper did not return a valid value
Root Cause: The install scripts used ~ for helper paths, but Claude Code doesn't expand ~ for the otelHeadersHelper setting (though it does for awsAuthRefresh).
Fix:
Symptom: Authentication hung with recursive error:
Error: Failed to get AWS credentials via Direct STS: Error when retrieving credentials from custom-process:
Root Cause: When using Direct STS federation, the credential-process created an STS client without clearing the AWS_PROFILE environment variable. Boto3 saw the profile,
which pointed back to the credential-process, causing infinite recursion.
Fix: Added environment variable clearing logic to get_aws_credentials_direct() method, matching the pattern already used in get_aws_credentials_cognito().
Changes Made
Files Modified
- Added GenerateSecret: false to UserPoolClient
- Changed ~ to $HOME in install.sh sed command
- Changed ~ to $HOME in install.bat PowerShell command
- Added CREDENTIAL_PROCESS_PATH placeholder
- Updated placeholder replacement logic for both helpers
- Added environment variable clearing before STS client creation
- Added finally block to restore environment variables
- Matches existing pattern in Cognito Identity Pool path
Testing
Before these fixes:
After these fixes:
Test Steps:
1. Deploy fresh Cognito User Pool (creates public client)
cd source && poetry run ccwb init
poetry run ccwb deploy
2. Package and install
poetry run ccwb package
cd dist && ./install.sh
3. Test authentication
AWS_PROFILE=ClaudeCode aws sts get-caller-identity
Should show assumed-role/BedrockCognitoFederatedRole
4. Test Claude Code integration
claude
Should start successfully and authenticate
Impact
Related Issues
Discovered during GovCloud partition support testing (#PR_NUMBER).