|
1 | | -function Check-Gitleaks { |
2 | | - Write-Host "Checking if Gitleaks is installed...`n" |
3 | | - |
4 | | - # Check if Gitleaks exists in system path |
5 | | - $gitleaksPath = Get-Command gitleaks -ErrorAction SilentlyContinue |
6 | | - |
7 | | - if ($null -eq $gitleaksPath) { |
8 | | - Write-Error "Gitleaks is not installed or not found in PATH!`n" |
9 | | - exit 1 |
10 | | - } else { |
11 | | - Write-Host "Gitleaks is installed at: $($gitleaksPath.Source)`n" |
12 | | - # Ensure /usr/local/bin is in PATH |
13 | | - $env:PATH += ":/usr/local/bin" |
14 | | - # Check if Gitleaks is executable |
15 | | - if (-not (Test-Path $gitleaksPath.Source)) { |
16 | | - Write-Error "Gitleaks is not executable!`n" |
17 | | - exit 1 |
18 | | - } else { |
19 | | - Write-Host "Gitleaks is executable.`n" |
20 | | - } |
21 | | - |
22 | | - } |
23 | | -} |
24 | | - |
25 | | -function Run-Gitleaks { |
26 | | - param ( |
27 | | - [string]$ReportFormat, |
28 | | - [string]$LogLevel, |
29 | | - [switch]$Redact, |
30 | | - [switch]$Verbose, |
31 | | - [switch]$NoGit |
32 | | - ) |
33 | | - |
34 | | - Write-Host "Run Gitleaks detect cmd...`n" |
35 | | - |
36 | | - $SourcePath = (Get-Location) |
37 | | - |
38 | | - # Get current git branch and check if repository has commits |
39 | | - $currentBranch = $null |
40 | | - $hasCommits = $false |
41 | | - |
42 | | - try { |
43 | | - Write-Host "Getting current git branch..." |
44 | | - $currentBranch = git branch --show-current |
45 | | - if ($LASTEXITCODE -ne 0) { |
46 | | - Write-Warning "Failed to get git branch, defaulting to 'unknown'" |
47 | | - $currentBranch = "unknown" |
48 | | - } else { |
49 | | - Write-Host "Current git branch: $currentBranch" |
50 | | - |
51 | | - # Check if the repository has any commits |
52 | | - try { |
53 | | - git rev-parse HEAD 2>$null | Out-Null |
54 | | - if ($LASTEXITCODE -eq 0) { |
55 | | - $hasCommits = $true |
56 | | - Write-Host "Repository has commits, will use branch reference in log options." |
57 | | - } else { |
58 | | - Write-Host "Repository has no commits yet, will skip branch reference in log options." |
59 | | - } |
60 | | - } catch { |
61 | | - Write-Host "Cannot determine commit history, will skip branch reference in log options." |
62 | | - } |
63 | | - } |
64 | | - } catch { |
65 | | - Write-Warning "Error getting git branch: $_" |
66 | | - $currentBranch = "unknown" |
67 | | - } |
68 | | - |
69 | | - # Construct command options as an array |
70 | | - $cmdOptions = @( |
71 | | - "detect" |
72 | | - "--config", "$SourcePath/.gitleaks.toml" |
73 | | - "--source", "$SourcePath" |
74 | | - "--report-path", "./gitleaks-report.$ReportFormat" |
75 | | - "--report-format", "$ReportFormat" |
76 | | - "--log-level", "$LogLevel" |
77 | | - ) |
78 | | - |
79 | | - # Only add log-opts if we have a valid branch name and the repository has commits. |
80 | | - if ($currentBranch -ne "unknown" -and $hasCommits) { |
81 | | - $cmdOptions += "--log-opts" |
82 | | - $cmdOptions += "$currentBranch" |
83 | | - } |
84 | | - |
85 | | - if ($Redact) { $cmdOptions += "--redact" } |
86 | | - if ($Verbose) { $cmdOptions += "--verbose" } |
87 | | - |
88 | | - Write-Verbose "gitleaks $($cmdOptions -join ' ')`n" |
89 | | - |
90 | | - # Run Gitleaks command |
91 | | - gitleaks @cmdOptions |
92 | | - $exitCode = $LASTEXITCODE |
93 | | - |
94 | | - if ($exitCode -ne 0) { |
95 | | - Write-Error "gitleaks failed`n" |
96 | | - exit 1 |
97 | | - } |
98 | | - else { |
99 | | - Write-Host "gitleaks passed`n" |
100 | | - exit 0 |
101 | | - } |
102 | | - |
103 | | -} |
104 | | - |
105 | | -# First, check if Gitleaks is installed |
106 | | -Check-Gitleaks |
107 | | - |
108 | | -# Then, run Gitleaks scan |
109 | | -Run-Gitleaks -ReportFormat "sarif" -LogLevel "info" -Redact -Verbose |
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +function Check-Gitleaks { |
| 5 | + Write-Host "Checking if Gitleaks is installed...`n" |
| 6 | + |
| 7 | + # Check if Gitleaks exists in system path |
| 8 | + $gitleaksPath = Get-Command gitleaks -ErrorAction SilentlyContinue |
| 9 | + |
| 10 | + if ($null -eq $gitleaksPath) { |
| 11 | + Write-Error "Gitleaks is not installed or not found in PATH!`n" |
| 12 | + exit 1 |
| 13 | + } else { |
| 14 | + Write-Host "Gitleaks is installed at: $($gitleaksPath.Source)`n" |
| 15 | + # Ensure /usr/local/bin is in PATH |
| 16 | + $env:PATH += ":/usr/local/bin" |
| 17 | + # Check if Gitleaks is executable |
| 18 | + if (-not (Test-Path $gitleaksPath.Source)) { |
| 19 | + Write-Error "Gitleaks is not executable!`n" |
| 20 | + exit 1 |
| 21 | + } else { |
| 22 | + Write-Host "Gitleaks is executable.`n" |
| 23 | + } |
| 24 | + |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function Run-Gitleaks { |
| 29 | + param ( |
| 30 | + [string]$ReportFormat, |
| 31 | + [string]$LogLevel, |
| 32 | + [switch]$Redact, |
| 33 | + [switch]$Verbose, |
| 34 | + [switch]$NoGit |
| 35 | + ) |
| 36 | + |
| 37 | + Write-Host "Run Gitleaks detect cmd...`n" |
| 38 | + |
| 39 | + $SourcePath = (Get-Location) |
| 40 | + |
| 41 | + # Get current git branch and check if repository has commits |
| 42 | + $currentBranch = $null |
| 43 | + $hasCommits = $false |
| 44 | + |
| 45 | + try { |
| 46 | + Write-Host "Getting current git branch..." |
| 47 | + $currentBranch = git branch --show-current |
| 48 | + if ($LASTEXITCODE -ne 0) { |
| 49 | + Write-Warning "Failed to get git branch, defaulting to 'unknown'" |
| 50 | + $currentBranch = "unknown" |
| 51 | + } else { |
| 52 | + Write-Host "Current git branch: $currentBranch" |
| 53 | + |
| 54 | + # Check if the repository has any commits |
| 55 | + try { |
| 56 | + git rev-parse HEAD 2>$null | Out-Null |
| 57 | + if ($LASTEXITCODE -eq 0) { |
| 58 | + $hasCommits = $true |
| 59 | + Write-Host "Repository has commits, will use branch reference in log options." |
| 60 | + } else { |
| 61 | + Write-Host "Repository has no commits yet, will skip branch reference in log options." |
| 62 | + } |
| 63 | + } catch { |
| 64 | + Write-Host "Cannot determine commit history, will skip branch reference in log options." |
| 65 | + } |
| 66 | + } |
| 67 | + } catch { |
| 68 | + Write-Warning "Error getting git branch: $_" |
| 69 | + $currentBranch = "unknown" |
| 70 | + } |
| 71 | + |
| 72 | + # Construct command options as an array |
| 73 | + $cmdOptions = @( |
| 74 | + "detect" |
| 75 | + "--config", "$SourcePath/.gitleaks.toml" |
| 76 | + "--source", "$SourcePath" |
| 77 | + "--report-path", "./gitleaks-report.$ReportFormat" |
| 78 | + "--report-format", "$ReportFormat" |
| 79 | + "--log-level", "$LogLevel" |
| 80 | + ) |
| 81 | + |
| 82 | + # Only add log-opts if we have a valid branch name and the repository has commits. |
| 83 | + if ($currentBranch -ne "unknown" -and $hasCommits) { |
| 84 | + $cmdOptions += "--log-opts" |
| 85 | + $cmdOptions += "$currentBranch" |
| 86 | + } |
| 87 | + |
| 88 | + if ($Redact) { $cmdOptions += "--redact" } |
| 89 | + if ($Verbose) { $cmdOptions += "--verbose" } |
| 90 | + |
| 91 | + Write-Verbose "gitleaks $($cmdOptions -join ' ')`n" |
| 92 | + |
| 93 | + # Run Gitleaks command |
| 94 | + gitleaks @cmdOptions |
| 95 | + $exitCode = $LASTEXITCODE |
| 96 | + |
| 97 | + if ($exitCode -ne 0) { |
| 98 | + Write-Error "gitleaks failed`n" |
| 99 | + exit 1 |
| 100 | + } |
| 101 | + else { |
| 102 | + Write-Host "gitleaks passed`n" |
| 103 | + exit 0 |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +# First, check if Gitleaks is installed |
| 109 | +Check-Gitleaks |
| 110 | + |
| 111 | +# Then, run Gitleaks scan |
| 112 | +Run-Gitleaks -ReportFormat "sarif" -LogLevel "info" -Redact -Verbose |
0 commit comments