Skip to content

Commit

Permalink
Update Source interface to use SourceID and JobID types (#1774)
Browse files Browse the repository at this point in the history
The previous implementation used int64 for both, which can be mixed up
easily. Using distinct types adds a layer of type safety checked by the
compiler.
  • Loading branch information
mcastorina authored Sep 14, 2023
1 parent bbc3be3 commit dbcb888
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pkg/detectors/detectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type ResultWithMetadata struct {
// SourceMetadata contains source-specific contextual information.
SourceMetadata *source_metadatapb.MetaData
// SourceID is the ID of the source that the API uses to map secrets to specific sources.
SourceID int64
SourceID sources.SourceID
// SourceType is the type of Source.
SourceType sourcespb.SourceType
// SourceName is the name of the Source.
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/circleci.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (e *Engine) ScanCircleCI(ctx context.Context, token string) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, circleci.SourceType)

circleSource := &circleci.Source{}
if err := circleSource.Init(ctx, "trufflehog - Circle CI", int64(jobID), int64(sourceID), true, &conn, runtime.NumCPU()); err != nil {
if err := circleSource.Init(ctx, "trufflehog - Circle CI", jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
_, err = e.sourceManager.Run(ctx, sourceName, circleSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (e *Engine) ScanDocker(ctx context.Context, conn *anypb.Any) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, docker.SourceType)

dockerSource := &docker.Source{}
if err := dockerSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, conn, runtime.NumCPU()); err != nil {
if err := dockerSource.Init(ctx, sourceName, jobID, sourceID, true, conn, runtime.NumCPU()); err != nil {
return err
}
_, err := e.sourceManager.Run(ctx, sourceName, dockerSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (e *Engine) ScanFileSystem(ctx context.Context, c sources.FilesystemConfig)

fileSystemSource := &filesystem.Source{}
fileSystemSource.WithFilter(c.Filter)
if err := fileSystemSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, runtime.NumCPU()); err != nil {
if err := fileSystemSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
_, err = e.sourceManager.Run(ctx, sourceName, fileSystemSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (e *Engine) ScanGCS(ctx context.Context, c sources.GCSConfig) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gcs.SourceType)

gcsSource := &gcs.Source{}
if err := gcsSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, int(c.Concurrency)); err != nil {
if err := gcsSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, int(c.Concurrency)); err != nil {
return err
}
_, err = e.sourceManager.Run(ctx, sourceName, gcsSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e *Engine) ScanGit(ctx context.Context, c sources.GitConfig) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, git.SourceType)

gitSource := &git.Source{}
if err := gitSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, runtime.NumCPU()); err != nil {
if err := gitSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
gitSource.WithScanOptions(scanOptions)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (e *Engine) ScanGitHub(ctx context.Context, c sources.GithubConfig) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, github.SourceType)

githubSource := &github.Source{}
if err := githubSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, c.Concurrency); err != nil {
if err := githubSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, c.Concurrency); err != nil {
return err
}
githubSource.WithScanOptions(scanOptions)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *Engine) ScanGitLab(ctx context.Context, c sources.GitlabConfig) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, gitlab.SourceType)

gitlabSource := &gitlab.Source{}
if err := gitlabSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, runtime.NumCPU()); err != nil {
if err := gitlabSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
gitlabSource.WithScanOptions(scanOptions)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (e *Engine) ScanS3(ctx context.Context, c sources.S3Config) error {
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, s3.SourceType)

s3Source := &s3.Source{}
if err := s3Source.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, runtime.NumCPU()); err != nil {
if err := s3Source.Init(ctx, sourceName, jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
return err
}
_, err = e.sourceManager.Run(ctx, sourceName, s3Source)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (e *Engine) ScanSyslog(ctx context.Context, c sources.SyslogConfig) error {
sourceName := "trufflehog - syslog"
sourceID, jobID, _ := e.sourceManager.GetIDs(ctx, sourceName, syslog.SourceType)
syslogSource := &syslog.Source{}
if err := syslogSource.Init(ctx, sourceName, int64(jobID), int64(sourceID), true, &conn, c.Concurrency); err != nil {
if err := syslogSource.Init(ctx, sourceName, jobID, sourceID, true, &conn, c.Concurrency); err != nil {
return err
}
syslogSource.InjectConnection(connection)
Expand Down
3 changes: 2 additions & 1 deletion pkg/output/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/source_metadatapb"
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
)

// JSONPrinter is a printer that prints results in JSON format.
Expand All @@ -20,7 +21,7 @@ func (p *JSONPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata)
// SourceMetadata contains source-specific contextual information.
SourceMetadata *source_metadatapb.MetaData
// SourceID is the ID of the source that the API uses to map secrets to specific sources.
SourceID int64
SourceID sources.SourceID
// SourceType is the type of Source.
SourceType sourcespb.SourceType
// SourceName is the name of the Source.
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/circleci/circleci.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const (
type Source struct {
name string
token string
sourceId int64
jobId int64
sourceId sources.SourceID
jobId sources.JobID
verify bool
jobPool *errgroup.Group
sources.Progress
Expand All @@ -47,16 +47,16 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

// Init returns an initialized CircleCI source.
func (s *Source) Init(_ context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(_ context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.name = name
s.sourceId = sourceId
s.jobId = jobId
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const SourceType = sourcespb.SourceType_SOURCE_TYPE_DOCKER

type Source struct {
name string
sourceId int64
jobId int64
sourceId sources.SourceID
jobId sources.JobID
verify bool
concurrency int
conn sourcespb.Docker
Expand All @@ -47,16 +47,16 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

// Init initializes the source.
func (s *Source) Init(_ context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(_ context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.name = name
s.sourceId = sourceId
s.jobId = jobId
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const SourceType = sourcespb.SourceType_SOURCE_TYPE_FILESYSTEM

type Source struct {
name string
sourceId int64
jobId int64
sourceId sources.SourceID
jobId sources.JobID
verify bool
paths []string
log logr.Logger
Expand All @@ -48,16 +48,16 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

// Init returns an initialized Filesystem source.
func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, _ int) error {
func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, _ int) error {
s.log = aCtx.Logger()

s.name = name
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func (s *Source) Type() sourcespb.SourceType {
}

// SourceID number for GCS Source.
func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

// JobID number for GCS Source.
func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

Expand All @@ -62,8 +62,8 @@ type objectManager interface {
// Source represents a GCS source.
type Source struct {
name string
jobId int64
sourceId int64
jobId sources.JobID
sourceId sources.SourceID
concurrency int
verify bool

Expand Down Expand Up @@ -111,7 +111,7 @@ func (c *persistableCache) shouldPersist() (bool, string) {
}

// Init returns an initialized GCS source.
func (s *Source) Init(aCtx context.Context, name string, id int64, sourceID int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(aCtx context.Context, name string, id sources.JobID, sourceID sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.log = aCtx.Logger()

s.name = name
Expand Down
16 changes: 8 additions & 8 deletions pkg/sources/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const SourceType = sourcespb.SourceType_SOURCE_TYPE_GIT

type Source struct {
name string
sourceId int64
jobId int64
sourceId sources.SourceID
jobId sources.JobID
verify bool
git *Git
sources.Progress
Expand All @@ -53,8 +53,8 @@ type Source struct {
type Git struct {
sourceType sourcespb.SourceType
sourceName string
sourceID int64
jobID int64
sourceID sources.SourceID
jobID sources.JobID
sourceMetadataFunc func(file, email, commit, timestamp, repository string, line int64) *source_metadatapb.MetaData
verify bool
metrics metrics
Expand All @@ -65,7 +65,7 @@ type metrics struct {
commitsScanned uint64
}

func NewGit(sourceType sourcespb.SourceType, jobID, sourceID int64, sourceName string, verify bool, concurrency int,
func NewGit(sourceType sourcespb.SourceType, jobID sources.JobID, sourceID sources.SourceID, sourceName string, verify bool, concurrency int,
sourceMetadataFunc func(file, email, commit, timestamp, repository string, line int64) *source_metadatapb.MetaData,
) *Git {
return &Git{
Expand All @@ -89,11 +89,11 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

Expand All @@ -111,7 +111,7 @@ func (s *Source) WithPreserveTempDirs(preserve bool) {
}

// Init returns an initialized GitHub source.
func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(aCtx context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.name = name
s.sourceId = sourceId
s.jobId = jobId
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type Source struct {
githubUser string
githubToken string

sourceID int64
jobID int64
sourceID sources.SourceID
jobID sources.JobID
verify bool
repos []string
members []string
Expand Down Expand Up @@ -109,11 +109,11 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceID
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobID
}

Expand Down Expand Up @@ -202,7 +202,7 @@ func (c *filteredRepoCache) includeRepo(s string) bool {
}

// Init returns an initialized GitHub source.
func (s *Source) Init(aCtx context.Context, name string, jobID, sourceID int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(aCtx context.Context, name string, jobID sources.JobID, sourceID sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.log = aCtx.Logger()

s.name = name
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const SourceType = sourcespb.SourceType_SOURCE_TYPE_GITLAB

type Source struct {
name string
sourceId int64
jobId int64
sourceId sources.SourceID
jobId sources.JobID
verify bool
authMethod string
user string
Expand Down Expand Up @@ -63,16 +63,16 @@ func (s *Source) Type() sourcespb.SourceType {
return SourceType
}

func (s *Source) SourceID() int64 {
func (s *Source) SourceID() sources.SourceID {
return s.sourceId
}

func (s *Source) JobID() int64 {
func (s *Source) JobID() sources.JobID {
return s.jobId
}

// Init returns an initialized Gitlab source.
func (s *Source) Init(_ context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, concurrency int) error {
func (s *Source) Init(_ context.Context, name string, jobId sources.JobID, sourceId sources.SourceID, verify bool, connection *anypb.Any, concurrency int) error {
s.name = name
s.sourceId = sourceId
s.jobId = jobId
Expand Down
10 changes: 5 additions & 5 deletions pkg/sources/job_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ type JobProgressHook interface {
// If the job supports it, the reference can also be used to cancel running via
// CancelRun.
type JobProgressRef struct {
JobID int64
SourceID int64
JobID JobID
SourceID SourceID
SourceName string
jobProgress *JobProgress
}
Expand Down Expand Up @@ -101,8 +101,8 @@ func (f ChunkError) Unwrap() error { return f.err }
// JobProgress aggregates information about a run of a Source.
type JobProgress struct {
// Unique identifiers for this job.
JobID int64
SourceID int64
JobID JobID
SourceID SourceID
SourceName string
// Tracks whether the job is finished or not.
ctx context.Context
Expand Down Expand Up @@ -155,7 +155,7 @@ func WithCancel(cancel context.CancelCauseFunc) func(*JobProgress) {
}

// NewJobProgress creates a new job report for the given source and job ID.
func NewJobProgress(jobID, sourceID int64, sourceName string, opts ...func(*JobProgress)) *JobProgress {
func NewJobProgress(jobID JobID, sourceID SourceID, sourceName string, opts ...func(*JobProgress)) *JobProgress {
ctx, cancel := context.WithCancel(context.Background())
jp := &JobProgress{
JobID: jobID,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sources/job_progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func TestJobProgressFatalErrors(t *testing.T) {
func TestJobProgressRef(t *testing.T) {
jp := NewJobProgress(123, 456, "source name")
ref := jp.Ref()
assert.Equal(t, int64(123), ref.JobID)
assert.Equal(t, int64(456), ref.SourceID)
assert.Equal(t, JobID(123), ref.JobID)
assert.Equal(t, SourceID(456), ref.SourceID)

// Test Done() blocks until Finish() is called.
select {
Expand Down
Loading

0 comments on commit dbcb888

Please sign in to comment.