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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package scp
     5  
     6  import (
     7  	"errors"
     8  	"os"
     9  	"strings"
    10  	"time"
    11  
    12  	scp_client "github.com/hnakamur/go-scp"
    13  	"github.com/spf13/afero"
    14  	"go.mondoo.com/cnquery/providers/os/connection/local/statutil"
    15  	"go.mondoo.com/cnquery/providers/os/connection/ssh/cat"
    16  	"golang.org/x/crypto/ssh"
    17  )
    18  
    19  func NewFs(commandRunner cat.CommandRunner, client *ssh.Client) *Fs {
    20  	return &Fs{
    21  		sshClient:     client,
    22  		scpClient:     scp_client.NewSCP(client),
    23  		commandRunner: commandRunner,
    24  		catFs:         cat.New(commandRunner),
    25  	}
    26  }
    27  
    28  type Fs struct {
    29  	sshClient     *ssh.Client
    30  	scpClient     *scp_client.SCP
    31  	commandRunner cat.CommandRunner
    32  	catFs         *cat.Fs
    33  }
    34  
    35  func (s Fs) Name() string { return "scpfs" }
    36  
    37  func (s Fs) Create(name string) (afero.File, error) {
    38  	return nil, errors.New("create not implemented")
    39  }
    40  
    41  func (s Fs) Mkdir(name string, perm os.FileMode) error {
    42  	return errors.New("mkdir not implemented")
    43  }
    44  
    45  func (s Fs) MkdirAll(path string, perm os.FileMode) error {
    46  	return errors.New("mkdirall not implemented")
    47  }
    48  
    49  func (s Fs) Open(path string) (afero.File, error) {
    50  	// NOTE: procfs cannot be read via scp, so we fall-back to catfs all paths there
    51  	if strings.HasPrefix(path, "/proc") {
    52  		return s.catFs.Open(path)
    53  	}
    54  
    55  	return FileOpen(s.scpClient, path)
    56  }
    57  
    58  func (s Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
    59  	return nil, errors.New("openfile not implemented")
    60  }
    61  
    62  func (s Fs) Remove(name string) error {
    63  	return errors.New("remove not implemented")
    64  }
    65  
    66  func (s Fs) RemoveAll(path string) error {
    67  	return errors.New("removeall not implemented")
    68  }
    69  
    70  func (s Fs) Rename(oldname, newname string) error {
    71  	return errors.New("rename not implemented")
    72  }
    73  
    74  func (s Fs) Stat(path string) (os.FileInfo, error) {
    75  	// NOTE we cannot use s.scpClient.Receive(path, io.Discard) since it would not work with directories
    76  	return statutil.New(s.commandRunner).Stat(path)
    77  }
    78  
    79  func (s Fs) Lstat(p string) (os.FileInfo, error) {
    80  	return nil, errors.New("lstat not implemented")
    81  }
    82  
    83  func (s Fs) Chmod(name string, mode os.FileMode) error {
    84  	return errors.New("chmod not implemented")
    85  }
    86  
    87  func (s Fs) Chtimes(name string, atime time.Time, mtime time.Time) error {
    88  	return errors.New("chtimes not implemented")
    89  }
    90  
    91  func (s Fs) Chown(name string, uid, gid int) error {
    92  	return errors.New("chown not implemented")
    93  }