-
Notifications
You must be signed in to change notification settings - Fork 63
CM-61986-add-mcp-and-email-enrichment-from-claude-json #421
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
Open
RoniCycode
wants to merge
2
commits into
main
Choose a base branch
from
CM-61986-enrich-data-for-guardrails
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| """Reader for ~/.claude.json configuration file. | ||
|
|
||
| Extracts user email from the Claude Code global config file | ||
| for use in AI guardrails scan enrichment. | ||
| """ | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| from cycode.logger import get_logger | ||
|
|
||
| logger = get_logger('AI Guardrails Claude Config') | ||
|
|
||
| _CLAUDE_CONFIG_PATH = Path.home() / '.claude.json' | ||
|
|
||
|
|
||
| def load_claude_config(config_path: Optional[Path] = None) -> Optional[dict]: | ||
| """Load and parse ~/.claude.json. | ||
|
|
||
| Args: | ||
| config_path: Override path for testing. Defaults to ~/.claude.json. | ||
|
|
||
| Returns: | ||
| Parsed dict or None if file is missing or invalid. | ||
| """ | ||
| path = config_path or _CLAUDE_CONFIG_PATH | ||
| if not path.exists(): | ||
| logger.debug('Claude config file not found', extra={'path': str(path)}) | ||
| return None | ||
| try: | ||
| content = path.read_text(encoding='utf-8') | ||
| return json.loads(content) | ||
| except Exception as e: | ||
| logger.debug('Failed to load Claude config file', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def get_user_email(config: dict) -> Optional[str]: | ||
| """Extract user email from Claude config. | ||
|
|
||
| Reads oauthAccount.emailAddress from the config dict. | ||
| """ | ||
| return config.get('oauthAccount', {}).get('emailAddress') | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """Tests for Claude Code config file reader.""" | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| from pyfakefs.fake_filesystem import FakeFilesystem | ||
|
|
||
| from cycode.cli.apps.ai_guardrails.scan.claude_config import get_user_email, load_claude_config | ||
|
|
||
|
|
||
| def test_load_claude_config_valid(fs: FakeFilesystem) -> None: | ||
| """Test loading a valid ~/.claude.json file.""" | ||
| config = {'oauthAccount': {'emailAddress': 'user@example.com'}} | ||
| config_path = Path.home() / '.claude.json' | ||
| fs.create_file(config_path, contents=json.dumps(config)) | ||
|
|
||
| result = load_claude_config(config_path) | ||
| assert result == config | ||
|
|
||
|
|
||
| def test_load_claude_config_missing_file(fs: FakeFilesystem) -> None: | ||
| """Test loading when ~/.claude.json does not exist.""" | ||
| fs.create_dir(Path.home()) | ||
| config_path = Path.home() / '.claude.json' | ||
|
|
||
| result = load_claude_config(config_path) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_load_claude_config_corrupt_file(fs: FakeFilesystem) -> None: | ||
| """Test loading when ~/.claude.json contains invalid JSON.""" | ||
| config_path = Path.home() / '.claude.json' | ||
| fs.create_file(config_path, contents='not valid json {{{') | ||
|
|
||
| result = load_claude_config(config_path) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_get_user_email_present() -> None: | ||
| """Test extracting email when oauthAccount.emailAddress exists.""" | ||
| config = {'oauthAccount': {'emailAddress': 'user@example.com'}} | ||
| assert get_user_email(config) == 'user@example.com' | ||
|
|
||
|
|
||
| def test_get_user_email_missing_oauth_account() -> None: | ||
| """Test extracting email when oauthAccount key is missing.""" | ||
| config = {'someOtherKey': 'value'} | ||
| assert get_user_email(config) is None | ||
|
|
||
|
|
||
| def test_get_user_email_missing_email_address() -> None: | ||
| """Test extracting email when oauthAccount exists but emailAddress is missing.""" | ||
| config = {'oauthAccount': {'someOtherField': 'value'}} | ||
| assert get_user_email(config) is None |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally you would account for windows systems also