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