2022-01-13 20:02:24 +00:00
|
|
|
package filesystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-08-10 22:32:49 +00:00
|
|
|
diskbufferreader "github.com/bill-rich/disk-buffer-reader"
|
2022-01-13 20:02:24 +00:00
|
|
|
"github.com/go-errors/errors"
|
2023-02-10 17:02:55 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-08-10 02:20:02 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
|
2023-01-26 17:33:45 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/common"
|
2022-08-29 18:45:37 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
|
2022-08-03 03:36:21 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/handlers"
|
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-02-16 01:38:19 +00:00
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sanitizer"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/sources"
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// These buffer sizes are mainly driven by our largest credential size, which is GCP @ ~2.25KB.
|
|
|
|
// Having a peek size larger than that ensures that we have complete credential coverage in our chunks.
|
2022-08-10 02:20:02 +00:00
|
|
|
BufferSize = 10 * 1024 // 10KB
|
|
|
|
PeekSize = 3 * 1024 // 3KB
|
2022-01-13 20:02:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Source struct {
|
|
|
|
name string
|
|
|
|
sourceId int64
|
|
|
|
jobId int64
|
|
|
|
verify bool
|
|
|
|
paths []string
|
2023-02-10 17:02:55 +00:00
|
|
|
log logr.Logger
|
2023-01-26 17:33:45 +00:00
|
|
|
filter *common.Filter
|
2022-01-13 20:02:24 +00:00
|
|
|
sources.Progress
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the Source satisfies the interface at compile time
|
|
|
|
var _ sources.Source = (*Source)(nil)
|
|
|
|
|
|
|
|
// Type returns the type of source.
|
|
|
|
// It is used for matching source types in configuration and job input.
|
|
|
|
func (s *Source) Type() sourcespb.SourceType {
|
|
|
|
return sourcespb.SourceType_SOURCE_TYPE_FILESYSTEM
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Source) SourceID() int64 {
|
|
|
|
return s.sourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Source) JobID() int64 {
|
|
|
|
return s.jobId
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init returns an initialized Filesystem source.
|
2022-08-10 02:20:02 +00:00
|
|
|
func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64, verify bool, connection *anypb.Any, _ int) error {
|
2023-02-10 17:02:55 +00:00
|
|
|
s.log = aCtx.Logger()
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
s.name = name
|
|
|
|
s.sourceId = sourceId
|
|
|
|
s.jobId = jobId
|
|
|
|
s.verify = verify
|
|
|
|
|
|
|
|
var conn sourcespb.Filesystem
|
2022-08-10 02:20:02 +00:00
|
|
|
if err := anypb.UnmarshalTo(connection, &conn, proto.UnmarshalOptions{}); err != nil {
|
|
|
|
return errors.WrapPrefix(err, "error unmarshalling connection", 0)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
2023-02-27 18:15:05 +00:00
|
|
|
s.paths = append(conn.Paths, conn.Directories...)
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-26 17:33:45 +00:00
|
|
|
func (s *Source) WithFilter(filter *common.Filter) {
|
|
|
|
s.filter = filter
|
|
|
|
}
|
|
|
|
|
2022-01-13 20:02:24 +00:00
|
|
|
// Chunks emits chunks of bytes over a channel.
|
|
|
|
func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) error {
|
|
|
|
for i, path := range s.paths {
|
2023-02-27 18:15:05 +00:00
|
|
|
logger := ctx.Logger().WithValues("path", path)
|
|
|
|
if common.IsDone(ctx) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 21:50:23 +00:00
|
|
|
s.SetProgressComplete(i, len(s.paths), fmt.Sprintf("Path: %s", path), "")
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
cleanPath := filepath.Clean(path)
|
2023-02-27 18:15:05 +00:00
|
|
|
fileInfo, err := os.Stat(cleanPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err, "unable to get file info")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
err = s.scanDir(ctx, cleanPath, chunksChan)
|
|
|
|
} else {
|
|
|
|
err = s.scanFile(ctx, cleanPath, chunksChan)
|
|
|
|
}
|
2022-01-13 20:02:24 +00:00
|
|
|
|
|
|
|
if err != nil && err != io.EOF {
|
2023-02-27 18:15:05 +00:00
|
|
|
logger.Info("error scanning filesystem", "error", err)
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
2023-02-27 18:15:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-13 20:02:24 +00:00
|
|
|
|
2023-02-27 18:15:05 +00:00
|
|
|
func (s *Source) scanDir(ctx context.Context, path string, chunksChan chan *sources.Chunk) error {
|
|
|
|
return fs.WalkDir(os.DirFS(path), ".", func(relativePath string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fullPath := filepath.Join(path, relativePath)
|
|
|
|
|
|
|
|
// Skip over non-regular files. We do this check here to suppress noisy
|
|
|
|
// logs for trying to scan directories and other non-regular files in
|
|
|
|
// our traversal.
|
|
|
|
fileStat, err := os.Stat(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Logger().Info("unable to stat file", "path", fullPath, "error", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !fileStat.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if s.filter != nil && !s.filter.Pass(fullPath) {
|
2022-01-13 20:02:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 18:15:05 +00:00
|
|
|
if err = s.scanFile(ctx, fullPath, chunksChan); err != nil {
|
|
|
|
ctx.Logger().Info("error scanning file", "path", fullPath, "error", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Source) scanFile(ctx context.Context, path string, chunksChan chan *sources.Chunk) error {
|
|
|
|
logger := ctx.Logger().WithValues("path", path)
|
|
|
|
fileStat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to stat file: %w", err)
|
|
|
|
}
|
|
|
|
if !fileStat.Mode().IsRegular() {
|
|
|
|
return fmt.Errorf("not a regular file")
|
|
|
|
}
|
|
|
|
|
|
|
|
inputFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to open file: %w", err)
|
|
|
|
}
|
|
|
|
defer inputFile.Close()
|
|
|
|
logger.V(3).Info("scanning file")
|
|
|
|
|
|
|
|
reReader, err := diskbufferreader.New(inputFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create re-readable reader: %w", err)
|
|
|
|
}
|
|
|
|
defer reReader.Close()
|
|
|
|
|
|
|
|
chunkSkel := &sources.Chunk{
|
|
|
|
SourceType: s.Type(),
|
|
|
|
SourceName: s.name,
|
|
|
|
SourceID: s.SourceID(),
|
|
|
|
SourceMetadata: &source_metadatapb.MetaData{
|
|
|
|
Data: &source_metadatapb.MetaData_Filesystem{
|
|
|
|
Filesystem: &source_metadatapb.Filesystem{
|
|
|
|
File: sanitizer.UTF8(path),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Verify: s.verify,
|
|
|
|
}
|
|
|
|
if handlers.HandleFile(ctx, reReader, chunkSkel, chunksChan) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := reReader.Reset(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reReader.Stop()
|
|
|
|
data, err := io.ReadAll(reReader)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read file: %w", err)
|
|
|
|
}
|
|
|
|
chunksChan <- &sources.Chunk{
|
|
|
|
SourceType: s.Type(),
|
|
|
|
SourceName: s.name,
|
|
|
|
SourceID: s.SourceID(),
|
|
|
|
Data: data,
|
|
|
|
SourceMetadata: &source_metadatapb.MetaData{
|
|
|
|
Data: &source_metadatapb.MetaData_Filesystem{
|
|
|
|
Filesystem: &source_metadatapb.Filesystem{
|
|
|
|
File: sanitizer.UTF8(path),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Verify: s.verify,
|
2022-01-13 20:02:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|