-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repository plugin - support scanning historical git commits
Fixes #66
- Loading branch information
Baruch Odem
committed
Jun 8, 2023
1 parent
4883ce9
commit b127f03
Showing
2 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,87 @@ | ||
package plugins | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gitleaks/go-gitdiff/gitdiff" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/zricethezav/gitleaks/v8/detect/git" | ||
) | ||
|
||
type GitPlugin struct { | ||
Plugin | ||
Channels | ||
|
||
path string | ||
} | ||
|
||
func (p *GitPlugin) GetName() string { | ||
return "git" | ||
} | ||
|
||
func (p *GitPlugin) DefineCommand(channels Channels) (*cobra.Command, error) { | ||
p.Channels = channels | ||
|
||
command := &cobra.Command{ | ||
Use: fmt.Sprintf("%s <PATH>", p.GetName()), | ||
Short: "Scan Git repository", | ||
Long: "Scan Git repository for sensitive information.", | ||
Args: cobra.MatchAll(cobra.ExactArgs(1), validGitRepoArgs), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
log.Info().Msg("Git plugin started") | ||
scanGit(args[0], channels.Items, channels.Errors) | ||
}, | ||
} | ||
|
||
return command, nil | ||
} | ||
|
||
func scanGit(path string, itemsChan chan Item, errChan chan error) { | ||
fileChan, err := git.GitLog(path, "") | ||
if err != nil { | ||
errChan <- fmt.Errorf("error while scanning git repository: %w", err) | ||
} | ||
for file := range fileChan { | ||
log.Debug().Msgf("file: %s; Commit: %s", file.NewName, file.PatchHeader.Title) | ||
if file.IsBinary || file.IsDelete { | ||
continue | ||
} | ||
|
||
fileChanges := "" | ||
for _, textFragment := range file.TextFragments { | ||
if textFragment != nil { | ||
raw := textFragment.Raw(gitdiff.OpAdd) | ||
fileChanges += raw | ||
} | ||
} | ||
if fileChanges != "" { | ||
log.Debug().Msgf("file: %s; Changes: %s", file.NewName, fileChanges) | ||
itemsChan <- Item{ | ||
Content: fileChanges, | ||
Source: file.NewName, | ||
ID: fmt.Sprintf("git show %s:%s", file.PatchHeader.SHA, file.NewName), | ||
} | ||
} | ||
} | ||
} | ||
|
||
func validGitRepoArgs(cmd *cobra.Command, args []string) error { | ||
stat, err := os.Stat(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
if !stat.IsDir() { | ||
return fmt.Errorf("%s is not a directory", args[0]) | ||
} | ||
gitFolder := fmt.Sprintf("%s/.git", args[0]) | ||
stat, err = os.Stat(gitFolder) | ||
if err != nil { | ||
return err | ||
} | ||
if !stat.IsDir() { | ||
return fmt.Errorf("%s is not a git repository", args[0]) | ||
} | ||
return nil | ||
} |