Skip to content

Commit 52997eb

Browse files
committed
check for nil metrics and add more comments
1 parent f90530c commit 52997eb

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

pkg/verificationcache/in_memory_metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66
)
77

8+
// InMemoryMetrics is a MetricsReporter that stores reported metrics in memory for retrieval at the end of a scan.
89
type InMemoryMetrics struct {
910
CredentialVerificationsSaved atomic.Int32
1011
FromDataVerifyTimeSpentMS atomic.Int64

pkg/verificationcache/result_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import (
55
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors"
66
)
77

8-
// ResultCache is a cache that holds individual detector results.
8+
// ResultCache is a cache that holds individual detector results. It serves as a component of a VerificationCache.
99
type ResultCache cache.Cache[detectors.Result]

pkg/verificationcache/verification_cache.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/trufflesecurity/trufflehog/v3/pkg/hasher"
1010
)
1111

12+
// VerificationCache is a structure that can be used to cache verification results from detectors so that a given
13+
// credential does not trigger multiple identical remote verification attempts.
1214
type VerificationCache struct {
1315
metrics MetricsReporter
1416
resultCache ResultCache
@@ -17,14 +19,32 @@ type VerificationCache struct {
1719
hasher hasher.Hasher
1820
}
1921

22+
// New creates a new verification cache with the provided result cache and metrics reporter. If resultCache is nil, the
23+
// verification cache will be a no-op passthrough, although it will still record relevant metrics to the provided
24+
// metrics reporter in this case.
2025
func New(resultCache ResultCache, metrics MetricsReporter) VerificationCache {
26+
if metrics == nil {
27+
metrics = &InMemoryMetrics{}
28+
}
29+
2130
return VerificationCache{
2231
metrics: metrics,
2332
resultCache: resultCache,
2433
hasher: hasher.NewBlake2B(),
2534
}
2635
}
2736

37+
// FromData is a cache-aware facade in front of the provided detector's FromData method.
38+
//
39+
// If the verification cache's underlying result cache is nil, or verify is false, or forceCacheUpdate is true, this
40+
// method invokes the provided detector's FromData method with the provided arguments and returns the result. If the
41+
// result cache is non-nil and forceCacheUpdate is true, the result cache is updated with the results before they are
42+
// returned.
43+
//
44+
// Otherwise, the detector's FromData method is called with verify=false. The results cache is then checked for each
45+
// returned result. If there is a cache hit for each result, these cached values are all returned. Otherwise, the
46+
// detector's FromData method is called again, but with verify=true, and the results are stored in the cache and then
47+
// returned.
2848
func (v *VerificationCache) FromData(
2949
ctx context.Context,
3050
detector detectors.Detector,

0 commit comments

Comments
 (0)