Skip to content

Commit 8d2dd62

Browse files
committed
fetcher controls fetch interval, prevent fetching loop
1 parent fda0446 commit 8d2dd62

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

main.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/felixge/fgprof"
18+
"github.com/gorilla/mux"
1719
"github.com/jpillora/overseer"
20+
"github.com/sirupsen/logrus"
1821
"github.com/trufflesecurity/trufflehog/v3/pkg/updater"
1922
"github.com/trufflesecurity/trufflehog/v3/pkg/version"
2023
"gopkg.in/alecthomas/kingpin.v2"
2124

22-
"github.com/felixge/fgprof"
23-
24-
"github.com/gorilla/mux"
25-
"github.com/sirupsen/logrus"
2625
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
2726
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
2827
"github.com/trufflesecurity/trufflehog/v3/pkg/engine"
@@ -91,6 +90,16 @@ func init() {
9190
}
9291

9392
cmd = kingpin.MustParse(cli.Parse(os.Args[1:]))
93+
94+
if *jsonOut {
95+
logrus.SetFormatter(&logrus.JSONFormatter{})
96+
}
97+
if *debug {
98+
logrus.SetLevel(logrus.DebugLevel)
99+
logrus.Debugf("running version %s", version.BuildVersion)
100+
} else {
101+
logrus.SetLevel(logrus.InfoLevel)
102+
}
94103
}
95104

96105
func main() {
@@ -112,25 +121,18 @@ func main() {
112121
}
113122

114123
func run(state overseer.State) {
115-
if *versionFlag {
124+
if *debug || *versionFlag {
116125
fmt.Println("trufflehog " + version.BuildVersion)
117-
return
126+
if *versionFlag {
127+
return
128+
}
118129
}
119130

120131
// When setting a base commit, chunks must be scanned in order.
121132
if *gitScanSinceCommit != "" {
122133
*concurrency = 1
123134
}
124135

125-
if *jsonOut {
126-
logrus.SetFormatter(&logrus.JSONFormatter{})
127-
}
128-
if *debug {
129-
logrus.SetLevel(logrus.DebugLevel)
130-
} else {
131-
logrus.SetLevel(logrus.InfoLevel)
132-
}
133-
134136
if *debug {
135137
go func() {
136138
router := mux.NewRouter()

pkg/engine/engine.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package engine
33
import (
44
"bytes"
55
"context"
6-
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
7-
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
86
"runtime"
97
"strings"
108
"sync"
@@ -15,6 +13,8 @@ import (
1513

1614
"github.com/trufflesecurity/trufflehog/v3/pkg/decoders"
1715
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
16+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
17+
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
1818
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
1919
)
2020

@@ -73,7 +73,7 @@ func Start(ctx context.Context, options ...EngineOption) *Engine {
7373
logrus.Warn("No concurrency specified, defaulting to ", numCPU)
7474
e.concurrency = numCPU
7575
}
76-
logrus.Debugf("running with up to %d workers", e.concurrency)
76+
logrus.Debugf("running with up to %d workers", e.concurrency)
7777

7878
var workerWg sync.WaitGroup
7979
for i := 0; i < e.concurrency; i++ {

pkg/updater/updater.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func Fetcher(version string) fetcher.Interface {
2929
type OSS struct {
3030
Interval time.Duration
3131
CurrentVersion string
32+
Updated bool
3233
}
3334

3435
// Init validates the provided config
@@ -49,7 +50,10 @@ type FormData struct {
4950

5051
// Fetch binary from URL via OSS client
5152
func (g *OSS) Fetch() (io.Reader, error) {
52-
log.Debug("fetching trufflehog update")
53+
if g.Updated {
54+
select {} // block until exit
55+
}
56+
g.Updated = true
5357

5458
zone, _ := time.Now().Zone()
5559
data := &FormData{
@@ -71,6 +75,12 @@ func (g *OSS) Fetch() (io.Reader, error) {
7175
}
7276
defer resp.Body.Close()
7377

78+
if resp.StatusCode == http.StatusNoContent {
79+
return nil, nil
80+
}
81+
82+
log.Debug("fetching trufflehog update")
83+
7484
newBinBytes, err := ioutil.ReadAll(resp.Body)
7585
if err != nil {
7686
return nil, err

0 commit comments

Comments
 (0)