-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathlocal.go
More file actions
177 lines (144 loc) · 4.04 KB
/
local.go
File metadata and controls
177 lines (144 loc) · 4.04 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package local
import (
"bytes"
"os"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
"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/connection/ssh/cat"
)
type LocalConnection struct {
plugin.Connection
shell []string
fs afero.Fs
Sudo *inventory.Sudo
asset *inventory.Asset
}
func NewConnection(id uint32, conf *inventory.Config, asset *inventory.Asset) *LocalConnection {
// expect unix shell by default
res := LocalConnection{
Connection: plugin.NewConnection(id, asset),
asset: asset,
}
if conf != nil {
res.Sudo = conf.Sudo
}
if runtime.GOOS == "windows" {
// It does not make any sense to use cmd as default shell
// shell = []string{"cmd", "/C"}
res.shell = []string{"powershell", "-c"}
} else {
res.shell = []string{"sh", "-c"}
}
return &res
}
func (p *LocalConnection) Name() string {
return "local"
}
func (p *LocalConnection) Type() shared.ConnectionType {
return shared.Type_Local
}
func (p *LocalConnection) Asset() *inventory.Asset {
return p.asset
}
func (p *LocalConnection) UpdateAsset(asset *inventory.Asset) {
p.asset = asset
}
func (p *LocalConnection) Capabilities() shared.Capabilities {
// local fs has experimental local search support that does not require find command
envSearch := os.Getenv("LOCAL_FS_SEARCH")
if envSearch == "1" || strings.ToLower(envSearch) == "true" {
return shared.Capability_File | shared.Capability_RunCommand | shared.Capability_FindFile
}
return shared.Capability_File | shared.Capability_RunCommand
}
func (p *LocalConnection) RunCommand(command string) (*shared.Command, error) {
if p.Sudo != nil {
command = shared.BuildSudoCommand(p.Sudo, command)
}
log.Debug().Msgf("local> run command %s", command)
c := &CommandRunner{Shell: p.shell}
args := []string{}
res, err := c.Exec(command, args)
return res, err
}
func (p *LocalConnection) FileSystem() afero.Fs {
if p.fs != nil {
return p.fs
}
if p.Sudo != nil && p.Sudo.Active {
p.fs = cat.New(p)
} else {
p.fs = NewFs()
}
return p.fs
}
func (p *LocalConnection) FileInfo(path string) (shared.FileInfoDetails, error) {
fs := p.FileSystem()
afs := &afero.Afero{Fs: fs}
stat, err := afs.Stat(path)
if err != nil {
return shared.FileInfoDetails{}, err
}
uid, gid := p.fileowner(stat)
mode := stat.Mode()
return shared.FileInfoDetails{
Mode: shared.FileModeDetails{mode},
Size: stat.Size(),
Uid: uid,
Gid: gid,
}, nil
}
type CommandRunner struct {
shared.Command
cmdExecutor *exec.Cmd
Shell []string
}
func (c *CommandRunner) Exec(usercmd string, args []string) (*shared.Command, error) {
c.Command.Stats.Start = time.Now()
var cmd string
cmdArgs := []string{}
if len(c.Shell) > 0 {
shellCommand, shellArgs := c.Shell[0], c.Shell[1:]
cmd = shellCommand
cmdArgs = append(cmdArgs, shellArgs...)
cmdArgs = append(cmdArgs, usercmd)
} else {
cmd = usercmd
}
cmdArgs = append(cmdArgs, args...)
// this only stores the user command, not the shell
c.Command.Command = usercmd + " " + strings.Join(args, " ")
c.cmdExecutor = exec.Command(cmd, cmdArgs...)
var stdoutBuffer bytes.Buffer
var stderrBuffer bytes.Buffer
// create buffered stream
c.Command.Stdout = &stdoutBuffer
c.Command.Stderr = &stderrBuffer
c.cmdExecutor.Stdout = c.Command.Stdout
c.cmdExecutor.Stderr = c.Command.Stderr
err := c.cmdExecutor.Run()
c.Command.Stats.Duration = time.Since(c.Command.Stats.Start)
// command completed successfully, great :-)
if err == nil {
return &c.Command, nil
}
// if the program failed, we do not return err but its exit code
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
c.Command.ExitStatus = status.ExitStatus()
}
return &c.Command, nil
}
// all other errors are real errors and not expected
return &c.Command, err
}