-
Notifications
You must be signed in to change notification settings - Fork 1
/
scorecard4purl.go
145 lines (116 loc) · 3.18 KB
/
scorecard4purl.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
)
type Package struct {
Name string `json:"name"`
Ecosystem string `json:"ecosystem"`
Description string `json:"description"`
Homepage string `json:"homepage"`
RepositoryURL string `json:"repository_url"`
}
type ScorecardCheck struct {
Name string `json:"name"`
Score int `json:"score"`
Reason string `json:"reason"`
}
type Repo struct {
Name string `json:"name"`
Commit string `json:"commit"`
}
type ScorecardVersion struct {
Version string `json:"version"`
Commit string `json:"commit"`
}
type ScorecardResult struct {
Date string `json:"date"`
Repo Repo `json:"repo"`
Score float64 `json:"score"`
Checks []ScorecardCheck `json:"checks"`
Scorecard ScorecardVersion `json:"scorecard"`
Metadata string `json:"metadata"`
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s <purl>", os.Args[0])
}
purl := os.Args[1]
// Step 1: Retrieve the GitHub URL from Ecosyste.ms API (and output it)
githubURL, err := getGitHubURL(purl)
if err != nil {
log.Fatal("Failed to retrieve GitHub URL:", err)
}
fmt.Println("GitHub URL:", githubURL)
// Step 2: Retrieve the scorecard data from Security Scorecards API
platform, org, repo, err := parseGitHubURL(githubURL)
if err != nil {
log.Fatal(err)
}
// Step 3: Output the results
result, err := getScorecard(platform, org, repo)
if err != nil {
log.Fatal(err)
}
for _, check := range result.Checks {
fmt.Printf("%s: %d\n", check.Name, check.Score)
}
}
func getGitHubURL(purl string) (string, error) {
apiURL := "https://packages.ecosyste.ms/api/v1/packages/lookup"
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return "", err
}
q := req.URL.Query()
q.Add("purl", purl)
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var lookupResults []Package
if err := json.NewDecoder(resp.Body).Decode(&lookupResults); err != nil {
return "", err
}
if len(lookupResults) == 0 {
return "", fmt.Errorf("no GitHub URL found for the provided PURL")
}
return lookupResults[0].RepositoryURL, nil
}
func getScorecard(platform, org, repo string) (*ScorecardResult, error) {
url := fmt.Sprintf("https://api.securityscorecards.dev/projects/%s/%s/%s", platform, org, repo)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API request failed with status code %d", resp.StatusCode)
}
var result ScorecardResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
}
func parseGitHubURL(input string) (platform, org, repo string, err error) {
u, err := url.Parse(input)
if err != nil {
return "", "", "", err
}
if u.Host != "github.com" {
return "", "", "", fmt.Errorf("only GitHub URLs are supported")
}
parts := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(parts) != 2 {
return "", "", "", fmt.Errorf("invalid GitHub URL")
}
return u.Host, parts[0], parts[1], nil
}