go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/connection/filesystem.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package connection
     5  
     6  import (
     7  	"errors"
     8  
     9  	"github.com/rs/zerolog/log"
    10  	"github.com/spf13/afero"
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    12  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    13  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    14  	"go.mondoo.com/cnquery/providers/os/fs"
    15  )
    16  
    17  const (
    18  	FileSystem shared.ConnectionType = "filesystem"
    19  )
    20  
    21  var _ shared.Connection = &FileSystemConnection{}
    22  
    23  func NewFileSystemConnectionWithClose(id uint32, conf *inventory.Config, assert *inventory.Asset, closeFN func()) (*FileSystemConnection, error) {
    24  	path, ok := conf.Options["path"]
    25  	if !ok {
    26  		// fallback to host + path option
    27  		path = conf.Host + conf.Path
    28  	}
    29  
    30  	if path == "" {
    31  		return nil, errors.New("missing filesystem mount path, use 'path' option")
    32  	}
    33  
    34  	log.Debug().Str("path", path).Msg("load filesystem")
    35  
    36  	return &FileSystemConnection{
    37  		id:         id,
    38  		Conf:       conf,
    39  		asset:      assert,
    40  		MountedDir: path,
    41  		closeFN:    closeFN,
    42  		fs:         fs.NewMountedFs(path),
    43  	}, nil
    44  }
    45  
    46  func NewFileSystemConnection(id uint32, conf *inventory.Config, assert *inventory.Asset) (*FileSystemConnection, error) {
    47  	return NewFileSystemConnectionWithClose(id, conf, assert, nil)
    48  }
    49  
    50  type FileSystemConnection struct {
    51  	id    uint32
    52  	Conf  *inventory.Config
    53  	asset *inventory.Asset
    54  
    55  	MountedDir   string
    56  	fs           afero.Fs
    57  	tcPlatformId string
    58  	closeFN      func()
    59  }
    60  
    61  func (c *FileSystemConnection) RunCommand(command string) (*shared.Command, error) {
    62  	return nil, plugin.ErrRunCommandNotImplemented
    63  }
    64  
    65  func (c *FileSystemConnection) FileSystem() afero.Fs {
    66  	if c.fs == nil {
    67  		c.fs = fs.NewMountedFs(c.MountedDir)
    68  	}
    69  	return c.fs
    70  }
    71  
    72  func (c *FileSystemConnection) FileInfo(path string) (shared.FileInfoDetails, error) {
    73  	fs := c.FileSystem()
    74  	afs := &afero.Afero{Fs: fs}
    75  	stat, err := afs.Stat(path)
    76  	if err != nil {
    77  		return shared.FileInfoDetails{}, err
    78  	}
    79  
    80  	uid, gid := c.fileowner(stat)
    81  
    82  	mode := stat.Mode()
    83  	return shared.FileInfoDetails{
    84  		Mode: shared.FileModeDetails{mode},
    85  		Size: stat.Size(),
    86  		Uid:  uid,
    87  		Gid:  gid,
    88  	}, nil
    89  }
    90  
    91  func (c *FileSystemConnection) Close() {
    92  	if c.closeFN != nil {
    93  		c.closeFN()
    94  	}
    95  }
    96  
    97  func (c *FileSystemConnection) Capabilities() shared.Capabilities {
    98  	return shared.Capability_FileSearch | shared.Capability_File
    99  }
   100  
   101  func (c *FileSystemConnection) Identifier() (string, error) {
   102  	if c.tcPlatformId == "" {
   103  		return "", errors.New("not platform id provided")
   104  	}
   105  	return c.tcPlatformId, nil
   106  }
   107  
   108  func (c *FileSystemConnection) ID() uint32 {
   109  	return c.id
   110  }
   111  
   112  func (c *FileSystemConnection) Name() string {
   113  	return string(FileSystem)
   114  }
   115  
   116  func (c *FileSystemConnection) Type() shared.ConnectionType {
   117  	return FileSystem
   118  }
   119  
   120  func (c *FileSystemConnection) Asset() *inventory.Asset {
   121  	return c.asset
   122  }