|
| 1 | +import json |
| 2 | +import difflib |
| 3 | +from rich.console import Console |
| 4 | +from rich.text import Text |
| 5 | + |
| 6 | +def clean_policy(policy): |
| 7 | + """ |
| 8 | + Remove empty statements ({} entries) from the policy's 'Statement' list. |
| 9 | + """ |
| 10 | + if isinstance(policy, dict) and "Statement" in policy: |
| 11 | + statements = policy.get("Statement", []) |
| 12 | + if isinstance(statements, list): |
| 13 | + policy["Statement"] = [s for s in statements if s] |
| 14 | + return policy |
| 15 | + |
| 16 | +def generate_diff_report(local_policy, aws_policy): |
| 17 | + """ |
| 18 | + Generate a Git-style unified diff report for two IAM policies (local vs AWS), |
| 19 | + with inline highlights for changed parts of a line. |
| 20 | + """ |
| 21 | + console = Console() |
| 22 | + |
| 23 | + # Clean out empty statements to reduce noise |
| 24 | + if isinstance(local_policy, dict): |
| 25 | + local_policy = clean_policy(local_policy) |
| 26 | + if isinstance(aws_policy, dict): |
| 27 | + aws_policy = clean_policy(aws_policy) |
| 28 | + |
| 29 | + # Convert dicts to pretty-printed JSON strings |
| 30 | + if isinstance(local_policy, dict): |
| 31 | + local_str = json.dumps(local_policy, indent=2, sort_keys=True) |
| 32 | + else: |
| 33 | + local_str = str(local_policy) |
| 34 | + |
| 35 | + if isinstance(aws_policy, dict): |
| 36 | + aws_str = json.dumps(aws_policy, indent=2, sort_keys=True) |
| 37 | + else: |
| 38 | + aws_str = str(aws_policy) |
| 39 | + |
| 40 | + # Split into lines |
| 41 | + local_lines = local_str.splitlines(keepends=True) |
| 42 | + aws_lines = aws_str.splitlines(keepends=True) |
| 43 | + |
| 44 | + # Generate unified diff |
| 45 | + diff_lines = list(difflib.unified_diff( |
| 46 | + local_lines, |
| 47 | + aws_lines, |
| 48 | + fromfile="local", |
| 49 | + tofile="aws", |
| 50 | + lineterm="" |
| 51 | + )) |
| 52 | + |
| 53 | + if not diff_lines: |
| 54 | + console.print("✅ No drift detected: Policies match.", style="green") |
| 55 | + return |
| 56 | + |
| 57 | + i = 0 |
| 58 | + while i < len(diff_lines): |
| 59 | + line = diff_lines[i] |
| 60 | + |
| 61 | + if line.startswith('---') or line.startswith('+++'): |
| 62 | + console.print(Text(line, style="bold")) |
| 63 | + elif line.startswith('@@'): |
| 64 | + console.print(Text(line, style="cyan")) |
| 65 | + elif line.startswith('-'): |
| 66 | + # Check if next line is a '+', for possible inline diff |
| 67 | + if (i + 1 < len(diff_lines)) and diff_lines[i + 1].startswith('+'): |
| 68 | + next_line = diff_lines[i + 1] |
| 69 | + old_content = line[1:].rstrip('\n') |
| 70 | + new_content = next_line[1:].rstrip('\n') |
| 71 | + |
| 72 | + # Use SequenceMatcher for inline diff |
| 73 | + matcher = difflib.SequenceMatcher(None, old_content, new_content) |
| 74 | + old_text = Text("-", style="red") |
| 75 | + new_text = Text("+", style="green") |
| 76 | + |
| 77 | + for tag, i1, i2, j1, j2 in matcher.get_opcodes(): |
| 78 | + if tag == 'equal': |
| 79 | + old_text.append(old_content[i1:i2], style="red") |
| 80 | + new_text.append(new_content[j1:j2], style="green") |
| 81 | + elif tag == 'replace': |
| 82 | + old_text.append(old_content[i1:i2], style="bold white on red") |
| 83 | + new_text.append(new_content[j1:j2], style="bold black on green") |
| 84 | + elif tag == 'delete': |
| 85 | + old_text.append(old_content[i1:i2], style="bold white on red") |
| 86 | + elif tag == 'insert': |
| 87 | + new_text.append(new_content[j1:j2], style="bold black on green") |
| 88 | + |
| 89 | + console.print(old_text) |
| 90 | + console.print(new_text) |
| 91 | + i += 1 # Skip next line since it's handled |
| 92 | + else: |
| 93 | + console.print(Text(line, style="red")) |
| 94 | + elif line.startswith('+'): |
| 95 | + console.print(Text(line, style="green")) |
| 96 | + elif line.startswith(' '): |
| 97 | + console.print(Text(line, style="bright_black")) |
| 98 | + else: |
| 99 | + console.print(Text(line)) # Fallback for any edge case lines |
| 100 | + i += 1 |
0 commit comments