trufflehog/pkg/sources/sources.go

72 lines
2.5 KiB
Go

package sources
import (
"context"
"sync"
"github.com/trufflesecurity/trufflehog/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/pkg/pb/sourcespb"
"google.golang.org/protobuf/types/known/anypb"
)
// Chunk contains data to be decoded and scanned along with context on where it came from.
type Chunk struct {
// SourceName is the name of the Source that produced the chunk.
SourceName string
// SourceID is the ID of the source that the Chunk originated from.
SourceID int64
// SourceType is the type of Source that produced the chunk.
SourceType sourcespb.SourceType
// SourceMetadata holds the context of where the Chunk was found.
SourceMetadata *source_metadatapb.MetaData
// Data is the data to decode and scan.
Data []byte
// Verify specifies whether any secrets in the Chunk should be verified.
Verify bool
}
// Source defines the interface required to implement a source chunker.
type Source interface {
// Type returns the source type, used for matching against configuration and jobs.
Type() sourcespb.SourceType
// SourceID returns the initialized source ID used for tracking relationships in the DB.
SourceID() int64
// JobID returns the initialized job ID used for tracking relationships in the DB.
JobID() int64
// Init initializes the source.
Init(aCtx context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error
// Chunks emits data over a channel that is decoded and scanned for secrets.
Chunks(ctx context.Context, chunksChan chan *Chunk) error
// Completion Percentage for Scanned Source
GetProgress() *Progress
}
// PercentComplete is used to update job completion percentages across sources
type Progress struct {
mut sync.Mutex
PercentComplete int64
Message string
SectionsCompleted int32
SectionsRemaining int32
}
// SetProgressComplete sets job complete percentage based on passed in scope array of highest level objects. i is the current iteration in the loop of target scope, scope should be len(scoped_items)
func (p *Progress) SetProgressComplete(i, scope int, message string) {
p.mut.Lock()
defer p.mut.Unlock()
if p == nil {
p = &Progress{}
}
p.Message = message
p.SectionsCompleted = int32(i)
p.SectionsRemaining = int32(scope)
p.PercentComplete = int64((float64(i) / float64(scope)) * 100)
}
//GetProgressComplete gets job completion percentage for metrics reporting
func (p *Progress) GetProgress() *Progress {
p.mut.Lock()
defer p.mut.Unlock()
return p
}