2022-03-15 00:04:19 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-06-26 16:39:57 +00:00
|
|
|
"fmt"
|
2022-05-25 16:35:44 +00:00
|
|
|
"runtime"
|
|
|
|
|
2022-03-15 00:04:19 +00:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2022-08-10 17:11:13 +00:00
|
|
|
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-08-10 17:11:13 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/sourcespb"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources/filesystem"
|
2022-03-15 00:04:19 +00:00
|
|
|
)
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
// ScanFileSystem scans a given file system.
|
2023-02-10 20:43:00 +00:00
|
|
|
func (e *Engine) ScanFileSystem(ctx context.Context, c sources.FilesystemConfig) error {
|
2022-03-15 00:04:19 +00:00
|
|
|
connection := &sourcespb.Filesystem{
|
2023-02-27 18:15:05 +00:00
|
|
|
Paths: c.Paths,
|
2022-03-15 00:04:19 +00:00
|
|
|
}
|
|
|
|
var conn anypb.Any
|
|
|
|
err := anypb.MarshalFrom(&conn, connection, proto.MarshalOptions{})
|
|
|
|
if err != nil {
|
2023-02-09 22:55:19 +00:00
|
|
|
ctx.Logger().Error(err, "failed to marshal filesystem connection")
|
2022-03-15 00:04:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSystemSource := filesystem.Source{}
|
2023-07-21 20:20:25 +00:00
|
|
|
fileSystemSource.WithFilter(c.Filter)
|
|
|
|
|
|
|
|
// TODO: This is how it would be used; everything after these comments would be removed.
|
|
|
|
// handle, err := e.sourceManager.Enroll(ctx, "trufflehog - filesystem", fileSystemSource.Type(),
|
|
|
|
// func(ctx context.Context, jobID, sourceID int64) (sources.Source, error) {
|
|
|
|
// fileSystemSource := filesystem.Source{}
|
|
|
|
// fileSystemSource.WithFilter(c.Filter)
|
|
|
|
// if err := fileSystemSource.Init(ctx, "trufflehog - filesystem", jobID, sourceID, true, &conn, runtime.NumCPU()); err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
// return &fileSystemSource, nil
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// return e.sourceManager.ScheduleRun(ctx, handle)
|
2023-02-10 17:02:55 +00:00
|
|
|
|
|
|
|
ctx = context.WithValues(ctx,
|
|
|
|
"source_type", fileSystemSource.Type().String(),
|
|
|
|
"source_name", "filesystem",
|
|
|
|
)
|
2022-05-13 21:35:06 +00:00
|
|
|
err = fileSystemSource.Init(ctx, "trufflehog - filesystem", 0, int64(sourcespb.SourceType_SOURCE_TYPE_FILESYSTEM), true, &conn, runtime.NumCPU())
|
2022-03-15 00:04:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.WrapPrefix(err, "could not init filesystem source", 0)
|
|
|
|
}
|
2023-06-26 16:39:57 +00:00
|
|
|
e.sourcesWg.Go(func() error {
|
2022-09-22 14:01:10 +00:00
|
|
|
defer common.RecoverWithExit(ctx)
|
2022-03-15 00:04:19 +00:00
|
|
|
err := fileSystemSource.Chunks(ctx, e.ChunksChan())
|
|
|
|
if err != nil {
|
2023-06-26 16:39:57 +00:00
|
|
|
return fmt.Errorf("error scanning filesystem: %w", err)
|
2022-03-15 00:04:19 +00:00
|
|
|
}
|
2023-06-26 16:39:57 +00:00
|
|
|
return nil
|
|
|
|
})
|
2022-03-15 00:04:19 +00:00
|
|
|
return nil
|
|
|
|
}
|