2022-01-13 20:02:24 +00:00
|
|
|
package sources
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-02-10 18:54:33 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2022-05-13 21:35:06 +00:00
|
|
|
Init(aCtx context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error
|
2022-01-13 20:02:24 +00:00
|
|
|
// Chunks emits data over a channel that is decoded and scanned for secrets.
|
|
|
|
Chunks(ctx context.Context, chunksChan chan *Chunk) error
|
2022-09-08 02:40:37 +00:00
|
|
|
// GetProgress is the completion progress (percentage) for Scanned Source.
|
2022-01-13 20:02:24 +00:00
|
|
|
GetProgress() *Progress
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:43:00 +00:00
|
|
|
// GitConfig defines the optional configuration for a git source.
|
|
|
|
type GitConfig struct {
|
2022-08-10 17:11:13 +00:00
|
|
|
// RepoPath is the path to the repository to scan.
|
|
|
|
RepoPath,
|
|
|
|
// HeadRef is the head reference to use to scan from.
|
|
|
|
HeadRef,
|
|
|
|
// BaseRef is the base reference to use to scan from.
|
|
|
|
BaseRef string
|
|
|
|
// MaxDepth is the maximum depth to scan the source.
|
|
|
|
MaxDepth int
|
2023-02-10 20:43:00 +00:00
|
|
|
// Filter is the filter to use to scan the source.
|
|
|
|
Filter *common.Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// GithubConfig defines the optional configuration for a github source.
|
|
|
|
type GithubConfig struct {
|
|
|
|
// Endpoint is the endpoint of the source.
|
|
|
|
Endpoint,
|
|
|
|
// Token is the token to use to authenticate with the source.
|
|
|
|
Token string
|
2022-08-10 17:11:13 +00:00
|
|
|
// IncludeForks indicates whether to include forks in the scan.
|
|
|
|
IncludeForks,
|
|
|
|
// IncludeMembers indicates whether to include members in the scan.
|
2023-02-10 20:43:00 +00:00
|
|
|
IncludeMembers bool
|
|
|
|
// Concurrency is the number of concurrent workers to use to scan the source.
|
|
|
|
Concurrency int
|
2022-08-10 17:11:13 +00:00
|
|
|
// Repos is the list of repositories to scan.
|
|
|
|
Repos,
|
|
|
|
// Orgs is the list of organizations to scan.
|
|
|
|
Orgs,
|
2022-12-16 21:28:16 +00:00
|
|
|
// ExcludeRepos is a list of repositories to exclude from the scan.
|
|
|
|
ExcludeRepos,
|
|
|
|
// IncludeRepos is a list of repositories to include in the scan.
|
2023-02-10 20:43:00 +00:00
|
|
|
IncludeRepos []string
|
2023-02-14 16:40:53 +00:00
|
|
|
// Filter is the filter to use to scan the source.
|
|
|
|
Filter *common.Filter
|
2023-02-10 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitlabConfig defines the optional configuration for a gitlab source.
|
|
|
|
type GitlabConfig struct {
|
|
|
|
// Endpoint is the endpoint of the source.
|
|
|
|
Endpoint,
|
|
|
|
// Token is the token to use to authenticate with the source.
|
|
|
|
Token string
|
|
|
|
// Repos is the list of repositories to scan.
|
|
|
|
Repos []string
|
|
|
|
// Filter is the filter to use to scan the source.
|
|
|
|
Filter *common.Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilesystemConfig defines the optional configuration for a filesystem source.
|
|
|
|
type FilesystemConfig struct {
|
2022-08-10 17:11:13 +00:00
|
|
|
// Directories is the list of directories to scan.
|
|
|
|
Directories []string
|
|
|
|
// Filter is the filter to use to scan the source.
|
|
|
|
Filter *common.Filter
|
|
|
|
}
|
|
|
|
|
2023-02-10 20:43:00 +00:00
|
|
|
// S3Config defines the optional configuration for an S3 source.
|
|
|
|
type S3Config struct {
|
|
|
|
// CloudCred determines whether to use cloud credentials.
|
|
|
|
// This can NOT be used with a secret.
|
|
|
|
CloudCred bool
|
|
|
|
// Key is any key to use to authenticate with the source.
|
|
|
|
Key,
|
|
|
|
// Secret is any secret to use to authenticate with the source.
|
|
|
|
Secret string
|
|
|
|
// Buckets is the list of buckets to scan.
|
|
|
|
Buckets []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyslogConfig defines the optional configuration for a syslog source.
|
|
|
|
type SyslogConfig struct {
|
|
|
|
// Address used to connect to the source.
|
|
|
|
Address,
|
|
|
|
// Protocol used to connect to the source.
|
|
|
|
Protocol,
|
|
|
|
// CertPath is the path to the certificate to use to connect to the source.
|
|
|
|
CertPath,
|
|
|
|
// Format is the format used to connect to the source.
|
|
|
|
Format,
|
|
|
|
// KeyPath is the path to the key to use to connect to the source.
|
|
|
|
KeyPath string
|
|
|
|
// Concurrency is the number of concurrent workers to use to scan the source.
|
|
|
|
Concurrency int
|
2022-08-10 17:11:13 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 02:40:37 +00:00
|
|
|
// Progress is used to update job completion progress across sources.
|
2022-01-13 20:02:24 +00:00
|
|
|
type Progress struct {
|
|
|
|
mut sync.Mutex
|
|
|
|
PercentComplete int64
|
|
|
|
Message string
|
2022-03-23 21:50:23 +00:00
|
|
|
EncodedResumeInfo string
|
2022-01-13 20:02:24 +00:00
|
|
|
SectionsCompleted int32
|
|
|
|
SectionsRemaining int32
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:50:23 +00:00
|
|
|
// SetProgressComplete sets job progress information for a running job based on the highest level objects in the source.
|
|
|
|
// i is the current iteration in the loop of target scope
|
|
|
|
// scope should be the len(scopedItems)
|
|
|
|
// message is the public facing user information about the current progress
|
|
|
|
// encodedResumeInfo is an optional string representing any information necessary to resume the job if interrupted
|
|
|
|
func (p *Progress) SetProgressComplete(i, scope int, message, encodedResumeInfo string) {
|
2022-01-13 20:02:24 +00:00
|
|
|
p.mut.Lock()
|
|
|
|
defer p.mut.Unlock()
|
2022-02-16 01:38:19 +00:00
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
p.Message = message
|
2022-03-23 21:50:23 +00:00
|
|
|
p.EncodedResumeInfo = encodedResumeInfo
|
2022-01-13 20:02:24 +00:00
|
|
|
p.SectionsCompleted = int32(i)
|
|
|
|
p.SectionsRemaining = int32(scope)
|
2022-09-08 02:40:37 +00:00
|
|
|
|
|
|
|
// If the iteration and scope are both 0, completion is 100%.
|
|
|
|
if i == 0 && scope == 0 {
|
|
|
|
p.PercentComplete = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
p.PercentComplete = int64((float64(i) / float64(scope)) * 100)
|
|
|
|
}
|
|
|
|
|
2022-09-08 02:40:37 +00:00
|
|
|
// GetProgress gets job completion percentage for metrics reporting.
|
2022-01-13 20:02:24 +00:00
|
|
|
func (p *Progress) GetProgress() *Progress {
|
|
|
|
p.mut.Lock()
|
|
|
|
defer p.mut.Unlock()
|
|
|
|
return p
|
|
|
|
}
|