-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
293 lines (246 loc) · 6.7 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
285
286
287
288
289
290
291
292
293
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/cli/go-gh/v2/pkg/repository"
"github.com/spf13/pflag"
"github.com/advanced-security/gh-sbom/pkg/cyclonedx"
dg "github.com/advanced-security/gh-sbom/pkg/dependency-graph"
)
type ClearlyDefinedDefinition struct {
Licensed struct {
Declared string
Facets struct {
Core struct {
Discovered struct {
Expressions []string
}
}
}
}
}
type Purl struct {
cdType string
cdProvider string
Provider string
Namespace string
Name string
Version string
}
type SPDXRestAPI struct {
sbom interface{}
}
func getPurl(packageManager, packageName, version string) Purl {
p := Purl{}
p.Namespace = ""
p.Name = packageName
p.Version = version
if packageManager == "actions" {
p.cdType = "git"
p.cdProvider = "github"
p.Provider = "github"
packageParts := strings.SplitN(packageName, "/", 2)
if len(packageParts) >= 2 {
p.Namespace = packageParts[0]
p.Name = packageParts[1]
}
} else if packageManager == "go" {
p.cdType = "go"
p.cdProvider = "golang"
p.Provider = "golang"
packageParts := strings.SplitN(packageName, "/", 3)
if len(packageParts) == 2 {
p.Namespace = packageParts[0]
p.Name = packageParts[1]
} else if len(packageParts) == 3 {
p.Namespace = packageParts[0] + "/" + packageParts[1]
p.Name = packageParts[2]
}
} else if packageManager == "rubygems" {
p.cdType = "gem"
p.cdProvider = "rubygems"
p.Provider = "gem"
} else if packageManager == "maven" {
p.cdType = "maven"
p.cdProvider = "mavenCentral"
p.Provider = "maven"
packageParts := strings.SplitN(packageName, ":", 2)
if len(packageParts) >= 2 {
p.Namespace = packageParts[0]
p.Name = packageParts[1]
}
} else if packageManager == "npm" {
p.cdType = "npm"
p.cdProvider = "npmjs"
p.Provider = "npm"
if strings.HasPrefix(packageName, "@") {
packageParts := strings.SplitN(packageName, "/", 2)
if len(packageParts) >= 2 {
p.Namespace = packageParts[0]
p.Name = packageParts[1]
}
}
} else if packageManager == "pip" {
p.cdType = "pypi"
p.cdProvider = "pypi"
p.Provider = "pypi"
}
return p
}
func (p Purl) String() string {
// https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst
prefix := "pkg:" + p.Provider + "/"
if p.Namespace != "" {
namespaceParts := strings.Split(p.Namespace, "/")
for _, part := range namespaceParts {
prefix = prefix + url.QueryEscape(part) + "/"
}
}
suffix := ""
if p.Version != "" {
suffix = "@" + url.QueryEscape(p.Version)
}
return prefix + url.QueryEscape(p.Name) + suffix
}
func getLicense(p *Purl) (string, string, error) {
client := &http.Client{}
if p.Namespace == "" {
p.Namespace = "-"
}
version := "v" + p.Version
if p.cdType == "gem" || p.cdType == "pypi" || p.cdType == "maven" || p.cdType == "npm" {
version = p.Version
}
req, err := http.NewRequest("GET", "https://api.clearlydefined.io/definitions/"+url.PathEscape(p.cdType)+"/"+url.PathEscape(p.cdProvider)+"/"+url.PathEscape(p.Namespace)+"/"+url.PathEscape(p.Name)+"/"+url.PathEscape(version), nil)
if err != nil {
log.Print(err)
return "", "", err
}
req.Header.Add("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Print(err)
return "", "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Print(err)
return "", "", err
}
def := ClearlyDefinedDefinition{}
err = json.Unmarshal(body, &def)
if err != nil {
log.Print(err)
return "", "", err
}
declared := def.Licensed.Declared
discovered := ""
if len(def.Licensed.Facets.Core.Discovered.Expressions) > 0 {
discovered = def.Licensed.Facets.Core.Discovered.Expressions[0]
}
return declared, discovered, nil
}
type RepoResp struct {
License struct {
SPDXId string `json:"spdx_id"`
}
}
func getRepoLicense(owner, repo string) string {
client, err := api.DefaultRESTClient()
if err != nil {
return ""
}
repoResp := RepoResp{}
err = client.Get("repos/"+owner+"/"+repo, &repoResp)
if err != nil {
return ""
}
return repoResp.License.SPDXId
}
func main() {
version := "0.0.9"
repoOverride := pflag.StringP("repository", "r", "", "Repository to query. Current directory used by default.")
cdx := pflag.BoolP("cyclonedx", "c", false, "Use CycloneDX SBOM format. Default is to use SPDX.")
includeLicense := pflag.BoolP("license", "l", false, "Include license information from clearlydefined.io for CycloneDX format (SPDX always includes license information).")
pflag.Parse()
var repo repository.Repository
var err error
if *repoOverride == "" {
repo, err = repository.Current()
} else {
repo, err = repository.Parse(*repoOverride)
}
if err != nil {
log.Fatal(err)
}
if *cdx {
dependencies := dg.GetDependencies(repo.Owner, repo.Name)
if len(dependencies) == 0 {
log.Fatal("No dependencies found\n\nIf you own this repository, check if Dependency Graph is enabled:\nhttps://" + repo.Host + "/" + repo.Owner + "/" + repo.Name + "/settings/security_analysis\n\n")
}
components := []cyclonedx.Component{}
for packageManager, packageManagerMap := range dependencies {
for packageName, requirementsMap := range packageManagerMap {
for requirements, _ := range requirementsMap {
p := getPurl(packageManager, packageName, requirements)
c := cyclonedx.Component{
Type: "library",
Name: p.Name,
Version: p.Version,
Purl: p.String(),
}
if p.Namespace != "" {
c.Group = p.Namespace
}
if *includeLicense {
l := []cyclonedx.LicenseExpression{}
declared, discovered, err := getLicense(&p)
if err == nil && len(declared) > 0 {
le := cyclonedx.LicenseExpression{
Expression: declared,
}
c.Licenses = append(l, le)
} else if err == nil && len(discovered) > 0 {
le := cyclonedx.LicenseExpression{
Expression: discovered,
}
c.Licenses = append(l, le)
}
}
components = append(components, c)
}
}
}
doc := cyclonedx.MakeDoc(version, components)
jsonBinary, err := json.Marshal(&doc)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonBinary))
} else {
client, err := api.DefaultRESTClient()
if err != nil {
log.Fatal(err)
}
url := "repos/" + repo.Owner + "/" + repo.Name + "/dependency-graph/sbom"
response := struct{
SBOM map[string]interface{}
}{}
err = client.Get(url, &response)
if err != nil {
log.Fatal(err)
}
jsonBinary, err := json.Marshal(&response.SBOM)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonBinary))
}
}