-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
github.go
169 lines (140 loc) · 4.51 KB
/
github.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
package github
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/clock"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/purl"
"github.com/aquasecurity/trivy/pkg/types"
)
const (
DirectRelationship string = "direct"
IndirectRelationship string = "indirect"
RuntimeScope string = "runtime"
DevelopmentScope string = "development"
)
type Package struct {
PackageUrl string `json:"package_url,omitempty"`
Relationship string `json:"relationship,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Scope string `json:"scope,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
type File struct {
SrcLocation string `json:"source_location,omitempty"`
}
type Metadata map[string]interface{}
type Manifest struct {
Name string `json:"name,omitempty"`
File *File `json:"file,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
Resolved map[string]Package `json:"resolved,omitempty"`
}
type Job struct {
Correlator string `json:"correlator,omitempty"`
Id string `json:"id,omitempty"`
}
type Detector struct {
Name string `json:"name"`
Version string `json:"version"`
Url string `json:"url"`
}
type DependencySnapshot struct {
Version int `json:"version"`
Detector Detector `json:"detector"`
Metadata Metadata `json:"metadata,omitempty"`
Ref string `json:"ref,omitempty"`
Sha string `json:"sha,omitempty"`
Job *Job `json:"job,omitempty"`
Scanned string `json:"scanned,omitempty"`
Manifests map[string]Manifest `json:"manifests,omitempty"`
}
// Writer generates JSON for GitHub Dependency Snapshots
type Writer struct {
Output io.Writer
Version string
}
func (w Writer) Write(report types.Report) error {
snapshot := &DependencySnapshot{}
//use now() method that can be overwritten while integration tests run
snapshot.Scanned = clock.Now().Format(time.RFC3339)
snapshot.Detector = Detector{
Name: "trivy",
Version: w.Version,
Url: "https://github.com/aquasecurity/trivy",
}
snapshot.Version = 0 // The version of the repository snapshot submission.
snapshot.Ref = os.Getenv("GITHUB_REF")
snapshot.Sha = os.Getenv("GITHUB_SHA")
snapshot.Job = &Job{
Correlator: fmt.Sprintf("%s_%s", os.Getenv("GITHUB_WORKFLOW"), os.Getenv("GITHUB_JOB")),
Id: os.Getenv("GITHUB_RUN_ID"),
}
snapshot.Metadata = getMetadata(report)
manifests := make(map[string]Manifest)
for _, result := range report.Results {
if result.Packages == nil {
continue
}
manifest := Manifest{}
manifest.Name = result.Type
// show path for language-specific packages only
if result.Class == types.ClassLangPkg {
manifest.File = &File{
SrcLocation: result.Target,
}
}
resolved := make(map[string]Package)
for _, pkg := range result.Packages {
var err error
githubPkg := Package{}
githubPkg.Scope = RuntimeScope
githubPkg.Relationship = getPkgRelationshipType(pkg)
githubPkg.Dependencies = pkg.DependsOn // TODO: replace with PURL
githubPkg.PackageUrl, err = buildPurl(result.Type, pkg)
if err != nil {
return xerrors.Errorf("unable to build purl for %s: %w", pkg.Name, err)
}
resolved[pkg.Name] = githubPkg
}
manifest.Resolved = resolved
manifests[result.Target] = manifest
}
snapshot.Manifests = manifests
output, err := json.MarshalIndent(snapshot, "", " ")
if err != nil {
return xerrors.Errorf("failed to marshal github dependency snapshots: %w", err)
}
if _, err = fmt.Fprint(w.Output, string(output)); err != nil {
return xerrors.Errorf("failed to write github dependency snapshots: %w", err)
}
return nil
}
func getMetadata(report types.Report) Metadata {
metadata := Metadata{}
if report.Metadata.RepoTags != nil {
metadata["aquasecurity:trivy:RepoTag"] = strings.Join(report.Metadata.RepoTags, ", ")
}
if report.Metadata.RepoDigests != nil {
metadata["aquasecurity:trivy:RepoDigest"] = strings.Join(report.Metadata.RepoDigests, ", ")
}
return metadata
}
func getPkgRelationshipType(pkg ftypes.Package) string {
if pkg.Indirect {
return IndirectRelationship
}
return DirectRelationship
}
func buildPurl(t string, pkg ftypes.Package) (string, error) {
packageUrl, err := purl.NewPackageURL(t, types.Metadata{}, pkg)
if err != nil {
return "", xerrors.Errorf("purl error: %w", err)
}
return packageUrl.ToString(), nil
}