-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.go
284 lines (235 loc) · 8.52 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"strings"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"github.com/ossf/package-analysis/internal/analysis"
"github.com/ossf/package-analysis/internal/featureflags"
"github.com/ossf/package-analysis/internal/log"
"github.com/ossf/package-analysis/internal/pkgmanager"
"github.com/ossf/package-analysis/internal/resultstore"
"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/utils"
"github.com/ossf/package-analysis/internal/worker"
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
)
var (
pkgName = flag.String("package", "", "package name")
localPkg = flag.String("local", "", "local package path")
ecosystem pkgecosystem.Ecosystem
version = flag.String("version", "", "version")
noPull = flag.Bool("nopull", false, "disables pulling down sandbox images")
imageTag = flag.String("image-tag", "", "set image tag for analysis sandboxes")
dynamicBucket = flag.String("dynamic-bucket", "", "bucket path for uploading dynamic analysis results")
staticBucket = flag.String("static-bucket", "", "bucket path for uploading static analysis results")
executionLogBucket = flag.String("execution-log-bucket", "", "bucket path for uploading execution log (dynamic analysis)")
fileWritesBucket = flag.String("file-writes-bucket", "", "bucket path for uploading file writes data (dynamic analysis)")
analyzedPkgBucket = flag.String("analyzed-pkg-bucket", "", "bucket path for uploading analyzed packages")
offline = flag.Bool("offline", false, "disables sandbox network access")
customSandbox = flag.String("sandbox-image", "", "override default dynamic analysis sandbox with custom image")
customAnalysisCmd = flag.String("analysis-command", "", "override default dynamic analysis script path (use with custom sandbox image)")
listModes = flag.Bool("list-modes", false, "prints out a list of available analysis modes")
features = flag.String("features", "", "override features that are enabled/disabled by default")
listFeatures = flag.Bool("list-features", false, "list available features that can be toggled")
help = flag.Bool("help", false, "print help on available options")
analysisMode = utils.CommaSeparatedFlags("mode", []string{"static", "dynamic"},
"list of analysis modes to run, separated by commas. Use -list-modes to see available options")
)
// usageError wraps an error, to signal that the error arises from incorrect user input.
type usageError struct {
error
}
func usagef(format string, args ...any) error {
return usageError{fmt.Errorf(format, args...)}
}
func makeResultStores() worker.ResultStores {
rs := worker.ResultStores{}
if *analyzedPkgBucket != "" {
rs.AnalyzedPackage = resultstore.New(*analyzedPkgBucket)
}
if *dynamicBucket != "" {
rs.DynamicAnalysis = resultstore.New(*dynamicBucket)
}
if *executionLogBucket != "" {
rs.ExecutionLog = resultstore.New(*executionLogBucket)
}
if *fileWritesBucket != "" {
rs.FileWrites = resultstore.New(*fileWritesBucket)
}
if *staticBucket != "" {
rs.StaticAnalysis = resultstore.New(*staticBucket)
}
return rs
}
func printAnalysisModes() {
fmt.Println("Available analysis modes:")
for _, mode := range analysis.AllModes() {
fmt.Println(mode)
}
fmt.Println()
}
func printFeatureFlags() {
fmt.Printf("Feature List\n\n")
fmt.Printf("%-30s %s\n", "Name", "Default")
fmt.Printf("----------------------------------------\n")
// print features in sorted order
state := featureflags.State()
sortedFeatures := maps.Keys(state)
slices.Sort(sortedFeatures)
// print Off/On rather than 'false' and 'true'
stateStrings := map[bool]string{false: "Off", true: "On"}
for _, feature := range sortedFeatures {
fmt.Printf("%-30s %s\n", feature, stateStrings[state[feature]])
}
fmt.Println()
}
// makeSandboxOptions prepares options for the sandbox based on command line arguments.
//
// In particular:
//
// 1. The image tag is always passed through. An empty tag is the same as "latest".
// 2. A local package is mapped into the sandbox if applicable.
// 3. Image pulling is disabled if the "-nopull" command-line flag was used.
func makeSandboxOptions() []sandbox.Option {
sbOpts := []sandbox.Option{sandbox.Tag(*imageTag)}
if *localPkg != "" {
sbOpts = append(sbOpts, sandbox.Copy(*localPkg, *localPkg))
}
if *noPull {
sbOpts = append(sbOpts, sandbox.NoPull())
}
if *offline {
sbOpts = append(sbOpts, sandbox.Offline())
}
return sbOpts
}
func dynamicAnalysis(ctx context.Context, pkg *pkgmanager.Pkg, resultStores *worker.ResultStores) {
if !*offline {
sandbox.InitNetwork(ctx)
}
sbOpts := append(worker.DynamicSandboxOptions(), makeSandboxOptions()...)
if *customSandbox != "" {
sbOpts = append(sbOpts, sandbox.Image(*customSandbox))
}
result, err := worker.RunDynamicAnalysis(ctx, pkg, sbOpts, *customAnalysisCmd)
if err != nil {
slog.ErrorContext(ctx, "Dynamic analysis aborted (run error)", "error", err)
return
}
// this is only valid if RunDynamicAnalysis() returns nil err
if result.LastStatus != analysis.StatusCompleted {
slog.WarnContext(ctx, "Dynamic analysis phase did not complete successfully",
"last_run_phase", string(result.LastRunPhase),
"status", string(result.LastStatus))
}
if err := worker.SaveDynamicAnalysisData(ctx, pkg, resultStores, result.Data); err != nil {
slog.ErrorContext(ctx, "Upload error", "error", err)
}
}
func staticAnalysis(ctx context.Context, pkg *pkgmanager.Pkg, resultStores *worker.ResultStores) {
if !*offline {
sandbox.InitNetwork(ctx)
}
sbOpts := append(worker.StaticSandboxOptions(), makeSandboxOptions()...)
data, status, err := worker.RunStaticAnalysis(ctx, pkg, sbOpts, staticanalysis.All)
if err != nil {
slog.ErrorContext(ctx, "Static analysis aborted", "error", err)
return
}
slog.InfoContext(ctx, "Static analysis completed", "status", string(status))
if err := worker.SaveStaticAnalysisData(ctx, pkg, resultStores, data); err != nil {
slog.ErrorContext(ctx, "Upload error", "error", err)
}
}
func run() error {
log.Initialize(os.Getenv("LOGGER_ENV"))
flag.TextVar(&ecosystem, "ecosystem", pkgecosystem.None, "package ecosystem. Available: "+
strings.Join(pkgecosystem.SupportedEcosystemsStrings, ", "))
analysisMode.InitFlag()
flag.Parse()
http.DefaultTransport = useragent.DefaultRoundTripper(http.DefaultTransport, "")
if err := featureflags.Update(*features); err != nil {
return usageError{err}
}
if *help {
flag.Usage()
return nil
}
if *listModes {
printAnalysisModes()
return nil
}
if *listFeatures {
printFeatureFlags()
return nil
}
if ecosystem == pkgecosystem.None {
flag.Usage()
return usagef("missing ecosystem")
}
manager := pkgmanager.Manager(ecosystem)
if manager == nil {
return usagef("unsupported package ecosystem %q", ecosystem)
}
if *pkgName == "" {
flag.Usage()
return usagef("missing package name")
}
runMode := make(map[analysis.Mode]bool)
for _, analysisName := range analysisMode.Values {
mode, ok := analysis.ModeFromString(strings.ToLower(analysisName))
if !ok {
printAnalysisModes()
return usagef("unknown analysis mode %q", analysisName)
}
runMode[mode] = true
}
ctx := log.ContextWithAttrs(context.Background(),
slog.Any("ecosystem", ecosystem),
)
slog.InfoContext(ctx, "Got request",
slog.String("requested_name", *pkgName),
slog.String("requested_version", *version),
)
pkg, err := worker.ResolvePkg(manager, *pkgName, *version, *localPkg)
if err != nil {
slog.ErrorContext(ctx, "Error resolving package", "error", err)
return err
}
ctx = log.ContextWithAttrs(ctx,
slog.String("name", pkg.Name()),
slog.String("version", pkg.Version()),
)
slog.InfoContext(ctx, "Processing resolved package", "package_path", *localPkg)
resultStores := makeResultStores()
if runMode[analysis.Static] {
slog.InfoContext(ctx, "Starting static analysis")
staticAnalysis(ctx, pkg, &resultStores)
}
// dynamicAnalysis() currently panics on error, so it's last
if runMode[analysis.Dynamic] {
slog.InfoContext(ctx, "Starting dynamic analysis")
dynamicAnalysis(ctx, pkg, &resultStores)
}
return nil
}
func main() {
if err := run(); err != nil {
if errors.As(err, &usageError{}) {
fmt.Fprintf(os.Stderr, "Usage error: %v\n", err)
os.Exit(2)
} else {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
}