-
Notifications
You must be signed in to change notification settings - Fork 37
/
plugins.go
187 lines (161 loc) · 4.52 KB
/
plugins.go
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
178
179
180
181
182
183
184
185
186
187
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os/exec"
"strings"
"sync"
extism "github.com/extism/go-sdk"
"github.com/nilslice/protolock"
"github.com/nilslice/protolock/extend"
)
const logPrefix = "[protolock]"
func runPlugins(
pluginList string,
report *protolock.Report,
debug bool,
) (*protolock.Report, error) {
inputData := &bytes.Buffer{}
err := json.NewEncoder(inputData).Encode(&extend.Data{
Current: report.Current,
Updated: report.Updated,
ProtolockWarnings: report.Warnings,
PluginWarnings: []protolock.Warning{},
})
if err != nil {
return nil, err
}
// collect plugin warnings and errors as they are returned from plugins
pluginWarningsChan := make(chan []protolock.Warning)
pluginsDone := make(chan struct{})
pluginErrsChan := make(chan error)
var allPluginErrors []error
go func() {
for {
select {
case <-pluginsDone:
return
case err := <-pluginErrsChan:
if err != nil {
allPluginErrors = append(allPluginErrors, err)
}
case warnings := <-pluginWarningsChan:
for _, warning := range warnings {
report.Warnings = append(report.Warnings, warning)
}
}
}
}()
wg := &sync.WaitGroup{}
plugins := strings.Split(pluginList, ",")
for _, name := range plugins {
wg.Add(1)
// copy input data to be passed in to and processed by each plugin
pluginInputData := bytes.NewReader(inputData.Bytes())
// run all provided plugins in parallel, each recieving the same current
// and updated Protolock structs from the `protolock status` call
go func(name string) {
defer wg.Done()
// output is populated either by the execution of an Extism plugin or a native binary
var output []byte
name = strings.TrimSpace(name)
path := name
if debug {
fmt.Println(logPrefix, name, "running plugin")
}
if strings.HasSuffix(name, ".wasm") {
// do extism call
manifest := extism.Manifest{
Wasm: []extism.Wasm{extism.WasmFile{Path: name}},
// TODO: consider enabling external configuration to add hosts and paths
// AllowedHosts: []string{},
// AllowedPaths: map[string]string{},
}
plugin, err := extism.NewPlugin(context.Background(), manifest, extism.PluginConfig{EnableWasi: true}, nil)
if err != nil {
fmt.Println(logPrefix, name, "failed to create extism plugin:", err)
return
}
var exitCode uint32
exitCode, output, err = plugin.Call("status", inputData.Bytes())
if err != nil {
fmt.Println(logPrefix, name, "plugin exec error: ", err, "code:", exitCode)
pluginErrsChan <- wrapPluginErr(name, path, err, output)
return
}
} else {
path, err = exec.LookPath(name)
if err != nil {
if path == "" {
path = name
}
fmt.Println(logPrefix, name, "plugin exec error:", err)
return
}
// initialize the executable to be called from protolock using the
// absolute path and copy of the input data
plugin := &exec.Cmd{
Path: path,
Stdin: pluginInputData,
}
// execute the plugin and capture the output
output, err = plugin.CombinedOutput()
if err != nil {
pluginErrsChan <- wrapPluginErr(name, path, err, output)
return
}
}
pluginData := &extend.Data{}
err = json.Unmarshal(output, pluginData)
if err != nil {
fmt.Println(logPrefix, name, "plugin data decode error:", err)
// TODO: depending on the plugin, "output" could be quite
// verbose, though may not warrant debug flag guard.
if debug {
fmt.Println(
logPrefix, name, "plugin output:", string(output),
)
}
return
}
// gather all warnings from each plugin, and send to warning chan
// collector as a slice to keep together
if pluginData.PluginWarnings != nil {
pluginWarningsChan <- pluginData.PluginWarnings
}
if pluginData.PluginErrorMessage != "" {
pluginErrsChan <- wrapPluginErr(
name,
path,
errors.New(pluginData.PluginErrorMessage),
output,
)
}
}(name)
}
wg.Wait()
pluginsDone <- struct{}{}
if allPluginErrors != nil {
var errorMsgs []string
for _, pluginError := range allPluginErrors {
errorMsgs = append(errorMsgs, pluginError.Error())
}
return nil, fmt.Errorf(
"accumulated plugin errors: \n%s",
strings.Join(errorMsgs, "\n"),
)
}
return report, nil
}
func wrapPluginErr(name, path string, err error, output []byte) error {
return fmt.Errorf(
"%s (%s): %v\n%s",
name, path, err, strings.ReplaceAll(
string(output),
protolock.ProtoSep, protolock.FileSep,
),
)
}