Skip to content

Commit

Permalink
Merge pull request #20 from bjompen/WriteSecretStatus
Browse files Browse the repository at this point in the history
Added Write-SecretStatus and guide to run it in Posh-git
  • Loading branch information
bjompen authored Jul 29, 2022
2 parents 7cce425 + 5abb388 commit 1d33583
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 28 deletions.
58 changes: 58 additions & 0 deletions Docs/Help/Write-SecretStatus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
external help file: PSSecretScanner-help.xml
Module Name: PSSecretScanner
online version:
schema: 2.0.0
---

# Write-SecretStatus

## SYNOPSIS

This command is created to get a quick and easy way of having secrets found shown in your prompt function.
You can use it side by side with [posh-git](https://github.com/dahlbyk/posh-git), or as a stand alone function.

## SYNTAX

```PowerShell
Write-SecretStatus
```

## DESCRIPTION

This command is created to get a quick and easy way of having secrets found shown in your prompt function.
You can use it side by side with [posh-git](https://github.com/dahlbyk/posh-git), or as a stand alone function.

---

To add output to your default prompt, create or edit your prompt function and add `Write-SecretStatus` where you want it to show.

---

To add this to your posh-git prompt add the following to your `$PROFILE` script **after the `Import-Module posh-git` statement!**

```PowerShell
$GitPromptSettings.DefaultPromptBeforeSuffix.Text = ' $(Write-SecretStatus)'
# You may also change the default white console output colour by running
$GitPromptSettings.DefaultPromptBeforeSuffix.ForegroundColor = 'LightBlue' # or any other colour of choice..
```

---

You _may_ also add this to your oh-my-posh thing, but I don't use it and have no idea how that works.

## EXAMPLES

## PARAMETERS

## INPUTS

### None

## OUTPUTS

### System.Object

## NOTES

## RELATED LINKS
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Yes, even keeping it simple there are stuff I might want to add some day, or if
- 2022-07-29
- Change from Get-ChildItem to Get-Item which is marginaly faster. (2 seconds/10000 objects)
- Add boolean-Recurse parameter defaulted to $true to support non recursive scans ([#18](https://github.com/bjompen/PSSecretScanner/issues/18))
- Added Write-SecretStatur to add to posh-git profile.
- 2022-07-28
- Added `-File` parameter ([#12](https://github.com/bjompen/PSSecretScanner/issues/12))
- Changed the **firebaseio** pattern to make scanning faster.
Expand Down
3 changes: 2 additions & 1 deletion Source/PSSecretScanner.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ PowerShellVersion = '5.1'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @(
'Find-Secret',
'New-PSSSConfig'
'New-PSSSConfig',
'Write-SecretStatus'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
58 changes: 31 additions & 27 deletions Source/Public/Find-Secret.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,40 +60,44 @@ function Find-Secret {
}
}

if ($ScanFiles.Count -ge 1) {
Write-Verbose "Scanning files:`n$($ScanFiles.FullName -join ""`n"")"

Write-Verbose "Scanning files:`n$($ScanFiles.FullName -join ""`n"")"
$Res = $Config['regexes'].Keys | ForEach-Object {
$RegexName = $_
$Pattern = ($Config['regexes'])."$RegexName"

$Res = $Config['regexes'].Keys | ForEach-Object {
$RegexName = $_
$Pattern = ($Config['regexes'])."$RegexName"
Write-Verbose "Performing $RegexName scan`nPattern '$Pattern'`n"

Write-Verbose "Performing $RegexName scan`nPattern '$Pattern'`n"

Get-Item $ScanFiles.FullName | Select-String -Pattern $Pattern
}

if (-not [string]::IsNullOrEmpty($Excludelist)) {
[string[]]$Exclusions = GetExclusions $Excludelist
Write-Verbose "Using excludelist $Excludelist. Found $($Exclusions.Count) exlude strings."
Get-Item $ScanFiles.FullName | Select-String -Pattern $Pattern
}

if (-not [string]::IsNullOrEmpty($Excludelist)) {
[string[]]$Exclusions = GetExclusions $Excludelist
Write-Verbose "Using excludelist $Excludelist. Found $($Exclusions.Count) exlude strings."

$Res = $Res | Where-Object {
"$($_.Path);$($_.LineNumber);$($_.Line)" -notin $Exclusions
$Res = $Res | Where-Object {
"$($_.Path);$($_.LineNumber);$($_.Line)" -notin $Exclusions
}
}
}

$Result = "Found $($Res.Count) strings.`n"

$Result = "Found $($Res.Count) strings.`n"

if ($res.Count -gt 0) {
$Result += "Path`tLine`tLineNumber`tPattern`n"
foreach ($line in $res) {
$Result += "$($line.Path)`t$($line.Line)`t$($line.LineNumber)`t$($line.Pattern)`n"
if ($res.Count -gt 0) {
$Result += "Path`tLine`tLineNumber`tPattern`n"
foreach ($line in $res) {
$Result += "$($line.Path)`t$($line.Line)`t$($line.LineNumber)`t$($line.Pattern)`n"
}
}
}

switch ($OutputPreference) {
'Output' { Write-Output $Result }
'Warning' { Write-Warning $Result }
'Error' { Write-Error $Result }
'Object' { $res }
else {
$Result = 'Found no files to scan'
$res = @()
}
switch ($OutputPreference) {
'Output' { Write-Output $Result }
'Warning' { Write-Warning $Result }
'Error' { Write-Error $Result }
'Object' { $res }
}
}
15 changes: 15 additions & 0 deletions Source/Public/Write-SecretStatus.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function Write-SecretStatus {
param ()

try {
[array]$IsGit = (git status *>&1).ToString()
if ( $IsGit[0] -eq 'fatal: not a git repository (or any of the parent directories): .git' ) {
break
}
else {
$SecretsCount = (Find-Secret -Recursive:$false -OutputPreference Object).Count
Write-Output "[$SecretsCount]"
}
}
catch {}
}

0 comments on commit 1d33583

Please sign in to comment.