Skip to content

Commit 9064b7c

Browse files
authored
Rename packages to improve readability. (#654)
* Rename packages to improve readability. This fixes #651 Changes: - internal/pkgecosystem -> internal/pkgmanager - pkg/api -> pkg/api/pkgecosystem + pkg/api/analysisrun.DynamicPhase - pkg/result -> pkg/api/analysisrun - pkg/notification -> pkg/api/notification - pkg/pkgidentifier -> pkg/api/analysisrun.Key Note - this is a breaking change. Signed-off-by: Caleb Brown <[email protected]> * Make it easier to use Ecosystem in a flag. Signed-off-by: Caleb Brown <[email protected]> * Add missing space. Signed-off-by: Caleb Brown <[email protected]> * Make the ecosystem flag usage even more flexible. Signed-off-by: Caleb Brown <[email protected]> * Remove unused FlagUsage Signed-off-by: Caleb Brown <[email protected]> * Tweak again the approach to returning a list of ecosystems. Signed-off-by: Caleb Brown <[email protected]> --------- Signed-off-by: Caleb Brown <[email protected]>
1 parent 169e5ce commit 9064b7c

File tree

29 files changed

+411
-210
lines changed

29 files changed

+411
-210
lines changed

cmd/analyze/main.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ import (
1010

1111
"github.com/ossf/package-analysis/internal/analysis"
1212
"github.com/ossf/package-analysis/internal/log"
13-
"github.com/ossf/package-analysis/internal/pkgecosystem"
13+
"github.com/ossf/package-analysis/internal/pkgmanager"
1414
"github.com/ossf/package-analysis/internal/resultstore"
1515
"github.com/ossf/package-analysis/internal/sandbox"
1616
"github.com/ossf/package-analysis/internal/staticanalysis"
1717
"github.com/ossf/package-analysis/internal/utils"
1818
"github.com/ossf/package-analysis/internal/worker"
19-
"github.com/ossf/package-analysis/pkg/api"
19+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
2020
)
2121

2222
var (
2323
pkgName = flag.String("package", "", "package name")
2424
localPkg = flag.String("local", "", "local package path")
25-
ecosystem = flag.String("ecosystem", "", "ecosystem (npm, pypi, or rubygems)")
25+
ecosystem pkgecosystem.Ecosystem
2626
version = flag.String("version", "", "version")
2727
noPull = flag.Bool("nopull", false, "disables pulling down sandbox images")
2828
imageTag = flag.String("image-tag", "", "set image tag for analysis sandboxes")
@@ -78,7 +78,7 @@ func makeSandboxOptions(mode analysis.Mode) []sandbox.Option {
7878
return sbOpts
7979
}
8080

81-
func dynamicAnalysis(pkg *pkgecosystem.Pkg) {
81+
func dynamicAnalysis(pkg *pkgmanager.Pkg) {
8282
if !*offline {
8383
sandbox.InitNetwork()
8484
}
@@ -116,7 +116,7 @@ func dynamicAnalysis(pkg *pkgecosystem.Pkg) {
116116
}
117117
}
118118

119-
func staticAnalysis(pkg *pkgecosystem.Pkg) {
119+
func staticAnalysis(pkg *pkgmanager.Pkg) {
120120
if !*offline {
121121
sandbox.InitNetwork()
122122
}
@@ -143,6 +143,8 @@ func staticAnalysis(pkg *pkgecosystem.Pkg) {
143143
func main() {
144144
log.Initialize(os.Getenv("LOGGER_ENV"))
145145

146+
flag.TextVar(&ecosystem, "ecosystem", pkgecosystem.None, fmt.Sprintf("package ecosystem. Can be %s", pkgecosystem.SupportedEcosystemsStrings))
147+
146148
analysisMode.InitFlag()
147149
flag.Parse()
148150

@@ -156,15 +158,15 @@ func main() {
156158
return
157159
}
158160

159-
if *ecosystem == "" {
161+
if ecosystem == pkgecosystem.None {
160162
flag.Usage()
161163
return
162164
}
163165

164-
manager := pkgecosystem.Manager(api.Ecosystem(*ecosystem), *combinedSandbox)
166+
manager := pkgmanager.Manager(ecosystem, *combinedSandbox)
165167
if manager == nil {
166168
log.Panic("Unsupported pkg manager",
167-
log.Label("ecosystem", *ecosystem))
169+
log.Label("ecosystem", string(ecosystem)))
168170
}
169171

170172
if *pkgName == "" {
@@ -183,12 +185,12 @@ func main() {
183185
runMode[mode] = true
184186
}
185187

186-
worker.LogRequest(*ecosystem, *pkgName, *version, *localPkg, "")
188+
worker.LogRequest(ecosystem, *pkgName, *version, *localPkg, "")
187189

188190
pkg, err := worker.ResolvePkg(manager, *pkgName, *version, *localPkg)
189191
if err != nil {
190192
log.Panic("Error resolving package",
191-
log.Label("ecosystem", *ecosystem),
193+
log.Label("ecosystem", ecosystem.String()),
192194
"name", *pkgName,
193195
"error", err)
194196
}

cmd/scheduler/main.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/ossf/package-analysis/cmd/scheduler/proxy"
1818
"github.com/ossf/package-analysis/internal/log"
19+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
1920
)
2021

2122
const (
@@ -31,7 +32,7 @@ type ManagerConfig struct {
3132
ExcludeVersions []*regexp.Regexp
3233

3334
// Ecosystem is the internal name of the ecosystem.
34-
Ecosystem string
35+
Ecosystem pkgecosystem.Ecosystem
3536
}
3637

3738
func (m *ManagerConfig) SkipVersion(version string) bool {
@@ -53,14 +54,14 @@ func (m *ManagerConfig) SkipVersion(version string) bool {
5354
// analyze. It is a map from ossf/package-feeds package types, to a
5455
// config for the package manager's feed.
5556
var supportedPkgManagers = map[string]*ManagerConfig{
56-
"npm": {Ecosystem: "npm"},
57-
"pypi": {Ecosystem: "pypi"},
58-
"rubygems": {Ecosystem: "rubygems"},
57+
"npm": {Ecosystem: pkgecosystem.NPM},
58+
"pypi": {Ecosystem: pkgecosystem.PyPI},
59+
"rubygems": {Ecosystem: pkgecosystem.RubyGems},
5960
"packagist": {
60-
Ecosystem: "packagist",
61+
Ecosystem: pkgecosystem.Packagist,
6162
ExcludeVersions: []*regexp.Regexp{regexp.MustCompile(`^dev-`), regexp.MustCompile(`\.x-dev$`)},
6263
},
63-
"crates": {Ecosystem: "crates.io"},
64+
"crates": {Ecosystem: pkgecosystem.CratesIO},
6465
}
6566

6667
func main() {
@@ -123,7 +124,7 @@ func listenLoop(subURL, topicURL string) error {
123124
Body: []byte{},
124125
Metadata: map[string]string{
125126
"name": pkg.Name,
126-
"ecosystem": config.Ecosystem,
127+
"ecosystem": config.Ecosystem.String(),
127128
"version": pkg.Version,
128129
},
129130
}, nil

cmd/worker/main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
"github.com/ossf/package-analysis/internal/analysis"
2424
"github.com/ossf/package-analysis/internal/log"
2525
"github.com/ossf/package-analysis/internal/notification"
26-
"github.com/ossf/package-analysis/internal/pkgecosystem"
26+
"github.com/ossf/package-analysis/internal/pkgmanager"
2727
"github.com/ossf/package-analysis/internal/resultstore"
2828
"github.com/ossf/package-analysis/internal/sandbox"
2929
"github.com/ossf/package-analysis/internal/staticanalysis"
3030
"github.com/ossf/package-analysis/internal/worker"
31-
"github.com/ossf/package-analysis/pkg/api"
32-
"github.com/ossf/package-analysis/pkg/result"
31+
"github.com/ossf/package-analysis/pkg/api/analysisrun"
32+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
3333
)
3434

3535
const (
@@ -80,7 +80,7 @@ func copyPackageToLocalFile(ctx context.Context, packagesBucket *blob.Bucket, bu
8080
return fmt.Sprintf(localPkgPathFmt, path.Base(bucketPath)), f, nil
8181
}
8282

83-
func saveResults(ctx context.Context, pkg *pkgecosystem.Pkg, dest resultBucketPaths, dynamicResults result.DynamicAnalysisResults, staticResults result.StaticAnalysisResults) error {
83+
func saveResults(ctx context.Context, pkg *pkgmanager.Pkg, dest resultBucketPaths, dynamicResults analysisrun.DynamicAnalysisResults, staticResults analysisrun.StaticAnalysisResults) error {
8484
if dest.dynamicAnalysis != "" {
8585
err := resultstore.New(dest.dynamicAnalysis, resultstore.ConstructPath()).Save(ctx, pkg, dynamicResults.StraceSummary)
8686
if err != nil {
@@ -111,18 +111,18 @@ func handleMessage(ctx context.Context, msg *pubsub.Message, packagesBucket *blo
111111
return nil
112112
}
113113

114-
ecosystem := msg.Metadata["ecosystem"]
114+
ecosystem := pkgecosystem.Ecosystem(msg.Metadata["ecosystem"])
115115
if ecosystem == "" {
116116
log.Warn("ecosystem is empty",
117117
"name", name)
118118
msg.Ack()
119119
return nil
120120
}
121121

122-
manager := pkgecosystem.Manager(api.Ecosystem(ecosystem), false)
122+
manager := pkgmanager.Manager(ecosystem, false)
123123
if manager == nil {
124124
log.Warn("Unsupported pkg manager",
125-
log.Label("ecosystem", ecosystem),
125+
log.Label("ecosystem", ecosystem.String()),
126126
"name", name)
127127
msg.Ack()
128128
return nil
@@ -165,7 +165,7 @@ func handleMessage(ctx context.Context, msg *pubsub.Message, packagesBucket *blo
165165
pkg, err := worker.ResolvePkg(manager, name, version, localPkgPath)
166166
if err != nil {
167167
log.Error("Error resolving package",
168-
log.Label("ecosystem", ecosystem),
168+
log.Label("ecosystem", ecosystem.String()),
169169
log.Label("name", name),
170170
"error", err)
171171
return err
@@ -176,7 +176,7 @@ func handleMessage(ctx context.Context, msg *pubsub.Message, packagesBucket *blo
176176
return err
177177
}
178178

179-
var staticResults result.StaticAnalysisResults
179+
var staticResults analysisrun.StaticAnalysisResults
180180
if resultsBuckets.staticAnalysis != "" {
181181
staticResults, _, err = worker.RunStaticAnalysis(pkg, staticSandboxOpts, staticanalysis.All)
182182
if err != nil {

internal/dynamicanalysis/analysis.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/ossf/package-analysis/internal/packetcapture"
1010
"github.com/ossf/package-analysis/internal/sandbox"
1111
"github.com/ossf/package-analysis/internal/strace"
12-
"github.com/ossf/package-analysis/pkg/result"
12+
"github.com/ossf/package-analysis/pkg/api/analysisrun"
1313
)
1414

1515
const (
@@ -18,12 +18,12 @@ const (
1818
)
1919

2020
type Result struct {
21-
StraceSummary result.StraceSummary
22-
FileWrites result.FileWrites
21+
StraceSummary analysisrun.StraceSummary
22+
FileWrites analysisrun.FileWrites
2323
}
2424

2525
var resultError = &Result{
26-
StraceSummary: result.StraceSummary{
26+
StraceSummary: analysisrun.StraceSummary{
2727
Status: analysis.StatusErrorOther,
2828
},
2929
}
@@ -67,7 +67,7 @@ func Run(sb sandbox.Sandbox, args []string) (*Result, error) {
6767
}
6868

6969
analysisResult := Result{
70-
StraceSummary: result.StraceSummary{
70+
StraceSummary: analysisrun.StraceSummary{
7171
Status: analysis.StatusForRunResult(r),
7272
Stdout: lastLines(r.Stdout(), maxOutputLines, maxOutputBytes),
7373
Stderr: lastLines(r.Stderr(), maxOutputLines, maxOutputBytes),
@@ -79,36 +79,42 @@ func Run(sb sandbox.Sandbox, args []string) (*Result, error) {
7979

8080
func (d *Result) setData(straceResult *strace.Result, dns *dnsanalyzer.DNSAnalyzer) {
8181
for _, f := range straceResult.Files() {
82-
d.StraceSummary.Files = append(d.StraceSummary.Files, result.FileResult{
82+
d.StraceSummary.Files = append(d.StraceSummary.Files, analysisrun.FileResult{
8383
Path: f.Path,
8484
Read: f.Read,
8585
Write: f.Write,
8686
Delete: f.Delete,
8787
})
8888
if len(f.WriteInfo) > 0 {
89-
d.FileWrites = append(d.FileWrites, result.FileWriteResult{f.Path, f.WriteInfo})
89+
w := analysisrun.FileWriteResult{Path: f.Path}
90+
for _, wi := range f.WriteInfo {
91+
w.WriteInfo = append(w.WriteInfo, analysisrun.WriteInfo{
92+
BytesWritten: wi.BytesWritten,
93+
})
94+
}
95+
d.FileWrites = append(d.FileWrites, w)
9096
}
9197
}
9298

9399
for _, s := range straceResult.Sockets() {
94-
d.StraceSummary.Sockets = append(d.StraceSummary.Sockets, result.SocketResult{
100+
d.StraceSummary.Sockets = append(d.StraceSummary.Sockets, analysisrun.SocketResult{
95101
Address: s.Address,
96102
Port: s.Port,
97103
Hostnames: dns.Hostnames(s.Address),
98104
})
99105
}
100106

101107
for _, c := range straceResult.Commands() {
102-
d.StraceSummary.Commands = append(d.StraceSummary.Commands, result.CommandResult{
108+
d.StraceSummary.Commands = append(d.StraceSummary.Commands, analysisrun.CommandResult{
103109
Command: c.Command,
104110
Environment: c.Env,
105111
})
106112
}
107113

108114
for dnsClass, queries := range dns.Questions() {
109-
c := result.DnsResult{Class: dnsClass}
115+
c := analysisrun.DNSResult{Class: dnsClass}
110116
for host, types := range queries {
111-
c.Queries = append(c.Queries, result.DnsQueries{
117+
c.Queries = append(c.Queries, analysisrun.DNSQueries{
112118
Hostname: host,
113119
Types: types,
114120
})

internal/notification/notification.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77

88
"gocloud.dev/pubsub"
99

10-
"github.com/ossf/package-analysis/pkg/notification"
11-
"github.com/ossf/package-analysis/pkg/pkgidentifier"
10+
"github.com/ossf/package-analysis/pkg/api/analysisrun"
11+
"github.com/ossf/package-analysis/pkg/api/notification"
12+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
1213
)
1314

14-
func PublishAnalysisCompletion(ctx context.Context, notificationTopic *pubsub.Topic, name, version, ecosystem string) error {
15-
pkgDetails := pkgidentifier.PkgIdentifier{Name: name, Version: version, Ecosystem: ecosystem}
16-
notificationMsg, err := json.Marshal(notification.AnalysisCompletion{Package: pkgDetails})
15+
func PublishAnalysisCompletion(ctx context.Context, notificationTopic *pubsub.Topic, name, version string, ecosystem pkgecosystem.Ecosystem) error {
16+
k := analysisrun.Key{Name: name, Version: version, Ecosystem: ecosystem}
17+
notificationMsg, err := json.Marshal(notification.AnalysisRunComplete{Key: k})
1718
if err != nil {
1819
return fmt.Errorf("failed to encode completion notification: %w", err)
1920
}

internal/pkgecosystem/crates.io.go renamed to internal/pkgmanager/crates.io.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package pkgecosystem
1+
package pkgmanager
22

33
import (
44
"encoding/json"
55
"fmt"
66
"net/http"
77

8-
"github.com/ossf/package-analysis/pkg/api"
8+
"github.com/ossf/package-analysis/pkg/api/analysisrun"
9+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
910
)
1011

1112
type cratesJSON struct {
@@ -32,21 +33,21 @@ func getCratesLatest(pkg string) (string, error) {
3233
}
3334

3435
var cratesPkgManager = PkgManager{
35-
ecosystem: api.EcosystemCratesIO,
36+
ecosystem: pkgecosystem.CratesIO,
3637
image: "gcr.io/ossf-malware-analysis/crates.io",
3738
command: "/usr/local/bin/analyze.py",
3839
latestVersion: getCratesLatest,
39-
runPhases: []api.RunPhase{
40-
api.RunPhaseInstall,
40+
dynamicPhases: []analysisrun.DynamicPhase{
41+
analysisrun.DynamicPhaseInstall,
4142
},
4243
}
4344

4445
var cratesPkgManagerCombinedSandbox = PkgManager{
45-
ecosystem: api.EcosystemCratesIO,
46+
ecosystem: pkgecosystem.CratesIO,
4647
image: combinedDynamicAnalysisImage,
4748
command: "/usr/local/bin/analyze-rust.py",
4849
latestVersion: getCratesLatest,
49-
runPhases: []api.RunPhase{
50-
api.RunPhaseInstall,
50+
dynamicPhases: []analysisrun.DynamicPhase{
51+
analysisrun.DynamicPhaseInstall,
5152
},
5253
}

internal/pkgecosystem/download.go renamed to internal/pkgmanager/download.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package pkgecosystem
1+
package pkgmanager
22

33
import (
44
"fmt"

internal/pkgecosystem/download_test.go renamed to internal/pkgmanager/download_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package pkgecosystem
1+
package pkgmanager
22

33
import (
44
"os"
55
"testing"
66

7-
"github.com/ossf/package-analysis/pkg/api"
7+
"github.com/ossf/package-analysis/pkg/api/pkgecosystem"
88
)
99

1010
type downloadTestSpec struct {
@@ -60,7 +60,7 @@ func TestDownloadNpmArchive(t *testing.T) {
6060
{"pkgname=(invalid)", "fr(2t5j923)", "latest", true},
6161
}
6262

63-
testDownload(t, tests, Manager(api.EcosystemNPM, false))
63+
testDownload(t, tests, Manager(pkgecosystem.NPM, false))
6464
}
6565

6666
func TestDownloadPyPIArchive(t *testing.T) {
@@ -70,7 +70,7 @@ func TestDownloadPyPIArchive(t *testing.T) {
7070
{"pkgname=(invalid)", "fr(2t5j923)", "123", true},
7171
}
7272

73-
testDownload(t, tests, Manager(api.EcosystemPyPI, false))
73+
testDownload(t, tests, Manager(pkgecosystem.PyPI, false))
7474
}
7575

7676
func TestDownloadToDirectory(t *testing.T) {

0 commit comments

Comments
 (0)