-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix pre-receive hook hangs and missing logs by flushing logs on signal and using CommandContext for git commands #4714
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
f12e1c9
296302c
3af2ec5
f8221bd
08e18b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ import ( | |
| "strings" | ||
| "sync" | ||
| "syscall" | ||
|
|
||
| "time" | ||
| "github.com/alecthomas/kingpin/v2" | ||
| "github.com/fatih/color" | ||
| "github.com/felixge/fgprof" | ||
|
|
@@ -61,6 +61,7 @@ var ( | |
| results = cli.Flag("results", "Specifies which type(s) of results to output: verified (confirmed valid by API), unknown (verification failed due to error), unverified (detected but not verified), filtered_unverified (unverified but would have been filtered out). Defaults to verified,unverified,unknown.").String() | ||
| noColor = cli.Flag("no-color", "Disable colorized output").Bool() | ||
| noColour = cli.Flag("no-colour", "Alias for --no-color").Hidden().Bool() | ||
| logSync func() error //Package-level variable for sync function | ||
|
|
||
| allowVerificationOverlap = cli.Flag("allow-verification-overlap", "Allow verification of similar credentials across detectors").Bool() | ||
| filterUnverified = cli.Flag("filter-unverified", "Only output first unverified result per chunk per detector if there are more than one results.").Bool() | ||
|
|
@@ -354,6 +355,7 @@ func main() { | |
| logFormat = log.WithJSONSink | ||
| } | ||
| logger, sync := log.New("trufflehog", logFormat(os.Stderr, log.WithGlobalRedaction())) | ||
| logSync = sync // | ||
| // make it the default logger for contexts | ||
| context.SetDefaultLogger(logger) | ||
|
|
||
|
|
@@ -413,6 +415,21 @@ func run(state overseer.State) { | |
| } else { | ||
| logger.Info("cleaned temporary artifacts") | ||
| } | ||
|
|
||
| // Flush logs with timeout to prevent hanging | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by this, because |
||
| if logSync != nil { | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| _ = logSync() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(100 * time.Millisecond): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Magic number for log flush timeoutLow Severity The |
||
| logger.Info("Log flush timed out, exiting") | ||
| } | ||
| } | ||
| os.Exit(0) | ||
| }() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -253,7 +253,7 @@ func (c *Parser) RepoPath( | |
| args = append(args, "--", ".", ":(exclude)"+glob) | ||
| } | ||
|
|
||
| cmd := exec.Command("git", args...) | ||
| cmd := exec.CommandContext(ctx, "git", args...) | ||
| absPath, err := filepath.Abs(source) | ||
| if err == nil { | ||
| if !isBare { | ||
|
|
@@ -281,7 +281,7 @@ func (c *Parser) Staged(ctx context.Context, source string) (chan *Diff, error) | |
| // Provide the --cached flag to diff to get the diff of the staged changes. | ||
| args := []string{"-C", source, "diff", "-p", "--cached", "--full-history", "--diff-filter=AM", "--date=iso-strict"} | ||
|
|
||
| cmd := exec.Command("git", args...) | ||
| cmd := exec.CommandContext(ctx, "git", args...) | ||
|
|
||
| absPath, err := filepath.Abs(source) | ||
| if err == nil { | ||
|
|
@@ -293,6 +293,8 @@ func (c *Parser) Staged(ctx context.Context, source string) (chan *Diff, error) | |
|
|
||
| // executeCommand runs an exec.Cmd, reads stdout and stderr, and waits for the Cmd to complete. | ||
| func (c *Parser) executeCommand(ctx context.Context, cmd *exec.Cmd, isStaged bool) (chan *Diff, error) { | ||
| const waitDelay = 5 * time.Second // Give the command a chance to finish before the timeout | ||
|
|
||
| diffChan := make(chan *Diff, 64) | ||
|
|
||
| stdOut, err := cmd.StdoutPipe() | ||
|
|
@@ -304,6 +306,9 @@ func (c *Parser) executeCommand(ctx context.Context, cmd *exec.Cmd, isStaged boo | |
| return diffChan, err | ||
| } | ||
|
|
||
| // Set WaitDelay to give the command a grace period to finish before being killed | ||
|
||
| cmd.WaitDelay = waitDelay | ||
|
|
||
| err = cmd.Start() | ||
| if err != nil { | ||
| return diffChan, err | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Burying this var amongst all the
cli.Flagvars that surround it is going to make it far more difficult to find than it needs to be. Can you put it either at the beginning or the end? I think it would be ok to take it out of the grouped declaration entirely (declaring it using its ownvarkeyword) if gofmt lets you do that.(That being said, I don't think you need to use a package var for this - read my other comments for details.)