-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain.go
270 lines (223 loc) · 7.55 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"path"
"gocloud.dev/blob"
_ "gocloud.dev/blob/fileblob"
_ "gocloud.dev/blob/gcsblob"
_ "gocloud.dev/blob/s3blob"
"gocloud.dev/pubsub"
_ "gocloud.dev/pubsub/gcppubsub"
_ "gocloud.dev/pubsub/kafkapubsub"
"github.com/ossf/package-analysis/cmd/worker/pubsubextender"
"github.com/ossf/package-analysis/internal/featureflags"
"github.com/ossf/package-analysis/internal/log"
"github.com/ossf/package-analysis/internal/notification"
"github.com/ossf/package-analysis/internal/pkgmanager"
"github.com/ossf/package-analysis/internal/sandbox"
"github.com/ossf/package-analysis/internal/staticanalysis"
"github.com/ossf/package-analysis/internal/useragent"
"github.com/ossf/package-analysis/internal/worker"
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
)
const (
localPkgPathFmt = "/local/%s"
)
func copyPackageToLocalFile(ctx context.Context, packagesBucket *blob.Bucket, bucketPath string) (string, *os.File, error) {
if packagesBucket == nil {
return "", nil, errors.New("packages bucket not set")
}
// Copy remote package path to temporary file.
r, err := packagesBucket.NewReader(ctx, bucketPath, nil)
if err != nil {
return "", nil, err
}
defer r.Close()
f, err := os.CreateTemp("", "")
if err != nil {
return "", nil, err
}
if _, err := io.Copy(f, r); err != nil {
return "", nil, err
}
if err := f.Close(); err != nil {
return "", nil, err
}
return fmt.Sprintf(localPkgPathFmt, path.Base(bucketPath)), f, nil
}
func handleMessage(ctx context.Context, msg *pubsub.Message, cfg *config, packagesBucket *blob.Bucket, notificationTopic *pubsub.Topic) error {
name := msg.Metadata["name"]
if name == "" {
slog.WarnContext(ctx, "name is empty")
return nil
}
ecosystem := pkgecosystem.Ecosystem(msg.Metadata["ecosystem"])
if ecosystem == "" {
slog.WarnContext(ctx, "ecosystem is empty", "name", name)
return nil
}
ctx = log.ContextWithAttrs(ctx,
log.Label("ecosystem", ecosystem.String()),
slog.String("name", name))
manager := pkgmanager.Manager(ecosystem)
if manager == nil {
slog.WarnContext(ctx, "Unsupported pkg manager")
return nil
}
version := msg.Metadata["version"]
remotePkgPath := msg.Metadata["package_path"]
ctx = log.ContextWithAttrs(ctx, slog.String("version", version))
slog.InfoContext(ctx, "Got request",
"package_path", remotePkgPath,
)
localPkgPath := ""
sandboxOpts := []sandbox.Option{sandbox.Tag(cfg.imageSpec.tag)}
if remotePkgPath != "" {
tmpPkgPath, pkgFile, err := copyPackageToLocalFile(ctx, packagesBucket, remotePkgPath)
if err != nil {
return err
}
defer os.Remove(pkgFile.Name())
localPkgPath = tmpPkgPath
sandboxOpts = append(sandboxOpts, sandbox.Volume(pkgFile.Name(), localPkgPath))
}
if cfg.imageSpec.noPull {
sandboxOpts = append(sandboxOpts, sandbox.NoPull())
}
pkg, err := worker.ResolvePkg(manager, name, version, localPkgPath)
if err != nil {
slog.ErrorContext(ctx, "Error resolving package", "error", err)
return err
}
staticSandboxOpts := append(worker.StaticSandboxOptions(), sandboxOpts...)
dynamicSandboxOpts := append(worker.DynamicSandboxOptions(), sandboxOpts...)
// propogate user agent extras to the static analysis sandbox if it is set.
if cfg.userAgentExtra != "" {
staticSandboxOpts = append(staticSandboxOpts, sandbox.SetEnv("OSSF_MALWARE_USER_AGENT_EXTRA", cfg.userAgentExtra))
}
// run both dynamic and static analysis regardless of error status of either
// and return combined error(s) afterwards, if applicable
staticResults, _, staticAnalysisErr := worker.RunStaticAnalysis(ctx, pkg, staticSandboxOpts, staticanalysis.All)
if staticAnalysisErr == nil {
staticAnalysisErr = worker.SaveStaticAnalysisData(ctx, pkg, cfg.resultStores, staticResults)
}
result, dynamicAnalysisErr := worker.RunDynamicAnalysis(ctx, pkg, dynamicSandboxOpts, "")
if dynamicAnalysisErr == nil {
dynamicAnalysisErr = worker.SaveDynamicAnalysisData(ctx, pkg, cfg.resultStores, result.Data)
}
cfg.resultStores.AnalyzedPackageSaved = false
// combine errors
if analysisErr := errors.Join(dynamicAnalysisErr, staticAnalysisErr); analysisErr != nil {
return analysisErr
}
if notificationTopic != nil {
if err := notification.PublishAnalysisCompletion(ctx, notificationTopic, name, version, ecosystem); err != nil {
return err
}
}
return nil
}
func messageLoop(ctx context.Context, cfg *config) error {
sub, err := pubsub.OpenSubscription(ctx, cfg.subURL)
if err != nil {
return err
}
extender, err := pubsubextender.New(ctx, cfg.subURL, sub)
if err != nil {
return err
}
slog.InfoContext(ctx, "Subscription deadline extender",
"deadline", extender.Deadline,
"grace_period", extender.GracePeriod)
// the default value of the notificationTopic object is nil
// if no environment variable for a notification topic is set,
// we pass in a nil notificationTopic object to handleMessage
// and continue with the analysis with no notifications published
var notificationTopic *pubsub.Topic
if cfg.notificationTopicURL != "" {
notificationTopic, err = pubsub.OpenTopic(ctx, cfg.notificationTopicURL)
if err != nil {
return err
}
defer notificationTopic.Shutdown(ctx)
}
var pkgsBkt *blob.Bucket
if cfg.packagesBucket != "" {
var err error
pkgsBkt, err = blob.OpenBucket(ctx, cfg.packagesBucket)
if err != nil {
return err
}
defer pkgsBkt.Close()
}
slog.InfoContext(ctx, "Listening for messages to process...")
for {
msg, err := sub.Receive(ctx)
if err != nil {
// All subsequent receive calls will return the same error, so we bail out.
return fmt.Errorf("error receiving message: %w", err)
}
// Create a message context to wrap the message_id that we are currently processing.
msgCtx := log.ContextWithAttrs(ctx, slog.String("message_id", msg.LoggableID))
me, err := extender.Start(msgCtx, msg, func() {
slog.InfoContext(msgCtx, "Message Ack deadline extended", "message_meta", msg.Metadata)
})
if err != nil {
// If Start fails it will always fail, so we bail out.
// Nack the message if we can to indicate the failure.
if msg.Nackable() {
msg.Nack()
}
return fmt.Errorf("error starting message ack deadline extender: %w", err)
}
if err := handleMessage(msgCtx, msg, cfg, pkgsBkt, notificationTopic); err != nil {
slog.ErrorContext(msgCtx, "Failed to process message", "error", err)
if err := me.Stop(); err != nil {
slog.ErrorContext(msgCtx, "Extender failed", "error", err)
}
if msg.Nackable() {
msg.Nack()
}
} else {
if err := me.Stop(); err != nil {
slog.ErrorContext(msgCtx, "Extender failed", "error", err)
}
msg.Ack()
}
}
}
func main() {
log.Initialize(os.Getenv("LOGGER_ENV"))
ctx := context.Background()
cfg := configFromEnv()
http.DefaultTransport = useragent.DefaultRoundTripper(http.DefaultTransport, cfg.userAgentExtra)
if err := featureflags.Update(os.Getenv("OSSF_MALWARE_FEATURE_FLAGS")); err != nil {
slog.Error("Failed to parse feature flags", "error", err)
os.Exit(1)
}
sandbox.InitNetwork(ctx)
// If configured, start a webserver so that Go's pprof can be accessed for
// debugging and profiling.
if os.Getenv("OSSF_MALWARE_ANALYSIS_ENABLE_PROFILER") != "" {
go func() {
slog.Info("Starting profiler")
http.ListenAndServe(":6060", nil)
}()
}
// Log the configuration of the worker at startup so we can observe it.
slog.InfoContext(ctx, "Starting worker",
"config", cfg,
"feature_flags", featureflags.State(),
)
err := messageLoop(ctx, cfg)
if err != nil {
slog.ErrorContext(ctx, "Error encountered", "error", err)
}
}