-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Playground PR for verification caching exploration #3457
Draft
rosecodym
wants to merge
15
commits into
main
Choose a base branch
from
detection-caching-playground
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6f54591
initial sketch
rosecodym 0a9a30b
allow forced cache misses
rosecodym 0f0917b
rearrange
rosecodym d074f6d
plug into engine
rosecodym 6e9d760
clear stuff in cached copy
rosecodym 61a70a3
fiddle with pointers more
rosecodym 3e5ff9c
rename to verificationCache
rosecodym 5ac0139
inject getCacheKey
rosecodym a4e092c
optimize when forcing a cache miss
rosecodym 8d83daa
tweak
rosecodym fd58380
tweak more
rosecodym c119f2b
flag when cache was used
rosecodym 15ddd63
store key builder in engine
rosecodym ea3a559
rename
rosecodym bf3170d
copy verification errors
rosecodym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,80 @@ | ||
package verificationcaching | ||
|
||
import ( | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/cache" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/context" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
// FromDataCached executes detection on chunk data in a way that uses a provided verification cache to deduplicate | ||
// verification requests when possible. | ||
// | ||
// If the provided cache is nil, this function simply returns the result of the provided detector's FromData method. | ||
// | ||
// If verify is false, this function returns the result of the provided detector's FromData method. In this case, the | ||
// cache is only updated if forceCacheUpdate is true. | ||
// | ||
// If verify is true, and forceCacheUpdate is false, this function first executes the provided detector's FromData | ||
// method with verification disabled. Then, the cache is queried for each result. If they are all present in the cache, | ||
// the cached values are returned. Otherwise, the provided detector's FromData method is invoked (again) with | ||
// verification enabled, and the results are used to update the cache before being returned. | ||
// | ||
// If verify is true, and forceCacheUpdate is also true, the provided detector's FromData method is invoked, and the | ||
// results are used to update the cache before being returned. | ||
func FromDataCached( | ||
ctx context.Context, | ||
verificationCache cache.Cache[*detectors.Result], | ||
getCacheKey func(result *detectors.Result) string, | ||
detector detectors.Detector, | ||
verify bool, | ||
forceCacheUpdate bool, | ||
data []byte, | ||
) ([]detectors.Result, error) { | ||
|
||
if verificationCache == nil { | ||
return detector.FromData(ctx, verify, data) | ||
} | ||
|
||
if !forceCacheUpdate { | ||
withoutRemoteVerification, err := detector.FromData(ctx, false, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !verify { | ||
return withoutRemoteVerification, nil | ||
} | ||
|
||
isEverythingCached := false | ||
for _, r := range withoutRemoteVerification { | ||
if cacheHit, ok := verificationCache.Get(getCacheKey(&r)); ok { | ||
r.CopyVerificationInfo(cacheHit) | ||
} else { | ||
isEverythingCached = false | ||
break | ||
} | ||
} | ||
|
||
if isEverythingCached { | ||
return withoutRemoteVerification, nil | ||
} | ||
} | ||
|
||
withRemoteVerification, err := detector.FromData(ctx, verify, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, r := range withRemoteVerification { | ||
copyForCaching := r | ||
// Do not persist raw secret values in a long-lived cache | ||
copyForCaching.Raw = nil | ||
copyForCaching.RawV2 = nil | ||
// Decoder type will be set later, so clear it out now to minimize the chance of accidentally cloning it | ||
copyForCaching.DecoderType = detectorspb.DecoderType_UNKNOWN | ||
verificationCache.Set(getCacheKey(&r), ©ForCaching) | ||
} | ||
|
||
return withRemoteVerification, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I was playing around converting this to slices.ContainsFunc, but then I realized maybe it's not optimal to
break
here? If we run into something that isn't yet cached, everything after it won't be updated from cache. Maybe that's fine though?(also dunno what I think about the
Func
functions inslices
; I guess the main use case is to not dupe logic in loops, but that doesn't apply here)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.
Yeah, this is just kinda unfortunate. If we find something that's not cached, we have to rerun
FromData
, which will return all the results for this chunk - whether they're cached or not. So there's no reason to read from the cache. (Did I successfully explain that?)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.
Essentially, this is a stopgap solution to handle duplicate chunks (e.g., scanning orgs + members that have multiple forks of
kubernetes/kubernetes
.)Proper caching on a granular level will require splitting
FromData
andVerify
into discrete functions.