-
Notifications
You must be signed in to change notification settings - Fork 344
Add run_scanner.ps1 helper to run SecretScanner via WSL or PowerShell #144
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
DecarloFreelance
wants to merge
1
commit into
deepfence:release-2.5
Choose a base branch
from
DecarloFreelance:add/run-scanner-script
base: release-2.5
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
Changes from all commits
Commits
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,140 @@ | ||
| <# | ||
| run_scanner.ps1 - helper to run Deepfence SecretScanner from PowerShell or WSL | ||
|
|
||
| Usage examples: | ||
| # Preferred: run inside WSL (bash/zsh) or use -UseWSL switch which will invoke docker under WSL | ||
| .\run_scanner.ps1 -License "YOUR_LICENSE_HERE" -Product ThreatMapper | ||
|
|
||
| # Run with explicit image and output file | ||
| .\run_scanner.ps1 -License "YOUR_LICENSE_HERE" -Product ThreatMapper -ImageTag 2.5.7 -OutputFile node.json | ||
|
|
||
| This script will attempt to use WSL if present (recommended) because that ensures /var/run/docker.sock is available. | ||
| If WSL is not available, it'll run the docker command in PowerShell. | ||
| #> | ||
|
|
||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory=$true)] | ||
| [string]$License, | ||
|
|
||
| [Parameter(Mandatory=$false)] | ||
| [string]$Product = "ThreatMapper", | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| [Parameter(Mandatory=$false)] | ||
| [string]$Image = "quay.io/deepfenceio/deepfence_secret_scanner_ce:2.5.7", | ||
|
|
||
| [Parameter(Mandatory=$false)] | ||
| [string]$ImageName = "node:8.11", | ||
|
|
||
| [Parameter(Mandatory=$false)] | ||
| [string]$OutputFile = "node.json", | ||
|
|
||
| [switch]$UseWSL | ||
| ) | ||
|
|
||
| function Test-DockerClient { | ||
| try { | ||
| docker --version > $null 2>&1 | ||
| return $true | ||
| } catch { | ||
| return $false | ||
| } | ||
| } | ||
|
|
||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Write-Host "Starting SecretScanner runner..." -ForegroundColor Cyan | ||
|
|
||
| if (-not $License -or $License -eq "YOUR_LICENSE_HERE") { | ||
| Write-Error "Please pass a valid license value with -License 'YOUR_LICENSE_HERE' (replace placeholder)." | ||
| exit 2 | ||
| } | ||
|
|
||
| # Determine whether to run inside WSL | ||
| $haveWsl = (Get-Command wsl -ErrorAction SilentlyContinue) -ne $null | ||
| if ($UseWSL) { | ||
| if (-not $haveWsl) { | ||
| Write-Warning "-UseWSL requested but WSL not found. Falling back to Windows PowerShell docker client."; | ||
| $UseWSL = $false | ||
| } | ||
| } else { | ||
| # default: prefer WSL if available | ||
| if ($haveWsl) { $UseWSL = $true } | ||
| } | ||
|
|
||
| if ($UseWSL) { | ||
| Write-Host "Running scanner under WSL (recommended). Output will be written to Linux-format file inside WSL, then copied to Windows if needed." -ForegroundColor Green | ||
|
|
||
| # Ensure docker is available in WSL | ||
| $wslDockerCheck = wsl docker --version 2>&1 | ||
| if ($LASTEXITCODE -ne 0) { | ||
| Write-Warning "Docker not available inside WSL. Ensure Docker Desktop WSL integration is enabled or run the script from Windows PowerShell." | ||
| } | ||
|
|
||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # Build the WSL command - quote arguments carefully | ||
| $wslCmd = @( | ||
| 'docker','run','-i','--rm','--name=deepfence-secretscanner', | ||
| '-e',"DEEPFENCE_PRODUCT=$Product", | ||
| '-e',"DEEPFENCE_LICENSE=$License", | ||
| '-v','/var/run/docker.sock:/var/run/docker.sock', | ||
| "$Image", | ||
| "--image-name","$ImageName","--output","json" | ||
| ) -join ' ' | ||
|
|
||
| # Run under wsl and redirect output to a file inside WSL /tmp then copy to Windows path | ||
| $tmpWslPath = "/tmp/$(Get-Random)-scanner-output.json" | ||
| $fullCmd = "bash -lc '$wslCmd > $tmpWslPath'" | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Write-Host "Invoking: wsl $fullCmd" -ForegroundColor Gray | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| wsl $fullCmd | ||
|
|
||
| if ($LASTEXITCODE -ne 0) { | ||
| Write-Error "Scanner container failed inside WSL (exit code $LASTEXITCODE). Check Docker logs or run interactively for more info." | ||
| exit $LASTEXITCODE | ||
| } | ||
|
|
||
| # Copy file from WSL to Windows working directory | ||
| $winDst = Join-Path -Path (Get-Location) -ChildPath $OutputFile | ||
| Write-Host "Copying $tmpWslPath -> $winDst" -ForegroundColor Gray | ||
| wsl cp $tmpWslPath - | Out-File -FilePath $winDst -Encoding utf8 | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Remove WSL temp file | ||
| wsl rm -f $tmpWslPath | ||
|
|
||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Write-Host "Finished — output saved to $winDst" -ForegroundColor Green | ||
| exit 0 | ||
| } | ||
|
|
||
| # Fallback to Windows PowerShell docker client | ||
| Write-Host "Running scanner directly from PowerShell (PowerShell will invoke docker)." -ForegroundColor Green | ||
|
|
||
| if (-not (Test-DockerClient)) { | ||
| Write-Error "Docker CLI not found in your PATH. Start Docker Desktop and try again or use -UseWSL if you have WSL installed." | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exit 3 | ||
| } | ||
|
|
||
| $psCmd = @( | ||
| 'docker','run','-i','--rm','--name=deepfence-secretscanner', | ||
| '-e',"DEEPFENCE_PRODUCT=$Product", | ||
| '-e',"DEEPFENCE_LICENSE=$License", | ||
| '-v','/var/run/docker.sock:/var/run/docker.sock', | ||
| "$Image", | ||
| '--image-name',"$ImageName",'--output','json' | ||
| ) -join ' ' | ||
|
|
||
| Write-Host "Invoking: $psCmd" -ForegroundColor Gray | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Run the command directly and capture output | ||
| try { | ||
| $out = & docker run -i --rm --name=deepfence-secretscanner -e "DEEPFENCE_PRODUCT=$Product" -e "DEEPFENCE_LICENSE=$License" -v /var/run/docker.sock:/var/run/docker.sock $Image --image-name $ImageName --output json 2>&1 | ||
DecarloFreelance marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ($LASTEXITCODE -ne 0) { | ||
| Write-Error "docker run failed (exit code $LASTEXITCODE). See output below:`n$out" | ||
| exit $LASTEXITCODE | ||
| } | ||
|
|
||
| # Save output to file | ||
| $out | Out-File -FilePath $OutputFile -Encoding utf8 | ||
| Write-Host "Finished — output saved to $OutputFile" -ForegroundColor Green | ||
| exit 0 | ||
| } catch { | ||
| Write-Error "Error running docker: $_" | ||
| exit 4 | ||
| } | ||
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.