Skip to content

Commit

Permalink
feat(gitleaks): add initial support for gitleaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Benjamin authored and pbnj committed Aug 16, 2023
1 parent fe38101 commit ebc4158
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ale_linters/terraform/gitleaks.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scriptencoding utf-8
" Author: Peter Benjamin <https://github.com/pbnj>
" Description: gitleaks support for terraform files.

call ale#handlers#gitleaks#DefineLinter('terraform')
55 changes: 55 additions & 0 deletions autoload/ale/handlers/gitleaks.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
scriptencoding utf-8
" Author: Peter Benjamin <https://github.com/pbnj>
" Description: Define a handler function for gitleaks

call ale#Set('gitleaks_executable', 'gitleaks')
call ale#Set('gitleaks_options', '')

function! ale#handlers#gitleaks#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'gitleaks_executable')
endfunction

function! ale#handlers#gitleaks#GetCommand(buffer) abort
let l:executable = ale#handlers#gitleaks#GetExecutable(a:buffer)
let l:options = ale#Var(a:buffer, 'gitleaks_options')

return l:executable
\ . ' detect --no-git --no-color --no-banner --redact --verbose'
\ . ale#Pad(l:options)
endfunction

function! ale#handlers#gitleaks#Handle(buffer, lines) abort
" Look for lines like the following:
"
" Finding: ACCESS_KEY_ID=REDACTED
" Secret: REDACTED
" RuleID: generic-api-key
" Entropy: 3.546594
" File: tmp/env
" Line: 1
" Fingerprint: tmp/env:generic-api-key:1
let l:pattern = '\v^Fingerprint: .*:(.*):(\d+)$'
let l:output = []

for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'lnum': l:match[2] + 0,
\ 'text': l:match[1],
\ 'type': 'E',
\})
endfor

return l:output
endfunction

function! ale#handlers#gitleaks#DefineLinter(filetype) abort
call ale#Set('gitleaks_executable', 'gitleaks')
call ale#Set('gitleaks_options', '')

call ale#linter#Define(a:filetype, {
\ 'name': 'gitleaks',
\ 'executable': function('ale#handlers#gitleaks#GetExecutable'),
\ 'command': function('ale#handlers#gitleaks#GetCommand'),
\ 'callback': 'ale#handlers#gitleaks#Handle',
\})
endfunction

0 comments on commit ebc4158

Please sign in to comment.