Files
Tim Strazzere 4cffadbd04 feat: allow input source from pipe (#4088)
* feat: allow input source from pipe

While performing a bit of experimentation,
I found it was easier to allow data to flow
from other segments of my machine via piping
it in, rather than flushing it to disk. This
can speed up the process when pre-processing
large amounts of data. This allow allowed me
to keep disk space low and just utilize a large
ram machine.

* (chore) update readme for new source pipe

* (fix) pipe -> stdin per PR
2025-05-23 09:53:20 -04:00

90 lines
2.3 KiB
Go

package stdin
import (
"os"
"github.com/go-logr/logr"
"google.golang.org/protobuf/types/known/anypb"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)
const SourceType = sourcespb.SourceType_SOURCE_TYPE_STDIN
type Source struct {
name string
sourceId sources.SourceID
jobId sources.JobID
verify bool
log logr.Logger
sources.Progress
sources.CommonSourceUnitUnmarshaller
}
var _ sources.Source = (*Source)(nil)
var _ sources.SourceUnitUnmarshaller = (*Source)(nil)
var _ sources.SourceUnitEnumChunker = (*Source)(nil)
func (s *Source) Type() sourcespb.SourceType {
return SourceType
}
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}
func (s *Source) JobID() sources.JobID {
return s.jobId
}
func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, _ *anypb.Any, _ int) error {
s.name = name
s.jobId = jobId
s.sourceId = sourceId
s.verify = verify
s.log = aCtx.Logger()
return nil
}
func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ ...sources.ChunkingTarget) error {
stdin := os.Stdin
chunkSkel := &sources.Chunk{
SourceType: s.Type(),
SourceName: s.name,
SourceID: s.SourceID(),
JobID: s.JobID(),
SourceMetadata: &source_metadatapb.MetaData{
Data: &source_metadatapb.MetaData_Stdin{},
},
Verify: s.verify,
}
ctx.Logger().Info("scanning stdin for secrets")
return handlers.HandleFile(ctx, stdin, chunkSkel, sources.ChanReporter{Ch: chunksChan})
}
func (s *Source) Enumerate(ctx context.Context, reporter sources.UnitReporter) error {
unit := sources.CommonSourceUnit{ID: "<stdin>"}
return reporter.UnitOk(ctx, unit)
}
func (s *Source) ChunkUnit(ctx context.Context, unit sources.SourceUnit, reporter sources.ChunkReporter) error {
ch := make(chan *sources.Chunk)
go func() {
defer close(ch)
_ = s.Chunks(ctx, ch)
}()
for chunk := range ch {
if chunk != nil {
if err := reporter.ChunkOk(ctx, *chunk); err != nil {
return err
}
}
}
return nil
}