-
Notifications
You must be signed in to change notification settings - Fork 419
7.2 ansieng 4330 #2159
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
ishikaa-p
wants to merge
36
commits into
7.4.x
Choose a base branch
from
7.2-ANSIENG-4330
base: 7.4.x
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.
Open
7.2 ansieng 4330 #2159
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
89ef366
[ANSIENG-4330] | mask secrets
ishikaa-p 033cdd8
[ANSIENG-4330] | mask authorization bearer token
ishikaa-p d644554
[ANSIENG-4330] | code review fix
ishikaa-p 081a8d3
[ANSIENG-4330] | ci check for secret leak
ishikaa-p 200ee57
[ANSIENG-4330] | Merge branch '7.2.x' of github.com:confluentinc/cp-a…
ishikaa-p 6326331
[ANSIENG-4330] | syntax fix
ishikaa-p 4f9e567
[ANSIENG-4330] | syntax fix
ishikaa-p c6fd48a
[ANSIENG-4330] | auth_indicators = ['Authorization']
ishikaa-p 0630143
[ANSIENG-4330] | testing secret leak
ishikaa-p f17958e
[ANSIENG-4330] | code refactored
ishikaa-p 85aeac0
[ANSIENG-4330] | shebang changed
ishikaa-p 5887ff8
[ANSIENG-4330] | fix
ishikaa-p 2b3c61a
[ANSIENG-4330] | whitespace added
ishikaa-p 4dc083e
[ANSIENG-4330] | lint fix
ishikaa-p 584a5db
[ANSIENG-4330] | Add URI authorization sanity check script
ishikaa-p 1a3d4ba
[ANSIENG-4330] | Add URI authorization sanity check script
ishikaa-p 83f2e3c
[ANSIENG-4330] | testing
ishikaa-p ebccf5b
[ANSIENG-4330] | debug mode
ishikaa-p 2fb3f11
[ANSIENG-4330] | debug
ishikaa-p 6a7b149
[ANSIENG-4330] | debug
ishikaa-p e5cc7fd
[ANSIENG-4330] | fix
ishikaa-p 3d81a1f
[ANSIENG-4330] | test
ishikaa-p 5b5de7e
[ANSIENG-4330] | test
ishikaa-p 91e1da9
[ANSIENG-4330] | test sanity check
ishikaa-p 7ef2ca3
[ANSIENG-4330] | debug
ishikaa-p 60e3a9f
[ANSIENG-4330] | test
ishikaa-p 09528ea
[ANSIENG-4330] | test
ishikaa-p 750afbd
[ANSIENG-4330] | test
ishikaa-p 06a7610
[ANSIENG-4330] | revert testing changes
ishikaa-p 536b027
[ANSIENG-4330] | optimize python files
ishikaa-p d3e96c4
[ANSIENG-4330] | remove trailing spaces
ishikaa-p 75bc1c3
[ANSIENG-4330] | sanity fix
ishikaa-p a9e0606
[ANSIENG-4330] | correction
ishikaa-p feef424
[ANSIENG-4330] | testing auth token leak
ishikaa-p 707b8c1
[ANSIENG-4330] | reverting testing changes
ishikaa-p a556aea
[ANSIENG-4330] | removing double hashtag
ishikaa-p 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
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,202 @@ | ||
| """ | ||
| set_fact Secret Leak Sanity Check | ||
| Scans git diff for set_fact tasks in changed lines to prevent potential secret leakage. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| import re | ||
| import subprocess | ||
| import yaml | ||
|
|
||
|
|
||
| def get_base_branch(): | ||
| """Determine the base branch for comparison.""" | ||
| # Priority: Environment variable > galaxy.yml version > fallback | ||
| base_branch = os.environ.get('BASE_BRANCH') | ||
| if base_branch: | ||
| return base_branch | ||
|
|
||
| try: | ||
| with open('galaxy.yml', 'r') as f: | ||
| galaxy_content = yaml.safe_load(f) | ||
|
|
||
| version = galaxy_content.get('version', '') | ||
| version_parts = version.split('.') | ||
|
|
||
| if len(version_parts) >= 2: | ||
| return f"{version_parts[0]}.{version_parts[1]}.x" | ||
|
|
||
| return version | ||
| except Exception as e: | ||
| print(f"Warning: Could not read galaxy.yml: {e}") | ||
| return None | ||
|
|
||
|
|
||
| def fetch_remote_branches(collection_root): | ||
| """Fetch remote branches to ensure we have the latest refs.""" | ||
| print("Fetching remote branches...") | ||
|
|
||
| # Try comprehensive fetch first, then basic fetch as fallback | ||
| fetch_commands = [ | ||
| ['git', 'fetch', 'origin', '+refs/heads/*:refs/remotes/origin/*'], | ||
| ['git', 'fetch', 'origin'] | ||
| ] | ||
|
|
||
| for cmd in fetch_commands: | ||
| result = subprocess.run(cmd, capture_output=True, text=True, cwd=collection_root, check=False) | ||
| if result.returncode == 0: | ||
| return True | ||
|
|
||
| print("Warning: Could not fetch remote branches") | ||
| return False | ||
|
|
||
|
|
||
| def find_valid_base_branch(base_branch, collection_root): | ||
| """Find a valid base branch that exists in the repository.""" | ||
| # First try the suggested base branch | ||
| if base_branch: | ||
| result = subprocess.run( | ||
| ['git', 'rev-parse', f'origin/{base_branch}'], | ||
| capture_output=True, text=True, cwd=collection_root, check=False | ||
| ) | ||
| if result.returncode == 0: | ||
| print(f"Using base branch: {base_branch}") | ||
| return base_branch | ||
|
|
||
| # Try common fallback branches | ||
| fallback_branches = ['main', 'master', 'develop'] | ||
| for branch in fallback_branches: | ||
| result = subprocess.run( | ||
| ['git', 'rev-parse', f'origin/{branch}'], | ||
| capture_output=True, text=True, cwd=collection_root, check=False | ||
| ) | ||
| if result.returncode == 0: | ||
| print(f"Using fallback base branch: {branch}") | ||
| return branch | ||
|
|
||
| print("No valid base branch found") | ||
| return None | ||
|
|
||
|
|
||
| def get_git_diff(base_branch, collection_root): | ||
| """Get the git diff content for analysis.""" | ||
| current_branch = os.environ.get('SEMAPHORE_GIT_PR_BRANCH', 'HEAD') | ||
| print(f"Comparing {current_branch} against base branch: {base_branch}") | ||
|
|
||
| # Build command list based on available base branch | ||
| if base_branch: | ||
| commands = [ | ||
| ['git', 'diff', f'origin/{base_branch}...HEAD'], | ||
| ['git', 'diff', f'origin/{base_branch}', 'HEAD'], | ||
| ['git', 'diff', f'origin/{base_branch}..HEAD'] | ||
| ] | ||
| else: | ||
| commands = [ | ||
| ['git', 'diff', 'HEAD~1...HEAD'], | ||
| ['git', 'diff', 'HEAD~5...HEAD'] | ||
| ] | ||
|
|
||
| # Try each command until one succeeds | ||
| for cmd in commands: | ||
| result = subprocess.run(cmd, capture_output=True, text=True, cwd=collection_root, check=False) | ||
| if result.returncode == 0: | ||
| print(f"Successfully got diff using: {' '.join(cmd)}") | ||
| return result.stdout | ||
| print(f"Failed command: {' '.join(cmd)}") | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def parse_diff_for_setfact(diff_content): | ||
| """ | ||
| Parse git diff to find set_fact usage in added/modified lines. | ||
|
|
||
| Returns list of issues found in the format: | ||
| [{'file': str, 'line': int, 'content': str, 'message': str}, ...] | ||
| """ | ||
| issues = [] | ||
| current_file = None | ||
| line_number = 0 | ||
|
|
||
| for line in diff_content.split('\n'): | ||
| if line.startswith('diff --git'): | ||
| # Extract target filename: "diff --git a/file.yml b/file.yml" | ||
| parts = line.split() | ||
| if len(parts) >= 4: | ||
| current_file = parts[3][2:] # Remove "b/" prefix | ||
| line_number = 0 | ||
|
|
||
| elif line.startswith('@@'): | ||
| # Parse hunk header: @@ -old_start,old_count +new_start,new_count @@ | ||
| match = re.search(r'@@\s*-\d+(?:,\d+)?\s*\+(\d+)(?:,(\d+))?\s*@@', line) | ||
| if match: | ||
| line_number = int(match.group(1)) - 1 # Will increment before checking | ||
|
|
||
| elif line.startswith('+') and not line.startswith('+++'): | ||
| # Added line - check for set_fact usage | ||
| line_number += 1 | ||
| line_content = line[1:] # Remove '+' prefix | ||
|
|
||
| if (current_file and current_file.endswith(('.yml', '.yaml')) and ('set_fact:' in line_content or 'ansible.builtin.set_fact:' in line_content)): | ||
|
|
||
| issues.append({ | ||
| 'file': current_file, | ||
| 'line': line_number, | ||
| 'content': line_content.strip(), | ||
| 'message': 'set_fact task found in changed lines - please verify no sensitive data is exposed' | ||
| }) | ||
|
|
||
| elif not line.startswith('-') and line_number > 0: | ||
| # Context line - increment line counter | ||
| line_number += 1 | ||
|
|
||
| return issues | ||
|
|
||
|
|
||
| def main(): | ||
| """Main function to run the set_fact sanity check.""" | ||
| collection_root = os.environ.get('PATH_TO_CPA', os.getcwd()) | ||
|
|
||
| try: | ||
| # Get base branch and fetch remote refs | ||
| base_branch = get_base_branch() | ||
| fetch_remote_branches(collection_root) | ||
| valid_base_branch = find_valid_base_branch(base_branch, collection_root) | ||
|
|
||
| # Get and parse git diff | ||
| diff_content = get_git_diff(valid_base_branch, collection_root) | ||
| if not diff_content: | ||
| print("Error: Could not retrieve git diff") | ||
| return 0 | ||
|
|
||
| issues = parse_diff_for_setfact(diff_content) | ||
|
|
||
| # Report results | ||
| if not issues: | ||
| print("✅ No set_fact tasks found in changed lines.") | ||
| return 0 | ||
|
|
||
| print(f"⚠️ Found {len(issues)} set_fact tasks in changed lines:") | ||
| print("=" * 60) | ||
|
|
||
| for issue in issues: | ||
| print(f"File: {issue['file']}") | ||
| print(f"Line: ~{issue['line']}") | ||
| print(f"Content: {issue['content']}") | ||
| print(f"Issue: {issue['message']}") | ||
| print("-" * 40) | ||
|
|
||
| print("\n⚠️ IMPORTANT: Please manually verify these set_fact tasks do not leak sensitive information!") | ||
| print(" Consider adding 'no_log: true' if they handle secrets.") | ||
| print(" These are warnings and will not fail the build.") | ||
|
|
||
| return 0 # Don't fail the build, just warn | ||
|
|
||
| except Exception as e: | ||
| print(f"Error during set_fact check: {e}") | ||
| return 0 # Don't fail on script errors | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
what is the reason for doing this if base_branch and the list of commands in both if and else blocks ?